As discusses a little while back on tech-userlevel:

If stdout is a tty, use vis(3) to print any filenames to prevent garbage
from being printed if the filename contains control- or other non-printable
characters.

While here, sprinkle some EXIT_FAILURE and NOTREACHED where appropriate.
This commit is contained in:
jschauma 2003-08-04 22:31:21 +00:00
parent 9254350d6a
commit 458ed23412
21 changed files with 916 additions and 260 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: cat.c,v 1.35 2002/09/13 18:07:52 thorpej Exp $ */
/* $NetBSD: cat.c,v 1.36 2003/08/04 22:31:21 jschauma Exp $ */
/*
* Copyright (c) 1989, 1993
@ -47,7 +47,7 @@ __COPYRIGHT(
#if 0
static char sccsid[] = "@(#)cat.c 8.2 (Berkeley) 4/27/95";
#else
__RCSID("$NetBSD: cat.c,v 1.35 2002/09/13 18:07:52 thorpej Exp $");
__RCSID("$NetBSD: cat.c,v 1.36 2003/08/04 22:31:21 jschauma Exp $");
#endif
#endif /* not lint */
@ -63,8 +63,9 @@ __RCSID("$NetBSD: cat.c,v 1.35 2002/09/13 18:07:52 thorpej Exp $");
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <vis.h>
int bflag, eflag, fflag, lflag, nflag, sflag, tflag, vflag;
int bflag, eflag, fflag, lflag, nflag, sflag, stdout_ok, tflag, vflag;
int rval;
const char *filename;
@ -73,6 +74,7 @@ void cook_args(char *argv[]);
void cook_buf(FILE *);
void raw_args(char *argv[]);
void raw_cat(int);
char *printescaped(const char *);
int
main(int argc, char *argv[])
@ -121,6 +123,8 @@ main(int argc, char *argv[])
}
argv += optind;
stdout_ok = isatty(STDOUT_FILENO);
if (lflag) {
stdout_lock.l_len = 0;
stdout_lock.l_start = 0;
@ -153,7 +157,10 @@ cook_args(char **argv)
fp = stdin;
else if ((fp = fopen(*argv,
fflag ? "rf" : "r")) == NULL) {
warn("%s", *argv);
char *fn;
fn = printescaped(*argv);
warn("%s", fn);
free(fn);
rval = 1;
++argv;
continue;
@ -229,7 +236,10 @@ cook_buf(FILE *fp)
break;
}
if (ferror(fp)) {
warn("%s", filename);
char *fn;
fn = printescaped(filename);
warn("%s", fn);
free(fn);
rval = 1;
clearerr(fp);
}
@ -241,6 +251,7 @@ void
raw_args(char **argv)
{
int fd;
char *fn;
fd = fileno(stdin);
filename = "stdin";
@ -260,13 +271,17 @@ raw_args(char **argv)
}
if (!S_ISREG(st.st_mode)) {
close(fd);
warnx("%s: not a regular file", *argv);
fn = printescaped(*argv);
warnx("%s: not a regular file", fn);
free(fn);
goto skipnomsg;
}
}
else if ((fd = open(*argv, O_RDONLY, 0)) < 0) {
skip:
warn("%s", *argv);
fn = printescaped(*argv);
warn("%s", fn);
free(fn);
skipnomsg:
rval = 1;
++argv;
@ -290,6 +305,7 @@ raw_cat(int rfd)
struct stat sbuf;
ssize_t nr, nw, off;
int wfd;
char *fn;
wfd = fileno(stdout);
if (buf == NULL) {
@ -308,7 +324,33 @@ raw_cat(int rfd)
if ((nw = write(wfd, buf + off, (size_t)nr)) < 0)
err(1, "stdout");
if (nr < 0) {
warn("%s", filename);
fn = printescaped(filename);
warn("%s", fn);
free(fn);
rval = 1;
}
}
char *
printescaped(const char *src)
{
size_t len;
char *retval;
len = strlen(src);
if (len != 0 && SIZE_T_MAX/len <= 4) {
errx(EXIT_FAILURE, "%s: name too long", src);
/* NOTREACHED */
}
retval = (char *)malloc(4*len+1);
if (retval != NULL) {
if (stdout_ok)
(void)strvis(retval, src, VIS_NL | VIS_CSTYLE);
else
(void)strcpy(retval, src);
return retval;
} else
errx(EXIT_FAILURE, "out of memory!");
/* NOTREACHED */
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: chio.c,v 1.19 2002/06/11 05:33:51 itojun Exp $ */
/* $NetBSD: chio.c,v 1.20 2003/08/04 22:31:21 jschauma Exp $ */
/*-
* Copyright (c) 1996, 1998, 1999 The NetBSD Foundation, Inc.
@ -46,7 +46,7 @@
__COPYRIGHT(
"@(#) Copyright (c) 1996, 1998, 1999\
The NetBSD Foundation, Inc. All rights reserved.");
__RCSID("$NetBSD: chio.c,v 1.19 2002/06/11 05:33:51 itojun Exp $");
__RCSID("$NetBSD: chio.c,v 1.20 2003/08/04 22:31:21 jschauma Exp $");
#endif
#include <sys/param.h>
@ -63,10 +63,13 @@ __RCSID("$NetBSD: chio.c,v 1.19 2002/06/11 05:33:51 itojun Exp $");
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <vis.h>
#include "defs.h"
#include "pathnames.h"
int stdout_ok;
int main(int, char *[]);
static void usage(void);
static void cleanup(void);
@ -75,6 +78,7 @@ static int parse_element_unit(const char *);
static int parse_special(const char *);
static int is_special(const char *);
static const char *bits_to_string(int, const char *);
char *printescaped(const char *);
static int do_move(const char *, int, char **);
static int do_exchange(const char *, int, char **);
@ -154,6 +158,7 @@ main(int argc, char *argv[])
break;
default:
usage();
/* NOTREACHED */
}
}
argc -= optind;
@ -161,6 +166,9 @@ main(int argc, char *argv[])
if (argc == 0)
usage();
/* NOTREACHED */
stdout_ok = isatty(STDOUT_FILENO);
/* Get the default changer if not already specified. */
if (changer_name == NULL)
@ -169,18 +177,21 @@ main(int argc, char *argv[])
/* Open the changer device. */
if ((changer_fd = open(changer_name, O_RDWR, 0600)) == -1)
err(1, "%s: open", changer_name);
err(EXIT_FAILURE, "%s: open", printescaped(changer_name));
/* NOTREACHED */
/* Register cleanup function. */
if (atexit(cleanup))
err(1, "can't register cleanup function");
err(EXIT_FAILURE, "can't register cleanup function");
/* NOTREACHED */
/* Find the specified command. */
for (i = 0; commands[i].cc_name != NULL; ++i)
if (strcmp(*argv, commands[i].cc_name) == 0)
break;
if (commands[i].cc_name == NULL)
errx(1, "unknown command: %s", *argv);
errx(EXIT_FAILURE, "unknown command: %s", *argv);
/* NOTREACHED */
/* Skip over the command name and call handler. */
++argv; --argc;
@ -204,9 +215,11 @@ do_move(const char *cname, int argc, char **argv)
if (argc < 4) {
warnx("%s: too few arguments", cname);
usage();
/*NOTREACHED*/
} else if (argc > 5) {
warnx("%s: too many arguments", cname);
usage();
/*NOTREACHED*/
}
(void)memset(&cmd, 0, sizeof(cmd));
@ -234,7 +247,7 @@ do_move(const char *cname, int argc, char **argv)
cmd.cm_flags |= CM_INVERT;
break;
default:
errx(1, "%s: inappropriate modifier `%s'",
errx(EXIT_FAILURE, "%s: inappropriate modifier `%s'",
cname, *argv);
/* NOTREACHED */
}
@ -242,7 +255,8 @@ do_move(const char *cname, int argc, char **argv)
/* Send command to changer. */
if (ioctl(changer_fd, CHIOMOVE, &cmd))
err(1, "%s: CHIOMOVE", changer_name);
err(EXIT_FAILURE, "%s: CHIOMOVE", printescaped(changer_name));
/* NOTREACHED */
return (0);
}
@ -263,9 +277,11 @@ do_exchange(const char *cname, int argc, char **argv)
if (argc < 4) {
warnx("%s: too few arguments", cname);
usage();
/*NOTREACHED*/
} else if (argc > 8) {
warnx("%s: too many arguments", cname);
usage();
/*NOTREACHED*/
}
(void)memset(&cmd, 0, sizeof(cmd));
@ -317,7 +333,7 @@ do_exchange(const char *cname, int argc, char **argv)
cmd.ce_flags |= CE_INVERT2;
break;
default:
errx(1, "%s: inappropriate modifier `%s'",
errx(EXIT_FAILURE, "%s: inappropriate modifier `%s'",
cname, *argv);
/* NOTREACHED */
}
@ -325,7 +341,8 @@ do_exchange(const char *cname, int argc, char **argv)
/* Send command to changer. */
if (ioctl(changer_fd, CHIOEXCHANGE, &cmd))
err(1, "%s: CHIOEXCHANGE", changer_name);
err(EXIT_FAILURE, "%s: CHIOEXCHANGE", printescaped(changer_name));
/* NOTREACHED */
return (0);
}
@ -346,9 +363,11 @@ do_position(const char *cname, int argc, char **argv)
if (argc < 2) {
warnx("%s: too few arguments", cname);
usage();
/*NOTREACHED*/
} else if (argc > 3) {
warnx("%s: too many arguments", cname);
usage();
/*NOTREACHED*/
}
(void)memset(&cmd, 0, sizeof(cmd));
@ -368,7 +387,7 @@ do_position(const char *cname, int argc, char **argv)
cmd.cp_flags |= CP_INVERT;
break;
default:
errx(1, "%s: inappropriate modifier `%s'",
errx(EXIT_FAILURE, "%s: inappropriate modifier `%s'",
cname, *argv);
/* NOTREACHED */
}
@ -376,7 +395,8 @@ do_position(const char *cname, int argc, char **argv)
/* Send command to changer. */
if (ioctl(changer_fd, CHIOPOSITION, &cmd))
err(1, "%s: CHIOPOSITION", changer_name);
err(EXIT_FAILURE, "%s: CHIOPOSITION", printescaped(changer_name));
/* NOTREACHED */
return (0);
}
@ -386,22 +406,27 @@ static int
do_params(const char *cname, int argc, char **argv)
{
struct changer_params data;
char *cn;
/* No arguments to this command. */
if (argc) {
warnx("%s: no arguements expected", cname);
usage();
/* NOTREACHED */
}
cn = printescaped(changer_name);
/* Get params from changer and display them. */
(void)memset(&data, 0, sizeof(data));
if (ioctl(changer_fd, CHIOGPARAMS, &data))
err(1, "%s: CHIOGPARAMS", changer_name);
err(EXIT_FAILURE, "%s: CHIOGPARAMS", cn);
/* NOTREACHED */
#define PLURAL(n) (n) > 1 ? "s" : ""
(void)printf("%s: %d slot%s, %d drive%s, %d picker%s",
changer_name,
cn,
data.cp_nslots, PLURAL(data.cp_nslots),
data.cp_ndrives, PLURAL(data.cp_ndrives),
data.cp_npickers, PLURAL(data.cp_npickers));
@ -411,8 +436,9 @@ do_params(const char *cname, int argc, char **argv)
#undef PLURAL
(void)printf("\n%s: current picker: %d\n", changer_name,
data.cp_curpicker);
(void)printf("\n%s: current picker: %d\n", cn, data.cp_curpicker);
free(cn);
return (0);
}
@ -422,18 +448,24 @@ static int
do_getpicker(const char *cname, int argc, char **argv)
{
int picker;
char *cn;
/* No arguments to this command. */
if (argc) {
warnx("%s: no arguments expected", cname);
usage();
/*NOTREACHED*/
}
cn = printescaped(changer_name);
/* Get current picker from changer and display it. */
if (ioctl(changer_fd, CHIOGPICKER, &picker))
err(1, "%s: CHIOGPICKER", changer_name);
err(EXIT_FAILURE, "%s: CHIOGPICKER", cn);
/* NOTREACHED */
(void)printf("%s: current picker: %d\n", changer_name, picker);
(void)printf("%s: current picker: %d\n", cn, picker);
free(cn);
return (0);
}
@ -446,16 +478,18 @@ do_setpicker(const char *cname, int argc, char **argv)
if (argc < 1) {
warnx("%s: too few arguments", cname);
usage();
/*NOTREACHED*/
} else if (argc > 1) {
warnx("%s: too many arguments", cname);
usage();
/*NOTREACHED*/
}
picker = parse_element_unit(*argv);
/* Set the changer picker. */
if (ioctl(changer_fd, CHIOSPICKER, &picker))
err(1, "%s: CHIOSPICKER", changer_name);
err(EXIT_FAILURE, "%s: CHIOSPICKER", printescaped(changer_name));
return (0);
}
@ -469,6 +503,7 @@ do_status(const char *cname, int argc, char **argv)
int i, chet, count, echet, flags, have_ucount, have_unit;
int schet, ucount, unit;
size_t size;
char *cn;
flags = 0;
have_ucount = 0;
@ -487,15 +522,19 @@ do_status(const char *cname, int argc, char **argv)
if (argc > 4) {
warnx("%s: too many arguments", cname);
usage();
/*NOTREACHED*/
}
cn = printescaped(changer_name);
/*
* Get params from changer. Specifically, we need the element
* counts.
*/
(void)memset(&data, 0, sizeof(data));
if (ioctl(changer_fd, CHIOGPARAMS, &data))
err(1, "%s: CHIOGPARAMS", changer_name);
err(EXIT_FAILURE, "%s: CHIOGPARAMS", cn);
/* NOTREACHED */
schet = CHET_MT;
echet = CHET_DT;
@ -509,10 +548,13 @@ do_status(const char *cname, int argc, char **argv)
if (argc != 1) {
warnx("%s: malformed command line", cname);
usage();
/*NOTREACHED*/
}
if (parse_special(argv[0]) != SW_VOLTAGS)
errx(1, "%s: inappropriate special word: %s",
errx(EXIT_FAILURE,
"%s: inappropriate special word: %s",
cname, argv[0]);
/* NOTREACHED */
flags |= CESR_VOLTAGS;
continue;
}
@ -526,6 +568,7 @@ do_status(const char *cname, int argc, char **argv)
have_ucount) {
warnx("%s: malformed command line", cname);
usage();
/*NOTREACHED*/
}
schet = echet = parse_element_type(argv[0]);
continue;
@ -538,6 +581,7 @@ do_status(const char *cname, int argc, char **argv)
if (schet != echet) {
warnx("%s: malformed command line", cname);
usage();
/*NOTREACHED*/
}
i = parse_element_unit(argv[0]);
@ -551,6 +595,7 @@ do_status(const char *cname, int argc, char **argv)
} else {
warnx("%s: malformed command line", cname);
usage();
/*NOTREACHED*/
}
}
@ -578,8 +623,9 @@ do_status(const char *cname, int argc, char **argv)
continue;
else {
(void)printf("%s: no %s elements\n",
changer_name,
cn,
elements[chet].et_name);
free(cn);
return (0);
}
}
@ -599,14 +645,16 @@ do_status(const char *cname, int argc, char **argv)
}
if ((unit + ucount) > count)
errx(1, "%s: unvalid unit/count %d/%d",
errx(EXIT_FAILURE, "%s: unvalid unit/count %d/%d",
cname, unit, ucount);
/* NOTREACHED */
size = ucount * sizeof(struct changer_element_status);
/* Allocate storage for the status bytes. */
if ((ces = malloc(size)) == NULL)
errx(1, "can't allocate status storage");
errx(EXIT_FAILURE, "can't allocate status storage");
/* NOTREACHED */
(void)memset(ces, 0, size);
(void)memset(&cmd, 0, sizeof(cmd));
@ -624,7 +672,8 @@ do_status(const char *cname, int argc, char **argv)
if (ioctl(changer_fd, CHIOGSTATUS, &cmd)) {
free(ces);
err(1, "%s: CHIOGSTATUS", changer_name);
err(EXIT_FAILURE, "%s: CHIOGSTATUS", cn);
/* NOTREACHED */
}
/* Dump the status for each element of this type. */
@ -670,7 +719,8 @@ do_ielem(const char *cname, int argc, char **argv)
{
if (ioctl(changer_fd, CHIOIELEM, NULL))
err(1, "%s: CHIOIELEM", changer_name);
err(EXIT_FAILURE, "%s: CHIOIELEM", printescaped(changer_name));
/* NOTREACHED */
return (0);
}
@ -699,6 +749,7 @@ do_cdlu(const char *cname, int argc, char **argv)
if (argc < 1 || argc > 2)
usage();
/*NOTREACHED*/
for (i = 0; cdlu_subcmds[i].sw_name != NULL; i++) {
if (strcmp(argv[0], cdlu_subcmds[i].sw_name) == 0) {
@ -708,6 +759,7 @@ do_cdlu(const char *cname, int argc, char **argv)
}
if (cdlu_subcmds[i].sw_name == NULL)
usage();
/*NOTREACHED*/
if (strcmp(argv[0], "abort") == 0)
cmd.slot = 0;
@ -719,7 +771,8 @@ do_cdlu(const char *cname, int argc, char **argv)
* XXX handling for cdlu; think about this some more.
*/
if (ioctl(changer_fd, CDIOCLOADUNLOAD, &cmd))
err(1, "%s: CDIOCLOADUNLOAD", changer_name);
err(EXIT_FAILURE, "%s: CDIOCLOADUNLOAD", printescaped(changer_name));
/* NOTREACHED */
return (0);
}
@ -733,7 +786,7 @@ parse_element_type(const char *cp)
if (strcmp(elements[i].et_name, cp) == 0)
return (elements[i].et_type);
errx(1, "invalid element type `%s'", cp);
errx(EXIT_FAILURE, "invalid element type `%s'", cp);
/* NOTREACHED */
}
@ -745,7 +798,7 @@ parse_element_unit(const char *cp)
i = (int)strtol(cp, &p, 10);
if ((i < 0) || (*p != '\0'))
errx(1, "invalid unit number `%s'", cp);
errx(EXIT_FAILURE, "invalid unit number `%s'", cp);
return (i);
}
@ -759,7 +812,7 @@ parse_special(const char *cp)
if (val)
return (val);
errx(1, "invalid modifier `%s'", cp);
errx(EXIT_FAILURE, "invalid modifier `%s'", cp);
/* NOTREACHED */
}
@ -826,3 +879,27 @@ usage(void)
exit(1);
/* NOTREACHED */
}
char *
printescaped(const char *src)
{
size_t len;
char *retval;
len = strlen(src);
if (len != 0 && SIZE_T_MAX/len <= 4) {
errx(EXIT_FAILURE, "%s: name too long", src);
/* NOTREACHED */
}
retval = (char *)malloc(4*len+1);
if (retval != NULL) {
if (stdout_ok)
(void)strvis(retval, src, VIS_NL | VIS_CSTYLE);
else
(void)strcpy(retval, src);
return retval;
} else
errx(EXIT_FAILURE, "out of memory!");
/* NOTREACHED */
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: chmod.c,v 1.28 2002/07/07 11:44:02 bjh21 Exp $ */
/* $NetBSD: chmod.c,v 1.29 2003/08/04 22:31:22 jschauma Exp $ */
/*
* Copyright (c) 1989, 1993, 1994
@ -44,12 +44,13 @@ __COPYRIGHT(
#if 0
static char sccsid[] = "@(#)chmod.c 8.8 (Berkeley) 4/1/94";
#else
__RCSID("$NetBSD: chmod.c,v 1.28 2002/07/07 11:44:02 bjh21 Exp $");
__RCSID("$NetBSD: chmod.c,v 1.29 2003/08/04 22:31:22 jschauma Exp $");
#endif
#endif /* not lint */
#include <sys/types.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <err.h>
#include <errno.h>
@ -60,9 +61,13 @@ __RCSID("$NetBSD: chmod.c,v 1.28 2002/07/07 11:44:02 bjh21 Exp $");
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <vis.h>
int main(int, char *[]);
void usage(void);
int stdout_ok;
int main(int, char *[]);
void usage(void);
char *printescaped(const char *);
int
main(int argc, char *argv[])
@ -71,7 +76,7 @@ main(int argc, char *argv[])
FTSENT *p;
mode_t *set;
int Hflag, Lflag, Rflag, ch, fflag, fts_options, hflag, rval;
char *mode;
char *mode, *fn;
int (*change_mode)(const char *, mode_t);
setprogname(argv[0]);
@ -131,11 +136,15 @@ done: argv += optind;
if (argc < 2)
usage();
stdout_ok = isatty(STDOUT_FILENO);
fts_options = FTS_PHYSICAL;
if (Rflag) {
if (hflag)
errx(1,
if (hflag) {
errx(EXIT_FAILURE,
"the -R and -h options may not be specified together.");
/* NOTREACHED */
}
if (Hflag)
fts_options |= FTS_COMFOLLOW;
if (Lflag) {
@ -150,11 +159,15 @@ done: argv += optind;
change_mode = chmod;
mode = *argv;
if ((set = setmode(mode)) == NULL)
errx(1, "invalid file mode: %s", mode);
if ((set = setmode(mode)) == NULL) {
errx(EXIT_FAILURE, "invalid file mode: %s", mode);
/* NOTREACHED */
}
if ((ftsp = fts_open(++argv, fts_options, 0)) == NULL)
err(1, "fts_open");
if ((ftsp = fts_open(++argv, fts_options, 0)) == NULL) {
err(EXIT_FAILURE, "fts_open");
/* NOTREACHED */
}
for (rval = 0; (p = fts_read(ftsp)) != NULL;) {
switch (p->fts_info) {
case FTS_D:
@ -162,14 +175,18 @@ done: argv += optind;
(void)fts_set(ftsp, p, FTS_SKIP);
break;
case FTS_DNR: /* Warn, chmod, continue. */
warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
fn = printescaped(p->fts_path);
warnx("%s: %s", fn, strerror(p->fts_errno));
free(fn);
rval = 1;
break;
case FTS_DP: /* Already changed at FTS_D. */
continue;
case FTS_ERR: /* Warn, continue. */
case FTS_NS:
warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
fn = printescaped(p->fts_path);
warnx("%s: %s", fn, strerror(p->fts_errno));
free(fn);
rval = 1;
continue;
case FTS_SL: /* Ignore. */
@ -188,12 +205,16 @@ done: argv += optind;
}
if ((*change_mode)(p->fts_accpath,
getmode(set, p->fts_statp->st_mode)) && !fflag) {
warn("%s", p->fts_path);
fn = printescaped(p->fts_path);
warn("%s", fn);
free(fn);
rval = 1;
}
}
if (errno)
err(1, "fts_read");
if (errno) {
err(EXIT_FAILURE, "fts_read");
/* NOTREACHED */
}
exit(rval);
/* NOTREACHED */
}
@ -207,3 +228,27 @@ usage(void)
exit(1);
/* NOTREACHED */
}
char *
printescaped(const char *src)
{
size_t len;
char *retval;
len = strlen(src);
if (len != 0 && SIZE_T_MAX/len <= 4) {
errx(EXIT_FAILURE, "%s: name too long", src);
/* NOTREACHED */
}
retval = (char *)malloc(4*len+1);
if (retval != NULL) {
if (stdout_ok)
(void)strvis(retval, src, VIS_NL | VIS_CSTYLE);
else
(void)strcpy(retval, src);
return retval;
} else
errx(EXIT_FAILURE, "out of memory!");
/* NOTREACHED */
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: cp.c,v 1.32 2002/12/16 14:44:14 jrf Exp $ */
/* $NetBSD: cp.c,v 1.33 2003/08/04 22:31:22 jschauma Exp $ */
/*
* Copyright (c) 1988, 1993, 1994
@ -47,7 +47,7 @@ __COPYRIGHT(
#if 0
static char sccsid[] = "@(#)cp.c 8.5 (Berkeley) 4/29/95";
#else
__RCSID("$NetBSD: cp.c,v 1.32 2002/12/16 14:44:14 jrf Exp $");
__RCSID("$NetBSD: cp.c,v 1.33 2003/08/04 22:31:22 jschauma Exp $");
#endif
#endif /* not lint */
@ -88,14 +88,14 @@ __RCSID("$NetBSD: cp.c,v 1.32 2002/12/16 14:44:14 jrf Exp $");
PATH_T to = { to.p_path, "" };
uid_t myuid;
int Rflag, fflag, iflag, pflag, rflag, vflag;
int Rflag, fflag, iflag, pflag, rflag, stdout_ok, vflag;
mode_t myumask;
enum op { FILE_TO_FILE, FILE_TO_DIR, DIR_TO_DNE };
int main(int, char *[]);
int copy(char *[], enum op, int);
int mastercmp(const FTSENT **, const FTSENT **);
int main(int, char *[]);
int copy(char *[], enum op, int);
int mastercmp(const FTSENT **, const FTSENT **);
int
main(int argc, char *argv[])
@ -153,14 +153,20 @@ main(int argc, char *argv[])
if (argc < 2)
usage();
stdout_ok = isatty(STDOUT_FILENO);
fts_options = FTS_NOCHDIR | FTS_PHYSICAL;
if (rflag) {
if (Rflag)
errx(1,
if (Rflag) {
errx(EXIT_FAILURE,
"the -R and -r options may not be specified together.");
if (Hflag || Lflag || Pflag)
errx(1,
/* NOTREACHED */
}
if (Hflag || Lflag || Pflag) {
errx(EXIT_FAILURE,
"the -H, -L, and -P options may not be specified with the -r option.");
/* NOTREACHED */
}
fts_options &= ~FTS_PHYSICAL;
fts_options |= FTS_LOGICAL;
}
@ -184,8 +190,10 @@ main(int argc, char *argv[])
/* Save the target base in "to". */
target = argv[--argc];
if (strlen(target) > MAXPATHLEN)
errx(1, "%s: name too long", target);
if (strlen(target) > MAXPATHLEN) {
errx(EXIT_FAILURE, "%s: name too long", printescaped(target));
/* NOTREACHED */
}
(void)strcpy(to.p_path, target);
to.p_end = to.p_path + strlen(to.p_path);
if (to.p_path == to.p_end) {
@ -213,8 +221,10 @@ main(int argc, char *argv[])
* In (2), the real target is not directory, but "directory/source".
*/
r = stat(to.p_path, &to_stat);
if (r == -1 && errno != ENOENT)
err(1, "%s", to.p_path);
if (r == -1 && errno != ENOENT) {
err(EXIT_FAILURE, "%s", printescaped(to.p_path));
/* NOTREACHED */
}
if (r == -1 || !S_ISDIR(to_stat.st_mode)) {
/*
* Case (1). Target is not a directory.
@ -235,8 +245,10 @@ main(int argc, char *argv[])
r = stat(*argv, &tmp_stat);
else
r = lstat(*argv, &tmp_stat);
if (r == -1)
err(1, "%s", *argv);
if (r == -1) {
err(EXIT_FAILURE, "%s", printescaped(*argv));
/* NOTREACHED */
}
if (S_ISDIR(tmp_stat.st_mode) && (Rflag || rflag))
type = DIR_TO_DNE;
@ -262,23 +274,27 @@ copy(char *argv[], enum op type, int fts_options)
FTS *ftsp;
FTSENT *curr;
int base, dne, nlen, rval;
char *p, *tmp;
char *p, *tmp, *fn;
base = 0; /* XXX gcc -Wuninitialized (see comment below) */
if ((ftsp = fts_open(argv, fts_options, mastercmp)) == NULL)
err(1, "%s", argv[0]);
err(EXIT_FAILURE, "%s", printescaped(argv[0]));
/* NOTREACHED */
for (rval = 0; (curr = fts_read(ftsp)) != NULL;) {
switch (curr->fts_info) {
case FTS_NS:
case FTS_DNR:
case FTS_ERR:
warnx("%s: %s",
curr->fts_path, strerror(curr->fts_errno));
fn = printescaped(curr->fts_path);
warnx("%s: %s", fn, strerror(curr->fts_errno));
free(fn);
rval = 1;
continue;
case FTS_DC: /* Warn, continue. */
warnx("%s: directory causes a cycle", curr->fts_path);
fn = printescaped(curr->fts_path);
warnx("%s: directory causes a cycle", fn);
free(fn);
rval = 1;
continue;
}
@ -290,8 +306,12 @@ copy(char *argv[], enum op type, int fts_options)
if (type != FILE_TO_FILE) {
if ((curr->fts_namelen +
to.target_end - to.p_path + 1) > MAXPATHLEN) {
warnx("%s/%s: name too long (not copied)",
to.p_path, curr->fts_name);
char *tn;
tn = printescaped(to.p_path);
fn = printescaped(curr->fts_name);
warnx("%s/%s: name too long (not copied)", tn, fn);
free(fn);
free(tn);
rval = 1;
continue;
}
@ -357,8 +377,13 @@ copy(char *argv[], enum op type, int fts_options)
}
if (!S_ISDIR(curr->fts_statp->st_mode) &&
S_ISDIR(to_stat.st_mode)) {
char *tn;
tn = printescaped(to.p_path);
fn = printescaped(curr->fts_path);
warnx("cannot overwrite directory %s with non-directory %s",
to.p_path, curr->fts_path);
tn, fn);
free(tn);
free(fn);
rval = 1;
continue;
}
@ -379,9 +404,12 @@ copy(char *argv[], enum op type, int fts_options)
break;
case S_IFDIR:
if (!Rflag && !rflag) {
if (curr->fts_info == FTS_D)
if (curr->fts_info == FTS_D) {
fn = printescaped(curr->fts_path);
warnx("%s is a directory (not copied).",
curr->fts_path);
fn);
free(fn);
}
(void)fts_set(ftsp, curr, FTS_SKIP);
rval = 1;
break;
@ -405,10 +433,14 @@ copy(char *argv[], enum op type, int fts_options)
if (dne) {
if (mkdir(to.p_path,
curr->fts_statp->st_mode | S_IRWXU) < 0)
err(1, "%s", to.p_path);
err(EXIT_FAILURE, "%s",
printescaped(to.p_path));
/* NOTREACHED */
} else if (!S_ISDIR(to_stat.st_mode)) {
errno = ENOTDIR;
err(1, "%s", to.p_path);
err(EXIT_FAILURE, "%s",
printescaped(to.p_path));
/* NOTREACHED */
}
}
else if (curr->fts_info == FTS_DP) /* Second pass */
@ -427,7 +459,9 @@ copy(char *argv[], enum op type, int fts_options)
}
else
{
warnx("directory %s encountered when not expected.", curr->fts_path);
fn = printescaped(curr->fts_path);
warnx("directory %s encountered when not expected.", fn);
free(fn);
rval = 1;
break;
}
@ -455,11 +489,19 @@ copy(char *argv[], enum op type, int fts_options)
rval = 1;
break;
}
if (vflag)
(void)printf("%s -> %s\n", curr->fts_path, to.p_path);
if (vflag) {
char *tn;
fn = printescaped(curr->fts_path);
tn = printescaped(to.p_path);
(void)printf("%s -> %s\n", fn, tn);
free(fn);
free(tn);
}
}
if (errno) {
err(EXIT_FAILURE, "fts_read");
/* NOTREACHED */
}
if (errno)
err(1, "fts_read");
return (rval);
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: extern.h,v 1.6 2001/09/13 09:53:59 wiz Exp $ */
/* $NetBSD: extern.h,v 1.7 2003/08/04 22:31:22 jschauma Exp $ */
/*-
* Copyright (c) 1991, 1993, 1994
@ -46,7 +46,7 @@ typedef struct {
extern PATH_T to;
extern uid_t myuid;
extern int fflag, iflag, pflag;
extern int fflag, iflag, pflag, stdout_ok;
extern mode_t myumask;
#include <sys/cdefs.h>
@ -59,6 +59,7 @@ int copy_special(struct stat *, int);
int set_utimes(const char *, struct stat *);
int setfile(struct stat *, int);
void usage(void);
char *printescaped(const char *);
__END_DECLS
#endif /* !_EXTERN_H_ */

View File

@ -1,4 +1,4 @@
/* $NetBSD: utils.c,v 1.23 2003/01/20 05:29:53 simonb Exp $ */
/* $NetBSD: utils.c,v 1.24 2003/08/04 22:31:22 jschauma Exp $ */
/*-
* Copyright (c) 1991, 1993, 1994
@ -38,7 +38,7 @@
#if 0
static char sccsid[] = "@(#)utils.c 8.3 (Berkeley) 4/1/94";
#else
__RCSID("$NetBSD: utils.c,v 1.23 2003/01/20 05:29:53 simonb Exp $");
__RCSID("$NetBSD: utils.c,v 1.24 2003/08/04 22:31:22 jschauma Exp $");
#endif
#endif /* not lint */
@ -55,6 +55,7 @@ __RCSID("$NetBSD: utils.c,v 1.23 2003/01/20 05:29:53 simonb Exp $");
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <vis.h>
#include "extern.h"
@ -336,3 +337,27 @@ usage(void)
exit(1);
/* NOTREACHED */
}
char *
printescaped(const char *src)
{
size_t len;
char *retval;
len = strlen(src);
if (len != 0 && SIZE_T_MAX/len <= 4) {
errx(EXIT_FAILURE, "%s: name too long", src);
/* NOTREACHED */
}
retval = (char *)malloc(4*len+1);
if (retval != NULL) {
if (stdout_ok)
(void)strvis(retval, src, VIS_NL | VIS_CSTYLE);
else
(void)strcpy(retval, src);
return retval;
} else
errx(EXIT_FAILURE, "out of memory!");
/* NOTREACHED */
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: date.c,v 1.35 2001/09/16 13:35:52 wiz Exp $ */
/* $NetBSD: date.c,v 1.36 2003/08/04 22:31:22 jschauma Exp $ */
/*
* Copyright (c) 1985, 1987, 1988, 1993
@ -44,7 +44,7 @@ __COPYRIGHT(
#if 0
static char sccsid[] = "@(#)date.c 8.2 (Berkeley) 4/28/95";
#else
__RCSID("$NetBSD: date.c,v 1.35 2001/09/16 13:35:52 wiz Exp $");
__RCSID("$NetBSD: date.c,v 1.36 2003/08/04 22:31:22 jschauma Exp $");
#endif
#endif /* not lint */
@ -138,7 +138,8 @@ badformat(void)
static void
badtime(void)
{
errx(1, "illegal time");
errx(EXIT_FAILURE, "illegal time");
/* NOTREACHED */
}
#define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0'))

View File

@ -1,4 +1,4 @@
/* $NetBSD: args.c,v 1.21 2002/11/29 13:11:10 lukem Exp $ */
/* $NetBSD: args.c,v 1.22 2003/08/04 22:31:23 jschauma Exp $ */
/*-
* Copyright (c) 1991, 1993, 1994
@ -42,7 +42,7 @@
#if 0
static char sccsid[] = "@(#)args.c 8.3 (Berkeley) 4/2/94";
#else
__RCSID("$NetBSD: args.c,v 1.21 2002/11/29 13:11:10 lukem Exp $");
__RCSID("$NetBSD: args.c,v 1.22 2003/08/04 22:31:23 jschauma Exp $");
#endif
#endif /* not lint */
@ -109,20 +109,28 @@ jcl(char **argv)
in.dbsz = out.dbsz = 512;
while ((oper = *++argv) != NULL) {
if ((arg = strchr(oper, '=')) == NULL)
errx(1, "unknown operand %s", oper);
if ((arg = strchr(oper, '=')) == NULL) {
errx(EXIT_FAILURE, "unknown operand %s", oper);
/* NOTREACHED */
}
*arg++ = '\0';
if (!*arg)
errx(1, "no value specified for %s", oper);
if (!*arg) {
errx(EXIT_FAILURE, "no value specified for %s", oper);
/* NOTREACHED */
}
tmp.name = oper;
if (!(ap = (struct arg *)bsearch(&tmp, args,
sizeof(args)/sizeof(struct arg), sizeof(struct arg),
c_arg)))
errx(1, "unknown operand %s", tmp.name);
if (ddflags & ap->noset)
errx(1,
c_arg))) {
errx(EXIT_FAILURE, "unknown operand %s", tmp.name);
/* NOTREACHED */
}
if (ddflags & ap->noset) {
errx(EXIT_FAILURE,
"%s: illegal argument combination or already set",
tmp.name);
/* NOTREACHED */
}
ddflags |= ap->set;
ap->f(arg);
}
@ -148,8 +156,10 @@ jcl(char **argv)
* Block/unblock requires cbs and vice-versa.
*/
if (ddflags & (C_BLOCK|C_UNBLOCK)) {
if (!(ddflags & C_CBS))
errx(1, "record operations require cbs");
if (!(ddflags & C_CBS)) {
errx(EXIT_FAILURE, "record operations require cbs");
/* NOTREACHED */
}
cfunc = ddflags & C_BLOCK ? block : unblock;
} else if (ddflags & C_CBS) {
if (ddflags & (C_ASCII|C_EBCDIC)) {
@ -160,9 +170,11 @@ jcl(char **argv)
ddflags |= C_BLOCK;
cfunc = block;
}
} else
errx(1,
} else {
errx(EXIT_FAILURE,
"cbs meaningless if not doing record operations");
/* NOTREACHED */
}
} else
cfunc = def;
@ -274,7 +286,8 @@ static void
f_conv(char *arg)
{
errx(1, "conv option disabled");
errx(EXIT_FAILURE, "conv option disabled");
/* NOTREACHED */
}
#else /* NO_CONV */
@ -309,10 +322,14 @@ f_conv(char *arg)
tmp.name = strsep(&arg, ",");
if (!(cp = (struct conv *)bsearch(&tmp, clist,
sizeof(clist)/sizeof(struct conv), sizeof(struct conv),
c_conv)))
errx(1, "unknown conversion %s", tmp.name);
if (ddflags & cp->noset)
errx(1, "%s: illegal conversion combination", tmp.name);
c_conv))) {
errx(EXIT_FAILURE, "unknown conversion %s", tmp.name);
/* NOTREACHED */
}
if (ddflags & cp->noset) {
errx(EXIT_FAILURE, "%s: illegal conversion combination", tmp.name);
/* NOTREACHED */
}
ddflags |= cp->set;
if (cp->ctab)
ctab = cp->ctab;

View File

@ -1,4 +1,4 @@
/* $NetBSD: conv.c,v 1.14 2001/11/26 00:13:23 lukem Exp $ */
/* $NetBSD: conv.c,v 1.15 2003/08/04 22:31:23 jschauma Exp $ */
/*-
* Copyright (c) 1991, 1993, 1994
@ -42,7 +42,7 @@
#if 0
static char sccsid[] = "@(#)conv.c 8.3 (Berkeley) 4/2/94";
#else
__RCSID("$NetBSD: conv.c,v 1.14 2001/11/26 00:13:23 lukem Exp $");
__RCSID("$NetBSD: conv.c,v 1.15 2003/08/04 22:31:23 jschauma Exp $");
#endif
#endif /* not lint */
@ -103,10 +103,10 @@ def_close(void)
/* Build a smaller version (i.e. for a miniroot) */
/* These can not be called, but just in case... */
static const char no_block[] = "unblock and -DNO_CONV?";
void block(void) { errx(1, "%s", no_block + 2); }
void block_close(void) { errx(1, "%s", no_block + 2); }
void unblock(void) { errx(1, "%s", no_block); }
void unblock_close(void) { errx(1, "%s", no_block); }
void block(void) { errx(EXIT_FAILURE, "%s", no_block + 2); }
void block_close(void) { errx(EXIT_FAILURE, "%s", no_block + 2); }
void unblock(void) { errx(EXIT_FAILURE, "%s", no_block); }
void unblock_close(void) { errx(EXIT_FAILURE, "%s", no_block); }
#else /* NO_CONV */
/*

View File

@ -1,4 +1,4 @@
/* $NetBSD: dd.c,v 1.29 2002/09/04 04:21:54 enami Exp $ */
/* $NetBSD: dd.c,v 1.30 2003/08/04 22:31:23 jschauma Exp $ */
/*-
* Copyright (c) 1991, 1993, 1994
@ -47,7 +47,7 @@ __COPYRIGHT("@(#) Copyright (c) 1991, 1993, 1994\n\
#if 0
static char sccsid[] = "@(#)dd.c 8.5 (Berkeley) 4/2/94";
#else
__RCSID("$NetBSD: dd.c,v 1.29 2002/09/04 04:21:54 enami Exp $");
__RCSID("$NetBSD: dd.c,v 1.30 2003/08/04 22:31:23 jschauma Exp $");
#endif
#endif /* not lint */
@ -131,13 +131,16 @@ setup(void)
} else {
in.fd = open(in.name, O_RDONLY, 0);
if (in.fd < 0)
err(1, "%s", in.name);
err(EXIT_FAILURE, "%s", printescaped(in.name));
/* NOTREACHED */
}
getfdtype(&in);
if (files_cnt > 1 && !(in.flags & ISTAPE))
errx(1, "files is not supported for non-tape devices");
if (files_cnt > 1 && !(in.flags & ISTAPE)) {
errx(EXIT_FAILURE, "files is not supported for non-tape devices");
/* NOTREACHED */
}
if (out.name == NULL) {
/* No way to check for read access here. */
@ -156,8 +159,10 @@ setup(void)
out.fd = open(out.name, O_WRONLY | OFLAGS, DEFFILEMODE);
out.flags |= NOREAD;
}
if (out.fd < 0)
err(1, "%s", out.name);
if (out.fd < 0) {
err(EXIT_FAILURE, "%s", printescaped(out.name));
/* NOTREACHED */
}
}
getfdtype(&out);
@ -167,13 +172,17 @@ setup(void)
* record oriented I/O, only need a single buffer.
*/
if (!(ddflags & (C_BLOCK|C_UNBLOCK))) {
if ((in.db = malloc(out.dbsz + in.dbsz - 1)) == NULL)
err(1, NULL);
if ((in.db = malloc(out.dbsz + in.dbsz - 1)) == NULL) {
err(EXIT_FAILURE, NULL);
/* NOTREACHED */
}
out.db = in.db;
} else if ((in.db =
malloc((u_int)(MAX(in.dbsz, cbsz) + cbsz))) == NULL ||
(out.db = malloc((u_int)(out.dbsz + cbsz))) == NULL)
err(1, NULL);
(out.db = malloc((u_int)(out.dbsz + cbsz))) == NULL) {
err(EXIT_FAILURE, NULL);
/* NOTREACHED */
}
in.dbp = in.db;
out.dbp = out.db;
@ -198,7 +207,8 @@ setup(void)
if (ddflags & (C_LCASE|C_UCASE)) {
#ifdef NO_CONV
/* Should not get here, but just in case... */
errx(1, "case conv and -DNO_CONV");
errx(EXIT_FAILURE, "case conv and -DNO_CONV");
/* NOTREACHED */
#else /* NO_CONV */
u_int cnt;
@ -233,8 +243,10 @@ getfdtype(IO *io)
struct mtget mt;
struct stat sb;
if (fstat(io->fd, &sb))
err(1, "%s", io->name);
if (fstat(io->fd, &sb)) {
err(EXIT_FAILURE, "%s", printescaped(io->name));
/* NOTREACHED */
}
if (S_ISCHR(sb.st_mode))
io->flags |= ioctl(io->fd, MTIOCGET, &mt) ? ISCHR : ISTAPE;
else if (lseek(io->fd, (off_t)0, SEEK_CUR) == -1 && errno == ESPIPE)
@ -273,13 +285,18 @@ dd_in(void)
/* Read error. */
if (n < 0) {
char *fn;
fn = printescaped(in.name);
/*
* If noerror not specified, die. POSIX requires that
* the warning message be followed by an I/O display.
*/
if (!(flags & C_NOERROR))
err(1, "%s", in.name);
warn("%s", in.name);
if (!(flags & C_NOERROR)) {
err(EXIT_FAILURE, "%s", fn);
/* NOTREACHED */
}
warn("%s", fn);
summary();
/*
@ -290,7 +307,7 @@ dd_in(void)
*/
if (!(in.flags & (ISPIPE|ISTAPE)) &&
lseek(in.fd, (off_t)in.dbsz, SEEK_CUR))
warn("%s", in.name);
warn("%s", fn);
/* If sync not specified, omit block and continue. */
if (!(ddflags & C_SYNC))
@ -300,6 +317,8 @@ dd_in(void)
in.dbcnt += in.dbrcnt = in.dbsz;
++st.in_full;
free(fn);
/* Handle full input blocks. */
} else if (n == in.dbsz) {
in.dbcnt += in.dbrcnt = n;
@ -368,10 +387,14 @@ dd_close(void)
* may be shared among with other processes and close(2) just
* decreases the reference count.
*/
if (out.fd == STDOUT_FILENO && fsync(out.fd) == -1 && errno != EINVAL)
err(1, "fsync stdout");
if (close(out.fd) == -1)
err(1, "close");
if (out.fd == STDOUT_FILENO && fsync(out.fd) == -1 && errno != EINVAL) {
err(EXIT_FAILURE, "fsync stdout");
/* NOTREACHED */
}
if (close(out.fd) == -1) {
err(EXIT_FAILURE, "close");
/* NOTREACHED */
}
}
void
@ -400,12 +423,18 @@ dd_out(int force)
outp = out.db;
for (n = force ? out.dbcnt : out.dbsz;; n = out.dbsz) {
for (cnt = n;; cnt -= nw) {
char *fn;
fn = printescaped(out.name);
nw = bwrite(out.fd, outp, cnt);
if (nw <= 0) {
if (nw == 0)
errx(1, "%s: end of device", out.name);
errx(EXIT_FAILURE,
"%s: end of device", fn);
/* NOTREACHED */
if (errno != EINTR)
err(1, "%s", out.name);
err(EXIT_FAILURE, "%s", fn);
/* NOTREACHED */
nw = 0;
}
outp += nw;
@ -422,12 +451,14 @@ dd_out(int force)
break;
if (out.flags & ISCHR && !warned) {
warned = 1;
warnx("%s: short write on character device",
out.name);
warnx("%s: short write on character device", fn);
}
if (out.flags & ISTAPE)
errx(1, "%s: short write on tape device",
out.name);
errx(EXIT_FAILURE,
"%s: short write on tape device", fn);
/* NOTREACHED */
free(fn);
}
if ((out.dbcnt -= n) < out.dbsz)
break;

View File

@ -1,4 +1,4 @@
/* $NetBSD: extern.h,v 1.13 2001/11/26 00:13:24 lukem Exp $ */
/* $NetBSD: extern.h,v 1.14 2003/08/04 22:31:23 jschauma Exp $ */
/*-
* Copyright (c) 1991, 1993, 1994
@ -55,6 +55,7 @@ void terminate(int);
void unblock(void);
void unblock_close(void);
ssize_t bwrite(int, const void *, size_t);
char *printescaped(const char *);
extern IO in, out;
extern STAT st;

View File

@ -1,4 +1,4 @@
/* $NetBSD: misc.c,v 1.14 2001/11/26 00:56:33 enami Exp $ */
/* $NetBSD: misc.c,v 1.15 2003/08/04 22:31:23 jschauma Exp $ */
/*-
* Copyright (c) 1991, 1993, 1994
@ -42,10 +42,11 @@
#if 0
static char sccsid[] = "@(#)misc.c 8.3 (Berkeley) 4/2/94";
#else
__RCSID("$NetBSD: misc.c,v 1.14 2001/11/26 00:56:33 enami Exp $");
__RCSID("$NetBSD: misc.c,v 1.15 2003/08/04 22:31:23 jschauma Exp $");
#endif
#endif /* not lint */
#include <sys/param.h>
#include <sys/types.h>
#include <sys/time.h>
@ -55,6 +56,7 @@ __RCSID("$NetBSD: misc.c,v 1.14 2001/11/26 00:56:33 enami Exp $");
#include <string.h>
#include <unistd.h>
#include <inttypes.h>
#include <vis.h>
#include "dd.h"
#include "extern.h"
@ -118,3 +120,27 @@ terminate(int notused)
exit(0);
/* NOTREACHED */
}
char *
printescaped(const char *src)
{
size_t len;
char *retval;
len = strlen(src);
if (len != 0 && SIZE_T_MAX/len <= 4) {
errx(EXIT_FAILURE, "%s: name too long", src);
/* NOTREACHED */
}
retval = (char *)malloc(4*len+1);
if (retval != NULL) {
if (isatty(STDOUT_FILENO))
(void)strvis(retval, src, VIS_NL | VIS_CSTYLE);
else
(void)strcpy(retval, src);
return retval;
} else
errx(EXIT_FAILURE, "out of memory!");
/* NOTREACHED */
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: position.c,v 1.12 2001/11/26 00:56:33 enami Exp $ */
/* $NetBSD: position.c,v 1.13 2003/08/04 22:31:23 jschauma Exp $ */
/*-
* Copyright (c) 1991, 1993, 1994
@ -42,7 +42,7 @@
#if 0
static char sccsid[] = "@(#)position.c 8.3 (Berkeley) 4/2/94";
#else
__RCSID("$NetBSD: position.c,v 1.12 2001/11/26 00:56:33 enami Exp $");
__RCSID("$NetBSD: position.c,v 1.13 2003/08/04 22:31:23 jschauma Exp $");
#endif
#endif /* not lint */
@ -54,6 +54,7 @@ __RCSID("$NetBSD: position.c,v 1.12 2001/11/26 00:56:33 enami Exp $");
#include <err.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
@ -74,9 +75,12 @@ pos_in(void)
/* If not a pipe or tape device, try to seek on it. */
if (!(in.flags & (ISPIPE|ISTAPE))) {
if (lseek(in.fd,
(off_t)in.offset * (off_t)in.dbsz, SEEK_CUR) == -1)
err(1, "%s", in.name);
(off_t)in.offset * (off_t)in.dbsz, SEEK_CUR) == -1) {
err(EXIT_FAILURE, "%s", printescaped(in.name));
/* NOTREACHED */
}
return;
/* NOTREACHED */
}
/*
@ -101,7 +105,8 @@ pos_in(void)
--files_cnt;
continue;
}
errx(1, "skip reached end of input");
errx(EXIT_FAILURE, "skip reached end of input");
/* NOTREACHED */
}
/*
@ -111,13 +116,17 @@ pos_in(void)
*/
if (ddflags & C_NOERROR) {
if (!warned) {
warn("%s", in.name);
char * fn;
fn = printescaped(in.name);
warn("%s", fn);
free(fn);
warned = 1;
summary();
}
continue;
}
err(1, "%s", in.name);
err(EXIT_FAILURE, "%s", printescaped(in.name));
/* NOTREACHED */
}
}
@ -135,7 +144,8 @@ pos_out(void)
if (!(out.flags & ISTAPE)) {
if (lseek(out.fd,
(off_t)out.offset * (off_t)out.dbsz, SEEK_SET) == -1)
err(1, "%s", out.name);
err(EXIT_FAILURE, "%s", printescaped(out.name));
/* NOTREACHED */
return;
}
@ -145,7 +155,8 @@ pos_out(void)
t_op.mt_count = out.offset;
if (ioctl(out.fd, MTIOCTOP, &t_op) < 0)
err(1, "%s", out.name);
err(EXIT_FAILURE, "%s", printescaped(out.name));
/* NOTREACHED */
return;
}
@ -155,7 +166,8 @@ pos_out(void)
continue;
if (n < 0)
err(1, "%s", out.name);
err(EXIT_FAILURE, "%s", printescaped(out.name));
/* NOTREACHED */
/*
* If reach EOF, fill with NUL characters; first, back up over
@ -165,11 +177,13 @@ pos_out(void)
t_op.mt_op = MTBSR;
t_op.mt_count = 1;
if (ioctl(out.fd, MTIOCTOP, &t_op) == -1)
err(1, "%s", out.name);
err(EXIT_FAILURE, "%s", printescaped(out.name));
/* NOTREACHED */
while (cnt++ < out.offset)
if ((n = bwrite(out.fd, out.db, out.dbsz)) != out.dbsz)
err(1, "%s", out.name);
err(EXIT_FAILURE, "%s", printescaped(out.name));
/* NOTREACHED */
break;
}
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: echo.c,v 1.9 2001/07/29 22:36:11 wiz Exp $ */
/* $NetBSD: echo.c,v 1.10 2003/08/04 22:31:23 jschauma Exp $ */
/*
* Copyright (c) 1989, 1993
@ -44,15 +44,23 @@ __COPYRIGHT(
#if 0
static char sccsid[] = "@(#)echo.c 8.1 (Berkeley) 5/31/93";
#else
__RCSID("$NetBSD: echo.c,v 1.9 2001/07/29 22:36:11 wiz Exp $");
__RCSID("$NetBSD: echo.c,v 1.10 2003/08/04 22:31:23 jschauma Exp $");
#endif
#endif /* not lint */
#include <sys/param.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <vis.h>
int stdout_ok;
int main(int, char *[]);
char *printescaped(const char *);
/* ARGSUSED */
int
@ -60,6 +68,7 @@ main(int argc, char *argv[])
{
int nflag;
stdout_ok = isatty(STDOUT_FILENO);
/* This utility may NOT do getopt(3) option parsing. */
if (*++argv && !strcmp(*argv, "-n")) {
++argv;
@ -69,7 +78,10 @@ main(int argc, char *argv[])
nflag = 0;
while (*argv) {
(void)printf("%s", *argv);
char *n;
n = printescaped(*argv);
(void)printf("%s", n);
free(n);
if (*++argv)
(void)putchar(' ');
}
@ -78,3 +90,27 @@ main(int argc, char *argv[])
exit(0);
/* NOTREACHED */
}
char *
printescaped(const char *src)
{
size_t len;
char *retval;
len = strlen(src);
if (len != 0 && SIZE_T_MAX/len <= 4) {
errx(EXIT_FAILURE, "%s: name too long", src);
/* NOTREACHED */
}
retval = (char *)malloc(4*len+1);
if (retval != NULL) {
if (stdout_ok)
(void)strvis(retval, src, VIS_NL | VIS_CSTYLE);
else
(void)strcpy(retval, src);
return retval;
} else
errx(EXIT_FAILURE, "out of memory!");
/* NOTREACHED */
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: kill.c,v 1.21 2002/11/25 14:23:07 christos Exp $ */
/* $NetBSD: kill.c,v 1.22 2003/08/04 22:31:24 jschauma Exp $ */
/*
* Copyright (c) 1988, 1993, 1994
@ -43,7 +43,7 @@ __COPYRIGHT("@(#) Copyright (c) 1988, 1993, 1994\n\
#if 0
static char sccsid[] = "@(#)kill.c 8.4 (Berkeley) 4/28/95";
#else
__RCSID("$NetBSD: kill.c,v 1.21 2002/11/25 14:23:07 christos Exp $");
__RCSID("$NetBSD: kill.c,v 1.22 2003/08/04 22:31:24 jschauma Exp $");
#endif
#endif /* not lint */
@ -92,8 +92,11 @@ main(int argc, char *argv[])
if (isdigit((unsigned char)**argv) == 0)
usage();
numsig = strtol(*argv, &ep, 10);
if (*ep != '\0')
errx(1, "illegal signal number: %s", *argv);
if (*ep != '\0') {
errx(EXIT_FAILURE, "illegal signal number: %s",
*argv);
/* NOTREACHED */
}
if (numsig >= 128)
numsig -= 128;
if (numsig <= 0 || numsig >= NSIG)
@ -124,8 +127,11 @@ main(int argc, char *argv[])
nosig(*argv);
} else if (isdigit((unsigned char)**argv)) {
numsig = strtol(*argv, &ep, 10);
if (!*argv || *ep)
errx(1, "illegal signal number: %s", *argv);
if (!*argv || *ep) {
errx(EXIT_FAILURE, "illegal signal number: %s",
*argv);
/* NOTREACHED */
}
if (numsig < 0 || numsig >= NSIG)
nosig(*argv);
} else

View File

@ -1,4 +1,4 @@
/* $NetBSD: ln.c,v 1.21 2002/12/27 03:50:05 jrf Exp $ */
/* $NetBSD: ln.c,v 1.22 2003/08/04 22:31:24 jschauma Exp $ */
/*
* Copyright (c) 1987, 1993, 1994
@ -43,7 +43,7 @@ __COPYRIGHT("@(#) Copyright (c) 1987, 1993, 1994\n\
#if 0
static char sccsid[] = "@(#)ln.c 8.2 (Berkeley) 3/31/94";
#else
__RCSID("$NetBSD: ln.c,v 1.21 2002/12/27 03:50:05 jrf Exp $");
__RCSID("$NetBSD: ln.c,v 1.22 2003/08/04 22:31:24 jschauma Exp $");
#endif
#endif /* not lint */
@ -56,11 +56,14 @@ __RCSID("$NetBSD: ln.c,v 1.21 2002/12/27 03:50:05 jrf Exp $");
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <vis.h>
int fflag; /* Unlink existing files. */
int hflag; /* Check new name for symlink first. */
int sflag; /* Symbolic, not hard, link. */
int vflag; /* Verbose output */
int vflag; /* Verbose output */
int stdout_ok; /* stdout connected to a terminal */
/* System link call. */
int (*linkf)(const char *, const char *);
char linkch;
@ -68,6 +71,7 @@ char linkch;
int linkit(char *, char *, int);
void usage(void);
int main(int, char *[]);
char *printescaped(const char *);
int
main(int argc, char *argv[])
@ -95,6 +99,7 @@ main(int argc, char *argv[])
case '?':
default:
usage();
/* NOTREACHED */
}
argv += optind;
@ -119,18 +124,26 @@ main(int argc, char *argv[])
exit(linkit(argv[0], argv[1], 0));
/* NOTREACHED */
}
stdout_ok = isatty(STDOUT_FILENO);
/* ln target1 target2 directory */
sourcedir = argv[argc - 1];
if (hflag && lstat(sourcedir, &sb) == 0 && S_ISLNK(sb.st_mode)) {
/* we were asked not to follow symlinks, but found one at
the target--simulate "not a directory" error */
errno = ENOTDIR;
err(1, "%s", sourcedir);
err(EXIT_FAILURE, "%s", printescaped(sourcedir));
/* NOTREACHED */
}
if (stat(sourcedir, &sb))
err(1, "%s", sourcedir);
if (!S_ISDIR(sb.st_mode))
if (stat(sourcedir, &sb)) {
err(EXIT_FAILURE, "%s", printescaped(sourcedir));
/* NOTREACHED */
}
if (!S_ISDIR(sb.st_mode)) {
usage();
/* NOTREACHED */
}
for (exitval = 0; *argv != sourcedir; ++argv)
exitval |= linkit(*argv, sourcedir, 1);
exit(exitval);
@ -142,11 +155,17 @@ linkit(char *target, char *source, int isdir)
{
struct stat sb;
char *p, path[MAXPATHLEN];
char *sn, *tn;
sn = printescaped(source);
tn = printescaped(target);
if (!sflag) {
/* If target doesn't exist, quit now. */
if (stat(target, &sb)) {
warn("%s", target);
warn("%s", tn);
free(sn);
free(tn);
return (1);
}
}
@ -160,7 +179,9 @@ linkit(char *target, char *source, int isdir)
p = target;
else
++p;
(void)snprintf(path, sizeof(path), "%s/%s", source, p);
p = printescaped(p);
(void)snprintf(path, sizeof(path), "%s/%s", sn, p);
free(p);
source = path;
}
@ -174,8 +195,10 @@ linkit(char *target, char *source, int isdir)
return (1);
}
if (vflag)
(void)printf("%s %c> %s\n", source, linkch, target);
(void)printf("%s %c> %s\n", sn, linkch, tn);
free(sn);
free(tn);
return (0);
}
@ -189,3 +212,27 @@ usage(void)
exit(1);
/* NOTREACHED */
}
char *
printescaped(const char *src)
{
size_t len;
char *retval;
len = strlen(src);
if (len != 0 && SIZE_T_MAX/len <= 4) {
errx(EXIT_FAILURE, "%s: name too long", src);
/* NOTREACHED */
}
retval = (char *)malloc(4*len+1);
if (retval != NULL) {
if (stdout_ok)
(void)strvis(retval, src, VIS_NL | VIS_CSTYLE);
else
(void)strcpy(retval, src);
return retval;
} else
errx(EXIT_FAILURE, "out of memory!");
/* NOTREACHED */
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: mkdir.c,v 1.29 2003/03/10 23:33:10 lukem Exp $ */
/* $NetBSD: mkdir.c,v 1.30 2003/08/04 22:31:24 jschauma Exp $ */
/*
* Copyright (c) 1983, 1992, 1993
@ -43,12 +43,13 @@ __COPYRIGHT("@(#) Copyright (c) 1983, 1992, 1993\n\
#if 0
static char sccsid[] = "@(#)mkdir.c 8.2 (Berkeley) 1/25/94";
#else
__RCSID("$NetBSD: mkdir.c,v 1.29 2003/03/10 23:33:10 lukem Exp $");
__RCSID("$NetBSD: mkdir.c,v 1.30 2003/08/04 22:31:24 jschauma Exp $");
#endif
#endif /* not lint */
#include <sys/types.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <err.h>
#include <errno.h>
@ -57,10 +58,14 @@ __RCSID("$NetBSD: mkdir.c,v 1.29 2003/03/10 23:33:10 lukem Exp $");
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <vis.h>
int stdout_ok; /* stdout connected to a terminal */
int mkpath(char *, mode_t, mode_t);
void usage(void);
int main(int, char *[]);
char *printescaped(const char *);
int
main(int argc, char *argv[])
@ -88,20 +93,28 @@ main(int argc, char *argv[])
pflag = 1;
break;
case 'm':
if ((set = setmode(optarg)) == NULL)
errx(1, "invalid file mode: %s", optarg);
if ((set = setmode(optarg)) == NULL) {
errx(EXIT_FAILURE, "invalid file mode: %s",
printescaped(optarg));
/* NOTREACHED */
}
mode = getmode(set, S_IRWXU | S_IRWXG | S_IRWXO);
free(set);
break;
case '?':
default:
usage();
/* NOTREACHED */
}
argc -= optind;
argv += optind;
if (*argv == NULL)
if (*argv == NULL) {
usage();
/* NOTREACHED */
}
stdout_ok = isatty(STDOUT_FILENO);
for (exitval = EXIT_SUCCESS; *argv != NULL; ++argv) {
char *slash;
@ -127,7 +140,10 @@ main(int argc, char *argv[])
*/
if ((mode & ~(S_IRWXU|S_IRWXG|S_IRWXO)) != 0 &&
chmod(*argv, mode) == -1) {
warn("%s", *argv);
char *fn;
fn = printescaped(*argv);
warn("%s", fn);
free(fn);
exitval = EXIT_FAILURE;
}
}
@ -147,11 +163,12 @@ int
mkpath(char *path, mode_t mode, mode_t dir_mode)
{
struct stat sb;
char *slash;
char *slash, *fn;
int done, rv;
done = 0;
slash = path;
fn = printescaped(path);
for (;;) {
slash += strspn(slash, "/");
@ -172,13 +189,15 @@ mkpath(char *path, mode_t mode, mode_t dir_mode)
if (stat(path, &sb) < 0) {
/* Not there; use mkdir()s error */
errno = sverrno;
warn("%s", path);
warn("%s", fn);
free(fn);
return -1;
}
if (!S_ISDIR(sb.st_mode)) {
/* Is there, but isn't a directory */
errno = ENOTDIR;
warn("%s", path);
warn("%s", fn);
free(fn);
return -1;
}
} else if (done) {
@ -193,7 +212,8 @@ mkpath(char *path, mode_t mode, mode_t dir_mode)
*/
if ((mode & ~(S_IRWXU|S_IRWXG|S_IRWXO)) != 0 &&
chmod(path, mode) == -1) {
warn("%s", path);
warn("%s", fn);
free(fn);
return -1;
}
}
@ -203,6 +223,8 @@ mkpath(char *path, mode_t mode, mode_t dir_mode)
}
*slash = '/';
}
free(fn);
return 0;
}
@ -215,3 +237,27 @@ usage(void)
exit(EXIT_FAILURE);
/* NOTREACHED */
}
char *
printescaped(const char *src)
{
size_t len;
char *retval;
len = strlen(src);
if (len != 0 && SIZE_T_MAX/len <= 4) {
errx(EXIT_FAILURE, "%s: name too long", src);
/* NOTREACHED */
}
retval = (char *)malloc(4*len+1);
if (retval != NULL) {
if (stdout_ok)
(void)strvis(retval, src, VIS_NL | VIS_CSTYLE);
else
(void)strcpy(retval, src);
return retval;
} else
errx(EXIT_FAILURE, "out of memory!");
/* NOTREACHED */
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: mv.c,v 1.29 2003/07/13 08:25:47 itojun Exp $ */
/* $NetBSD: mv.c,v 1.30 2003/08/04 22:31:25 jschauma Exp $ */
/*
* Copyright (c) 1989, 1993, 1994
@ -46,7 +46,7 @@ __COPYRIGHT("@(#) Copyright (c) 1989, 1993, 1994\n\
#if 0
static char sccsid[] = "@(#)mv.c 8.2 (Berkeley) 4/2/94";
#else
__RCSID("$NetBSD: mv.c,v 1.29 2003/07/13 08:25:47 itojun Exp $");
__RCSID("$NetBSD: mv.c,v 1.30 2003/08/04 22:31:25 jschauma Exp $");
#endif
#endif /* not lint */
@ -65,17 +65,19 @@ __RCSID("$NetBSD: mv.c,v 1.29 2003/07/13 08:25:47 itojun Exp $");
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <vis.h>
#include "pathnames.h"
int fflg, iflg, vflg;
int stdin_ok;
int stdin_ok, stdout_ok;
int copy(char *, char *);
int do_move(char *, char *);
int fastcopy(char *, char *, struct stat *);
void usage(void);
int main(int, char *[]);
int copy(char *, char *);
int do_move(char *, char *);
int fastcopy(char *, char *, struct stat *);
void usage(void);
int main(int, char *[]);
char *printescaped(const char *);
int
main(int argc, char *argv[])
@ -112,6 +114,7 @@ main(int argc, char *argv[])
usage();
stdin_ok = isatty(STDIN_FILENO);
stdout_ok = isatty(STDOUT_FILENO);
/*
* If the stat on the target fails or the target isn't a directory,
@ -139,7 +142,9 @@ main(int argc, char *argv[])
++p;
if ((baselen + (len = strlen(p))) >= MAXPATHLEN) {
warnx("%s: destination pathname too long", *argv);
char *fn = printescaped(*argv);
warnx("%s: destination pathname too long", fn);
free(fn);
rval = 1;
} else {
memmove(endp, p, len + 1);
@ -156,6 +161,10 @@ do_move(char *from, char *to)
{
struct stat sb;
char modep[15];
char *fn, *tn;
fn = printescaped(from);
tn = printescaped(to);
/*
* (1) If the destination path exists, the -f option is not specified
@ -176,27 +185,34 @@ do_move(char *from, char *to)
if (iflg) {
if (access(from, F_OK)) {
warn("rename %s", from);
warn("rename %s", fn);
free(fn);
free(tn);
return (1);
}
(void)fprintf(stderr, "overwrite %s? ", to);
(void)fprintf(stderr, "overwrite %s? ", tn);
} else if (stdin_ok && access(to, W_OK) && !stat(to, &sb)) {
if (access(from, F_OK)) {
warn("rename %s", from);
warn("rename %s", fn);
free(fn);
free(tn);
return (1);
}
strmode(sb.st_mode, modep);
(void)fprintf(stderr, "override %s%s%s/%s for %s? ",
modep + 1, modep[9] == ' ' ? "" : " ",
user_from_uid(sb.st_uid, 0),
group_from_gid(sb.st_gid, 0), to);
group_from_gid(sb.st_gid, 0), tn);
} else
ask = 0;
if (ask) {
if ((ch = getchar()) != EOF && ch != '\n')
while (getchar() != '\n');
if (ch != 'y' && ch != 'Y')
if (ch != 'y' && ch != 'Y') {
free(fn);
free(tn);
return (0);
}
}
}
@ -215,12 +231,16 @@ do_move(char *from, char *to)
*/
if (!rename(from, to)) {
if (vflg)
printf("%s -> %s\n", from, to);
printf("%s -> %s\n", fn, tn);
free(fn);
free(tn);
return (0);
}
if (errno != EXDEV) {
warn("rename %s to %s", from, to);
warn("rename %s to %s", fn, tn);
free(fn);
free(tn);
return (1);
}
@ -232,7 +252,9 @@ do_move(char *from, char *to)
*/
if (!lstat(to, &sb)) {
if ((S_ISDIR(sb.st_mode)) ? rmdir(to) : unlink(to)) {
warn("can't remove %s", to);
warn("can't remove %s", tn);
free(fn);
free(tn);
return (1);
}
}
@ -242,9 +264,15 @@ do_move(char *from, char *to)
* as a file hierarchy rooted in the destination path...
*/
if (lstat(from, &sb)) {
warn("%s", from);
warn("%s", fn);
free(fn);
free(tn);
return (1);
}
free(fn);
free(tn);
return (S_ISREG(sb.st_mode) ?
fastcopy(from, to, &sb) : copy(from, to));
}
@ -256,32 +284,44 @@ fastcopy(char *from, char *to, struct stat *sbp)
static u_int blen;
static char *bp;
int nread, from_fd, to_fd;
char *fn, *tn;
fn = printescaped(from);
tn = printescaped(to);
if ((from_fd = open(from, O_RDONLY, 0)) < 0) {
warn("%s", from);
warn("%s", fn);
free(fn);
free(tn);
return (1);
}
if ((to_fd =
open(to, O_CREAT | O_TRUNC | O_WRONLY, sbp->st_mode)) < 0) {
warn("%s", to);
warn("%s", tn);
free(fn);
free(tn);
(void)close(from_fd);
return (1);
}
if (!blen && !(bp = malloc(blen = sbp->st_blksize))) {
warn(NULL);
free(fn);
free(tn);
return (1);
}
while ((nread = read(from_fd, bp, blen)) > 0)
if (write(to_fd, bp, nread) != nread) {
warn("%s", to);
warn("%s", tn);
goto err;
}
if (nread < 0) {
warn("%s", from);
warn("%s", fn);
err: if (unlink(to))
warn("%s: remove", to);
warn("%s: remove", tn);
(void)close(from_fd);
(void)close(to_fd);
free(fn);
free(tn);
return (1);
}
(void)close(from_fd);
@ -299,30 +339,36 @@ err: if (unlink(to))
#else
if (futimes(to_fd, tval))
#endif
warn("%s: set times", to);
warn("%s: set times", tn);
if (fchown(to_fd, sbp->st_uid, sbp->st_gid)) {
if (errno != EPERM)
warn("%s: set owner/group", to);
warn("%s: set owner/group", tn);
sbp->st_mode &= ~(S_ISUID | S_ISGID);
}
if (fchmod(to_fd, sbp->st_mode))
warn("%s: set mode", to);
warn("%s: set mode", tn);
if (fchflags(to_fd, sbp->st_flags) && (errno != EOPNOTSUPP))
warn("%s: set flags (was: 0%07o)", to, sbp->st_flags);
warn("%s: set flags (was: 0%07o)", tn, sbp->st_flags);
if (close(to_fd)) {
warn("%s", to);
warn("%s", tn);
free(fn);
free(tn);
return (1);
}
if (unlink(from)) {
warn("%s: remove", from);
warn("%s: remove", fn);
free(fn);
free(tn);
return (1);
}
if (vflg)
printf("%s -> %s\n", from, to);
printf("%s -> %s\n", fn, tn);
free(fn);
free(tn);
return (0);
}
@ -379,3 +425,27 @@ usage(void)
exit(1);
/* NOTREACHED */
}
char *
printescaped(const char *src)
{
size_t len;
char *retval;
len = strlen(src);
if (len != 0 && SIZE_T_MAX/len <= 4) {
errx(EXIT_FAILURE, "%s: name too long", src);
/* NOTREACHED */
}
retval = (char *)malloc(4*len+1);
if (retval != NULL) {
if (stdout_ok)
(void)strvis(retval, src, VIS_NL | VIS_CSTYLE);
else
(void)strcpy(retval, src);
return retval;
} else
errx(EXIT_FAILURE, "out of memory!");
/* NOTREACHED */
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: pwd.c,v 1.14 2001/09/16 22:11:56 wiz Exp $ */
/* $NetBSD: pwd.c,v 1.15 2003/08/04 22:31:25 jschauma Exp $ */
/*
* Copyright (c) 1991, 1993, 1994
@ -43,12 +43,13 @@ __COPYRIGHT("@(#) Copyright (c) 1991, 1993, 1994\n\
#if 0
static char sccsid[] = "@(#)pwd.c 8.3 (Berkeley) 4/1/94";
#else
__RCSID("$NetBSD: pwd.c,v 1.14 2001/09/16 22:11:56 wiz Exp $");
__RCSID("$NetBSD: pwd.c,v 1.15 2003/08/04 22:31:25 jschauma Exp $");
#endif
#endif /* not lint */
#include <sys/types.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <err.h>
#include <errno.h>
@ -56,16 +57,21 @@ __RCSID("$NetBSD: pwd.c,v 1.14 2001/09/16 22:11:56 wiz Exp $");
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <vis.h>
int stdout_ok; /* stdout connected to a terminal */
static char *getcwd_logical(char *, size_t);
static void usage(void);
int main(int, char *[]);
char *printescaped(const char *);
int
main(int argc, char *argv[])
{
int ch, lFlag;
const char *p;
char *pn;
setprogname(argv[0]);
lFlag = 0;
@ -95,7 +101,11 @@ main(int argc, char *argv[])
if (p == NULL)
err(EXIT_FAILURE, NULL);
(void)printf("%s\n", p);
stdout_ok = isatty(STDOUT_FILENO);
pn = printescaped(p);
(void)printf("%s\n", pn);
free(pn);
exit(EXIT_SUCCESS);
/* NOTREACHED */
@ -147,3 +157,27 @@ usage(void)
exit(EXIT_FAILURE);
/* NOTREACHED */
}
char *
printescaped(const char *src)
{
size_t len;
char *retval;
len = strlen(src);
if (len != 0 && SIZE_T_MAX/len <= 4) {
errx(EXIT_FAILURE, "%s: name too long", src);
/* NOTREACHED */
}
retval = (char *)malloc(4*len+1);
if (retval != NULL) {
if (stdout_ok)
(void)strvis(retval, src, VIS_NL | VIS_CSTYLE);
else
(void)strcpy(retval, src);
return retval;
} else
errx(EXIT_FAILURE, "out of memory!");
/* NOTREACHED */
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: rm.c,v 1.34 2003/03/01 07:57:33 enami Exp $ */
/* $NetBSD: rm.c,v 1.35 2003/08/04 22:31:25 jschauma Exp $ */
/*-
* Copyright (c) 1990, 1993, 1994, 2003
@ -43,29 +43,32 @@ __COPYRIGHT("@(#) Copyright (c) 1990, 1993, 1994\n\
#if 0
static char sccsid[] = "@(#)rm.c 8.8 (Berkeley) 4/27/95";
#else
__RCSID("$NetBSD: rm.c,v 1.34 2003/03/01 07:57:33 enami Exp $");
__RCSID("$NetBSD: rm.c,v 1.35 2003/08/04 22:31:25 jschauma Exp $");
#endif
#endif /* not lint */
#include <sys/types.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <locale.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <fts.h>
#include <grp.h>
#include <locale.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <vis.h>
int dflag, eval, fflag, iflag, Pflag, stdin_ok, vflag, Wflag;
int dflag, eval, fflag, iflag, Pflag, stdin_ok, stdout_ok, vflag, Wflag;
int check(char *, char *, struct stat *);
void checkdot(char **);
char *printescaped(const char *);
void rm_file(char **);
void rm_overwrite(char *, struct stat *);
void rm_tree(char **);
@ -136,6 +139,7 @@ main(int argc, char *argv[])
if (*argv) {
stdin_ok = isatty(STDIN_FILENO);
stdout_ok = isatty(STDOUT_FILENO);
if (rflag)
rm_tree(argv);
@ -153,7 +157,8 @@ rm_tree(char **argv)
FTS *fts;
FTSENT *p;
int flags, needstat, rval;
char *fn;
/*
* Remove a file hierarchy. If forcing removal (-f), or interactive
* (-i) or can't ask anyway (stdin_ok), don't stat the file.
@ -175,16 +180,19 @@ rm_tree(char **argv)
(int (*)(const FTSENT **, const FTSENT **))NULL)))
err(1, NULL);
while ((p = fts_read(fts)) != NULL) {
switch (p->fts_info) {
case FTS_DNR:
if (!fflag || p->fts_errno != ENOENT) {
warnx("%s: %s",
p->fts_path, strerror(p->fts_errno));
fn = printescaped(p->fts_path);
warnx("%s: %s", fn, strerror(p->fts_errno));
free(fn);
eval = 1;
}
continue;
case FTS_ERR:
errx(1, "%s: %s", p->fts_path, strerror(p->fts_errno));
errx(EXIT_FAILURE, "%s: %s", printescaped(p->fts_path),
strerror(p->fts_errno));
/* NOTREACHED */
case FTS_NS:
/*
@ -194,8 +202,9 @@ rm_tree(char **argv)
if (fflag && NONEXISTENT(p->fts_errno))
continue;
if (needstat) {
warnx("%s: %s",
p->fts_path, strerror(p->fts_errno));
fn = printescaped(p->fts_path);
warnx("%s: %s", fn, strerror(p->fts_errno));
free(fn);
eval = 1;
continue;
}
@ -248,10 +257,15 @@ rm_tree(char **argv)
break;
}
if (rval != 0) {
warn("%s", p->fts_path);
fn = printescaped(p->fts_path);
warn("%s", fn);
free(fn);
eval = 1;
} else if (vflag)
(void)printf("%s\n", p->fts_path);
} else if (vflag) {
fn = printescaped(p->fts_path);
(void)printf("%s\n", fn);
free(fn);
}
}
if (errno)
err(1, "fts_read");
@ -262,7 +276,7 @@ rm_file(char **argv)
{
struct stat sb;
int rval;
char *f;
char *f, *fn;
/*
* Remove a file. POSIX 1003.2 states that, by default, attempting
@ -275,19 +289,25 @@ rm_file(char **argv)
sb.st_mode = S_IFWHT|S_IWUSR|S_IRUSR;
} else {
if (!fflag || !NONEXISTENT(errno)) {
warn("%s", f);
fn = printescaped(f);
warn("%s", fn);
free(fn);
eval = 1;
}
continue;
}
} else if (Wflag) {
warnx("%s: %s", f, strerror(EEXIST));
fn = printescaped(f);
warnx("%s: %s", fn, strerror(EEXIST));
free(fn);
eval = 1;
continue;
}
if (S_ISDIR(sb.st_mode) && !dflag) {
warnx("%s: is a directory", f);
fn = printescaped(f);
warnx("%s: is a directory", fn);
free(fn);
eval = 1;
continue;
}
@ -303,11 +323,16 @@ rm_file(char **argv)
rval = unlink(f);
}
if (rval && (!fflag || !NONEXISTENT(errno))) {
warn("%s", f);
fn = printescaped(f);
warn("%s", fn);
free(fn);
eval = 1;
}
if (vflag && rval == 0)
(void)printf("%s\n", f);
if (vflag && rval == 0) {
fn = printescaped(f);
(void)printf("%s\n", fn);
free(fn);
}
}
}
@ -329,6 +354,7 @@ rm_overwrite(char *file, struct stat *sbp)
off_t len;
int fd, wlen;
char buf[8 * 1024];
char *fn;
fd = -1;
if (sbp == NULL) {
@ -360,7 +386,9 @@ rm_overwrite(char *file, struct stat *sbp)
return;
err: eval = 1;
warn("%s", file);
fn = printescaped(file);
warn("%s", fn);
free(fn);
}
int
@ -368,11 +396,14 @@ check(char *path, char *name, struct stat *sp)
{
int ch, first;
char modep[15];
char *fn;
/* Check -i first. */
if (iflag)
(void)fprintf(stderr, "remove %s? ", path);
else {
if (iflag) {
fn = printescaped(path);
(void)fprintf(stderr, "remove '%s'? ", fn);
free(fn);
} else {
/*
* If it's not a symbolic link and it's unwritable and we're
* talking to a terminal, ask. Symbolic links are excluded
@ -383,10 +414,12 @@ check(char *path, char *name, struct stat *sp)
!(access(name, W_OK) && (errno != ETXTBSY)))
return (1);
strmode(sp->st_mode, modep);
(void)fprintf(stderr, "override %s%s%s/%s for %s? ",
fn = printescaped(path);
(void)fprintf(stderr, "override %s%s%s/%s for '%s'? ",
modep + 1, modep[9] == ' ' ? "" : " ",
user_from_uid(sp->st_uid, 0),
group_from_gid(sp->st_gid, 0), path);
group_from_gid(sp->st_gid, 0), fn);
free(fn);
}
(void)fflush(stderr);
@ -436,6 +469,30 @@ checkdot(char **argv)
}
}
char *
printescaped(const char *src)
{
size_t len;
char *retval;
len = strlen(src);
if (len != 0 && SIZE_T_MAX/len <= 4) {
errx(EXIT_FAILURE, "%s: name too long", src);
/* NOTREACHED */
}
retval = (char *)malloc(4*len+1);
if (retval != NULL) {
if (stdout_ok)
(void)strvis(retval, src, VIS_NL | VIS_CSTYLE);
else
(void)strcpy(retval, src);
return retval;
} else
errx(EXIT_FAILURE, "out of memory!");
/* NOTREACHED */
}
void
usage(void)
{

View File

@ -1,4 +1,4 @@
/* $NetBSD: rmdir.c,v 1.17 2001/09/16 21:21:14 wiz Exp $ */
/* $NetBSD: rmdir.c,v 1.18 2003/08/04 22:31:25 jschauma Exp $ */
/*-
* Copyright (c) 1992, 1993, 1994
@ -43,10 +43,12 @@ __COPYRIGHT("@(#) Copyright (c) 1992, 1993, 1994\n\
#if 0
static char sccsid[] = "@(#)rmdir.c 8.3 (Berkeley) 4/2/94";
#else
__RCSID("$NetBSD: rmdir.c,v 1.17 2001/09/16 21:21:14 wiz Exp $");
__RCSID("$NetBSD: rmdir.c,v 1.18 2003/08/04 22:31:25 jschauma Exp $");
#endif
#endif /* not lint */
#include <sys/param.h>
#include <err.h>
#include <errno.h>
#include <locale.h>
@ -54,10 +56,14 @@ __RCSID("$NetBSD: rmdir.c,v 1.17 2001/09/16 21:21:14 wiz Exp $");
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <vis.h>
int rm_path(char *);
void usage(void);
int main(int, char *[]);
int stdout_ok; /* stdout connected to a terminal */
int rm_path(char *);
void usage(void);
int main(int, char *[]);
char *printescaped(const char *);
int
main(int argc, char *argv[])
@ -83,6 +89,8 @@ main(int argc, char *argv[])
if (argc == 0)
usage();
stdout_ok = isatty(STDIN_FILENO);
for (errors = 0; *argv; argv++) {
char *p;
@ -93,7 +101,10 @@ main(int argc, char *argv[])
*++p = '\0';
if (rmdir(*argv) < 0) {
warn("%s", *argv);
char *dn;
dn = printescaped(*argv);
warn("%s", dn);
free(dn);
errors = 1;
} else if (pflag)
errors |= rm_path(*argv);
@ -115,7 +126,10 @@ rm_path(char *path)
*++p = '\0';
if (rmdir(path) < 0) {
warn("%s", path);
char *pn;
pn = printescaped(path);
warn("%s", pn);
free(pn);
return (1);
}
}
@ -130,3 +144,27 @@ usage(void)
exit(1);
/* NOTREACHED */
}
char *
printescaped(const char *src)
{
size_t len;
char *retval;
len = strlen(src);
if (len != 0 && SIZE_T_MAX/len <= 4) {
errx(EXIT_FAILURE, "%s: name too long", src);
/* NOTREACHED */
}
retval = (char *)malloc(4*len+1);
if (retval != NULL) {
if (stdout_ok)
(void)strvis(retval, src, VIS_NL | VIS_CSTYLE);
else
(void)strcpy(retval, src);
return retval;
} else
errx(EXIT_FAILURE, "out of memory!");
/* NOTREACHED */
}