Home | History | Annotate | Line # | Download | only in mv
mv.c revision 1.30
      1  1.30  jschauma /* $NetBSD: mv.c,v 1.30 2003/08/04 22:31:25 jschauma Exp $ */
      2   1.9       cgd 
      3   1.1       cgd /*
      4   1.8   mycroft  * Copyright (c) 1989, 1993, 1994
      5   1.8   mycroft  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * This code is derived from software contributed to Berkeley by
      8   1.1       cgd  * Ken Smith of The State University of New York at Buffalo.
      9   1.1       cgd  *
     10   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     11   1.1       cgd  * modification, are permitted provided that the following conditions
     12   1.1       cgd  * are met:
     13   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     14   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     15   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     17   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     18   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     19   1.1       cgd  *    must display the following acknowledgement:
     20   1.1       cgd  *	This product includes software developed by the University of
     21   1.1       cgd  *	California, Berkeley and its contributors.
     22   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     23   1.1       cgd  *    may be used to endorse or promote products derived from this software
     24   1.1       cgd  *    without specific prior written permission.
     25   1.1       cgd  *
     26   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36   1.1       cgd  * SUCH DAMAGE.
     37   1.1       cgd  */
     38   1.1       cgd 
     39  1.11  christos #include <sys/cdefs.h>
     40   1.1       cgd #ifndef lint
     41  1.11  christos __COPYRIGHT("@(#) Copyright (c) 1989, 1993, 1994\n\
     42  1.11  christos 	The Regents of the University of California.  All rights reserved.\n");
     43   1.1       cgd #endif /* not lint */
     44   1.1       cgd 
     45   1.1       cgd #ifndef lint
     46   1.9       cgd #if 0
     47   1.9       cgd static char sccsid[] = "@(#)mv.c	8.2 (Berkeley) 4/2/94";
     48   1.9       cgd #else
     49  1.30  jschauma __RCSID("$NetBSD: mv.c,v 1.30 2003/08/04 22:31:25 jschauma Exp $");
     50   1.9       cgd #endif
     51   1.1       cgd #endif /* not lint */
     52   1.1       cgd 
     53   1.1       cgd #include <sys/param.h>
     54   1.1       cgd #include <sys/time.h>
     55   1.1       cgd #include <sys/wait.h>
     56   1.1       cgd #include <sys/stat.h>
     57   1.8   mycroft 
     58   1.8   mycroft #include <err.h>
     59   1.8   mycroft #include <errno.h>
     60   1.1       cgd #include <fcntl.h>
     61  1.27       wiz #include <grp.h>
     62  1.14    kleink #include <locale.h>
     63  1.27       wiz #include <pwd.h>
     64   1.1       cgd #include <stdio.h>
     65   1.1       cgd #include <stdlib.h>
     66   1.1       cgd #include <string.h>
     67   1.8   mycroft #include <unistd.h>
     68  1.30  jschauma #include <vis.h>
     69   1.8   mycroft 
     70   1.1       cgd #include "pathnames.h"
     71   1.1       cgd 
     72  1.28       jrf int fflg, iflg, vflg;
     73  1.30  jschauma int stdin_ok, stdout_ok;
     74   1.1       cgd 
     75  1.30  jschauma int	copy(char *, char *);
     76  1.30  jschauma int	do_move(char *, char *);
     77  1.30  jschauma int	fastcopy(char *, char *, struct stat *);
     78  1.30  jschauma void	usage(void);
     79  1.30  jschauma int	main(int, char *[]);
     80  1.30  jschauma char	*printescaped(const char *);
     81   1.8   mycroft 
     82   1.8   mycroft int
     83  1.27       wiz main(int argc, char *argv[])
     84   1.1       cgd {
     85  1.27       wiz 	int baselen, ch, len, rval;
     86  1.10       tls 	char *p, *endp;
     87   1.1       cgd 	struct stat sb;
     88   1.1       cgd 	char path[MAXPATHLEN + 1];
     89   1.1       cgd 
     90  1.27       wiz 	setprogname(argv[0]);
     91  1.17   mycroft 	(void)setlocale(LC_ALL, "");
     92  1.14    kleink 
     93  1.28       jrf 	while ((ch = getopt(argc, argv, "ifv")) != -1)
     94   1.8   mycroft 		switch (ch) {
     95   1.1       cgd 		case 'i':
     96   1.5       jtc 			fflg = 0;
     97   1.1       cgd 			iflg = 1;
     98   1.1       cgd 			break;
     99   1.1       cgd 		case 'f':
    100   1.5       jtc 			iflg = 0;
    101   1.1       cgd 			fflg = 1;
    102   1.1       cgd 			break;
    103  1.28       jrf 		case 'v':
    104  1.28       jrf 			vflg = 1;
    105  1.28       jrf 			break;
    106   1.1       cgd 		case '?':
    107   1.1       cgd 		default:
    108   1.1       cgd 			usage();
    109   1.1       cgd 		}
    110   1.5       jtc 	argc -= optind;
    111   1.1       cgd 	argv += optind;
    112   1.1       cgd 
    113   1.1       cgd 	if (argc < 2)
    114   1.1       cgd 		usage();
    115   1.1       cgd 
    116   1.6       jtc 	stdin_ok = isatty(STDIN_FILENO);
    117  1.30  jschauma 	stdout_ok = isatty(STDOUT_FILENO);
    118   1.6       jtc 
    119   1.1       cgd 	/*
    120   1.1       cgd 	 * If the stat on the target fails or the target isn't a directory,
    121   1.1       cgd 	 * try the move.  More than 2 arguments is an error in this case.
    122   1.1       cgd 	 */
    123   1.1       cgd 	if (stat(argv[argc - 1], &sb) || !S_ISDIR(sb.st_mode)) {
    124   1.1       cgd 		if (argc > 2)
    125   1.1       cgd 			usage();
    126   1.1       cgd 		exit(do_move(argv[0], argv[1]));
    127   1.1       cgd 	}
    128   1.1       cgd 
    129   1.1       cgd 	/* It's a directory, move each file into it. */
    130  1.29    itojun 	(void)strlcpy(path, argv[argc - 1], sizeof(path));
    131   1.1       cgd 	baselen = strlen(path);
    132   1.1       cgd 	endp = &path[baselen];
    133   1.1       cgd 	*endp++ = '/';
    134   1.1       cgd 	++baselen;
    135   1.8   mycroft 	for (rval = 0; --argc; ++argv) {
    136  1.14    kleink 		p = *argv + strlen(*argv) - 1;
    137  1.14    kleink 		while (*p == '/' && p != *argv)
    138  1.14    kleink 			*p-- = '\0';
    139   1.8   mycroft 		if ((p = strrchr(*argv, '/')) == NULL)
    140   1.1       cgd 			p = *argv;
    141   1.1       cgd 		else
    142   1.1       cgd 			++p;
    143  1.14    kleink 
    144   1.8   mycroft 		if ((baselen + (len = strlen(p))) >= MAXPATHLEN) {
    145  1.30  jschauma 			char *fn = printescaped(*argv);
    146  1.30  jschauma 			warnx("%s: destination pathname too long", fn);
    147  1.30  jschauma 			free(fn);
    148   1.8   mycroft 			rval = 1;
    149   1.8   mycroft 		} else {
    150   1.8   mycroft 			memmove(endp, p, len + 1);
    151   1.8   mycroft 			if (do_move(*argv, path))
    152   1.8   mycroft 				rval = 1;
    153   1.1       cgd 		}
    154   1.1       cgd 	}
    155   1.8   mycroft 	exit(rval);
    156  1.18   mycroft 	/* NOTREACHED */
    157   1.1       cgd }
    158   1.1       cgd 
    159   1.8   mycroft int
    160  1.27       wiz do_move(char *from, char *to)
    161   1.1       cgd {
    162   1.1       cgd 	struct stat sb;
    163   1.8   mycroft 	char modep[15];
    164  1.30  jschauma 	char *fn, *tn;
    165  1.30  jschauma 
    166  1.30  jschauma 	fn = printescaped(from);
    167  1.30  jschauma 	tn = printescaped(to);
    168   1.1       cgd 
    169   1.8   mycroft 	/*
    170   1.8   mycroft 	 * (1)	If the destination path exists, the -f option is not specified
    171   1.6       jtc 	 *	and either of the following conditions are true:
    172   1.6       jtc 	 *
    173  1.24  jdolecek 	 *	(a) The permissions of the destination path do not permit
    174   1.6       jtc 	 *	    writing and the standard input is a terminal.
    175   1.6       jtc 	 *	(b) The -i option is specified.
    176   1.6       jtc 	 *
    177   1.8   mycroft 	 *	the mv utility shall write a prompt to standard error and
    178   1.6       jtc 	 *	read a line from standard input.  If the response is not
    179   1.6       jtc 	 *	affirmative, mv shall do nothing more with the current
    180   1.6       jtc 	 *	source file...
    181   1.1       cgd 	 */
    182   1.1       cgd 	if (!fflg && !access(to, F_OK)) {
    183   1.8   mycroft 		int ask = 1;
    184   1.6       jtc 		int ch;
    185   1.6       jtc 
    186   1.1       cgd 		if (iflg) {
    187  1.21  sommerfe 			if (access(from, F_OK)) {
    188  1.30  jschauma 				warn("rename %s", fn);
    189  1.30  jschauma 				free(fn);
    190  1.30  jschauma 				free(tn);
    191  1.21  sommerfe 				return (1);
    192  1.21  sommerfe 			}
    193  1.30  jschauma 			(void)fprintf(stderr, "overwrite %s? ", tn);
    194   1.6       jtc 		} else if (stdin_ok && access(to, W_OK) && !stat(to, &sb)) {
    195  1.21  sommerfe 			if (access(from, F_OK)) {
    196  1.30  jschauma 				warn("rename %s", fn);
    197  1.30  jschauma 				free(fn);
    198  1.30  jschauma 				free(tn);
    199  1.21  sommerfe 				return (1);
    200  1.21  sommerfe 			}
    201   1.8   mycroft 			strmode(sb.st_mode, modep);
    202   1.8   mycroft 			(void)fprintf(stderr, "override %s%s%s/%s for %s? ",
    203   1.8   mycroft 			    modep + 1, modep[9] == ' ' ? "" : " ",
    204   1.8   mycroft 			    user_from_uid(sb.st_uid, 0),
    205  1.30  jschauma 			    group_from_gid(sb.st_gid, 0), tn);
    206   1.8   mycroft 		} else
    207   1.8   mycroft 			ask = 0;
    208   1.1       cgd 		if (ask) {
    209   1.1       cgd 			if ((ch = getchar()) != EOF && ch != '\n')
    210   1.1       cgd 				while (getchar() != '\n');
    211  1.30  jschauma 			if (ch != 'y' && ch != 'Y') {
    212  1.30  jschauma 				free(fn);
    213  1.30  jschauma 				free(tn);
    214   1.8   mycroft 				return (0);
    215  1.30  jschauma 			}
    216   1.1       cgd 		}
    217   1.1       cgd 	}
    218   1.6       jtc 
    219   1.8   mycroft 	/*
    220   1.8   mycroft 	 * (2)	If rename() succeeds, mv shall do nothing more with the
    221   1.6       jtc 	 *	current source file.  If it fails for any other reason than
    222   1.6       jtc 	 *	EXDEV, mv shall write a diagnostic message to the standard
    223   1.6       jtc 	 *	error and do nothing more with the current source file.
    224   1.6       jtc 	 *
    225   1.6       jtc 	 * (3)	If the destination path exists, and it is a file of type
    226   1.6       jtc 	 *	directory and source_file is not a file of type directory,
    227   1.6       jtc 	 *	or it is a file not of type directory, and source file is
    228   1.8   mycroft 	 *	a file of type directory, mv shall write a diagnostic
    229   1.6       jtc 	 *	message to standard error, and do nothing more with the
    230   1.6       jtc 	 *	current source file...
    231   1.6       jtc 	 */
    232  1.28       jrf 	if (!rename(from, to)) {
    233  1.28       jrf 		if (vflg)
    234  1.30  jschauma 			printf("%s -> %s\n", fn, tn);
    235  1.30  jschauma 		free(fn);
    236  1.30  jschauma 		free(tn);
    237   1.8   mycroft 		return (0);
    238  1.28       jrf 	}
    239   1.1       cgd 
    240   1.1       cgd 	if (errno != EXDEV) {
    241  1.30  jschauma 		warn("rename %s to %s", fn, tn);
    242  1.30  jschauma 		free(fn);
    243  1.30  jschauma 		free(tn);
    244   1.8   mycroft 		return (1);
    245   1.1       cgd 	}
    246   1.1       cgd 
    247   1.8   mycroft 	/*
    248   1.8   mycroft 	 * (4)	If the destination path exists, mv shall attempt to remove it.
    249   1.8   mycroft 	 *	If this fails for any reason, mv shall write a diagnostic
    250   1.6       jtc 	 *	message to the standard error and do nothing more with the
    251   1.6       jtc 	 *	current source file...
    252   1.6       jtc 	 */
    253  1.15     mikel 	if (!lstat(to, &sb)) {
    254   1.6       jtc 		if ((S_ISDIR(sb.st_mode)) ? rmdir(to) : unlink(to)) {
    255  1.30  jschauma 			warn("can't remove %s", tn);
    256  1.30  jschauma 			free(fn);
    257  1.30  jschauma 			free(tn);
    258   1.6       jtc 			return (1);
    259   1.6       jtc 		}
    260   1.6       jtc 	}
    261   1.6       jtc 
    262   1.8   mycroft 	/*
    263   1.8   mycroft 	 * (5)	The file hierarchy rooted in source_file shall be duplicated
    264  1.15     mikel 	 *	as a file hierarchy rooted in the destination path...
    265   1.1       cgd 	 */
    266  1.15     mikel 	if (lstat(from, &sb)) {
    267  1.30  jschauma 		warn("%s", fn);
    268  1.30  jschauma 		free(fn);
    269  1.30  jschauma 		free(tn);
    270   1.8   mycroft 		return (1);
    271   1.1       cgd 	}
    272  1.30  jschauma 
    273  1.30  jschauma 	free(fn);
    274  1.30  jschauma 	free(tn);
    275  1.30  jschauma 
    276   1.8   mycroft 	return (S_ISREG(sb.st_mode) ?
    277   1.1       cgd 	    fastcopy(from, to, &sb) : copy(from, to));
    278   1.1       cgd }
    279   1.1       cgd 
    280   1.8   mycroft int
    281  1.27       wiz fastcopy(char *from, char *to, struct stat *sbp)
    282   1.1       cgd {
    283   1.1       cgd 	struct timeval tval[2];
    284   1.1       cgd 	static u_int blen;
    285   1.1       cgd 	static char *bp;
    286  1.10       tls 	int nread, from_fd, to_fd;
    287  1.30  jschauma 	char *fn, *tn;
    288  1.30  jschauma 
    289  1.30  jschauma 	fn = printescaped(from);
    290  1.30  jschauma 	tn = printescaped(to);
    291   1.1       cgd 
    292   1.1       cgd 	if ((from_fd = open(from, O_RDONLY, 0)) < 0) {
    293  1.30  jschauma 		warn("%s", fn);
    294  1.30  jschauma 		free(fn);
    295  1.30  jschauma 		free(tn);
    296   1.8   mycroft 		return (1);
    297   1.1       cgd 	}
    298   1.8   mycroft 	if ((to_fd =
    299   1.8   mycroft 	    open(to, O_CREAT | O_TRUNC | O_WRONLY, sbp->st_mode)) < 0) {
    300  1.30  jschauma 		warn("%s", tn);
    301  1.30  jschauma 		free(fn);
    302  1.30  jschauma 		free(tn);
    303   1.1       cgd 		(void)close(from_fd);
    304   1.8   mycroft 		return (1);
    305   1.1       cgd 	}
    306   1.1       cgd 	if (!blen && !(bp = malloc(blen = sbp->st_blksize))) {
    307  1.23  drochner 		warn(NULL);
    308  1.30  jschauma 		free(fn);
    309  1.30  jschauma 		free(tn);
    310   1.8   mycroft 		return (1);
    311   1.1       cgd 	}
    312   1.1       cgd 	while ((nread = read(from_fd, bp, blen)) > 0)
    313   1.1       cgd 		if (write(to_fd, bp, nread) != nread) {
    314  1.30  jschauma 			warn("%s", tn);
    315   1.1       cgd 			goto err;
    316   1.1       cgd 		}
    317   1.1       cgd 	if (nread < 0) {
    318  1.30  jschauma 		warn("%s", fn);
    319   1.8   mycroft err:		if (unlink(to))
    320  1.30  jschauma 			warn("%s: remove", tn);
    321   1.1       cgd 		(void)close(from_fd);
    322   1.1       cgd 		(void)close(to_fd);
    323  1.30  jschauma 		free(fn);
    324  1.30  jschauma 		free(tn);
    325   1.8   mycroft 		return (1);
    326   1.1       cgd 	}
    327   1.8   mycroft 	(void)close(from_fd);
    328  1.22  christos #ifdef BSD4_4
    329  1.16   mycroft 	TIMESPEC_TO_TIMEVAL(&tval[0], &sbp->st_atimespec);
    330  1.16   mycroft 	TIMESPEC_TO_TIMEVAL(&tval[1], &sbp->st_mtimespec);
    331  1.22  christos #else
    332  1.22  christos 	tval[0].tv_sec = sbp->st_atime;
    333  1.22  christos 	tval[1].tv_sec = sbp->st_mtime;
    334  1.22  christos 	tval[0].tv_usec = 0;
    335  1.22  christos 	tval[1].tv_usec = 0;
    336  1.22  christos #endif
    337  1.20  christos #ifdef __SVR4
    338  1.20  christos 	if (utimes(to, tval))
    339  1.20  christos #else
    340  1.16   mycroft 	if (futimes(to_fd, tval))
    341  1.20  christos #endif
    342  1.30  jschauma 		warn("%s: set times", tn);
    343  1.16   mycroft 	if (fchown(to_fd, sbp->st_uid, sbp->st_gid)) {
    344  1.13   hubertf 		if (errno != EPERM)
    345  1.30  jschauma 			warn("%s: set owner/group", tn);
    346  1.16   mycroft 		sbp->st_mode &= ~(S_ISUID | S_ISGID);
    347  1.16   mycroft 	}
    348   1.8   mycroft 	if (fchmod(to_fd, sbp->st_mode))
    349  1.30  jschauma 		warn("%s: set mode", tn);
    350  1.26   darrenr 	if (fchflags(to_fd, sbp->st_flags) && (errno != EOPNOTSUPP))
    351  1.30  jschauma 		warn("%s: set flags (was: 0%07o)", tn, sbp->st_flags);
    352   1.8   mycroft 
    353   1.8   mycroft 	if (close(to_fd)) {
    354  1.30  jschauma 		warn("%s", tn);
    355  1.30  jschauma 		free(fn);
    356  1.30  jschauma 		free(tn);
    357   1.8   mycroft 		return (1);
    358   1.8   mycroft 	}
    359   1.8   mycroft 
    360   1.8   mycroft 	if (unlink(from)) {
    361  1.30  jschauma 		warn("%s: remove", fn);
    362  1.30  jschauma 		free(fn);
    363  1.30  jschauma 		free(tn);
    364   1.8   mycroft 		return (1);
    365   1.8   mycroft 	}
    366  1.28       jrf 
    367  1.28       jrf 	if (vflg)
    368  1.30  jschauma 		printf("%s -> %s\n", fn, tn);
    369  1.28       jrf 
    370  1.30  jschauma 	free(fn);
    371  1.30  jschauma 	free(tn);
    372   1.8   mycroft 	return (0);
    373   1.1       cgd }
    374   1.1       cgd 
    375   1.8   mycroft int
    376  1.27       wiz copy(char *from, char *to)
    377   1.1       cgd {
    378   1.1       cgd 	int pid, status;
    379   1.1       cgd 
    380   1.8   mycroft 	if ((pid = vfork()) == 0) {
    381  1.28       jrf 		execl(_PATH_CP, "mv", vflg ? "-PRpv" : "-PRp", from, to, NULL);
    382   1.8   mycroft 		warn("%s", _PATH_CP);
    383   1.1       cgd 		_exit(1);
    384   1.1       cgd 	}
    385   1.8   mycroft 	if (waitpid(pid, &status, 0) == -1) {
    386   1.8   mycroft 		warn("%s: waitpid", _PATH_CP);
    387   1.8   mycroft 		return (1);
    388   1.8   mycroft 	}
    389   1.8   mycroft 	if (!WIFEXITED(status)) {
    390   1.8   mycroft 		warn("%s: did not terminate normally", _PATH_CP);
    391   1.8   mycroft 		return (1);
    392   1.8   mycroft 	}
    393   1.8   mycroft 	if (WEXITSTATUS(status)) {
    394   1.8   mycroft 		warn("%s: terminated with %d (non-zero) status",
    395   1.8   mycroft 		    _PATH_CP, WEXITSTATUS(status));
    396   1.8   mycroft 		return (1);
    397   1.8   mycroft 	}
    398   1.1       cgd 	if (!(pid = vfork())) {
    399   1.1       cgd 		execl(_PATH_RM, "mv", "-rf", from, NULL);
    400   1.8   mycroft 		warn("%s", _PATH_RM);
    401   1.1       cgd 		_exit(1);
    402   1.1       cgd 	}
    403   1.8   mycroft 	if (waitpid(pid, &status, 0) == -1) {
    404   1.8   mycroft 		warn("%s: waitpid", _PATH_RM);
    405   1.8   mycroft 		return (1);
    406   1.8   mycroft 	}
    407   1.8   mycroft 	if (!WIFEXITED(status)) {
    408   1.8   mycroft 		warn("%s: did not terminate normally", _PATH_RM);
    409   1.8   mycroft 		return (1);
    410   1.8   mycroft 	}
    411   1.8   mycroft 	if (WEXITSTATUS(status)) {
    412   1.8   mycroft 		warn("%s: terminated with %d (non-zero) status",
    413   1.8   mycroft 		    _PATH_RM, WEXITSTATUS(status));
    414   1.8   mycroft 		return (1);
    415   1.8   mycroft 	}
    416   1.8   mycroft 	return (0);
    417   1.1       cgd }
    418   1.1       cgd 
    419   1.8   mycroft void
    420  1.27       wiz usage(void)
    421   1.1       cgd {
    422  1.28       jrf 	(void)fprintf(stderr, "usage: %s [-fiv] source target\n"
    423  1.28       jrf 	    "       %s [-fiv] source ... directory\n", getprogname(),
    424  1.27       wiz 	    getprogname());
    425   1.1       cgd 	exit(1);
    426  1.18   mycroft 	/* NOTREACHED */
    427  1.30  jschauma }
    428  1.30  jschauma 
    429  1.30  jschauma char *
    430  1.30  jschauma printescaped(const char *src)
    431  1.30  jschauma {
    432  1.30  jschauma 	size_t len;
    433  1.30  jschauma 	char *retval;
    434  1.30  jschauma 
    435  1.30  jschauma 	len = strlen(src);
    436  1.30  jschauma 	if (len != 0 && SIZE_T_MAX/len <= 4) {
    437  1.30  jschauma 		errx(EXIT_FAILURE, "%s: name too long", src);
    438  1.30  jschauma 		/* NOTREACHED */
    439  1.30  jschauma 	}
    440  1.30  jschauma 
    441  1.30  jschauma 	retval = (char *)malloc(4*len+1);
    442  1.30  jschauma 	if (retval != NULL) {
    443  1.30  jschauma 		if (stdout_ok)
    444  1.30  jschauma 			(void)strvis(retval, src, VIS_NL | VIS_CSTYLE);
    445  1.30  jschauma 		else
    446  1.30  jschauma 			(void)strcpy(retval, src);
    447  1.30  jschauma 		return retval;
    448  1.30  jschauma 	} else
    449  1.30  jschauma 		errx(EXIT_FAILURE, "out of memory!");
    450  1.30  jschauma 		/* NOTREACHED */
    451   1.1       cgd }
    452