Home | History | Annotate | Line # | Download | only in ln
ln.c revision 1.30
      1 /* $NetBSD: ln.c,v 1.30 2005/06/26 19:10:49 christos 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.30 2005/06/26 19:10:49 christos 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 
     56 int	fflag;				/* Unlink existing files. */
     57 int	hflag;				/* Check new name for symlink first. */
     58 int	sflag;				/* Symbolic, not hard, link. */
     59 int	vflag;                          /* Verbose output */
     60 
     61 					/* System link call. */
     62 int (*linkf)(const char *, const char *);
     63 char   linkch;
     64 
     65 int	linkit(const char *, const char *, int);
     66 void	usage(void);
     67 int	main(int, char *[]);
     68 
     69 int
     70 main(int argc, char *argv[])
     71 {
     72 	struct stat sb;
     73 	int ch, exitval;
     74 	char *sourcedir;
     75 
     76 	setprogname(argv[0]);
     77 	while ((ch = getopt(argc, argv, "fhnsv")) != -1)
     78 		switch (ch) {
     79 		case 'f':
     80 			fflag = 1;
     81 			break;
     82 		case 'h':
     83 		case 'n':
     84 			hflag = 1;
     85 			break;
     86 		case 's':
     87 			sflag = 1;
     88 			break;
     89 		case 'v':
     90 			vflag = 1;
     91 			break;
     92 		case '?':
     93 		default:
     94 			usage();
     95 			/* NOTREACHED */
     96 		}
     97 
     98 	argv += optind;
     99 	argc -= optind;
    100 
    101 	if (sflag) {
    102 		linkf  = symlink;
    103 		linkch = '-';
    104 	} else {
    105 		linkf  = link;
    106 		linkch = '=';
    107 	}
    108 
    109 	switch(argc) {
    110 	case 0:
    111 		usage();
    112 		/* NOTREACHED */
    113 	case 1:				/* ln target */
    114 		exit(linkit(argv[0], ".", 1));
    115 		/* NOTREACHED */
    116 	case 2:				/* ln target source */
    117 		exit(linkit(argv[0], argv[1], 0));
    118 		/* NOTREACHED */
    119 	}
    120 
    121 					/* ln target1 target2 directory */
    122 	sourcedir = argv[argc - 1];
    123 	if (hflag && lstat(sourcedir, &sb) == 0 && S_ISLNK(sb.st_mode)) {
    124 		/* we were asked not to follow symlinks, but found one at
    125 		   the target--simulate "not a directory" error */
    126 		errno = ENOTDIR;
    127 		err(EXIT_FAILURE, "%s", sourcedir);
    128 		/* NOTREACHED */
    129 	}
    130 	if (stat(sourcedir, &sb)) {
    131 		err(EXIT_FAILURE, "%s", sourcedir);
    132 		/* NOTREACHED */
    133 	}
    134 	if (!S_ISDIR(sb.st_mode)) {
    135 		usage();
    136 		/* NOTREACHED */
    137 	}
    138 	for (exitval = 0; *argv != sourcedir; ++argv)
    139 		exitval |= linkit(*argv, sourcedir, 1);
    140 	exit(exitval);
    141 	/* NOTREACHED */
    142 }
    143 
    144 int
    145 linkit(const char *target, const char *source, int isdir)
    146 {
    147 	struct stat sb;
    148 	const char *p;
    149 	char path[MAXPATHLEN];
    150 
    151 	if (!sflag) {
    152 		/* If target doesn't exist, quit now. */
    153 		if (stat(target, &sb)) {
    154 			warn("%s", target);
    155 			return (1);
    156 		}
    157 	}
    158 
    159 	/* If the source is a directory (and not a symlink if hflag),
    160 	   append the target's name. */
    161 	if (isdir ||
    162 	    (!lstat(source, &sb) && S_ISDIR(sb.st_mode)) ||
    163 	    (!hflag && !stat(source, &sb) && S_ISDIR(sb.st_mode))) {
    164 		if ((p = strrchr(target, '/')) == NULL)
    165 			p = target;
    166 		else
    167 			++p;
    168 		(void)snprintf(path, sizeof(path), "%s/%s", source, p);
    169 		source = path;
    170 	}
    171 
    172 	/*
    173 	 * If the file exists, and -f was specified, unlink it.
    174 	 * Attempt the link.
    175 	 */
    176 	if ((fflag && unlink(source) < 0 && errno != ENOENT) ||
    177 	    (*linkf)(target, source)) {
    178 		warn("%s", source);
    179 		return (1);
    180 	}
    181 	if (vflag)
    182 		(void)printf("%s %c> %s\n", source, linkch, target);
    183 
    184 	return (0);
    185 }
    186 
    187 void
    188 usage(void)
    189 {
    190 
    191 	(void)fprintf(stderr,
    192 	    "usage:\t%s [-fhnsv] file1 file2\n\t%s [-fhnsv] file ... directory\n",
    193 	    getprogname(), getprogname());
    194 	exit(1);
    195 	/* NOTREACHED */
    196 }
    197