Home | History | Annotate | Line # | Download | only in ln
ln.c revision 1.26
      1 /* $NetBSD: ln.c,v 1.26 2003/09/14 19:20:21 jschauma Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1987, 1993, 1994
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 __COPYRIGHT("@(#) Copyright (c) 1987, 1993, 1994\n\
     35 	The Regents of the University of California.  All rights reserved.\n");
     36 #endif /* not lint */
     37 
     38 #ifndef lint
     39 #if 0
     40 static char sccsid[] = "@(#)ln.c	8.2 (Berkeley) 3/31/94";
     41 #else
     42 __RCSID("$NetBSD: ln.c,v 1.26 2003/09/14 19:20:21 jschauma Exp $");
     43 #endif
     44 #endif /* not lint */
     45 
     46 #include <sys/param.h>
     47 #include <sys/stat.h>
     48 
     49 #include <err.h>
     50 #include <errno.h>
     51 #include <stdio.h>
     52 #include <stdlib.h>
     53 #include <string.h>
     54 #include <unistd.h>
     55 #include <vis.h>
     56 
     57 int	fflag;				/* Unlink existing files. */
     58 int	hflag;				/* Check new name for symlink first. */
     59 int	sflag;				/* Symbolic, not hard, link. */
     60 int	vflag;                          /* Verbose output */
     61 int	stdout_ok;			/* stdout connected to a terminal */
     62 
     63 					/* System link call. */
     64 int (*linkf)(const char *, const char *);
     65 char   linkch;
     66 
     67 int	linkit(char *, char *, int);
     68 void	usage(void);
     69 int	main(int, char *[]);
     70 
     71 int
     72 main(int argc, char *argv[])
     73 {
     74 	struct stat sb;
     75 	int ch, exitval;
     76 	char *sourcedir;
     77 
     78 	setprogname(argv[0]);
     79 	while ((ch = getopt(argc, argv, "fhnsv")) != -1)
     80 		switch (ch) {
     81 		case 'f':
     82 			fflag = 1;
     83 			break;
     84 		case 'h':
     85 		case 'n':
     86 			hflag = 1;
     87 			break;
     88 		case 's':
     89 			sflag = 1;
     90 			break;
     91 		case 'v':
     92 		vflag = 1;
     93 		break;
     94 		case '?':
     95 		default:
     96 			usage();
     97 			/* NOTREACHED */
     98 		}
     99 
    100 	argv += optind;
    101 	argc -= optind;
    102 
    103 	if (sflag) {
    104 		linkf  = symlink;
    105 		linkch = '-';
    106 	} else {
    107 		linkf  = link;
    108 		linkch = '=';
    109 	}
    110 
    111 	switch(argc) {
    112 	case 0:
    113 		usage();
    114 		/* NOTREACHED */
    115 	case 1:				/* ln target */
    116 		exit(linkit(argv[0], ".", 1));
    117 		/* NOTREACHED */
    118 	case 2:				/* ln target source */
    119 		exit(linkit(argv[0], argv[1], 0));
    120 		/* NOTREACHED */
    121 	}
    122 
    123 	stdout_ok = isatty(STDOUT_FILENO);
    124 
    125 					/* ln target1 target2 directory */
    126 	sourcedir = argv[argc - 1];
    127 	if (hflag && lstat(sourcedir, &sb) == 0 && S_ISLNK(sb.st_mode)) {
    128 		/* we were asked not to follow symlinks, but found one at
    129 		   the target--simulate "not a directory" error */
    130 		errno = ENOTDIR;
    131 		err(EXIT_FAILURE, "%s", sourcedir);
    132 		/* NOTREACHED */
    133 	}
    134 	if (stat(sourcedir, &sb)) {
    135 		err(EXIT_FAILURE, "%s", sourcedir);
    136 		/* NOTREACHED */
    137 	}
    138 	if (!S_ISDIR(sb.st_mode)) {
    139 		usage();
    140 		/* NOTREACHED */
    141 	}
    142 	for (exitval = 0; *argv != sourcedir; ++argv)
    143 		exitval |= linkit(*argv, sourcedir, 1);
    144 	exit(exitval);
    145 	/* NOTREACHED */
    146 }
    147 
    148 int
    149 linkit(char *target, char *source, int isdir)
    150 {
    151 	struct stat sb;
    152 	char *p, path[MAXPATHLEN];
    153 
    154 	if (!sflag) {
    155 		/* If target doesn't exist, quit now. */
    156 		if (stat(target, &sb)) {
    157 			warn("%s", target);
    158 			return (1);
    159 		}
    160 	}
    161 
    162 	/* If the source is a directory (and not a symlink if hflag),
    163 	   append the target's name. */
    164 	if (isdir ||
    165 	    (!lstat(source, &sb) && S_ISDIR(sb.st_mode)) ||
    166 	    (!hflag && !stat(source, &sb) && S_ISDIR(sb.st_mode))) {
    167 		if ((p = strrchr(target, '/')) == NULL)
    168 			p = target;
    169 		else
    170 			++p;
    171 		(void)snprintf(path, sizeof(path), "%s/%s", source, p);
    172 		source = path;
    173 	}
    174 
    175 	/*
    176 	 * If the file exists, and -f was specified, unlink it.
    177 	 * Attempt the link.
    178 	 */
    179 	if ((fflag && unlink(source) < 0 && errno != ENOENT) ||
    180 	    (*linkf)(target, source)) {
    181 		warn("%s", source);
    182 		return (1);
    183 	}
    184 	if (vflag)
    185 		(void)printf("%s %c> %s\n", source, linkch, target);
    186 
    187 	return (0);
    188 }
    189 
    190 void
    191 usage(void)
    192 {
    193 
    194 	(void)fprintf(stderr,
    195 	    "Usage:\t%s [-fhns] file1 file2\n\t%s [-fhnsv] file ... directory\n",
    196 	    getprogname(), getprogname());
    197 	exit(1);
    198 	/* NOTREACHED */
    199 }
    200