Home | History | Annotate | Line # | Download | only in rm
rm.c revision 1.36
      1  1.36       agc /* $NetBSD: rm.c,v 1.36 2003/08/07 09:05:28 agc Exp $ */
      2  1.18       cgd 
      3   1.1       cgd /*-
      4  1.32       jrf  * Copyright (c) 1990, 1993, 1994, 2003
      5  1.15   mycroft  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15  1.36       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1       cgd  *    may be used to endorse or promote products derived from this software
     17   1.1       cgd  *    without specific prior written permission.
     18   1.1       cgd  *
     19   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1       cgd  * SUCH DAMAGE.
     30   1.1       cgd  */
     31   1.1       cgd 
     32  1.20  christos #include <sys/cdefs.h>
     33   1.1       cgd #ifndef lint
     34  1.20  christos __COPYRIGHT("@(#) Copyright (c) 1990, 1993, 1994\n\
     35  1.20  christos 	The Regents of the University of California.  All rights reserved.\n");
     36   1.1       cgd #endif /* not lint */
     37   1.1       cgd 
     38   1.1       cgd #ifndef lint
     39  1.18       cgd #if 0
     40  1.19       jtc static char sccsid[] = "@(#)rm.c	8.8 (Berkeley) 4/27/95";
     41  1.18       cgd #else
     42  1.36       agc __RCSID("$NetBSD: rm.c,v 1.36 2003/08/07 09:05:28 agc Exp $");
     43  1.18       cgd #endif
     44   1.1       cgd #endif /* not lint */
     45   1.1       cgd 
     46  1.35  jschauma #include <sys/param.h>
     47  1.35  jschauma #include <sys/stat.h>
     48  1.15   mycroft #include <sys/types.h>
     49  1.15   mycroft 
     50  1.15   mycroft #include <err.h>
     51  1.15   mycroft #include <errno.h>
     52  1.15   mycroft #include <fcntl.h>
     53  1.15   mycroft #include <fts.h>
     54  1.27       wiz #include <grp.h>
     55  1.35  jschauma #include <locale.h>
     56  1.27       wiz #include <pwd.h>
     57   1.7       jtc #include <stdio.h>
     58  1.15   mycroft #include <stdlib.h>
     59   1.7       jtc #include <string.h>
     60   1.7       jtc #include <unistd.h>
     61  1.35  jschauma #include <vis.h>
     62   1.7       jtc 
     63  1.35  jschauma int dflag, eval, fflag, iflag, Pflag, stdin_ok, stdout_ok, vflag, Wflag;
     64   1.1       cgd 
     65  1.27       wiz int	check(char *, char *, struct stat *);
     66  1.27       wiz void	checkdot(char **);
     67  1.35  jschauma char 	*printescaped(const char *);
     68  1.27       wiz void	rm_file(char **);
     69  1.27       wiz void	rm_overwrite(char *, struct stat *);
     70  1.27       wiz void	rm_tree(char **);
     71  1.27       wiz void	usage(void);
     72  1.27       wiz int	main(int, char *[]);
     73   1.1       cgd 
     74   1.1       cgd /*
     75  1.21    kleink  * For the sake of the `-f' flag, check whether an error number indicates the
     76  1.21    kleink  * failure of an operation due to an non-existent file, either per se (ENOENT)
     77  1.21    kleink  * or because its filename argument was illegal (ENAMETOOLONG, ENOTDIR).
     78  1.21    kleink  */
     79  1.21    kleink #define NONEXISTENT(x) \
     80  1.21    kleink     ((x) == ENOENT || (x) == ENAMETOOLONG || (x) == ENOTDIR)
     81  1.21    kleink 
     82  1.21    kleink /*
     83   1.1       cgd  * rm --
     84   1.1       cgd  *	This rm is different from historic rm's, but is expected to match
     85   1.1       cgd  *	POSIX 1003.2 behavior.  The most visible difference is that -f
     86   1.1       cgd  *	has two specific effects now, ignore non-existent files and force
     87   1.1       cgd  * 	file removal.
     88   1.1       cgd  */
     89   1.7       jtc int
     90  1.27       wiz main(int argc, char *argv[])
     91   1.1       cgd {
     92   1.1       cgd 	int ch, rflag;
     93   1.1       cgd 
     94  1.27       wiz 	setprogname(argv[0]);
     95  1.22   mycroft 	(void)setlocale(LC_ALL, "");
     96   1.7       jtc 
     97  1.15   mycroft 	Pflag = rflag = 0;
     98  1.32       jrf 	while ((ch = getopt(argc, argv, "dfiPRrvW")) != -1)
     99  1.30     enami 		switch (ch) {
    100   1.1       cgd 		case 'd':
    101   1.1       cgd 			dflag = 1;
    102   1.1       cgd 			break;
    103   1.1       cgd 		case 'f':
    104   1.1       cgd 			fflag = 1;
    105   1.1       cgd 			iflag = 0;
    106   1.1       cgd 			break;
    107   1.1       cgd 		case 'i':
    108   1.1       cgd 			fflag = 0;
    109   1.1       cgd 			iflag = 1;
    110   1.1       cgd 			break;
    111  1.15   mycroft 		case 'P':
    112  1.15   mycroft 			Pflag = 1;
    113  1.15   mycroft 			break;
    114   1.1       cgd 		case 'R':
    115  1.15   mycroft 		case 'r':			/* Compatibility. */
    116   1.1       cgd 			rflag = 1;
    117   1.1       cgd 			break;
    118  1.32       jrf 		case 'v':
    119  1.32       jrf 			vflag = 1;
    120  1.32       jrf 			break;
    121  1.17   mycroft 		case 'W':
    122  1.17   mycroft 			Wflag = 1;
    123  1.17   mycroft 			break;
    124   1.1       cgd 		case '?':
    125   1.1       cgd 		default:
    126   1.1       cgd 			usage();
    127   1.1       cgd 		}
    128   1.1       cgd 	argc -= optind;
    129   1.1       cgd 	argv += optind;
    130   1.1       cgd 
    131   1.1       cgd 	if (argc < 1)
    132   1.1       cgd 		usage();
    133   1.1       cgd 
    134   1.1       cgd 	checkdot(argv);
    135   1.1       cgd 
    136  1.11       jtc 	if (*argv) {
    137  1.11       jtc 		stdin_ok = isatty(STDIN_FILENO);
    138  1.35  jschauma 		stdout_ok = isatty(STDOUT_FILENO);
    139   1.1       cgd 
    140  1.11       jtc 		if (rflag)
    141  1.15   mycroft 			rm_tree(argv);
    142  1.11       jtc 		else
    143  1.15   mycroft 			rm_file(argv);
    144  1.11       jtc 	}
    145   1.9       jtc 
    146  1.24   mycroft 	exit(eval);
    147  1.24   mycroft 	/* NOTREACHED */
    148   1.1       cgd }
    149   1.1       cgd 
    150   1.7       jtc void
    151  1.27       wiz rm_tree(char **argv)
    152   1.1       cgd {
    153  1.15   mycroft 	FTS *fts;
    154  1.15   mycroft 	FTSENT *p;
    155  1.32       jrf 	int flags, needstat, rval;
    156  1.35  jschauma 	char *fn;
    157  1.35  jschauma 
    158   1.1       cgd 	/*
    159   1.1       cgd 	 * Remove a file hierarchy.  If forcing removal (-f), or interactive
    160   1.1       cgd 	 * (-i) or can't ask anyway (stdin_ok), don't stat the file.
    161   1.1       cgd 	 */
    162   1.1       cgd 	needstat = !fflag && !iflag && stdin_ok;
    163   1.1       cgd 
    164   1.1       cgd 	/*
    165   1.1       cgd 	 * If the -i option is specified, the user can skip on the pre-order
    166   1.1       cgd 	 * visit.  The fts_number field flags skipped directories.
    167   1.1       cgd 	 */
    168   1.1       cgd #define	SKIPPED	1
    169   1.1       cgd 
    170  1.17   mycroft 	flags = FTS_PHYSICAL;
    171  1.17   mycroft 	if (!needstat)
    172  1.17   mycroft 		flags |= FTS_NOSTAT;
    173  1.17   mycroft 	if (Wflag)
    174  1.17   mycroft 		flags |= FTS_WHITEOUT;
    175  1.20  christos 	if (!(fts = fts_open(argv, flags,
    176  1.31     enami 	    (int (*)(const FTSENT **, const FTSENT **))NULL)))
    177  1.26  drochner 		err(1, NULL);
    178   1.7       jtc 	while ((p = fts_read(fts)) != NULL) {
    179  1.35  jschauma 
    180  1.15   mycroft 		switch (p->fts_info) {
    181   1.1       cgd 		case FTS_DNR:
    182  1.15   mycroft 			if (!fflag || p->fts_errno != ENOENT) {
    183  1.35  jschauma 				fn = printescaped(p->fts_path);
    184  1.35  jschauma 				warnx("%s: %s", fn, strerror(p->fts_errno));
    185  1.35  jschauma 				free(fn);
    186  1.15   mycroft 				eval = 1;
    187  1.15   mycroft 			}
    188  1.15   mycroft 			continue;
    189   1.1       cgd 		case FTS_ERR:
    190  1.35  jschauma 			errx(EXIT_FAILURE, "%s: %s", printescaped(p->fts_path),
    191  1.35  jschauma 					strerror(p->fts_errno));
    192  1.24   mycroft 			/* NOTREACHED */
    193   1.1       cgd 		case FTS_NS:
    194  1.15   mycroft 			/*
    195  1.15   mycroft 			 * FTS_NS: assume that if can't stat the file, it
    196  1.15   mycroft 			 * can't be unlinked.
    197  1.15   mycroft 			 */
    198  1.28       jmc 			if (fflag && NONEXISTENT(p->fts_errno))
    199  1.28       jmc 				continue;
    200  1.28       jmc 			if (needstat) {
    201  1.35  jschauma 				fn = printescaped(p->fts_path);
    202  1.35  jschauma 				warnx("%s: %s", fn, strerror(p->fts_errno));
    203  1.35  jschauma 				free(fn);
    204  1.15   mycroft 				eval = 1;
    205  1.28       jmc 				continue;
    206  1.15   mycroft 			}
    207  1.28       jmc 			break;
    208   1.1       cgd 		case FTS_D:
    209  1.15   mycroft 			/* Pre-order: give user chance to skip. */
    210   1.8       jtc 			if (!fflag && !check(p->fts_path, p->fts_accpath,
    211   1.6   deraadt 			    p->fts_statp)) {
    212   1.1       cgd 				(void)fts_set(fts, p, FTS_SKIP);
    213   1.1       cgd 				p->fts_number = SKIPPED;
    214   1.1       cgd 			}
    215   1.1       cgd 			continue;
    216   1.1       cgd 		case FTS_DP:
    217  1.15   mycroft 			/* Post-order: see if user skipped. */
    218   1.1       cgd 			if (p->fts_number == SKIPPED)
    219   1.1       cgd 				continue;
    220   1.1       cgd 			break;
    221   1.9       jtc 		default:
    222  1.15   mycroft 			if (!fflag &&
    223  1.15   mycroft 			    !check(p->fts_path, p->fts_accpath, p->fts_statp))
    224   1.9       jtc 				continue;
    225   1.1       cgd 		}
    226   1.1       cgd 
    227  1.32       jrf 		rval = 0;
    228   1.1       cgd 		/*
    229   1.1       cgd 		 * If we can't read or search the directory, may still be
    230   1.1       cgd 		 * able to remove it.  Don't print out the un{read,search}able
    231   1.1       cgd 		 * message unless the remove fails.
    232   1.1       cgd 		 */
    233  1.17   mycroft 		switch (p->fts_info) {
    234  1.17   mycroft 		case FTS_DP:
    235  1.17   mycroft 		case FTS_DNR:
    236  1.32       jrf 			rval = rmdir(p->fts_accpath);
    237  1.32       jrf 			if (rval != 0 && fflag && errno == ENOENT)
    238   1.1       cgd 				continue;
    239  1.17   mycroft 			break;
    240  1.17   mycroft 
    241  1.17   mycroft 		case FTS_W:
    242  1.32       jrf 			rval = undelete(p->fts_accpath);
    243  1.34     enami 			if (rval != 0 && fflag && errno == ENOENT)
    244  1.17   mycroft 				continue;
    245  1.17   mycroft 			break;
    246  1.17   mycroft 
    247  1.17   mycroft 		default:
    248  1.15   mycroft 			if (Pflag)
    249  1.15   mycroft 				rm_overwrite(p->fts_accpath, NULL);
    250  1.32       jrf 			rval = unlink(p->fts_accpath);
    251  1.32       jrf 			if (rval != 0 && fflag && NONEXISTENT(errno))
    252  1.14       jtc 				continue;
    253  1.32       jrf 			break;
    254  1.14       jtc 		}
    255  1.32       jrf 		if (rval != 0) {
    256  1.35  jschauma 			fn = printescaped(p->fts_path);
    257  1.35  jschauma 			warn("%s", fn);
    258  1.35  jschauma 			free(fn);
    259  1.32       jrf 			eval = 1;
    260  1.35  jschauma 		} else if (vflag) {
    261  1.35  jschauma 			fn = printescaped(p->fts_path);
    262  1.35  jschauma 			(void)printf("%s\n", fn);
    263  1.35  jschauma 			free(fn);
    264  1.35  jschauma 		}
    265   1.1       cgd 	}
    266  1.15   mycroft 	if (errno)
    267  1.15   mycroft 		err(1, "fts_read");
    268   1.1       cgd }
    269   1.1       cgd 
    270   1.7       jtc void
    271  1.27       wiz rm_file(char **argv)
    272   1.1       cgd {
    273   1.1       cgd 	struct stat sb;
    274  1.15   mycroft 	int rval;
    275  1.35  jschauma 	char *f, *fn;
    276   1.1       cgd 
    277   1.1       cgd 	/*
    278   1.1       cgd 	 * Remove a file.  POSIX 1003.2 states that, by default, attempting
    279   1.1       cgd 	 * to remove a directory is an error, so must always stat the file.
    280   1.1       cgd 	 */
    281   1.7       jtc 	while ((f = *argv++) != NULL) {
    282  1.15   mycroft 		/* Assume if can't stat the file, can't unlink it. */
    283   1.1       cgd 		if (lstat(f, &sb)) {
    284  1.17   mycroft 			if (Wflag) {
    285  1.17   mycroft 				sb.st_mode = S_IFWHT|S_IWUSR|S_IRUSR;
    286  1.17   mycroft 			} else {
    287  1.21    kleink 				if (!fflag || !NONEXISTENT(errno)) {
    288  1.35  jschauma 					fn = printescaped(f);
    289  1.35  jschauma 					warn("%s", fn);
    290  1.35  jschauma 					free(fn);
    291  1.17   mycroft 					eval = 1;
    292  1.17   mycroft 				}
    293  1.17   mycroft 				continue;
    294   1.9       jtc 			}
    295  1.17   mycroft 		} else if (Wflag) {
    296  1.35  jschauma 			fn = printescaped(f);
    297  1.35  jschauma 			warnx("%s: %s", fn, strerror(EEXIST));
    298  1.35  jschauma 			free(fn);
    299  1.17   mycroft 			eval = 1;
    300   1.1       cgd 			continue;
    301   1.1       cgd 		}
    302  1.17   mycroft 
    303   1.7       jtc 		if (S_ISDIR(sb.st_mode) && !dflag) {
    304  1.35  jschauma 			fn = printescaped(f);
    305  1.35  jschauma 			warnx("%s: is a directory", fn);
    306  1.35  jschauma 			free(fn);
    307  1.15   mycroft 			eval = 1;
    308   1.1       cgd 			continue;
    309   1.1       cgd 		}
    310  1.17   mycroft 		if (!fflag && !S_ISWHT(sb.st_mode) && !check(f, f, &sb))
    311   1.1       cgd 			continue;
    312  1.17   mycroft 		if (S_ISWHT(sb.st_mode))
    313  1.17   mycroft 			rval = undelete(f);
    314  1.17   mycroft 		else if (S_ISDIR(sb.st_mode))
    315  1.15   mycroft 			rval = rmdir(f);
    316  1.15   mycroft 		else {
    317  1.15   mycroft 			if (Pflag)
    318  1.15   mycroft 				rm_overwrite(f, &sb);
    319  1.15   mycroft 			rval = unlink(f);
    320  1.15   mycroft 		}
    321  1.21    kleink 		if (rval && (!fflag || !NONEXISTENT(errno))) {
    322  1.35  jschauma 			fn = printescaped(f);
    323  1.35  jschauma 			warn("%s", fn);
    324  1.35  jschauma 			free(fn);
    325  1.15   mycroft 			eval = 1;
    326   1.7       jtc 		}
    327  1.35  jschauma 		if (vflag && rval == 0) {
    328  1.35  jschauma 			fn = printescaped(f);
    329  1.35  jschauma 			(void)printf("%s\n", fn);
    330  1.35  jschauma 			free(fn);
    331  1.35  jschauma 		}
    332  1.15   mycroft 	}
    333  1.15   mycroft }
    334   1.7       jtc 
    335  1.15   mycroft /*
    336  1.15   mycroft  * rm_overwrite --
    337  1.15   mycroft  *	Overwrite the file 3 times with varying bit patterns.
    338  1.15   mycroft  *
    339  1.15   mycroft  * XXX
    340  1.15   mycroft  * This is a cheap way to *really* delete files.  Note that only regular
    341  1.15   mycroft  * files are deleted, directories (and therefore names) will remain.
    342  1.15   mycroft  * Also, this assumes a fixed-block file system (like FFS, or a V7 or a
    343  1.15   mycroft  * System V file system).  In a logging file system, you'll have to have
    344  1.15   mycroft  * kernel support.
    345  1.15   mycroft  */
    346  1.15   mycroft void
    347  1.27       wiz rm_overwrite(char *file, struct stat *sbp)
    348  1.15   mycroft {
    349  1.15   mycroft 	struct stat sb;
    350  1.15   mycroft 	off_t len;
    351  1.15   mycroft 	int fd, wlen;
    352  1.15   mycroft 	char buf[8 * 1024];
    353  1.35  jschauma 	char *fn;
    354  1.15   mycroft 
    355  1.15   mycroft 	fd = -1;
    356  1.15   mycroft 	if (sbp == NULL) {
    357  1.15   mycroft 		if (lstat(file, &sb))
    358  1.15   mycroft 			goto err;
    359  1.15   mycroft 		sbp = &sb;
    360   1.1       cgd 	}
    361  1.15   mycroft 	if (!S_ISREG(sbp->st_mode))
    362  1.15   mycroft 		return;
    363  1.15   mycroft 	if ((fd = open(file, O_WRONLY, 0)) == -1)
    364  1.15   mycroft 		goto err;
    365  1.15   mycroft 
    366  1.30     enami #define	PASS(byte) do {							\
    367  1.15   mycroft 	memset(buf, byte, sizeof(buf));					\
    368  1.15   mycroft 	for (len = sbp->st_size; len > 0; len -= wlen) {		\
    369  1.15   mycroft 		wlen = len < sizeof(buf) ? len : sizeof(buf);		\
    370  1.15   mycroft 		if (write(fd, buf, wlen) != wlen)			\
    371  1.15   mycroft 			goto err;					\
    372  1.15   mycroft 	}								\
    373  1.30     enami } while (/* CONSTCOND */ 0)
    374  1.15   mycroft 	PASS(0xff);
    375  1.15   mycroft 	if (fsync(fd) || lseek(fd, (off_t)0, SEEK_SET))
    376  1.15   mycroft 		goto err;
    377  1.15   mycroft 	PASS(0x00);
    378  1.15   mycroft 	if (fsync(fd) || lseek(fd, (off_t)0, SEEK_SET))
    379  1.15   mycroft 		goto err;
    380  1.15   mycroft 	PASS(0xff);
    381  1.15   mycroft 	if (!fsync(fd) && !close(fd))
    382  1.15   mycroft 		return;
    383  1.15   mycroft 
    384  1.15   mycroft err:	eval = 1;
    385  1.35  jschauma 	fn = printescaped(file);
    386  1.35  jschauma 	warn("%s", fn);
    387  1.35  jschauma 	free(fn);
    388   1.1       cgd }
    389   1.1       cgd 
    390   1.7       jtc int
    391  1.27       wiz check(char *path, char *name, struct stat *sp)
    392   1.1       cgd {
    393  1.15   mycroft 	int ch, first;
    394  1.15   mycroft 	char modep[15];
    395  1.35  jschauma 	char *fn;
    396   1.1       cgd 
    397   1.1       cgd 	/* Check -i first. */
    398  1.35  jschauma 	if (iflag) {
    399  1.35  jschauma 		fn = printescaped(path);
    400  1.35  jschauma 		(void)fprintf(stderr, "remove '%s'? ", fn);
    401  1.35  jschauma 		free(fn);
    402  1.35  jschauma 	} else {
    403   1.1       cgd 		/*
    404   1.1       cgd 		 * If it's not a symbolic link and it's unwritable and we're
    405   1.1       cgd 		 * talking to a terminal, ask.  Symbolic links are excluded
    406  1.15   mycroft 		 * because their permissions are meaningless.  Check stdin_ok
    407  1.15   mycroft 		 * first because we may not have stat'ed the file.
    408   1.1       cgd 		 */
    409  1.25        is 		if (!stdin_ok || S_ISLNK(sp->st_mode) ||
    410  1.30     enami 		    !(access(name, W_OK) && (errno != ETXTBSY)))
    411  1.15   mycroft 			return (1);
    412   1.1       cgd 		strmode(sp->st_mode, modep);
    413  1.35  jschauma 		fn =  printescaped(path);
    414  1.35  jschauma 		(void)fprintf(stderr, "override %s%s%s/%s for '%s'? ",
    415   1.8       jtc 		    modep + 1, modep[9] == ' ' ? "" : " ",
    416   1.8       jtc 		    user_from_uid(sp->st_uid, 0),
    417  1.35  jschauma 		    group_from_gid(sp->st_gid, 0), fn);
    418  1.35  jschauma 		free(fn);
    419   1.1       cgd 	}
    420   1.1       cgd 	(void)fflush(stderr);
    421   1.1       cgd 
    422   1.1       cgd 	first = ch = getchar();
    423   1.1       cgd 	while (ch != '\n' && ch != EOF)
    424   1.1       cgd 		ch = getchar();
    425  1.15   mycroft 	return (first == 'y' || first == 'Y');
    426   1.1       cgd }
    427   1.1       cgd 
    428  1.16       jtc /*
    429  1.16       jtc  * POSIX.2 requires that if "." or ".." are specified as the basename
    430  1.16       jtc  * portion of an operand, a diagnostic message be written to standard
    431  1.16       jtc  * error and nothing more be done with such operands.
    432  1.16       jtc  *
    433  1.16       jtc  * Since POSIX.2 defines basename as the final portion of a path after
    434  1.16       jtc  * trailing slashes have been removed, we'll remove them here.
    435  1.16       jtc  */
    436  1.20  christos #define ISDOT(a) ((a)[0] == '.' && (!(a)[1] || ((a)[1] == '.' && !(a)[2])))
    437   1.7       jtc void
    438  1.27       wiz checkdot(char **argv)
    439   1.1       cgd {
    440  1.15   mycroft 	char *p, **save, **t;
    441   1.1       cgd 	int complained;
    442   1.1       cgd 
    443   1.1       cgd 	complained = 0;
    444   1.1       cgd 	for (t = argv; *t;) {
    445  1.16       jtc 		/* strip trailing slashes */
    446  1.31     enami 		p = strrchr(*t, '\0');
    447  1.16       jtc 		while (--p > *t && *p == '/')
    448  1.16       jtc 			*p = '\0';
    449  1.16       jtc 
    450  1.16       jtc 		/* extract basename */
    451  1.15   mycroft 		if ((p = strrchr(*t, '/')) != NULL)
    452   1.1       cgd 			++p;
    453   1.1       cgd 		else
    454   1.1       cgd 			p = *t;
    455  1.16       jtc 
    456   1.1       cgd 		if (ISDOT(p)) {
    457   1.8       jtc 			if (!complained++)
    458  1.15   mycroft 				warnx("\".\" and \"..\" may not be removed");
    459  1.15   mycroft 			eval = 1;
    460  1.15   mycroft 			for (save = t; (t[0] = t[1]) != NULL; ++t)
    461  1.13       jtc 				continue;
    462   1.1       cgd 			t = save;
    463   1.1       cgd 		} else
    464   1.1       cgd 			++t;
    465   1.1       cgd 	}
    466  1.35  jschauma }
    467  1.35  jschauma 
    468  1.35  jschauma char *
    469  1.35  jschauma printescaped(const char *src)
    470  1.35  jschauma {
    471  1.35  jschauma 	size_t len;
    472  1.35  jschauma 	char *retval;
    473  1.35  jschauma 
    474  1.35  jschauma 	len = strlen(src);
    475  1.35  jschauma 	if (len != 0 && SIZE_T_MAX/len <= 4) {
    476  1.35  jschauma 		errx(EXIT_FAILURE, "%s: name too long", src);
    477  1.35  jschauma 		/* NOTREACHED */
    478  1.35  jschauma 	}
    479  1.35  jschauma 
    480  1.35  jschauma 	retval = (char *)malloc(4*len+1);
    481  1.35  jschauma 	if (retval != NULL) {
    482  1.35  jschauma 		if (stdout_ok)
    483  1.35  jschauma 			(void)strvis(retval, src, VIS_NL | VIS_CSTYLE);
    484  1.35  jschauma 		else
    485  1.35  jschauma 			(void)strcpy(retval, src);
    486  1.35  jschauma 		return retval;
    487  1.35  jschauma 	} else
    488  1.35  jschauma 		errx(EXIT_FAILURE, "out of memory!");
    489  1.35  jschauma 		/* NOTREACHED */
    490   1.1       cgd }
    491   1.1       cgd 
    492   1.7       jtc void
    493  1.27       wiz usage(void)
    494   1.1       cgd {
    495  1.30     enami 
    496  1.32       jrf 	(void)fprintf(stderr, "usage: %s [-f|-i] [-dPRrvW] file ...\n",
    497  1.29     soren 	    getprogname());
    498   1.1       cgd 	exit(1);
    499  1.23   mycroft 	/* NOTREACHED */
    500   1.1       cgd }
    501