Home | History | Annotate | Line # | Download | only in ln
ln.c revision 1.23
      1 /* $NetBSD: ln.c,v 1.23 2003/08/07 09:05:14 agc 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.23 2003/08/07 09:05:14 agc 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 char	*printescaped(const char *);
     71 
     72 int
     73 main(int argc, char *argv[])
     74 {
     75 	struct stat sb;
     76 	int ch, exitval;
     77 	char *sourcedir;
     78 
     79 	setprogname(argv[0]);
     80 	while ((ch = getopt(argc, argv, "fhnsv")) != -1)
     81 		switch (ch) {
     82 		case 'f':
     83 			fflag = 1;
     84 			break;
     85 		case 'h':
     86 		case 'n':
     87 			hflag = 1;
     88 			break;
     89 		case 's':
     90 			sflag = 1;
     91 			break;
     92 		case 'v':
     93 		vflag = 1;
     94 		break;
     95 		case '?':
     96 		default:
     97 			usage();
     98 			/* NOTREACHED */
     99 		}
    100 
    101 	argv += optind;
    102 	argc -= optind;
    103 
    104 	if (sflag) {
    105 		linkf  = symlink;
    106 		linkch = '-';
    107 	} else {
    108 		linkf  = link;
    109 		linkch = '=';
    110 	}
    111 
    112 	switch(argc) {
    113 	case 0:
    114 		usage();
    115 		/* NOTREACHED */
    116 	case 1:				/* ln target */
    117 		exit(linkit(argv[0], ".", 1));
    118 		/* NOTREACHED */
    119 	case 2:				/* ln target source */
    120 		exit(linkit(argv[0], argv[1], 0));
    121 		/* NOTREACHED */
    122 	}
    123 
    124 	stdout_ok = isatty(STDOUT_FILENO);
    125 
    126 					/* ln target1 target2 directory */
    127 	sourcedir = argv[argc - 1];
    128 	if (hflag && lstat(sourcedir, &sb) == 0 && S_ISLNK(sb.st_mode)) {
    129 		/* we were asked not to follow symlinks, but found one at
    130 		   the target--simulate "not a directory" error */
    131 		errno = ENOTDIR;
    132 		err(EXIT_FAILURE, "%s", printescaped(sourcedir));
    133 		/* NOTREACHED */
    134 	}
    135 	if (stat(sourcedir, &sb)) {
    136 		err(EXIT_FAILURE, "%s", printescaped(sourcedir));
    137 		/* NOTREACHED */
    138 	}
    139 	if (!S_ISDIR(sb.st_mode)) {
    140 		usage();
    141 		/* NOTREACHED */
    142 	}
    143 	for (exitval = 0; *argv != sourcedir; ++argv)
    144 		exitval |= linkit(*argv, sourcedir, 1);
    145 	exit(exitval);
    146 	/* NOTREACHED */
    147 }
    148 
    149 int
    150 linkit(char *target, char *source, int isdir)
    151 {
    152 	struct stat sb;
    153 	char *p, path[MAXPATHLEN];
    154 	char *sn, *tn;
    155 
    156 	sn = printescaped(source);
    157 	tn = printescaped(target);
    158 
    159 	if (!sflag) {
    160 		/* If target doesn't exist, quit now. */
    161 		if (stat(target, &sb)) {
    162 			warn("%s", tn);
    163 			free(sn);
    164 			free(tn);
    165 			return (1);
    166 		}
    167 	}
    168 
    169 	/* If the source is a directory (and not a symlink if hflag),
    170 	   append the target's name. */
    171 	if (isdir ||
    172 	    (!lstat(source, &sb) && S_ISDIR(sb.st_mode)) ||
    173 	    (!hflag && !stat(source, &sb) && S_ISDIR(sb.st_mode))) {
    174 		if ((p = strrchr(target, '/')) == NULL)
    175 			p = target;
    176 		else
    177 			++p;
    178 		p = printescaped(p);
    179 		(void)snprintf(path, sizeof(path), "%s/%s", sn, p);
    180 		free(p);
    181 		source = path;
    182 	}
    183 
    184 	/*
    185 	 * If the file exists, and -f was specified, unlink it.
    186 	 * Attempt the link.
    187 	 */
    188 	if ((fflag && unlink(source) < 0 && errno != ENOENT) ||
    189 	    (*linkf)(target, source)) {
    190 		warn("%s", source);
    191 		return (1);
    192 	}
    193 	if (vflag)
    194 		(void)printf("%s %c> %s\n", sn, linkch, tn);
    195 
    196 	free(sn);
    197 	free(tn);
    198 	return (0);
    199 }
    200 
    201 void
    202 usage(void)
    203 {
    204 
    205 	(void)fprintf(stderr,
    206 	    "Usage:\t%s [-fhns] file1 file2\n\t%s [-fhnsv] file ... directory\n",
    207 	    getprogname(), getprogname());
    208 	exit(1);
    209 	/* NOTREACHED */
    210 }
    211 
    212 char *
    213 printescaped(const char *src)
    214 {
    215 	size_t len;
    216 	char *retval;
    217 
    218 	len = strlen(src);
    219 	if (len != 0 && SIZE_T_MAX/len <= 4) {
    220 		errx(EXIT_FAILURE, "%s: name too long", src);
    221 		/* NOTREACHED */
    222 	}
    223 
    224 	retval = (char *)malloc(4*len+1);
    225 	if (retval != NULL) {
    226 		if (stdout_ok)
    227 			(void)strvis(retval, src, VIS_NL | VIS_CSTYLE);
    228 		else
    229 			(void)strcpy(retval, src);
    230 		return retval;
    231 	} else
    232 		errx(EXIT_FAILURE, "out of memory!");
    233 		/* NOTREACHED */
    234 }
    235