WARNS=4 fixes, plus a healthy dose of fixes inspired by lint.
This commit is contained in:
parent
0a7fe9cc0b
commit
9052855a15
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=90150
@ -3,7 +3,6 @@
|
||||
|
||||
PROG= ls
|
||||
SRCS= cmp.c lomac.c ls.c print.c util.c
|
||||
WARNS?= 0
|
||||
DPADD= ${LIBM}
|
||||
LDADD= -lm
|
||||
|
||||
|
@ -47,7 +47,7 @@ void printcol(DISPLAY *);
|
||||
void printlong(DISPLAY *);
|
||||
void printscol(DISPLAY *);
|
||||
void usage(void);
|
||||
int len_octal(const char *, int);
|
||||
size_t len_octal(const char *, int);
|
||||
int prn_octal(const char *);
|
||||
int prn_printable(const char *);
|
||||
#ifdef COLORLS
|
||||
|
@ -55,8 +55,8 @@
|
||||
|
||||
#define LOMAC_DEVICE "/dev/lomac"
|
||||
|
||||
int devlomac; /* file descriptor for LOMAC_DEVICE */
|
||||
struct lomac_fioctl2 ioctl_args;
|
||||
static int devlomac; /* file descriptor for LOMAC_DEVICE */
|
||||
static struct lomac_fioctl2 ioctl_args;
|
||||
|
||||
/* lomac_start()
|
||||
*
|
||||
|
97
bin/ls/ls.c
97
bin/ls/ls.c
@ -91,33 +91,32 @@ long blocksize; /* block size units */
|
||||
int termwidth = 80; /* default terminal width */
|
||||
|
||||
/* flags */
|
||||
int f_accesstime; /* use time of last access */
|
||||
int f_column; /* columnated format */
|
||||
int f_flags; /* show flags associated with a file */
|
||||
int f_humanval; /* show human-readable file sizes */
|
||||
int f_inode; /* print inode */
|
||||
int f_kblocks; /* print size in kilobytes */
|
||||
int f_listdir; /* list actual directory, not contents */
|
||||
int f_listdot; /* list files beginning with . */
|
||||
int f_longform; /* long listing format */
|
||||
int f_nonprint; /* show unprintables as ? */
|
||||
int f_nosort; /* don't sort output */
|
||||
int f_notabs; /* don't use tab-separated multi-col output */
|
||||
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 */
|
||||
int f_recursive; /* ls subdirectories also */
|
||||
int f_reversesort; /* reverse whatever sort is used */
|
||||
int f_sectime; /* print the real time for all files */
|
||||
int f_singlecol; /* use single column output */
|
||||
int f_size; /* list size in short listing */
|
||||
int f_statustime; /* use time of last mode change */
|
||||
int f_timesort; /* sort by time vice name */
|
||||
int f_type; /* add type character for non-regular files */
|
||||
int f_whiteout; /* show whiteout entries */
|
||||
int f_lomac; /* show LOMAC attributes */
|
||||
int f_accesstime; /* use time of last access */
|
||||
int f_flags; /* show flags associated with a file */
|
||||
int f_humanval; /* show human-readable file sizes */
|
||||
int f_inode; /* print inode */
|
||||
static int f_kblocks; /* print size in kilobytes */
|
||||
static int f_listdir; /* list actual directory, not contents */
|
||||
static int f_listdot; /* list files beginning with . */
|
||||
int f_longform; /* long listing format */
|
||||
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_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 */
|
||||
static int f_reversesort; /* reverse whatever sort is used */
|
||||
int f_sectime; /* print the real time for all files */
|
||||
static int f_singlecol; /* use single column output */
|
||||
int f_size; /* list size in short listing */
|
||||
int f_statustime; /* use time of last mode change */
|
||||
static int f_timesort; /* sort by time vice name */
|
||||
int f_type; /* add type character for non-regular files */
|
||||
static int f_whiteout; /* show whiteout entries */
|
||||
int f_lomac; /* show LOMAC attributes */
|
||||
#ifdef COLORLS
|
||||
int f_color; /* add type in color for non-regular files */
|
||||
int f_color; /* add type in color for non-regular files */
|
||||
|
||||
char *ansi_bgcol; /* ANSI sequence to set background colour */
|
||||
char *ansi_fgcol; /* ANSI sequence to set foreground colour */
|
||||
@ -126,7 +125,7 @@ char *attrs_off; /* ANSI sequence to turn off attributes */
|
||||
char *enter_bold; /* ANSI sequence to set color to bold mode */
|
||||
#endif
|
||||
|
||||
int rval;
|
||||
static int rval;
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
@ -151,11 +150,12 @@ main(int argc, char *argv[])
|
||||
termwidth = atoi(p);
|
||||
} else
|
||||
termwidth = win.ws_col;
|
||||
f_column = f_nonprint = 1;
|
||||
f_nonprint = 1;
|
||||
} else {
|
||||
f_singlecol = 1;
|
||||
/* retrieve environment variable, in case of explicit -C */
|
||||
if ((p = getenv("COLUMNS")))
|
||||
p = getenv("COLUMNS");
|
||||
if (p)
|
||||
termwidth = atoi(p);
|
||||
}
|
||||
|
||||
@ -172,7 +172,7 @@ main(int argc, char *argv[])
|
||||
*/
|
||||
case '1':
|
||||
f_singlecol = 1;
|
||||
f_column = f_longform = 0;
|
||||
f_longform = 0;
|
||||
break;
|
||||
case 'B':
|
||||
f_nonprint = 0;
|
||||
@ -180,12 +180,11 @@ main(int argc, char *argv[])
|
||||
f_octal_escape = 0;
|
||||
break;
|
||||
case 'C':
|
||||
f_column = 1;
|
||||
f_longform = f_singlecol = 0;
|
||||
break;
|
||||
case 'l':
|
||||
f_longform = 1;
|
||||
f_column = f_singlecol = 0;
|
||||
f_singlecol = 0;
|
||||
break;
|
||||
/* The -c and -u options override each other. */
|
||||
case 'c':
|
||||
@ -459,6 +458,7 @@ traverse(int argc, char *argv[], int options)
|
||||
if (!f_recursive && chp != NULL)
|
||||
(void)fts_set(ftsp, p, FTS_SKIP);
|
||||
break;
|
||||
default:
|
||||
}
|
||||
if (errno)
|
||||
err(1, "fts_read");
|
||||
@ -476,12 +476,15 @@ display(FTSENT *p, FTSENT *list)
|
||||
DISPLAY d;
|
||||
FTSENT *cur;
|
||||
NAMES *np;
|
||||
u_quad_t maxsize;
|
||||
u_long btotal, maxblock, maxinode, maxlen, maxnlink, maxlattr;
|
||||
int bcfile, flen, glen, ulen, lattrlen, maxflags, maxgroup, maxuser;
|
||||
off_t maxsize;
|
||||
u_long btotal, lattrlen, maxblock, maxinode, maxlen, maxnlink, maxlattr;
|
||||
int bcfile, maxflags;
|
||||
gid_t maxgroup;
|
||||
uid_t maxuser;
|
||||
size_t flen, ulen, glen;
|
||||
char *initmax;
|
||||
int entries, needstats;
|
||||
char *user, *group, *flags, *lattr;
|
||||
char *user, *group, *flags, *lattr = NULL;
|
||||
char buf[STRBUF_SIZEOF(u_quad_t) + 1];
|
||||
char ngroup[STRBUF_SIZEOF(uid_t) + 1];
|
||||
char nuser[STRBUF_SIZEOF(gid_t) + 1];
|
||||
@ -501,6 +504,8 @@ display(FTSENT *p, FTSENT *list)
|
||||
btotal = 0;
|
||||
initmax = getenv("LS_COLWIDTHS");
|
||||
/* Fields match -lios order. New ones should be added at the end. */
|
||||
maxlattr = maxblock = maxinode = maxlen = maxnlink =
|
||||
maxuser = maxgroup = maxflags = maxsize = 0;
|
||||
if (initmax != NULL && *initmax != '\0') {
|
||||
char *initmax2, *jinitmax;
|
||||
int ninitmax;
|
||||
@ -527,41 +532,50 @@ display(FTSENT *p, FTSENT *list)
|
||||
strcpy(initmax2, "0");
|
||||
|
||||
ninitmax = sscanf(jinitmax,
|
||||
" %lu : %lu : %lu : %i : %i : %i : %qu : %lu : %lu ",
|
||||
" %lu : %lu : %lu : %i : %i : %i : %llu : %lu : %lu ",
|
||||
&maxinode, &maxblock, &maxnlink, &maxuser,
|
||||
&maxgroup, &maxflags, &maxsize, &maxlen, &maxlattr);
|
||||
f_notabs = 1;
|
||||
switch (ninitmax) {
|
||||
case 0:
|
||||
maxinode = 0;
|
||||
/* fall through */
|
||||
case 1:
|
||||
maxblock = 0;
|
||||
/* fall through */
|
||||
case 2:
|
||||
maxnlink = 0;
|
||||
/* fall through */
|
||||
case 3:
|
||||
maxuser = 0;
|
||||
/* fall through */
|
||||
case 4:
|
||||
maxgroup = 0;
|
||||
/* fall through */
|
||||
case 5:
|
||||
maxflags = 0;
|
||||
/* fall through */
|
||||
case 6:
|
||||
maxsize = 0;
|
||||
/* fall through */
|
||||
case 7:
|
||||
maxlen = 0;
|
||||
/* fall through */
|
||||
case 8:
|
||||
maxlattr = 0;
|
||||
/* fall through */
|
||||
#ifdef COLORLS
|
||||
if (!f_color)
|
||||
#endif
|
||||
f_notabs = 0;
|
||||
/* fall through */
|
||||
default:
|
||||
}
|
||||
maxinode = makenines(maxinode);
|
||||
maxblock = makenines(maxblock);
|
||||
maxnlink = makenines(maxnlink);
|
||||
maxsize = makenines(maxsize);
|
||||
} else if (initmax == NULL || *initmax == '\0')
|
||||
maxlattr = maxblock = maxinode = maxlen = maxnlink =
|
||||
maxuser = maxgroup = maxflags = maxsize = 0;
|
||||
}
|
||||
if (f_lomac)
|
||||
lomac_start();
|
||||
bcfile = 0;
|
||||
@ -635,7 +649,8 @@ display(FTSENT *p, FTSENT *list)
|
||||
}
|
||||
if (flags == NULL)
|
||||
err(1, NULL);
|
||||
if ((flen = strlen(flags)) > maxflags)
|
||||
flen = strlen(flags);
|
||||
if (flen > (size_t)maxflags)
|
||||
maxflags = flen;
|
||||
} else
|
||||
flen = 0;
|
||||
|
16
bin/ls/ls.h
16
bin/ls/ls.h
@ -66,14 +66,14 @@ typedef struct {
|
||||
int bcfile;
|
||||
int entries;
|
||||
int maxlen;
|
||||
int s_block;
|
||||
int s_flags;
|
||||
int s_lattr;
|
||||
int s_group;
|
||||
int s_inode;
|
||||
int s_nlink;
|
||||
int s_size;
|
||||
int s_user;
|
||||
u_int s_block;
|
||||
u_int s_flags;
|
||||
u_int s_lattr;
|
||||
u_int s_group;
|
||||
u_int s_inode;
|
||||
u_int s_nlink;
|
||||
u_int s_size;
|
||||
u_int s_user;
|
||||
} DISPLAY;
|
||||
|
||||
typedef struct {
|
||||
|
@ -56,7 +56,6 @@ static const char rcsid[] =
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
#ifdef COLORLS
|
||||
#include <ctype.h>
|
||||
@ -78,7 +77,6 @@ static int colortype(mode_t);
|
||||
#endif
|
||||
|
||||
#define IS_NOPRINT(p) ((p)->fts_number == NO_PRINT)
|
||||
#define UNITS_2 2
|
||||
|
||||
#define KILO_SZ(n) (n)
|
||||
#define MEGA_SZ(n) ((n) * (n))
|
||||
@ -92,14 +90,14 @@ static int colortype(mode_t);
|
||||
#define TERA_2_SZ (TERA_SZ(1024ULL))
|
||||
#define PETA_2_SZ (PETA_SZ(1024ULL))
|
||||
|
||||
unsigned long long vals_base2[] = {1, KILO_2_SZ, MEGA_2_SZ, GIGA_2_SZ, TERA_2_SZ, PETA_2_SZ};
|
||||
static unsigned long long vals_base2[] = {1, KILO_2_SZ, MEGA_2_SZ, GIGA_2_SZ, TERA_2_SZ, PETA_2_SZ};
|
||||
|
||||
typedef enum {
|
||||
NONE, KILO, MEGA, GIGA, TERA, PETA, UNIT_MAX
|
||||
} unit_t;
|
||||
static unit_t unit_adjust(off_t *);
|
||||
|
||||
int unitp[] = {NONE, KILO, MEGA, GIGA, TERA, PETA};
|
||||
static int unitp[] = {NONE, KILO, MEGA, GIGA, TERA, PETA};
|
||||
|
||||
#ifdef COLORLS
|
||||
/* Most of these are taken from <sys/stat.h> */
|
||||
@ -120,7 +118,7 @@ typedef enum Colors {
|
||||
C_NUMCOLORS /* just a place-holder */
|
||||
} Colors;
|
||||
|
||||
const char *defcolors = "exfxcxdxbxegedabagacad";
|
||||
static const char *defcolors = "exfxcxdxbxegedabagacad";
|
||||
|
||||
/* colors for file types */
|
||||
static struct {
|
||||
@ -177,9 +175,9 @@ printlong(DISPLAY *dp)
|
||||
if (f_inode)
|
||||
(void)printf("%*lu ", dp->s_inode, (u_long)sp->st_ino);
|
||||
if (f_size)
|
||||
(void)printf("%*qd ",
|
||||
(void)printf("%*lld ",
|
||||
dp->s_block, howmany(sp->st_blocks, blocksize));
|
||||
(void)strmode(sp->st_mode, buf);
|
||||
strmode(sp->st_mode, buf);
|
||||
np = p->fts_pointer;
|
||||
(void)printf("%s %*u %-*s %-*s ", buf, dp->s_nlink,
|
||||
sp->st_nlink, dp->s_user, np->user, dp->s_group,
|
||||
@ -197,7 +195,7 @@ printlong(DISPLAY *dp)
|
||||
(void)printf("%3d, %3d ",
|
||||
major(sp->st_rdev), minor(sp->st_rdev));
|
||||
else if (dp->bcfile)
|
||||
(void)printf("%*s%*qd ",
|
||||
(void)printf("%*s%*lld ",
|
||||
8 - dp->s_size, "", dp->s_size, sp->st_size);
|
||||
else
|
||||
printsize(dp->s_size, sp->st_size);
|
||||
@ -320,7 +318,7 @@ printaname(FTSENT *p, u_long inodefield, u_long sizefield)
|
||||
if (f_inode)
|
||||
chcnt += printf("%*lu ", (int)inodefield, (u_long)sp->st_ino);
|
||||
if (f_size)
|
||||
chcnt += printf("%*qd ",
|
||||
chcnt += printf("%*lld ",
|
||||
(int)sizefield, howmany(sp->st_blocks, blocksize));
|
||||
#ifdef COLORLS
|
||||
if (f_color)
|
||||
@ -382,6 +380,7 @@ printtype(u_int mode)
|
||||
case S_IFWHT:
|
||||
(void)putchar('%');
|
||||
return (1);
|
||||
default:
|
||||
}
|
||||
if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
|
||||
(void)putchar('*');
|
||||
@ -554,7 +553,7 @@ printlink(FTSENT *p)
|
||||
}
|
||||
path[lnklen] = '\0';
|
||||
(void)printf(" -> ");
|
||||
printname(path);
|
||||
(void)printname(path);
|
||||
}
|
||||
|
||||
static void
|
||||
@ -568,10 +567,10 @@ printsize(size_t width, off_t bytes)
|
||||
if (bytes == 0)
|
||||
(void)printf("%*s ", width, "0B");
|
||||
else
|
||||
(void)printf("%*qd%c ", width - 1, bytes,
|
||||
(void)printf("%*lld%c ", width - 1, bytes,
|
||||
"BKMGTPE"[unit]);
|
||||
} else
|
||||
(void)printf("%*qd ", width, bytes);
|
||||
(void)printf("%*lld ", width, bytes);
|
||||
}
|
||||
|
||||
/*
|
||||
@ -587,7 +586,7 @@ unit_adjust(off_t *val)
|
||||
unit_t unit;
|
||||
unsigned int unit_sz;
|
||||
|
||||
abval = fabs(*val);
|
||||
abval = fabs((double)*val);
|
||||
|
||||
unit_sz = abval ? ilogb(abval) / 10 : 0;
|
||||
|
||||
|
@ -59,7 +59,7 @@ static const char rcsid[] =
|
||||
int
|
||||
prn_printable(const char *s)
|
||||
{
|
||||
unsigned char c;
|
||||
char c;
|
||||
int n;
|
||||
|
||||
for (n = 0; (c = *s) != '\0'; ++s, ++n)
|
||||
@ -83,13 +83,13 @@ prn_printable(const char *s)
|
||||
* DES 1998/04/23
|
||||
*/
|
||||
|
||||
int
|
||||
size_t
|
||||
len_octal(const char *s, int len)
|
||||
{
|
||||
int r = 0;
|
||||
size_t r = 0;
|
||||
|
||||
while (len--)
|
||||
if (isprint((unsigned char)*s++)) r++; else r += 4;
|
||||
if (isprint((unsigned const char)*s++)) r++; else r += 4;
|
||||
return r;
|
||||
}
|
||||
|
||||
@ -99,8 +99,7 @@ prn_octal(const char *s)
|
||||
unsigned char ch;
|
||||
int len = 0;
|
||||
|
||||
while ((ch = *s++))
|
||||
{
|
||||
while ((ch = (unsigned char)*s++)) {
|
||||
if (isprint(ch) && (ch != '\"') && (ch != '\\'))
|
||||
putchar(ch), len++;
|
||||
else if (f_octal_escape) {
|
||||
|
Loading…
Reference in New Issue
Block a user