summaryrefslogtreecommitdiffstats
path: root/bin/ls
diff options
context:
space:
mode:
authorallanjude <allanjude@FreeBSD.org>2015-07-21 05:03:59 +0000
committerallanjude <allanjude@FreeBSD.org>2015-07-21 05:03:59 +0000
commitf137e0726b5a82a8cf998a522716bd66f5c6f083 (patch)
tree88176e334c7f23cc08501f0fd422200d40861aac /bin/ls
parent70dd4560e3fbcd49dadc97d9ee327f1b02bace2d (diff)
downloadFreeBSD-src-f137e0726b5a82a8cf998a522716bd66f5c6f083.zip
FreeBSD-src-f137e0726b5a82a8cf998a522716bd66f5c6f083.tar.gz
Fix some issues with the application of libxo to ls(1)
* Add whitespace trimming to some fields (username, group, size, inode, blocks) to avoid whitespace in JSON strings * fix -m mode, was invalid JSON (repeated keys), and was missing outer array container * in -n mode, numeric uids and gids were returned as strings Approved by: eadler (mentor) Sponsored by: ScaleEngine Inc. Differential Revision: https://reviews.freebsd.org/D2854
Diffstat (limited to 'bin/ls')
-rw-r--r--bin/ls/ls.c3
-rw-r--r--bin/ls/ls.h3
-rw-r--r--bin/ls/print.c50
3 files changed, 45 insertions, 11 deletions
diff --git a/bin/ls/ls.c b/bin/ls/ls.c
index df8cc2d..91ef9ea 100644
--- a/bin/ls/ls.c
+++ b/bin/ls/ls.c
@@ -119,7 +119,7 @@ static int f_nofollow; /* don't follow symbolic link arguments */
int f_nonprint; /* show unprintables as ? */
static int f_nosort; /* don't sort output */
int f_notabs; /* don't use tab-separated multi-col output */
-static int f_numericonly; /* don't convert uid/gid to name */
+ int f_numericonly; /* don't convert uid/gid to name */
int f_octal; /* show unprintables as \xxx */
int f_octal_escape; /* like f_octal but use C escapes if possible */
static int f_recursive; /* ls subdirectories also */
@@ -195,6 +195,7 @@ main(int argc, char *argv[])
if (argc < 0)
return (1);
xo_set_flags(NULL, XOF_COLUMNS);
+ xo_set_version(LS_XO_VERSION);
while ((ch = getopt(argc, argv,
"1ABCD:FGHILPRSTUWXZabcdfghiklmnopqrstuwxy,")) != -1) {
diff --git a/bin/ls/ls.h b/bin/ls/ls.h
index 1a45eb4..e486777 100644
--- a/bin/ls/ls.h
+++ b/bin/ls/ls.h
@@ -37,6 +37,8 @@
#define HUMANVALSTR_LEN 5
+#define LS_XO_VERSION "1"
+
extern long blocksize; /* block size units */
extern int f_accesstime; /* use time of last access */
@@ -58,6 +60,7 @@ extern int f_statustime; /* use time of last mode change */
extern int f_thousands; /* show file sizes with thousands separators */
extern char *f_timeformat; /* user-specified time format */
extern int f_notabs; /* don't use tab-separated multi-col output */
+extern int f_numericonly; /* don't convert uid/gid to name */
extern int f_type; /* add type character for non-regular files */
#ifdef COLORLS
extern int f_color; /* add type in color for non-regular files */
diff --git a/bin/ls/print.c b/bin/ls/print.c
index 2d1425d..08701c0 100644
--- a/bin/ls/print.c
+++ b/bin/ls/print.c
@@ -171,7 +171,7 @@ printlong(const DISPLAY *dp)
xo_open_list("entry");
for (p = dp->list; p; p = p->fts_link) {
- char *name;
+ char *name, *type;
if (IS_NOPRINT(p))
continue;
xo_open_instance("entry");
@@ -180,22 +180,46 @@ printlong(const DISPLAY *dp)
if (name)
xo_emit("{ke:name/%hs}", name);
if (f_inode)
- xo_emit("{:inode/%*ju} ",
+ xo_emit("{t:inode/%*ju} ",
dp->s_inode, (uintmax_t)sp->st_ino);
if (f_size)
- xo_emit("{:blocks/%*jd} ",
+ xo_emit("{t:blocks/%*jd} ",
dp->s_block, howmany(sp->st_blocks, blocksize));
strmode(sp->st_mode, buf);
aclmode(buf, p);
np = p->fts_pointer;
xo_attr("value", "%03o", (int) sp->st_mode & ALLPERMS);
- xo_emit("{t:mode/%s} {:links/%*u} {:user/%-*s} {:group/%-*s} ",
- buf, dp->s_nlink, sp->st_nlink,
- dp->s_user, np->user, dp->s_group, np->group);
+ if (f_numericonly) {
+ xo_emit("{t:mode/%s}{e:mode_octal/%03o} {t:links/%*u} {td:user/%-*s}{e:user/%ju} {td:group/%-*s}{e:group/%ju} ",
+ buf, (int) sp->st_mode & ALLPERMS, dp->s_nlink, sp->st_nlink,
+ dp->s_user, np->user, sp->st_uid, dp->s_group, np->group, sp->st_gid);
+ } else {
+ xo_emit("{t:mode/%s}{e:mode_octal/%03o} {t:links/%*u} {t:user/%-*s} {t:group/%-*s} ",
+ buf, (int) sp->st_mode & ALLPERMS, dp->s_nlink, sp->st_nlink,
+ dp->s_user, np->user, dp->s_group, np->group);
+ }
+ if (S_ISBLK(sp->st_mode))
+ asprintf(&type, "block");
+ if (S_ISCHR(sp->st_mode))
+ asprintf(&type, "character");
+ if (S_ISDIR(sp->st_mode))
+ asprintf(&type, "directory");
+ if (S_ISFIFO(sp->st_mode))
+ asprintf(&type, "fifo");
+ if (S_ISLNK(sp->st_mode))
+ asprintf(&type, "symlink");
+ if (S_ISREG(sp->st_mode))
+ asprintf(&type, "regular");
+ if (S_ISSOCK(sp->st_mode))
+ asprintf(&type, "socket");
+ if (S_ISWHT(sp->st_mode))
+ asprintf(&type, "whiteout");
+ xo_emit("{e:type/%s}", type);
+ free(type);
if (f_flags)
xo_emit("{:flags/%-*s} ", dp->s_flags, np->flags);
if (f_label)
- xo_emit("{:label/%-*s} ", dp->s_label, np->label);
+ xo_emit("{t:label/%-*s} ", dp->s_label, np->label);
if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode))
printdev(dp->s_size, sp->st_rdev);
else
@@ -238,6 +262,7 @@ printstream(const DISPLAY *dp)
FTSENT *p;
int chcnt;
+ xo_open_list("entry");
for (p = dp->list, chcnt = 0; p; p = p->fts_link) {
if (p->fts_number == NO_PRINT)
continue;
@@ -247,12 +272,15 @@ printstream(const DISPLAY *dp)
xo_emit("\n");
chcnt = 0;
}
+ xo_open_instance("file");
chcnt += printaname(p, dp->s_inode, dp->s_block);
+ xo_close_instance("file");
if (p->fts_link) {
xo_emit(", ");
chcnt += 2;
}
}
+ xo_close_list("entry");
if (chcnt)
xo_emit("\n");
}
@@ -369,10 +397,10 @@ printaname(const FTSENT *p, u_long inodefield, u_long sizefield)
sp = p->fts_statp;
chcnt = 0;
if (f_inode)
- chcnt += xo_emit("{:inode/%*ju} ",
+ chcnt += xo_emit("{t:inode/%*ju} ",
(int)inodefield, (uintmax_t)sp->st_ino);
if (f_size)
- chcnt += xo_emit("{:size/%*jd} ",
+ chcnt += xo_emit("{t:size/%*jd} ",
(int)sizefield, howmany(sp->st_blocks, blocksize));
#ifdef COLORLS
if (f_color)
@@ -425,9 +453,11 @@ printtime(const char *field, time_t ftime)
format = d_first ? "%e %b %Y" : "%b %e %Y";
strftime(longstring, sizeof(longstring), format, localtime(&ftime));
- snprintf(fmt, sizeof(fmt), "{:%s/%%hs} ", field);
+ snprintf(fmt, sizeof(fmt), "{d:%s/%%hs} ", field);
xo_attr("value", "%ld", (long) ftime);
xo_emit(fmt, longstring);
+ snprintf(fmt, sizeof(fmt), "{en:%s/%%ld} ", field);
+ xo_emit(fmt, (long) ftime);
}
static int
OpenPOWER on IntegriCloud