[hackers] [sbase] Add q-flag to ls(1) || FRIGN

From: <git_AT_suckless.org>
Date: Tue, 17 Feb 2015 19:12:19 +0100 (CET)

commit a2fa0c1a669216a5193ef58c83fb78d716934645
Author: FRIGN <dev_AT_frign.de>
Date: Tue Feb 17 18:59:01 2015 +0100

    Add q-flag to ls(1)
    
    of course, UTF-8-aware. ;)

diff --git a/README b/README
index 6b9e854..f8b92da 100644
--- a/README
+++ b/README
_AT_@ -40,7 +40,7 @@ The following tools are implemented ('*' == finished, '#' == UTF-8 support,
 =* ln yes none
 =* logger yes none
 =* logname yes none
-= ls no -C, -R, -q, -u
+= ls no -C, -R, -u
 =* md5sum non-posix none
 =* mkdir yes none
 =* mkfifo yes none
diff --git a/ls.1 b/ls.1
index 993d29e..7389350 100644
--- a/ls.1
+++ b/ls.1
_AT_@ -1,4 +1,4 @@
-.Dd January 20, 2015
+.Dd February 17, 2015
 .Dt LS 1
 .Os sbase
 .Sh NAME
_AT_@ -6,7 +6,7 @@
 .Nd list directory contents
 .Sh SYNOPSIS
 .Nm
-.Op Fl 1acdFHhiLlrtU
+.Op Fl 1acdFHhiLlqrtU
 .Op Ar file ...
 .Sh DESCRIPTION
 .Nm
_AT_@ -36,6 +36,8 @@ themselves.
 .It Fl l
 List detailed information about each file, including their type, permissions,
 links, owner, group, size, and last file status/modification time.
+.It Fl q
+Replace non-printable characters in filenames with '?'.
 .It Fl r
 Reverse the sort order.
 .It Fl t
diff --git a/ls.c b/ls.c
index 6809b77..be5a9cd 100644
--- a/ls.c
+++ b/ls.c
_AT_@ -10,6 +10,7 @@
 #include <time.h>
 #include <unistd.h>
 
+#include "utf.h"
 #include "util.h"
 
 struct entry {
_AT_@ -32,6 +33,7 @@ static int hflag = 0;
 static int iflag = 0;
 static int Lflag = 0;
 static int lflag = 0;
+static int qflag = 0;
 static int rflag = 0;
 static int tflag = 0;
 static int Uflag = 0;
_AT_@ -174,9 +176,10 @@ static void
 lsdir(const char *path)
 {
         DIR *dp;
+ Rune r;
         struct entry ent, *ents = NULL;
         struct dirent *d;
- size_t i, n = 0;
+ size_t i, j, k, n = 0, len;
         char *cwd, *p;
 
         cwd = agetcwd();
_AT_@ -201,6 +204,28 @@ lsdir(const char *path)
                 } else {
                         ents = erealloc(ents, ++n * sizeof(*ents));
                         p = estrdup(d->d_name);
+ if (qflag) {
+ len = strlen(p);
+ for (i = 1, j = 0; j + i <= len;) {
+ if (!fullrune(p + j, i)) {
+ i++;
+ continue;
+ }
+ charntorune(&r, p + j, i);
+ if (isprintrune(r)) {
+ j += i;
+ i = 1;
+ continue;
+ }
+ p[j] = '?';
+ for (k = j + 1; k < len - i + 2; k++) {
+ p[k] = p[k + i - 1];
+ }
+ len -= i - 1;
+ j += 1;
+ i = 1;
+ }
+ }
                         mkent(&ents[n - 1], p, tflag || Fflag || lflag || iflag, Lflag);
                 }
         }
_AT_@ -231,7 +256,7 @@ ls(const struct entry *ent)
 static void
 usage(void)
 {
- eprintf("usage: %s [-1acdFHhiLlrtU] [file ...]\n", argv0);
+ eprintf("usage: %s [-1acdFHhiLlqrtU] [file ...]\n", argv0);
 }
 
 int
_AT_@ -271,6 +296,9 @@ main(int argc, char *argv[])
         case 'l':
                 lflag = 1;
                 break;
+ case 'q':
+ qflag = 1;
+ break;
         case 'r':
                 rflag = 1;
                 break;
Received on Tue Feb 17 2015 - 19:12:19 CET

This archive was generated by hypermail 2.3.0 : Tue Feb 17 2015 - 19:24:08 CET