observe -t option when no arguments
[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 *
8 * Redistribution and use in source and binary forms are permitted
9 * provided that the above copyright notice and this paragraph are
10 * duplicated in all such forms and that any documentation,
11 * advertising materials, and other materials related to such
12 * distribution and use acknowledge that the software was developed
13 * by the University of California, Berkeley. The name of the
14 * University may not be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
bcf1365c 19 */
9ffe97fe
KB
20
21#ifndef lint
22char copyright[] =
23"@(#) Copyright (c) 1989 The Regents of the University of California.\n\
24 All rights reserved.\n";
25#endif /* not lint */
26
27#ifndef lint
c7f12bd6 28static char sccsid[] = "@(#)ls.c 5.36 (Berkeley) %G%";
9ffe97fe
KB
29#endif /* not lint */
30
2fd18a0a 31#include <sys/param.h>
9ffe97fe 32#include <sys/stat.h>
4847d483 33#include <sys/ioctl.h>
2fd18a0a 34#include <dirent.h>
9ffe97fe 35#include <strings.h>
9ffe97fe
KB
36#include <errno.h>
37#include <stdio.h>
b3f77fd1 38#include "ls.h"
2fd18a0a 39
4e9923b0 40int (*sortfcn)(), (*printfcn)();
219ae3c3 41int lstat(), strlen();
b3f77fd1 42char *emalloc();
bcf1365c 43
fb29be9b
KB
44int termwidth = 80; /* default terminal width */
45
9ffe97fe 46/* flags */
9ffe97fe 47int f_accesstime; /* use time of last access */
4845c19f 48int f_column; /* columnated format */
9ffe97fe 49int f_group; /* show group ownership of a file */
38eeee6d 50int f_ignorelink; /* indirect through symbolic link operands */
9ffe97fe 51int f_inode; /* print inode */
a657fc85 52int f_kblocks; /* print size in kilobytes */
38eeee6d
KB
53int f_listalldot; /* list . and .. as well */
54int f_listdir; /* list actual directory, not contents */
55int f_listdot; /* list files beginning with . */
9ffe97fe 56int f_longform; /* long listing format */
4dfe4db2
KB
57int f_needstat; /* if need to stat files */
58int f_newline; /* if precede with newline */
9ffe97fe 59int f_nonprint; /* show unprintables as ? */
9d5fbfa8 60int f_nosort; /* don't sort output */
9ffe97fe 61int f_recursive; /* ls subdirectories also */
38eeee6d
KB
62int f_reversesort; /* reverse whatever sort is used */
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 */
4dfe4db2 68int f_total; /* if precede with "total" line */
38eeee6d 69int f_type; /* add type character for non-regular files */
2fd18a0a 70
9ffe97fe
KB
71main(argc, argv)
72 int argc;
73 char **argv;
547b0031 74{
9ffe97fe 75 extern int optind, stat();
4847d483 76 struct winsize win;
9ffe97fe 77 int ch;
4847d483 78 char *p, *getenv();
4845c19f
KB
79 int acccmp(), bcopy(), modcmp(), namecmp(), prcopy(), printcol();
80 int printlong(), printscol(), revacccmp(), revmodcmp(), revnamecmp();
81 int revstatcmp(), statcmp();
2fd18a0a 82
4845c19f 83 /* terminal defaults to -Cq, non-terminal defaults to -1 */
547b0031 84 if (isatty(1)) {
9ffe97fe 85 f_nonprint = 1;
fb29be9b
KB
86 if (ioctl(1, TIOCGWINSZ, &win) == -1 || !win.ws_col) {
87 if (p = getenv("COLUMNS"))
88 termwidth = atoi(p);
89 }
4847d483
KB
90 else
91 termwidth = win.ws_col;
4845c19f 92 f_column = 1;
9ffe97fe
KB
93 } else
94 f_singlecol = 1;
2fd18a0a 95
38eeee6d 96 /* root is -A automatically */
9ffe97fe 97 if (!getuid())
b3f77fd1 98 f_listdot = 1;
9ffe97fe 99
9d5fbfa8 100 while ((ch = getopt(argc, argv, "1ACFLRacdfgiklqrstu")) != EOF) {
9ffe97fe 101 switch (ch) {
38eeee6d
KB
102 /*
103 * -1, -C and -l all override each other
104 * so shell aliasing works right
105 */
9ffe97fe
KB
106 case '1':
107 f_singlecol = 1;
4845c19f 108 f_column = f_longform = 0;
2fd18a0a 109 break;
9ffe97fe 110 case 'C':
4845c19f 111 f_column = 1;
38eeee6d
KB
112 f_longform = f_singlecol = 0;
113 break;
114 case 'l':
115 f_longform = 1;
4845c19f 116 f_column = f_singlecol = 0;
9ffe97fe 117 break;
38eeee6d
KB
118 /* -c and -u override each other */
119 case 'c':
120 f_statustime = 1;
121 f_accesstime = 0;
122 break;
123 case 'u':
124 f_accesstime = 1;
125 f_statustime = 0;
126 break;
9ffe97fe
KB
127 case 'F':
128 f_type = 1;
129 break;
130 case 'L':
38eeee6d 131 f_ignorelink = 1;
9ffe97fe
KB
132 break;
133 case 'R':
134 f_recursive = 1;
9ffe97fe
KB
135 break;
136 case 'a':
b3f77fd1 137 f_listalldot = 1;
9ffe97fe 138 /* FALLTHROUGH */
2fd18a0a 139 case 'A':
b3f77fd1 140 f_listdot = 1;
2fd18a0a 141 break;
1c52c7b1
JB
142 case 'S':
143 Sflg++; /* fall into... */
547b0031 144 case 'd':
9ffe97fe 145 f_listdir = 1;
2fd18a0a 146 break;
9d5fbfa8
KB
147 case 'f':
148 f_nosort = 1;
149 break;
547b0031 150 case 'g':
9ffe97fe 151 f_group = 1;
2fd18a0a 152 break;
ec0c22c1 153 case 'i':
9ffe97fe 154 f_inode = 1;
2fd18a0a 155 break;
a657fc85
KB
156 case 'k':
157 f_kblocks = 1;
158 break;
ec0c22c1 159 case 'q':
9ffe97fe 160 f_nonprint = 1;
2fd18a0a 161 break;
547b0031 162 case 'r':
9ffe97fe 163 f_reversesort = 1;
2fd18a0a 164 break;
ec0c22c1 165 case 's':
9ffe97fe 166 f_size = 1;
2fd18a0a 167 break;
547b0031 168 case 't':
9ffe97fe 169 f_timesort = 1;
2fd18a0a 170 break;
ec0c22c1 171 default:
2fd18a0a 172 case '?':
9ffe97fe 173 usage();
2955376b 174 }
9ffe97fe 175 }
b3f77fd1
KB
176 argc -= optind;
177 argv += optind;
9ffe97fe 178
b3f77fd1
KB
179 /* -d turns off -R */
180 if (f_listdir)
181 f_recursive = 0;
182
95fa2ad8 183 /* if need to stat files */
4dfe4db2
KB
184 f_needstat = f_longform || f_recursive || f_timesort ||
185 f_size || f_type;
b3f77fd1 186
9ffe97fe
KB
187 /* select a sort function */
188 if (f_reversesort) {
189 if (!f_timesort)
190 sortfcn = revnamecmp;
191 else if (f_accesstime)
192 sortfcn = revacccmp;
9ffe97fe
KB
193 else if (f_statustime)
194 sortfcn = revstatcmp;
38eeee6d
KB
195 else /* use modification time */
196 sortfcn = revmodcmp;
2fd18a0a 197 } else {
9ffe97fe
KB
198 if (!f_timesort)
199 sortfcn = namecmp;
200 else if (f_accesstime)
201 sortfcn = acccmp;
9ffe97fe
KB
202 else if (f_statustime)
203 sortfcn = statcmp;
38eeee6d
KB
204 else /* use modification time */
205 sortfcn = modcmp;
9ffe97fe
KB
206 }
207
4847d483
KB
208 /* select a print function */
209 if (f_singlecol)
210 printfcn = printscol;
211 else if (f_longform)
212 printfcn = printlong;
b3f77fd1 213 else
4e9923b0 214 printfcn = printcol;
9ffe97fe 215
4e9923b0
KB
216 if (!argc) {
217 argc = 1;
218 argv[0] = ".";
219 argv[1] = NULL;
2fd18a0a 220 }
4e9923b0
KB
221 doargs(argc, argv);
222 exit(0);
2fd18a0a
KB
223}
224
be4f2604
KB
225static char path[MAXPATHLEN + 1];
226static char *endofpath = path;
227
918d99af 228doargs(argc, argv)
b3f77fd1
KB
229 int argc;
230 char **argv;
2fd18a0a 231{
c573514d 232 register LS *dstatp, *rstatp;
4845c19f 233 register int cnt, dircnt, maxlen, regcnt;
c573514d 234 LS *dstats, *rstats;
b3f77fd1 235 struct stat sb;
4845c19f
KB
236 int (*statfcn)(), stat(), lstat();
237 char top[MAXPATHLEN + 1];
238 u_long blocks;
b3f77fd1 239
c573514d
KB
240 /*
241 * walk through the operands, building separate arrays of LS
242 * structures for directory and non-directory files.
243 */
b3f77fd1 244 dstats = rstats = NULL;
562f122f 245 statfcn = (f_longform || f_listdir) && !f_ignorelink ? lstat : stat;
b3f77fd1
KB
246 for (dircnt = regcnt = 0; *argv; ++argv) {
247 if (statfcn(*argv, &sb)) {
562f122f
KB
248 if (statfcn != stat || lstat(*argv, &sb)) {
249 (void)fprintf(stderr, "ls: %s: %s\n", *argv,
250 strerror(errno));
251 if (errno == ENOENT)
252 continue;
253 exit(1);
254 }
9ffe97fe 255 }
4845c19f 256 if (S_ISDIR(sb.st_mode) && !f_listdir) {
b3f77fd1 257 if (!dstats)
c573514d 258 dstatp = dstats = (LS *)emalloc((u_int)argc *
b3f77fd1 259 (sizeof(LS)));
c573514d
KB
260 dstatp->name = *argv;
261 dstatp->lstat = sb;
262 ++dstatp;
b3f77fd1
KB
263 ++dircnt;
264 }
265 else {
4845c19f 266 if (!rstats) {
c573514d 267 rstatp = rstats = (LS *)emalloc((u_int)argc *
b3f77fd1 268 (sizeof(LS)));
4845c19f
KB
269 blocks = 0;
270 maxlen = -1;
271 }
c573514d
KB
272 rstatp->name = *argv;
273 rstatp->lstat = sb;
4845c19f
KB
274
275 /* save name length for -C format */
276 rstatp->len = strlen(*argv);
277
278 if (f_nonprint)
279 prcopy(*argv, *argv, rstatp->len);
280
281 /* calculate number of blocks if -l/-s formats */
282 if (f_longform || f_size)
283 blocks += sb.st_blocks;
284
285 /* save max length if -C format */
286 if (f_column && maxlen < rstatp->len)
287 maxlen = rstatp->len;
288
c573514d 289 ++rstatp;
b3f77fd1 290 ++regcnt;
9ffe97fe 291 }
9ffe97fe 292 }
c573514d 293 /* display regular files */
b3f77fd1 294 if (regcnt) {
4845c19f
KB
295 rstats[0].lstat.st_btotal = blocks;
296 rstats[0].lstat.st_maxlen = maxlen;
297 displaydir(rstats, regcnt);
4dfe4db2 298 f_newline = f_dirname = 1;
547b0031 299 }
c573514d 300 /* display directories */
b3f77fd1 301 if (dircnt) {
be4f2604
KB
302 register char *p;
303
4dfe4db2 304 f_total = 1;
95fa2ad8
KB
305 if (dircnt > 1) {
306 (void)getwd(top);
b3f77fd1 307 qsort((char *)dstats, dircnt, sizeof(LS), sortfcn);
4dfe4db2 308 f_dirname = 1;
95fa2ad8
KB
309 }
310 for (cnt = 0; cnt < dircnt; ++dstats) {
be4f2604
KB
311 for (endofpath = path, p = dstats->name;
312 *endofpath = *p++; ++endofpath);
4dfe4db2
KB
313 subdir(dstats);
314 f_newline = 1;
95fa2ad8
KB
315 if (++cnt < dircnt && chdir(top)) {
316 (void)fprintf(stderr, "ls: %s: %s\n",
317 top, strerror(errno));
318 exit(1);
319 }
be4f2604 320 }
547b0031 321 }
547b0031
BJ
322}
323
918d99af 324displaydir(stats, num)
b3f77fd1
KB
325 LS *stats;
326 register int num;
547b0031 327{
be4f2604 328 register char *p, *savedpath;
b3f77fd1 329 LS *lp;
9ffe97fe 330
9d5fbfa8 331 if (num > 1 && !f_nosort) {
82567da7
KB
332 u_long save1, save2;
333
334 save1 = stats[0].lstat.st_btotal;
335 save2 = stats[0].lstat.st_maxlen;
b3f77fd1 336 qsort((char *)stats, num, sizeof(LS), sortfcn);
82567da7
KB
337 stats[0].lstat.st_btotal = save1;
338 stats[0].lstat.st_maxlen = save2;
219ae3c3 339 }
9ffe97fe 340
4847d483 341 printfcn(stats, num);
547b0031 342
be4f2604
KB
343 if (f_recursive) {
344 savedpath = endofpath;
b3f77fd1
KB
345 for (lp = stats; num--; ++lp) {
346 if (!S_ISDIR(lp->lstat.st_mode))
347 continue;
348 p = lp->name;
349 if (p[0] == '.' && (!p[1] || p[1] == '.' && !p[2]))
350 continue;
be4f2604
KB
351 if (endofpath != path && endofpath[-1] != '/')
352 *endofpath++ = '/';
353 for (; *endofpath = *p++; ++endofpath);
4dfe4db2
KB
354 f_newline = f_dirname = f_total = 1;
355 subdir(lp);
be4f2604 356 *(endofpath = savedpath) = '\0';
b3f77fd1 357 }
be4f2604 358 }
547b0031
BJ
359}
360
4dfe4db2 361subdir(lp)
b3f77fd1 362 LS *lp;
2fd18a0a 363{
b3f77fd1
KB
364 LS *stats;
365 int num;
be4f2604 366 char *names;
b3f77fd1 367
4dfe4db2 368 if (f_newline)
b3f77fd1 369 (void)putchar('\n');
4dfe4db2 370 if (f_dirname)
b3f77fd1
KB
371 (void)printf("%s:\n", path);
372
373 if (chdir(lp->name)) {
d6bb46f2
KB
374 (void)fprintf(stderr, "ls: %s: %s\n", lp->name,
375 strerror(errno));
b3f77fd1 376 return;
2fd18a0a 377 }
c7f12bd6 378 if (num = tabdir(lp, &stats, &names)) {
918d99af 379 displaydir(stats, num);
c7f12bd6
KB
380 (void)free((char *)stats);
381 (void)free((char *)names);
382 }
b3f77fd1
KB
383 if (chdir("..")) {
384 (void)fprintf(stderr, "ls: ..: %s\n", strerror(errno));
385 exit(1);
2fd18a0a 386 }
547b0031
BJ
387}
388
918d99af 389tabdir(lp, s_stats, s_names)
b3f77fd1
KB
390 LS *lp, **s_stats;
391 char **s_names;
547b0031 392{
219ae3c3
KB
393 register DIR *dirp;
394 register int cnt, maxentry, maxlen;
b3f77fd1 395 register char *p, *names;
7a122474 396 struct dirent *dp;
219ae3c3 397 u_long blocks;
b3f77fd1 398 LS *stats;
547b0031 399
4e9923b0 400 if (!(dirp = opendir("."))) {
219ae3c3
KB
401 (void)fprintf(stderr, "ls: %s: %s\n", lp->name,
402 strerror(errno));
b3f77fd1 403 return(0);
9ffe97fe 404 }
c7f12bd6
KB
405 blocks = maxentry = maxlen = 0;
406 stats = NULL;
7a122474 407 for (cnt = 0; dp = readdir(dirp);) {
c573514d 408 /* this does -A and -a */
7a122474 409 p = dp->d_name;
b3f77fd1
KB
410 if (p[0] == '.') {
411 if (!f_listdot)
412 continue;
413 if (!f_listalldot && (!p[1] || p[1] == '.' && !p[2]))
414 continue;
415 }
416 if (cnt == maxentry) {
c7f12bd6
KB
417 if (!maxentry)
418 *s_names = names =
419 emalloc((u_int)lp->lstat.st_size);
420#define DEFNUM 256
421 maxentry += DEFNUM;
422 if (!(*s_stats = stats = (LS *)realloc((char *)stats,
b3f77fd1
KB
423 (u_int)maxentry * sizeof(LS))))
424 nomem();
425 }
4dfe4db2 426 if (f_needstat && lstat(dp->d_name, &stats[cnt].lstat)) {
b3f77fd1 427 (void)fprintf(stderr, "ls: %s: %s\n",
7a122474 428 dp->d_name, strerror(errno));
b3f77fd1
KB
429 if (errno == ENOENT)
430 continue;
431 exit(1);
432 }
433 stats[cnt].name = names;
219ae3c3 434
219ae3c3
KB
435 if (f_nonprint)
436 prcopy(dp->d_name, names, (int)dp->d_namlen);
437 else
438 bcopy(dp->d_name, names, (int)dp->d_namlen);
7a122474 439 names += dp->d_namlen;
b3f77fd1 440 *names++ = '\0';
219ae3c3
KB
441
442 /*
443 * get the inode from the directory, so the -f flag
444 * works right.
445 */
446 stats[cnt].lstat.st_ino = dp->d_ino;
447
448 /* save name length for -C format */
449 stats[cnt].len = dp->d_namlen;
4845c19f
KB
450
451 /* calculate number of blocks if -l/-s formats */
82567da7 452 if (f_longform || f_size)
219ae3c3 453 blocks += stats[cnt].lstat.st_blocks;
4845c19f 454
219ae3c3 455 /* save max length if -C format */
4845c19f 456 if (f_column && maxlen < (int)dp->d_namlen)
219ae3c3 457 maxlen = dp->d_namlen;
b3f77fd1 458 ++cnt;
2fd18a0a 459 }
c7f12bd6
KB
460 (void)closedir(dirp);
461
462 if (cnt) {
463 stats[0].lstat.st_btotal = blocks;
464 stats[0].lstat.st_maxlen = maxlen;
465 } else if (stats) {
466 (void)free((char *)stats);
467 (void)free((char *)names);
468 }
b3f77fd1 469 return(cnt);
547b0031 470}