Subject: Re: rm(1) and cp(1) printable characters diff
To: None <tech-userlevel@netbsd.org>
From: Jan Schaumann <jschauma@netmeister.org>
List: tech-userlevel
Date: 07/20/2003 19:51:26
--EuxKj2iCbKjpUGkD
Content-Type: multipart/mixed; boundary="vtzGhvizbBRQ85DL"
Content-Disposition: inline
--vtzGhvizbBRQ85DL
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable
"Martin J. Laubach" <mjl+usenet-2003-05@emsi.priv.at> wrote:
=20
> Looks ok but for two minor points:
>=20
> You leak memory with each printescaped() call. I'm not sure it's
> worth the bother to free everything after use or add a wrapper for
> the output routines.
>=20
> sizeof(char) is 1. Always, per definitionem.
Ok, one more round, then.
Thanks,
-Jan
--=20
DON'T PANIC!
--vtzGhvizbBRQ85DL
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename=diff
Content-Transfer-Encoding: quoted-printable
Index: cp/cp.c
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /cvsroot/src/bin/cp/cp.c,v
retrieving revision 1.32
diff -u -r1.32 cp.c
--- cp/cp.c 16 Dec 2002 14:44:14 -0000 1.32
+++ cp/cp.c 20 Jul 2003 23:45:41 -0000
@@ -77,6 +77,7 @@
#include <stdio.h>
#include <string.h>
#include <unistd.h>
+#include <vis.h>
=20
#include "extern.h"
=20
@@ -88,14 +89,15 @@
PATH_T to =3D { to.p_path, "" };
=20
uid_t myuid;
-int Rflag, fflag, iflag, pflag, rflag, vflag;=20
+int Rflag, fflag, iflag, pflag, rflag, stdin_ok, vflag;=20
mode_t myumask;
=20
enum op { FILE_TO_FILE, FILE_TO_DIR, DIR_TO_DNE };
=20
-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 **);
+char *printescaped(const char *);
=20
int
main(int argc, char *argv[])
@@ -153,6 +155,8 @@
if (argc < 2)
usage();
=20
+ stdin_ok =3D isatty(STDIN_FILENO);
+
fts_options =3D FTS_NOCHDIR | FTS_PHYSICAL;
if (rflag) {
if (Rflag)
@@ -184,8 +188,12 @@
=20
/* Save the target base in "to". */
target =3D argv[--argc];
- if (strlen(target) > MAXPATHLEN)
- errx(1, "%s: name too long", target);
+ if (strlen(target) > MAXPATHLEN) {
+ char *fn;
+ fn =3D printescaped(target);
+ errx(1, "%s: name too long", fn);
+ free(fn);
+ }
(void)strcpy(to.p_path, target);
to.p_end =3D to.p_path + strlen(to.p_path);
if (to.p_path =3D=3D to.p_end) {
@@ -262,7 +270,7 @@
FTS *ftsp;
FTSENT *curr;
int base, dne, nlen, rval;
- char *p, *tmp;
+ char *p, *tmp, *fn;
=20
base =3D 0; /* XXX gcc -Wuninitialized (see comment below) */
=20
@@ -273,12 +281,15 @@
case FTS_NS:
case FTS_DNR:
case FTS_ERR:
- warnx("%s: %s",
- curr->fts_path, strerror(curr->fts_errno));
+ fn =3D printescaped(curr->fts_path);
+ warnx("%s: %s", fn, strerror(curr->fts_errno));
+ free(fn);
rval =3D 1;
continue;
case FTS_DC: /* Warn, continue. */
- warnx("%s: directory causes a cycle", curr->fts_path);
+ fn =3D printescaped(curr->fts_path);
+ warnx("%s: directory causes a cycle", fn);
+ free(fn);
rval =3D 1;
continue;
}
@@ -290,8 +301,12 @@
if (type !=3D FILE_TO_FILE) {
if ((curr->fts_namelen +
to.target_end - to.p_path + 1) > MAXPATHLEN) {
- warnx("%s/%s: name too long (not copied)",=20
- to.p_path, curr->fts_name);
+ char *tn;
+ tn =3D printescaped(to.p_path);
+ fn =3D printescaped(curr->fts_name);
+ warnx("%s/%s: name too long (not copied)", tn, fn);
+ free(fn);
+ free(tn);
rval =3D 1;
continue;
}
@@ -455,8 +470,14 @@
rval =3D 1;
break;
}
- if (vflag)
- (void)printf("%s -> %s\n", curr->fts_path, to.p_path);
+ if (vflag) {
+ char *tn;
+ fn =3D printescaped(curr->fts_path);
+ tn =3D printescaped(to.p_path);
+ (void)printf("%s -> %s\n", fn, tn);
+ free(fn);
+ free(tn);
+ }
}
if (errno)
err(1, "fts_read");
@@ -487,4 +508,17 @@
if (b_info =3D=3D FTS_D)
return (1);
return (0);
+}
+
+char *
+printescaped(const char *src)
+{
+ char *retval;
+
+ retval =3D (char *)malloc(((strlen(src) * 4) + 1));
+ if (stdin_ok && (retval !=3D NULL)) {
+ (void)strvis(retval, src, VIS_NL | VIS_CSTYLE);
+ return retval;
+ } else=20
+ return strdup(src);
}
Index: rm/rm.c
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /cvsroot/src/bin/rm/rm.c,v
retrieving revision 1.34
diff -u -r1.34 rm.c
--- rm/rm.c 1 Mar 2003 07:57:33 -0000 1.34
+++ rm/rm.c 20 Jul 2003 23:45:42 -0000
@@ -47,25 +47,27 @@
#endif
#endif /* not lint */
=20
-#include <sys/types.h>
#include <sys/stat.h>
+#include <sys/types.h>
=20
-#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>
=20
int dflag, eval, fflag, iflag, Pflag, stdin_ok, vflag, Wflag;
=20
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 **);
@@ -153,6 +155,7 @@
FTS *fts;
FTSENT *p;
int flags, needstat, rval;
+ char *fn;
=20
/*
* Remove a file hierarchy. If forcing removal (-f), or interactive
@@ -178,13 +181,15 @@
switch (p->fts_info) {
case FTS_DNR:
if (!fflag || p->fts_errno !=3D ENOENT) {
- warnx("%s: %s",
- p->fts_path, strerror(p->fts_errno));
+ fn =3D printescaped(p->fts_path);
+ warnx("%s: %s", fn, strerror(p->fts_errno));
+ free(fn);
eval =3D 1;
}
continue;
case FTS_ERR:
- errx(1, "%s: %s", p->fts_path, strerror(p->fts_errno));
+ errx(1, "%s: %s", printescaped(p->fts_path),
+ strerror(p->fts_errno));
/* NOTREACHED */
case FTS_NS:
/*
@@ -194,8 +199,9 @@
if (fflag && NONEXISTENT(p->fts_errno))
continue;
if (needstat) {
- warnx("%s: %s",
- p->fts_path, strerror(p->fts_errno));
+ fn =3D printescaped(p->fts_path);
+ warnx("%s: %s", fn, strerror(p->fts_errno));
+ free(fn);
eval =3D 1;
continue;
}
@@ -262,7 +268,7 @@
{
struct stat sb;
int rval;
- char *f;
+ char *f, *fn;
=20
/*
* Remove a file. POSIX 1003.2 states that, by default, attempting
@@ -275,19 +281,25 @@
sb.st_mode =3D S_IFWHT|S_IWUSR|S_IRUSR;
} else {
if (!fflag || !NONEXISTENT(errno)) {
- warn("%s", f);
+ fn =3D printescaped(f);
+ warn("%s", fn);
+ free(fn);
eval =3D 1;
}
continue;
}
} else if (Wflag) {
- warnx("%s: %s", f, strerror(EEXIST));
+ fn =3D printescaped(f);
+ warnx("%s: %s", fn, strerror(EEXIST));
+ free(fn);
eval =3D 1;
continue;
}
=20
if (S_ISDIR(sb.st_mode) && !dflag) {
- warnx("%s: is a directory", f);
+ fn =3D printescaped(f);
+ warnx("%s: is a directory", fn);
+ free(fn);
eval =3D 1;
continue;
}
@@ -368,11 +380,14 @@
{
int ch, first;
char modep[15];
+ char *fn;
=20
/* Check -i first. */
- if (iflag)
- (void)fprintf(stderr, "remove %s? ", path);
- else {
+ if (iflag) {
+ fn =3D 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 +398,11 @@
!(access(name, W_OK) && (errno !=3D ETXTBSY)))
return (1);
strmode(sp->st_mode, modep);
- (void)fprintf(stderr, "override %s%s%s/%s for %s? ",
+ fn =3D printescaped(path);
+ (void)fprintf(stderr, "override %s%s%s/%s for '%s'? ",
modep + 1, modep[9] =3D=3D ' ' ? "" : " ",
user_from_uid(sp->st_uid, 0),
- group_from_gid(sp->st_gid, 0), path);
+ group_from_gid(sp->st_gid, 0), fn);
}
(void)fflush(stderr);
=20
@@ -434,6 +450,19 @@
} else
++t;
}
+}
+
+char *
+printescaped(const char *src)
+{
+ char *retval;
+
+ retval =3D (char *)malloc(((strlen(src) * 4) + 1));
+ if (stdin_ok && (retval !=3D NULL)) {
+ (void)strvis(retval, src, VIS_NL | VIS_CSTYLE);
+ return retval;
+ } else=20
+ return strdup(src);
}
=20
void
--vtzGhvizbBRQ85DL--
--EuxKj2iCbKjpUGkD
Content-Type: application/pgp-signature
Content-Disposition: inline
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (NetBSD)
iD8DBQE/Gyr+fFtkr68iakwRArG9AJ9iMsK7kMZFlmNUhCUy2FWNWrtsnACeLw+9
dR1nmnCv6sgB23YWt7r3kYs=
=QOTT
-----END PGP SIGNATURE-----
--EuxKj2iCbKjpUGkD--