diff options
-rw-r--r-- | bin/ls/cmp.c | 15 | ||||
-rw-r--r-- | bin/ls/ls.c | 74 | ||||
-rw-r--r-- | bin/ls/main.c | 6 | ||||
-rw-r--r-- | bin/ls/print.c | 84 | ||||
-rw-r--r-- | bin/ls/util.c | 14 |
5 files changed, 73 insertions, 120 deletions
diff --git a/bin/ls/cmp.c b/bin/ls/cmp.c index 21d50e1..71f7e51 100644 --- a/bin/ls/cmp.c +++ b/bin/ls/cmp.c @@ -32,15 +32,6 @@ * SUCH DAMAGE. */ -#include <sys/cdefs.h> -#ifndef lint -#if 0 -static char sccsid[] = "@(#)cmp.c 8.1 (Berkeley) 5/31/93"; -#else -__RCSID("$NetBSD: cmp.c,v 1.17 2003/08/07 09:05:14 agc Exp $"); -#endif -#endif /* not lint */ - #include <sys/types.h> #include <sys/stat.h> @@ -52,9 +43,9 @@ __RCSID("$NetBSD: cmp.c,v 1.17 2003/08/07 09:05:14 agc Exp $"); #if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) || \ defined(_XOPEN_SOURCE) || defined(__NetBSD__) -#define ATIMENSEC_CMP(x, op, y) ((x)->st_atimensec op (y)->st_atimensec) -#define CTIMENSEC_CMP(x, op, y) ((x)->st_ctimensec op (y)->st_ctimensec) -#define MTIMENSEC_CMP(x, op, y) ((x)->st_mtimensec op (y)->st_mtimensec) +#define ATIMENSEC_CMP(x, op, y) ((x)->st_atim.tv_nsec op (y)->st_atim.tv_nsec) +#define CTIMENSEC_CMP(x, op, y) ((x)->st_ctim.tv_nsec op (y)->st_ctim.tv_nsec) +#define MTIMENSEC_CMP(x, op, y) ((x)->st_mtim.tv_nsec op (y)->st_mtim.tv_nsec) #else #define ATIMENSEC_CMP(x, op, y) \ ((x)->st_atimespec.tv_nsec op (y)->st_atimespec.tv_nsec) diff --git a/bin/ls/ls.c b/bin/ls/ls.c index 282207f..532da47 100644 --- a/bin/ls/ls.c +++ b/bin/ls/ls.c @@ -32,20 +32,6 @@ * SUCH DAMAGE. */ -#include <sys/cdefs.h> -#ifndef lint -__COPYRIGHT("@(#) Copyright (c) 1989, 1993, 1994\ - The Regents of the University of California. All rights reserved."); -#endif /* not lint */ - -#ifndef lint -#if 0 -static char sccsid[] = "@(#)ls.c 8.7 (Berkeley) 8/5/94"; -#else -__RCSID("$NetBSD: ls.c,v 1.76 2017/02/06 21:06:04 rin Exp $"); -#endif -#endif /* not lint */ - #include <sys/param.h> #include <sys/types.h> #include <sys/stat.h> @@ -58,12 +44,12 @@ __RCSID("$NetBSD: ls.c,v 1.76 2017/02/06 21:06:04 rin Exp $"); #include <locale.h> #include <stdio.h> #include <stdlib.h> +#include <bsd/stdlib.h> #include <string.h> #include <unistd.h> #include <termios.h> #include <pwd.h> #include <grp.h> -#include <util.h> #include "ls.h" #include "extern.h" @@ -114,7 +100,7 @@ int f_whiteout; /* show whiteout entries */ int f_fullpath; /* print full pathname, not filename */ int f_leafonly; /* when recursing, print leaf names only */ -__dead static void +static void usage(void) { @@ -346,7 +332,7 @@ ls_main(int argc, char *argv[]) /* If -i, -l, or -s, figure out block size. */ if (f_inode || f_longform || f_size) { if (!kflag) - (void)getbsize(NULL, &blocksize); + blocksize = 4096; blocksize /= 512; } @@ -499,13 +485,14 @@ display(FTSENT *p, FTSENT *list) ino_t maxinode; int maxmajor, maxminor; uint32_t maxnlink; - int bcfile, entries, flen, glen, ulen, maxflags, maxgroup; + int bcfile, entries, glen, ulen, maxgroup; unsigned int maxlen; int maxuser, needstats; - const char *user, *group; + struct passwd *user; + struct group *group; + const char *user_name, *group_name; char buf[21]; /* 64 bits == 20 digits, +1 for NUL */ char nuser[12], ngroup[12]; - char *flags = NULL; /* * If list is NULL there are two possibilities: that the parent @@ -518,10 +505,9 @@ display(FTSENT *p, FTSENT *list) return; needstats = f_inode || f_longform || f_size; - flen = 0; maxinode = maxnlink = 0; bcfile = 0; - maxuser = maxgroup = maxflags = maxlen = 0; + maxuser = maxgroup = maxlen = 0; btotal = stotal = maxblock = maxsize = 0; maxmajor = maxminor = 0; for (cur = list, entries = 0; cur; cur = cur->fts_link) { @@ -573,46 +559,37 @@ display(FTSENT *p, FTSENT *list) btotal += sp->st_blocks; stotal += sp->st_size; if (f_longform) { - if (f_numericonly || - (user = user_from_uid(sp->st_uid, 0)) == - NULL) { + user = getpwuid(sp->st_uid); + if (user != NULL) { + user_name = user->pw_name; + } + if (f_numericonly) { (void)snprintf(nuser, sizeof(nuser), "%u", sp->st_uid); - user = nuser; + user_name = nuser; } - if (f_numericonly || - (group = group_from_gid(sp->st_gid, 0)) == - NULL) { + group = getgrgid(sp->st_gid); + if (group != NULL) { + group_name = group->gr_name; + } + if (f_numericonly) { (void)snprintf(ngroup, sizeof(ngroup), "%u", sp->st_gid); - group = ngroup; + group_name = ngroup; } - if ((ulen = strlen(user)) > maxuser) + if ((ulen = strlen(user_name)) > maxuser) maxuser = ulen; - if ((glen = strlen(group)) > maxgroup) + if ((glen = strlen(group_name)) > maxgroup) maxgroup = glen; - if (f_flags) { - flags = - flags_to_string((u_long)sp->st_flags, "-"); - if ((flen = strlen(flags)) > maxflags) - maxflags = flen; - } else - flen = 0; if ((np = malloc(sizeof(NAMES) + - ulen + glen + flen + 2)) == NULL) + ulen + glen + 2)) == NULL) err(EXIT_FAILURE, NULL); np->user = &np->data[0]; - (void)strcpy(np->user, user); + (void)strcpy(np->user, user_name); np->group = &np->data[ulen + 1]; - (void)strcpy(np->group, group); - - if (f_flags) { - np->flags = &np->data[ulen + glen + 2]; - (void)strcpy(np->flags, flags); - free(flags); - } + (void)strcpy(np->group, group_name); cur->fts_pointer = np; } } @@ -637,7 +614,6 @@ display(FTSENT *p, FTSENT *list) if (f_commas) /* allow for commas before every third digit */ d.s_block += (d.s_block - 1) / 3; } - d.s_flags = maxflags; d.s_group = maxgroup; (void)snprintf(buf, sizeof(buf), "%llu", (unsigned long long)maxinode); diff --git a/bin/ls/main.c b/bin/ls/main.c index c1316aa..221a989 100644 --- a/bin/ls/main.c +++ b/bin/ls/main.c @@ -29,12 +29,6 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#include <sys/cdefs.h> - -#ifndef lint -__RCSID("$NetBSD: main.c,v 1.5 2016/09/05 01:00:07 sevan Exp $"); -#endif /* not lint */ - #include <sys/types.h> #include <fts.h> diff --git a/bin/ls/print.c b/bin/ls/print.c index b3aecd8..8e62329 100644 --- a/bin/ls/print.c +++ b/bin/ls/print.c @@ -32,15 +32,6 @@ * SUCH DAMAGE. */ -#include <sys/cdefs.h> -#ifndef lint -#if 0 -static char sccsid[] = "@(#)print.c 8.5 (Berkeley) 7/28/94"; -#else -__RCSID("$NetBSD: print.c,v 1.55 2014/05/10 09:39:18 martin Exp $"); -#endif -#endif /* not lint */ - #include <sys/param.h> #include <sys/stat.h> @@ -49,14 +40,15 @@ __RCSID("$NetBSD: print.c,v 1.55 2014/05/10 09:39:18 martin Exp $"); #include <inttypes.h> #include <fts.h> #include <grp.h> +#include <math.h> #include <pwd.h> +#include <stdbool.h> #include <stdio.h> #include <stdlib.h> +#include <bsd/string.h> #include <string.h> #include <time.h> -#include <tzfile.h> #include <unistd.h> -#include <util.h> #include "ls.h" #include "extern.h" @@ -119,13 +111,45 @@ printscol(DISPLAY *dp) } } + +char *scale[] = { + "", + "k", + "M", + "G", + "T", + "P", + "E", + "Z", + "Y" +}; + +char *humanize(uint64_t size, bool si) { + uint16_t unit = si ? 1000 : 1024; + int8_t radix = (int8_t)(log(size)/log(unit)); + char suffix[2], ret[6]; + double val; + uint64_t temp = size; + + if (radix > 9) { + radix = 9; + } + + val = size / (double)pow(unit, radix); + + snprintf(ret, 6, "%.0f%s", val, scale[radix]); + + return strndup(ret, 6); +} + + void printlong(DISPLAY *dp) { struct stat *sp; FTSENT *p; NAMES *np; - char buf[20], szbuf[5]; + char buf[20]; now = time(NULL); @@ -140,12 +164,7 @@ printlong(DISPLAY *dp) (void)printf("%*"PRIu64" ", dp->s_inode, sp->st_ino); if (f_size) { if (f_humanize) { - if ((humanize_number(szbuf, sizeof(szbuf), - sp->st_blocks * S_BLKSIZE, - "", HN_AUTOSCALE, - (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1) - err(1, "humanize_number"); - (void)printf("%*s ", dp->s_block, szbuf); + printf("%*s ", 10, humanize(sp->st_size, false)); } else { (void)printf(f_commas ? "%'*llu " : "%*llu ", dp->s_block, @@ -160,19 +179,13 @@ printlong(DISPLAY *dp) if (!f_grouponly) (void)printf("%-*s ", dp->s_user, np->user); (void)printf("%-*s ", dp->s_group, np->group); - if (f_flags) - (void)printf("%-*s ", dp->s_flags, np->flags); if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode)) (void)printf("%*lld, %*lld ", dp->s_major, (long long)major(sp->st_rdev), dp->s_minor, (long long)minor(sp->st_rdev)); else if (f_humanize) { - if ((humanize_number(szbuf, sizeof(szbuf), - sp->st_size, "", HN_AUTOSCALE, - (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1) - err(1, "humanize_number"); - (void)printf("%*s ", dp->s_size, szbuf); + (void)printf("%*s ", 10, humanize(sp->st_size, false)); } else { (void)printf(f_commas ? "%'*llu " : "%*llu ", dp->s_size, (unsigned long long) @@ -360,7 +373,6 @@ printaname(FTSENT *p, int inodefield, int sizefield) { struct stat *sp; int chcnt; - char szbuf[5]; sp = p->fts_statp; chcnt = 0; @@ -368,11 +380,7 @@ printaname(FTSENT *p, int inodefield, int sizefield) chcnt += printf("%*"PRIu64" ", inodefield, sp->st_ino); if (f_size) { if (f_humanize) { - if ((humanize_number(szbuf, sizeof(szbuf), sp->st_size, - "", HN_AUTOSCALE, - (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1) - err(1, "humanize_number"); - chcnt += printf("%*s ", sizefield, szbuf); + chcnt += printf("%*s ", 10, humanize(sp->st_size, false)); } else { chcnt += printf(f_commas ? "%'*llu " : "%*llu ", sizefield, (unsigned long long) @@ -402,7 +410,8 @@ printtime(time_t ftime) } for (i = 4; i < 11; ++i) (void)putchar(longstring[i]); - +#define DAYSPERNYEAR 365 +#define SECSPERDAY 24*60*60 #define SIXMONTHS ((DAYSPERNYEAR / 2) * SECSPERDAY) if (f_sectime) for (i = 11; i < 24; i++) @@ -427,15 +436,9 @@ printtime(time_t ftime) static void printtotal(DISPLAY *dp) { - char szbuf[5]; - if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size)) { if (f_humanize) { - if ((humanize_number(szbuf, sizeof(szbuf), (int64_t)dp->stotal, - "", HN_AUTOSCALE, - (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1) - err(1, "humanize_number"); - (void)printf("total %s\n", szbuf); + printf("total %s\n", humanize(dp->stotal, false)); } else { (void)printf(f_commas ? "total %'llu\n" : "total %llu\n", (unsigned long long) @@ -460,9 +463,6 @@ printtype(u_int mode) case S_IFSOCK: (void)putchar('='); return (1); - case S_IFWHT: - (void)putchar('%'); - return (1); } if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) { (void)putchar('*'); diff --git a/bin/ls/util.c b/bin/ls/util.c index 61b0fda..e87c56b 100644 --- a/bin/ls/util.c +++ b/bin/ls/util.c @@ -32,25 +32,17 @@ * SUCH DAMAGE. */ -#include <sys/cdefs.h> -#ifndef lint -#if 0 -static char sccsid[] = "@(#)util.c 8.5 (Berkeley) 4/28/95"; -#else -__RCSID("$NetBSD: util.c,v 1.34 2011/08/29 14:44:21 joerg Exp $"); -#endif -#endif /* not lint */ - #include <sys/types.h> #include <sys/stat.h> +#include <bsd/vis.h> + #include <err.h> #include <fts.h> #include <limits.h> #include <stdio.h> #include <stdlib.h> #include <string.h> -#include <vis.h> #include <wchar.h> #include <wctype.h> @@ -69,7 +61,7 @@ safe_print(const char *src) flags |= VIS_CSTYLE; len = strlen(src); - if (len != 0 && SIZE_T_MAX/len <= 4) { + if (len != 0 && UINT_MAX/len <= 4) { errx(EXIT_FAILURE, "%s: name too long", src); /* NOTREACHED */ } |