9front - general discussion about 9front
 help / color / mirror / Atom feed
From: Romano <unobe@cpan.org>
To: 9front@9front.org
Subject: [9front] PATCH walk(1) to (optionally) quote name and path
Date: Fri, 11 Aug 2023 20:25:56 -0700	[thread overview]
Message-ID: <B8984F09B7B464CE69520E05416879CF@smtp.pobox.com> (raw)

I'm working with files on a backup drive that have spaces and
quotation marks in their names, using walk(1).  I can pipe to sed
s/''''/''''''''''/g and do similarly for other characters requiring
escaping for rc(1), but figured patching walk(1) would be better.  I
don't like how I just copied case 'p': the quoting "works", but isn't
as clean as the output for ls(1).  For example:

Desktop/Movies/'A Bug''s Life.mp4'
'Desktop/Movies/TV/TV Library.tvlibrary'/'Library Preferences.tvdb'
Desktop/Movies/'The American President.mp4'

instead of:

'Desktop/Movies/A Bug''s Life.mp4'
'Desktop/Movies/TV/TV Library.tvlibrary/Library Preferences.tvdb'
'Desktop/Movies/The American President.mp4'

But it's better than what happens now, where there's no quoting.

From: Romano <unobe@cpan.org>
Date: Sat, 12 Aug 2023 03:18:22 +0000
Subject: [PATCH] walk: quote name and paths using N and P

---
diff deb39a43ae5ed09c7971726cedeb06a9f65ccc6d 2da52094832662c533f60629813dbd5ac805e3ad
--- a/sys/man/1/walk
+++ b/sys/man/1/walk
@@ -81,8 +81,14 @@
 .B n
 final path element (name)
 .TP
+.B N
+final path element (name), rc (1) quoted when necessary
+.TP
 .B p
 path
+.TP
+.B P
+path, rc (1) quoted when necessary
 .TP
 .B q
 qid path.version.type (see
--- a/sys/src/cmd/walk.c
+++ b/sys/src/cmd/walk.c
@@ -43,7 +43,7 @@
 void
 dofile(char *path, Dir *f, int pathonly)
 {
-	char *p;
+	char *p, *quoted;
 
 	if(
 		(f == dotdir)
@@ -60,6 +60,10 @@
 		case 'a': Bprint(bout, "%uld", f->atime); break;
 		case 'm': Bprint(bout, "%uld", f->mtime); break;
 		case 'n': Bwrite(bout, f->name, strlen(f->name)); break;
+		case 'N':
+			quoted = quotestrdup(f->name);
+			Bwrite(bout, quoted, strlen(quoted));
+			break;
 		case 'p':
 			if(path != dotpath)
 				Bwrite(bout, path, strlen(path));
@@ -69,6 +73,18 @@
 				Bwrite(bout, f->name, strlen(f->name));
 			}
 			break;
+		case 'P':
+			if(path != dotpath) {
+				quoted = quotestrdup(path);
+				Bwrite(bout, quoted, strlen(quoted));
+			}
+			if(! (f->qid.type & QTDIR) && !pathonly){
+				if(path != dotpath)
+					Bputc(bout, '/');
+				quoted = quotestrdup(f->name);
+				Bwrite(bout, quoted, strlen(quoted));
+			}
+			break;
 		case 'q': Bprint(bout, "%ullx.%uld.%.2uhhx", f->qid.path, f->qid.vers, f->qid.type); break;
 		case 's': Bprint(bout, "%lld", f->length); break;
 		case 'x': Bprint(bout, "%M", f->mode); break;
@@ -243,7 +259,7 @@
 		if((stfmt = s_reset(stfmt)) == nil)
 			sysfatal("s_reset: %r");
 		s_append(stfmt, EARGF(usage()));
-		i = strspn(s_to_c(stfmt), "UGMamnpqsxDT");
+		i = strspn(s_to_c(stfmt), "UGMamnNpPqsxDT");
 		if(i != s_len(stfmt))
 			sysfatal("bad stfmt: %s", s_to_c(stfmt));
 		break;


             reply	other threads:[~2023-08-12  3:30 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-12  3:25 Romano [this message]
2023-08-12  5:46 ` ori
2023-08-12  6:34   ` unobe
2023-08-12  6:44   ` unobe
2023-08-12 19:40     ` ori
2023-08-12 20:07       ` unobe
2023-08-12 13:21 ` hiro
2023-08-12 20:11   ` unobe
2023-08-13 20:10     ` hiro

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=B8984F09B7B464CE69520E05416879CF@smtp.pobox.com \
    --to=unobe@cpan.org \
    --cc=9front@9front.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).