* Copyright (c) 1987 Regents of the University of California.
* Redistribution and use in source and binary forms are permitted
* provided that the above copyright notice and this paragraph are
* duplicated in all such forms and that any documentation,
* advertising materials, and other materials related to such
* distribution and use acknowledge that the software was developed
* by the University of California, Berkeley. The name of the
* University may not be used to endorse or promote products derived
* from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
"@(#) Copyright (c) 1987 Regents of the University of California.\n\
static char sccsid
[] = "@(#)ln.c 4.11 (Berkeley) 6/18/88";
static int fflag
, /* undocumented force flag */
sflag
, /* symbolic, not hard, link */
(*linkf
)(); /* system link call */
int ch
, exitval
, link(), symlink();
while ((ch
= getopt(argc
, argv
, "fs")) != EOF
)
linkf
= sflag
? symlink
: link
;
exit(linkit(argv
[0], ".", 1));
case 2: /* ln target source */
exit(linkit(argv
[0], argv
[1], 0));
default: /* ln target1 target2 directory */
sourcedir
= argv
[argc
- 1];
if (stat(sourcedir
, &buf
)) {
if ((buf
.st_mode
& S_IFMT
) != S_IFDIR
)
for (exitval
= 0; *argv
!= sourcedir
; ++argv
)
exitval
|= linkit(*argv
, sourcedir
, 1);
linkit(target
, source
, isdir
)
*cp
, *rindex(), *strcpy();
/* if target doesn't exist, quit now */
if (stat(target
, &buf
)) {
/* only symbolic links to directories, unless -f option used */
if (!fflag
&& (buf
.st_mode
& S_IFMT
) == S_IFDIR
) {
printf("%s is a directory.\n", target
);
/* if the source is a directory, append the target's name */
if (isdir
|| !stat(source
, &buf
) && (buf
.st_mode
& S_IFMT
) == S_IFDIR
) {
if (!(cp
= rindex(target
, '/')))
(void)sprintf(path
, "%s/%s", source
, cp
);
if ((*linkf
)(target
, source
)) {
fputs("usage:\tln [-s] targetname [sourcename]\n\tln [-s] targetname1 targetname2 [... targetnameN] sourcedirectory\n", stderr
);