* Copyright (c) 1987 Regents of the University of California.
* %sccs.include.redist.c%
"@(#) Copyright (c) 1987 Regents of the University of California.\n\
static char sccsid
[] = "@(#)ln.c 4.14 (Berkeley) %G%";
static int dirflag
, /* 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
)) {
"ln: %s: %s\n", sourcedir
, strerror(errno
));
if (!S_ISDIR(buf
.st_mode
))
for (exitval
= 0; *argv
!= sourcedir
; ++argv
)
exitval
|= linkit(*argv
, sourcedir
, 1);
linkit(target
, source
, isdir
)
char path
[MAXPATHLEN
], *cp
;
/* if target doesn't exist, quit now */
if (stat(target
, &buf
)) {
"ln: %s: %s\n", target
, strerror(errno
));
/* only symbolic links to directories, unless -F option used */
if (!dirflag
&& (buf
.st_mode
& S_IFMT
) == S_IFDIR
) {
(void)printf("ln: %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
)) {
(void)fprintf(stderr
, "ln: %s: %s\n", source
, strerror(errno
));
"usage:\tln [-s] file1 file2\n\tln [-s] file ... directory\n");