snprintf used incorrectly, lint; from Craig
[unix-history] / usr / src / bin / ls / ls.c
CommitLineData
bcf1365c 1/*
9ffe97fe
KB
2 * Copyright (c) 1989 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Michael Fischbein.
7 *
ae14fb92 8 * %sccs.include.redist.c%
bcf1365c 9 */
9ffe97fe
KB
10
11#ifndef lint
3952e42c 12static char copyright[] =
9ffe97fe
KB
13"@(#) Copyright (c) 1989 The Regents of the University of California.\n\
14 All rights reserved.\n";
15#endif /* not lint */
16
17#ifndef lint
3952e42c 18static char sccsid[] = "@(#)ls.c 5.68 (Berkeley) %G%";
9ffe97fe
KB
19#endif /* not lint */
20
abe0683d 21#include <sys/types.h>
9ffe97fe 22#include <sys/stat.h>
4847d483 23#include <sys/ioctl.h>
f59807e9 24#include <dirent.h>
0ce0ae04 25#include <unistd.h>
f0a884e9 26#include <fts.h>
0ce0ae04 27#include <stdlib.h>
6ebcb998 28#include <string.h>
9ffe97fe
KB
29#include <errno.h>
30#include <stdio.h>
b3f77fd1 31#include "ls.h"
0ce0ae04 32#include "extern.h"
2fd18a0a 33
abe0683d
EA
34char *getbsize __P((char *, int *, long *));
35char *group_from_gid __P((u_int, int));
36char *user_from_uid __P((u_int, int));
f0a884e9 37
abe0683d
EA
38static void display __P((FTSENT *, FTSENT *));
39static char *flags_from_fid __P((u_long));
40static int mastercmp __P((const FTSENT **, const FTSENT **));
41static void traverse __P((int, char **, int));
42
43static void (*printfcn) __P((DISPLAY *));
f0a884e9 44static int (*sortfcn) __P((const FTSENT *, const FTSENT *));
bcf1365c 45
abe0683d 46long blocksize; /* block size units */
fb29be9b
KB
47int termwidth = 80; /* default terminal width */
48
9ffe97fe 49/* flags */
9ffe97fe 50int f_accesstime; /* use time of last access */
4845c19f 51int f_column; /* columnated format */
32980675 52int f_flags; /* show flags associated with a file */
9ffe97fe 53int f_inode; /* print inode */
38eeee6d
KB
54int f_listdir; /* list actual directory, not contents */
55int f_listdot; /* list files beginning with . */
9ffe97fe 56int f_longform; /* long listing format */
4dfe4db2 57int f_newline; /* if precede with newline */
9ffe97fe 58int f_nonprint; /* show unprintables as ? */
9d5fbfa8 59int f_nosort; /* don't sort output */
9ffe97fe 60int f_recursive; /* ls subdirectories also */
38eeee6d 61int f_reversesort; /* reverse whatever sort is used */
926d5d7d 62int f_sectime; /* print the real time for all files */
38eeee6d 63int f_singlecol; /* use single column output */
9ffe97fe 64int f_size; /* list size in short listing */
38eeee6d 65int f_statustime; /* use time of last mode change */
4dfe4db2 66int f_dirname; /* if precede with directory name */
9ffe97fe 67int f_timesort; /* sort by time vice name */
38eeee6d 68int f_type; /* add type character for non-regular files */
2fd18a0a 69
4db4a32d 70int
9ffe97fe
KB
71main(argc, argv)
72 int argc;
f0a884e9 73 char *argv[];
547b0031 74{
f59807e9 75 static char dot[] = ".", *dotav[] = { dot, NULL };
4847d483 76 struct winsize win;
4933888a 77 int ch, fts_options, notused;
0ce0ae04 78 char *p;
2fd18a0a 79
f0a884e9 80 /* Terminal defaults to -Cq, non-terminal defaults to -1. */
f59807e9
KB
81 if (isatty(STDOUT_FILENO)) {
82 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) == -1 ||
83 !win.ws_col) {
fb29be9b
KB
84 if (p = getenv("COLUMNS"))
85 termwidth = atoi(p);
86 }
4847d483
KB
87 else
88 termwidth = win.ws_col;
f59807e9 89 f_column = f_nonprint = 1;
9ffe97fe
KB
90 } else
91 f_singlecol = 1;
2fd18a0a 92
f0a884e9 93 /* Root is -A automatically. */
9ffe97fe 94 if (!getuid())
b3f77fd1 95 f_listdot = 1;
9ffe97fe 96
4db4a32d 97 fts_options = FTS_PHYSICAL;
32980675 98 while ((ch = getopt(argc, argv, "1ACFLRTacdfgikloqrstu")) != EOF) {
9ffe97fe 99 switch (ch) {
38eeee6d 100 /*
f0a884e9
EA
101 * The -1, -C and -l options all override each other so shell
102 * aliasing works right.
38eeee6d 103 */
9ffe97fe
KB
104 case '1':
105 f_singlecol = 1;
4845c19f 106 f_column = f_longform = 0;
2fd18a0a 107 break;
9ffe97fe 108 case 'C':
4845c19f 109 f_column = 1;
38eeee6d
KB
110 f_longform = f_singlecol = 0;
111 break;
112 case 'l':
113 f_longform = 1;
4845c19f 114 f_column = f_singlecol = 0;
9ffe97fe 115 break;
f59807e9 116 /* The -c and -u options override each other. */
38eeee6d
KB
117 case 'c':
118 f_statustime = 1;
119 f_accesstime = 0;
120 break;
121 case 'u':
122 f_accesstime = 1;
123 f_statustime = 0;
124 break;
9ffe97fe
KB
125 case 'F':
126 f_type = 1;
127 break;
128 case 'L':
4db4a32d
KB
129 fts_options &= ~FTS_PHYSICAL;
130 fts_options |= FTS_LOGICAL;
9ffe97fe
KB
131 break;
132 case 'R':
133 f_recursive = 1;
9ffe97fe
KB
134 break;
135 case 'a':
f0a884e9 136 fts_options |= FTS_SEEDOT;
9ffe97fe 137 /* FALLTHROUGH */
2fd18a0a 138 case 'A':
b3f77fd1 139 f_listdot = 1;
2fd18a0a 140 break;
f59807e9 141 /* The -d option turns off the -R option. */
1c52c7b1
JB
142 case 'S':
143 Sflg++; /* fall into... */
547b0031 144 case 'd':
9ffe97fe 145 f_listdir = 1;
f59807e9 146 f_recursive = 0;
2fd18a0a 147 break;
9d5fbfa8
KB
148 case 'f':
149 f_nosort = 1;
150 break;
abe0683d 151 case 'g': /* Compatibility with 4.3BSD. */
2fd18a0a 152 break;
ec0c22c1 153 case 'i':
9ffe97fe 154 f_inode = 1;
2fd18a0a 155 break;
4933888a
KB
156 case 'k': /* Delete before 4.4BSD. */
157 (void)fprintf(stderr, "ls: -k no longer supported\n");
a657fc85 158 break;
32980675
KM
159 case 'o':
160 f_flags = 1;
161 break;
ec0c22c1 162 case 'q':
9ffe97fe 163 f_nonprint = 1;
2fd18a0a 164 break;
547b0031 165 case 'r':
9ffe97fe 166 f_reversesort = 1;
2fd18a0a 167 break;
ec0c22c1 168 case 's':
9ffe97fe 169 f_size = 1;
2fd18a0a 170 break;
926d5d7d
KB
171 case 'T':
172 f_sectime = 1;
173 break;
547b0031 174 case 't':
9ffe97fe 175 f_timesort = 1;
2fd18a0a 176 break;
ec0c22c1 177 default:
2fd18a0a 178 case '?':
9ffe97fe 179 usage();
2955376b 180 }
9ffe97fe 181 }
b3f77fd1
KB
182 argc -= optind;
183 argv += optind;
9ffe97fe 184
65c9f129
KB
185 /*
186 * If not -F, -i, -l, -s or -t options, don't require stat
a86fb882
EA
187 * information.
188 */
65c9f129 189 if (!f_inode && !f_longform && !f_size && !f_timesort && !f_type)
f0a884e9 190 fts_options |= FTS_NOSTAT;
b3f77fd1 191
91d39f6b
KB
192 /*
193 * If not -F, -d or -l options, follow any symbolic links listed on
194 * the command line.
195 */
196 if (!f_longform && !f_listdir && !f_type)
197 fts_options |= FTS_COMFOLLOW;
198
4933888a
KB
199 /* If -l or -s, figure out block size. */
200 if (f_longform || f_size) {
201 (void)getbsize("ls", &notused, &blocksize);
202 blocksize /= 512;
203 }
204
f0a884e9 205 /* Select a sort function. */
9ffe97fe
KB
206 if (f_reversesort) {
207 if (!f_timesort)
208 sortfcn = revnamecmp;
209 else if (f_accesstime)
210 sortfcn = revacccmp;
9ffe97fe
KB
211 else if (f_statustime)
212 sortfcn = revstatcmp;
f0a884e9 213 else /* Use modification time. */
38eeee6d 214 sortfcn = revmodcmp;
2fd18a0a 215 } else {
9ffe97fe
KB
216 if (!f_timesort)
217 sortfcn = namecmp;
218 else if (f_accesstime)
219 sortfcn = acccmp;
9ffe97fe
KB
220 else if (f_statustime)
221 sortfcn = statcmp;
f0a884e9 222 else /* Use modification time. */
38eeee6d 223 sortfcn = modcmp;
9ffe97fe
KB
224 }
225
f0a884e9 226 /* Select a print function. */
4847d483
KB
227 if (f_singlecol)
228 printfcn = printscol;
229 else if (f_longform)
230 printfcn = printlong;
b3f77fd1 231 else
4e9923b0 232 printfcn = printcol;
9ffe97fe 233
0a3501e4 234 if (argc)
f0a884e9 235 traverse(argc, argv, fts_options);
f59807e9 236 else
f0a884e9 237 traverse(1, dotav, fts_options);
4e9923b0 238 exit(0);
2fd18a0a
KB
239}
240
f59807e9
KB
241static int output; /* If anything output. */
242
f0a884e9
EA
243/*
244 * Traverse() walks the logical directory structure specified by the argv list
245 * in the order specified by the mastercmp() comparison function. During the
246 * traversal it passes linked lists of structures to display() which represent
247 * a superset (may be exact set) of the files to be displayed.
248 */
abe0683d 249static void
f0a884e9
EA
250traverse(argc, argv, options)
251 int argc, options;
252 char *argv[];
253{
254 register FTS *ftsp;
255 register FTSENT *p;
f59807e9 256 int ch_options;
abe0683d 257
f0a884e9
EA
258 if ((ftsp =
259 fts_open(argv, options, f_nosort ? NULL : mastercmp)) == NULL)
38f75f3b 260 err(1, "%s", strerror(errno));
91d39f6b 261
f59807e9 262 display(NULL, fts_children(ftsp, 0));
f0a884e9
EA
263 if (f_listdir)
264 return;
f59807e9 265
db658853
KB
266 /*
267 * If not recursing down this tree and don't need stat info, just get
268 * the names.
269 */
f59807e9 270 ch_options = !f_recursive && options & FTS_NOSTAT ? FTS_NAMEONLY : 0;
db658853 271
f0a884e9
EA
272 while (p = fts_read(ftsp))
273 switch(p->fts_info) {
274 case FTS_DC:
275 err(0, "%s: directory causes a cycle", p->fts_name);
276 break;
277 case FTS_DNR:
278 case FTS_ERR:
279 err(0, "%s: %s",
280 p->fts_name, strerror(p->fts_errno));
281 break;
282 case FTS_D:
4db4a32d
KB
283 if (p->fts_level != FTS_ROOTLEVEL &&
284 p->fts_name[0] == '.' && !f_listdot)
285 break;
f59807e9
KB
286
287 /*
288 * If already output something, put out a newline as
289 * a separator. If multiple arguments, precede each
290 * directory with its name.
291 */
292 if (output)
293 (void)printf("\n%s:\n", p->fts_path);
294 else if (argc > 1) {
295 (void)printf("%s:\n", p->fts_path);
296 output = 1;
297 }
298
f59807e9
KB
299 display(p, fts_children(ftsp, ch_options));
300
4db4a32d
KB
301 if (!f_recursive)
302 (void)fts_set(ftsp, p, FTS_SKIP);
f0a884e9
EA
303 break;
304 }
305 (void)fts_close(ftsp);
306}
be4f2604 307
f0a884e9 308/*
09cf81e2
KB
309 * Display() takes a linked list of FTSENT structures and passes the list
310 * along with any other necessary information to the print function. P
311 * points to the parent directory of the display list.
f0a884e9 312 */
abe0683d 313static void
f59807e9 314display(p, list)
f0a884e9
EA
315 register FTSENT *p;
316 FTSENT *list;
2fd18a0a 317{
f0a884e9 318 register FTSENT *cur;
abe0683d
EA
319 struct stat *sp;
320 DISPLAY d;
321 NAMES *np;
0e3001c9
KB
322 u_long btotal, maxblock, maxinode, maxlen, maxnlink;
323 u_quad_t maxsize;
09cf81e2 324 int bcfile, flen, glen, ulen, maxflags, maxgroup, maxuser;
abe0683d
EA
325 int entries, needstats;
326 char *user, *group, *flags, buf[20]; /* 32 bits == 10 digits */
327
f0a884e9
EA
328 /*
329 * If list is NULL there are two possibilities: that the parent
330 * directory p has no children, or that fts_children() returned an
91d39f6b 331 * error. We ignore the error case since it will be replicated
f0a884e9
EA
332 * on the next call to fts_read() on the post-order visit to the
333 * directory p, and will be signalled in traverse().
334 */
f59807e9 335 if (list == NULL)
f0a884e9 336 return;
b3f77fd1 337
abe0683d
EA
338 needstats = f_inode || f_longform || f_size;
339 flen = 0;
0e3001c9 340 btotal = maxblock = maxinode = maxlen = maxnlink = 0;
09cf81e2 341 bcfile = 0;
abe0683d 342 maxuser = maxgroup = maxflags = 0;
0e3001c9 343 maxsize = 0;
f59807e9
KB
344 for (cur = list, entries = 0; cur; cur = cur->fts_link) {
345 if (cur->fts_info == FTS_ERR || cur->fts_info == FTS_NS) {
346 err(0, "%s: %s",
347 cur->fts_name, strerror(cur->fts_errno));
348 cur->fts_number = NO_PRINT;
349 continue;
9ffe97fe 350 }
f59807e9
KB
351
352 /*
abe0683d
EA
353 * P is NULL if list is the argv list, to which different rules
354 * apply.
f59807e9
KB
355 */
356 if (p == NULL) {
357 /* Directories will be displayed later. */
358 if (cur->fts_info == FTS_D && !f_listdir) {
f0a884e9 359 cur->fts_number = NO_PRINT;
b3f77fd1 360 continue;
f0a884e9 361 }
f59807e9
KB
362 } else {
363 /* Only display dot file if -a/-A set. */
f0a884e9
EA
364 if (cur->fts_name[0] == '.' && !f_listdot) {
365 cur->fts_number = NO_PRINT;
b3f77fd1 366 continue;
f0a884e9 367 }
b3f77fd1 368 }
f59807e9
KB
369 if (f_nonprint)
370 prcopy(cur->fts_name, cur->fts_name, cur->fts_namelen);
abe0683d 371 if (cur->fts_namelen > maxlen)
f59807e9 372 maxlen = cur->fts_namelen;
abe0683d
EA
373 if (needstats) {
374 sp = cur->fts_statp;
375 if (sp->st_blocks > maxblock)
376 maxblock = sp->st_blocks;
377 if (sp->st_ino > maxinode)
378 maxinode = sp->st_ino;
379 if (sp->st_nlink > maxnlink)
380 maxnlink = sp->st_nlink;
381 if (sp->st_size > maxsize)
382 maxsize = sp->st_size;
383
384 btotal += sp->st_blocks;
385 if (f_longform) {
386 user = user_from_uid(sp->st_uid, 0);
387 if ((ulen = strlen(user)) > maxuser)
388 maxuser = ulen;
389 group = group_from_gid(sp->st_gid, 0);
390 if ((glen = strlen(group)) > maxgroup)
391 maxgroup = glen;
392 if (f_flags) {
393 flags = flags_from_fid(sp->st_flags);
394 if ((flen = strlen(flags)) > maxflags)
395 maxflags = flen;
396 } else
397 flen = 0;
398
399 if ((np = malloc(sizeof(NAMES) +
400 ulen + glen + flen + 3)) == NULL)
401 err(1, "%s", strerror(errno));
402
403 np->user = &np->data[0];
404 (void)strcpy(np->user, user);
405 np->group = &np->data[ulen + 1];
406 (void)strcpy(np->group, group);
407
09cf81e2
KB
408 if (S_ISCHR(sp->st_mode) ||
409 S_ISBLK(sp->st_mode))
410 bcfile = 1;
411
abe0683d
EA
412 if (f_flags) {
413 np->flags = &np->data[ulen + glen + 2];
414 (void)strcpy(np->flags, flags);
415 }
416 cur->fts_pointer = np;
417 }
418 }
f59807e9 419 ++entries;
be4f2604 420 }
547b0031 421
abe0683d
EA
422 if (!entries)
423 return;
424
425 d.list = list;
426 d.entries = entries;
427 d.maxlen = maxlen;
428 if (needstats) {
09cf81e2 429 d.bcfile = bcfile;
abe0683d 430 d.btotal = btotal;
0e3001c9 431 d.s_block = snprintf(buf, sizeof(buf), "%lu", maxblock);
abe0683d
EA
432 d.s_flags = maxflags;
433 d.s_group = maxgroup;
0e3001c9
KB
434 d.s_inode = snprintf(buf, sizeof(buf), "%lu", maxinode);
435 d.s_nlink = snprintf(buf, sizeof(buf), "%lu", maxnlink);
436 d.s_size = snprintf(buf, sizeof(buf), "%qu", maxsize);
abe0683d 437 d.s_user = maxuser;
f59807e9 438 }
abe0683d
EA
439
440 printfcn(&d);
441 output = 1;
442
443 if (f_longform)
444 for (cur = list; cur; cur = cur->fts_link)
445 free(cur->fts_pointer);
547b0031
BJ
446}
447
f0a884e9
EA
448/*
449 * Ordering for mastercmp:
450 * If ordering the argv (fts_level = FTS_ROOTLEVEL) return non-directories
451 * as larger than directories. Within either group, use the sort function.
452 * All other levels use the sort function. Error entries remain unsorted.
453 */
abe0683d 454static int
f0a884e9
EA
455mastercmp(a, b)
456 const FTSENT **a, **b;
547b0031 457{
f0a884e9 458 register int a_info, b_info;
547b0031 459
f0a884e9
EA
460 a_info = (*a)->fts_info;
461 if (a_info == FTS_ERR)
0ce0ae04 462 return (0);
f0a884e9
EA
463 b_info = (*b)->fts_info;
464 if (b_info == FTS_ERR)
465 return (0);
4db4a32d
KB
466
467 if (a_info == FTS_NS || b_info == FTS_NS)
f0a884e9 468 return (namecmp(*a, *b));
4845c19f 469
abe0683d 470 if (a_info == b_info)
f0a884e9 471 return (sortfcn(*a, *b));
c7f12bd6 472
f0a884e9 473 if ((*a)->fts_level == FTS_ROOTLEVEL)
abe0683d 474 if (a_info == FTS_D)
f0a884e9
EA
475 return (1);
476 else if (b_info == FTS_D)
477 return (-1);
478 else
479 return (sortfcn(*a, *b));
480 else
481 return (sortfcn(*a, *b));
547b0031 482}
abe0683d
EA
483
484static char *
485flags_from_fid(flags)
486 u_long flags;
487{
488 static char buf[20];
489 register int comma;
490 register char *p;
491
492 p = buf;
493 if (flags & ARCHIVED) {
494 (void)strcpy(p, "arch");
495 p += sizeof("arch") - 1;
496 comma = 1;
497 } else
498 comma = 0;
499 if (flags & NODUMP) {
9c63f507 500 if (comma++)
abe0683d
EA
501 *p++ = ',';
502 (void)strcpy(p, "nodump");
503 p += sizeof("nodump") - 1;
504 }
505 if (flags & IMMUTABLE) {
9c63f507 506 if (comma++)
abe0683d
EA
507 *p++ = ',';
508 (void)strcpy(p, "nochg");
509 p += sizeof("nochg") - 1;
510 }
9c63f507
KB
511 if (!comma)
512 (void)strcpy(p, "-");
abe0683d
EA
513 return (buf);
514}