-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlc
544 lines (481 loc) · 11.2 KB
/
lc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
Path: uunet!rs
From: [email protected] (Rich Salz)
Newsgroups: comp.sources.unix
Subject: v10i055: An "ls" program
Message-ID: <[email protected]>
Date: 16 Jul 87 00:12:10 GMT
Organization: UUNET Communications Services, Arlington, VA
Lines: 534
Approved: [email protected]
Submitted-by: Guy Middleton <gamiddleton%[email protected]>
Posting-Number: Volume 10, Issue 55
Archive-name: lc
[ It's nice to get a public ls-type program out on to the net.
Please send your 52 patches that implement options [a-zA-Z]
on to Guy so that he can incorporate them. --r$ ]
# This is a shell archive. Remove anything before this line,
# then unpack it by saving it in a file and typing "sh file".
#
# Wrapped by orchid!root on Sat May 16 18:53:36 EDT 1987
# Contents: Makefile README lc.1 lc.c
echo x - Makefile
sed 's/^@//' > "Makefile" <<'@//E*O*F Makefile//'
DESTDIR =
CFLAGS = -O
lc: lc.c
${CC} ${CFLAGS} -o lc lc.c
install: lc
install -s lc $(DESTDIR)/usr/bin/lc
clean:
rm -f lc a.out core *.o *.bak
@//E*O*F Makefile//
chmod u=rw,g=r,o=r Makefile
echo x - README
sed 's/^@//' > "README" <<'@//E*O*F README//'
This is 'lc', the program that everybody here uses instead of ls.
It may not seem like a big deal, but whenever somebody moves away, they
realise that lc isn't part of standard Unix, and call back to get a copy.
This is known to work on 4.2, 4.3, and Ultrix 1.2. We don't have any
SysV machines here, so it may not work there.
@//E*O*F README//
chmod u=rw,g=r,o=r README
echo x - lc.1
sed 's/^@//' > "lc.1" <<'@//E*O*F lc.1//'
@.TH LC 1 UW
@.SH NAME
lc \- list directory and file names in columns
@.SH SYNOPSIS
@.B lc
[
@.B \-fdbcspan1
]
[ directory ... ]
@.SH DESCRIPTION
@.I Lc
lists the elements of the given directories.
The elements are
divided into the five basic types (files, directories, character
special files, block special files, and pipes)
and are printed in alphabetical order.
They are normally printed 5 to a line,
preceeded by a title indicating the type,
but the
@.B \-1
option can be used to force single-column untitled output.
@.PP
If the argument list contains more than one name, the contents of the
directories named in the list are displayed and all other names
are ignored.
If no directory is specified the current working directory is listed
by default.
The special entries ``.'' and ``..''
are normally not listed;
the
@.B \-a
option causes them to be listed as well.
@.PP
The
@.B \-n
option turns off all output; this is useful when only the exit status is
wanted.
(The exit status is 0 if something would have been printed,
1 otherwise.)
@.PP
If any of the following option arguments are given,
@.I lc
lists only those types of entries;
otherwise, all entries are listed.
The options and their meanings are:
@.PP
@.RS 5n
@.TP
@.B f
list ordinary files
@.TP
@.B d
list directories
@.TP
@.B b
list block special files
@.TP
@.B c
list character special files
@.TP
@.B s
list all special files
@.TP
@.B p
list permanent pipes
@.RE
@.PP
@.I Lc
may be used with the substitution features of the Shell
to select particular kinds of files (eg, directories) to
take part in some processing,
for example:
@.PP
@.RS
@.B "ls \-l \`lc -1d\`"
@.RE
@.PP
This lists the contents of all sub-directories of the current directory.
@.SH DIAGNOSTICS
Symbolic links are normally followed, and each prints under the category
of the type of thing to which it is linked.
If the symbolic link points to a nonexistent pathname, or if you do
not have permission to resolve the pathname,
@.I lc
will list the link under the category
@.IR "Unresolved Symbolic Links" .
@//E*O*F lc.1//
chmod u=rwx,g=rx,o=rx lc.1
echo x - lc.c
sed 's/^@//' > "lc.c" <<'@//E*O*F lc.c//'
#ifndef lint
static char *copyright =
"LC - Copyright University of Waterloo, 1978,1985,1987";
#endif lint
/*
* lc [directory ...]
*/
#include <stdio.h>
#include <strings.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/dir.h>
#include <errno.h>
#ifndef MAXPATHLEN
#define MAXPATHLEN 1024 /* For older systems */
#endif
/* this assumes that S_IFMT is a mask of contiguous 1s */
#define IFMT_OFFSET 12 /* may be system-dependent */
#define IFMT (S_IFMT >> IFMT_OFFSET)
#define MODEX(m) (((m)>>IFMT_OFFSET)&IFMT) /* IFMT to table index */
extern int errno,sys_nerr;
extern char *strcpy(),*strncpy();
extern char *sys_errlist[];
int pstrcmp();
#define NMALLOC 100 /* how much to malloc in growlist() */
#define COLUMNWIDTH 15 /* number of characters in a column */
#define INDENT " " /* blanks at beginning of a column */
struct filelist {
char **names; /* pointer to array of names */
int size; /* how many elements */
int end; /* next element to use */
int flag;
char *title;
}
dlist = {(char **) NULL, 0, 0, 0, "Directories"},
flist = {(char **) NULL, 0, 0, 0, "Files"},
plist = {(char **) NULL, 0, 0, 0, "Pipes"},
blist = {(char **) NULL, 0, 0, 0, "Block Spec. Files"},
clist = {(char **) NULL, 0, 0, 0, "Char. Spec. Files"},
mpclist = {(char **) NULL, 0, 0, 0, "MPX Char. Files"},
mpblist = {(char **) NULL, 0, 0, 0, "MPX Block Files"},
symlist = {(char **) NULL, 0, 0, 0, "Unsatisfied Symbolic Links"},
soclist = {(char **) NULL, 0, 0, 0, "Sockets"},
/* flag is always on for this list */
goklist = {(char **) NULL, 0, 0, 1, "**GOK"};
struct filelist *printlist[] = {
&dlist, &flist, &plist, &blist, &clist, &mpclist, &mpblist, &symlist,
&soclist, &goklist, 0
};
struct filelist *listtype[IFMT-1];
struct xinit {
int x_flag;
struct filelist *x_list;
} xinit[] ={
#ifdef S_IPIPE
S_IPIPE, &plist,
#endif
#ifdef S_IFPIP
S_IFPIP, &plist,
#endif
S_IFREG, &flist,
S_IFDIR, &dlist,
S_IFCHR, &clist,
S_IFBLK, &blist,
#ifdef S_IFMPC
S_IFMPC, &mpclist,
#endif
#ifdef S_IFMPB
S_IFMPB, &mpblist,
#endif
#ifdef S_IFLNK
S_IFLNK, &symlist,
#endif
#ifdef S_IFSOCK
S_IFSOCK, &soclist,
#endif
-1,
};
int nfl; /* flag; this is not the first line of output */
int errcall; /* flag; last line of output was error message */
int notfirst; /* flag; means not the first thing in this directory */
int nflg; /* flag; no output - just want exit status */
int ncols = 5; /* number of columns */
int linewidth;
int manyflg;
int anyfound;
int all = 1;
char pathbuf[MAXPATHLEN];
char *fnptr = pathbuf;
int aflg;
main(argc, argv)
char *argv[];
{
register char *path, *p, *q;
struct xinit *xp;
int i;
for (i = 0 ; i < IFMT ; i++)
listtype[i] = &goklist;
for (xp = xinit; xp->x_flag != -1; xp++)
listtype[MODEX(xp->x_flag)] = xp->x_list;
if(argc >= 2 && argv[1][0] == '-') {
argv++;
argc--;
for(p = &argv[0][1]; *p; p++)
switch(*p) {
case 'a':
aflg++;
break;
case 'n':
nflg++;
break;
case '1':
ncols=1;
break;
default:
switch(*p) {
case 'f':
flist.flag++;
break;
case 'd':
dlist.flag++;
break;
case 'b':
blist.flag++;
break;
case 'c':
clist.flag++;
break;
case 'B':
mpblist.flag++;
break;
case 'C':
mpclist.flag++;
break;
case 's':
blist.flag++;
clist.flag++;
mpblist.flag++;
mpclist.flag++;
break;
case 'p':
plist.flag++;
break;
default:
fprintf(stderr, "Unknown flag: %c\n",
*p);
continue;
}
all = 0;
}
}
if (all)
flist.flag = dlist.flag = blist.flag = clist.flag
= mpblist.flag = mpclist.flag
= symlist.flag = soclist.flag
= plist.flag = 1;
linewidth = ncols * COLUMNWIDTH;
if(argc < 3) {
path = argc == 1 ? "." : argv[1];
listdir(path);
} else {
manyflg++;
while(--argc) {
path = p = *++argv;
if(strlen(path) >= MAXPATHLEN) {/* = for '/' */
errno=ENAMETOOLONG;
warn(path);
continue;
}
q = pathbuf;
while(*q++ = *p++);
q[-1] = '/';
fnptr = q;
listdir(path);
}
}
exit(anyfound==0);
}
listdir(path)
char *path;
{
register char *p;
struct direct *d;
struct filelist **pr;
DIR *u;
struct stat ibuf;
if(stat(path, &ibuf) < 0) {
warn(path);
return;
}
if((ibuf.st_mode&S_IFMT) != S_IFDIR) {
if(!manyflg){
errno=ENOTDIR;
warn(path);
}
return;
}
if((u = opendir(path)) == NULL) {
warn(path);
return;
}
if(manyflg) {
if(nfl++)
putchar('\n');
printf("%s:\n", path);
} else
chdir(path);
while((d = readdir(u)) != NULL) {
if(fnptr + d->d_namlen >= &pathbuf[MAXPATHLEN]) {
errno=ENAMETOOLONG;
warn(pathbuf);
/*
err("%s: Component `%s' makes pathname > %d bytes.\n",
pathbuf, d->d_name, MAXPATHLEN);
*/
continue;
}
(void)strcpy(fnptr, d->d_name);
if(stat(pathbuf, &ibuf) < 0 && lstat(pathbuf, &ibuf) < 0) {
warn(pathbuf);
continue;
}
/*
* ignore "." and ".."
*/
if((ibuf.st_mode&S_IFMT) == S_IFDIR && aflg == 0) {
p = d->d_name;
if(*p++ == '.' && (*p == '\0'
|| *p++ == '.' && *p == '\0'))
continue;
}
build(listtype[MODEX(ibuf.st_mode)], d->d_name);
}
closedir(u);
notfirst = 0;
for(pr = printlist; *pr; pr++)
if((*pr)->names && (*pr)->end != 0) {
qsort( (*pr)->names, (*pr)->end,
sizeof (char *), pstrcmp );
print(*pr);
}
fflush(stdout);
}
warn(name)
char *name;
{
if(nfl && !errcall)
putchar('\n');
if((errno>=0)&&(errno<sys_nerr))
fprintf(stdout,"%s: %s\n", name,sys_errlist[errno]);
else
fprintf(stdout,"%s: unknown error (%d).\n", name,errno);
fflush(stdout);
errcall=nfl=1;
errno=0;
}
print(list)
struct filelist *list;
{
register int cursor, i;
register char **p;
if(notfirst++)
putchar('\n');
if(ncols!=1) {
if (manyflg)
fputs(INDENT, stdout);
printf("%s:\n", list->title);
}
cursor = 0;
for(p = list->names, i = 0; i < list->end; i++) {
int len;
int posused; /* positions used by blanks and filename */
register char *fname;
fname = p[i];
len = strlen(fname);
posused = len + ((cursor == 0) ? 0 : COLUMNWIDTH - cursor%COLUMNWIDTH);
if ((cursor + posused) > linewidth) {
printf("\n");
cursor = 0;
posused = len;
}
if (manyflg && (cursor == 0)) fputs(INDENT, stdout);
printf("%*s", posused, fname);
cursor += posused;
}
if(cursor != 0)
printf("\n");
errcall = 0;
if (manyflg)
release(list);
}
build(list, name)
struct filelist *list;
char *name;
{
register int n;
if(list->flag == 0)
return;
anyfound++;
if(nflg)
return;
if (list->end >= list->size)
growlist(list);
n = strlen( name ) + 1;
(list->names)[list->end++] = strncpy( malloc( n ), name, n );
}
growlist( list )
struct filelist *list;
{
register int oldsize, newsize;
register char **newlist;
oldsize = list->size * sizeof (char *);
newsize = oldsize + (NMALLOC * sizeof (char *));
newlist = (char **) malloc( newsize );
if (oldsize != 0) {
bcopy( list->names, newlist, oldsize );
free( list->names );
}
list->names = newlist;
list->size += NMALLOC;
}
release( list )
struct filelist *list;
{
register char **p;
register int i;
for (p = list->names, i = 0; i < list->end; i++)
free( p[i] );
list->end = 0;
}
pstrcmp( a, b )
char **a, **b;
{
return (strcmp( *a, *b ));
}
@//E*O*F lc.c//
chmod u=rwx,g=rx,o=rx lc.c
exit 0
__
-Guy Middleton, University of Waterloo MFCF/ICR, gamiddleton@watmath
--
Rich $alz "Anger is an energy"
Cronus Project, BBN Labs [email protected]
Moderator, comp.sources.unix [email protected]
--
Rich $alz "Anger is an energy"
Cronus Project, BBN Labs [email protected]
Moderator, comp.sources.unix [email protected]