tls.c revision 1.1 1 /* $NetBSD: tls.c,v 1.1 1995/10/08 23:08:47 gwr Exp $ */
2
3 /*
4 * Copyright (c) 1995 Gordon W. Ross
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 * 4. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by Gordon W. Ross
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include <sys/types.h>
34 #include <sys/stat.h>
35
36 #include <dirent.h>
37 #include <stdio.h>
38 #include <time.h>
39
40 int iflag;
41
42 void show_long(char *fname);
43
44 main(argc, argv)
45 int argc;
46 char **argv;
47 {
48 DIR *dfp;
49 struct dirent *d;
50
51 /* If given an arg, just cd there first. */
52 if (argc > 1) {
53 if (chdir(argv[1])) {
54 perror(argv[1]);
55 exit(1);
56 }
57 }
58 if (argc > 2)
59 fprintf(stderr, "extra args ignored\n");
60
61 dfp = opendir(".");
62 if (dfp == NULL) {
63 perror("opendir");
64 return;
65 }
66
67 while ((d = readdir(dfp)) != NULL)
68 show_long(d->d_name);
69
70 closedir(dfp);
71 exit(0);
72 }
73
74 /* XXX - This is system dependent... */
75 char ifmt_name[16] = {
76 '?', /* 0: nothing */
77 'P', /* 1: fifo (pipe) */
78 'C', /* 2: chr device */
79 '?', /* 3: ? */
80 'D', /* 4: dir */
81 '?', /* 5: ? */
82 'B', /* 6: blk device */
83 '?', /* 7: ? */
84 'F', /* 8: file */
85 '?', /* 9: ? */
86 'L', /* A: link */
87 '?', /* B: ? */
88 'S', /* C: socket */
89 '?', /* D: ? */
90 'W', /* E: whiteout */
91 '?' /* F: ? */
92 };
93
94 void
95 show_long(fname)
96 char *fname;
97 {
98 struct stat st;
99 int ifmt;
100 char ifmt_c;
101 char *date;
102
103 if (lstat(fname, &st)) {
104 perror(fname);
105 return;
106 }
107 ifmt = (st.st_mode >> 12) & 15;
108 ifmt_c = ifmt_name[ifmt];
109
110 if (iflag) {
111 /* inode number */
112 printf("%6d ", st.st_ino);
113 }
114
115 /* fmt/mode */
116 printf("%c:", ifmt_c);
117 printf("%04o ", st.st_mode & 07777);
118
119 /* nlinks, uid, gid */
120 printf("%2d ", st.st_nlink);
121 printf("%4d ", st.st_uid);
122 printf("%4d ", st.st_gid);
123
124 /* size or major/minor */
125 if ((ifmt_c == 'B') || (ifmt_c == 'C')) {
126 printf("%2d, ", major(st.st_rdev));
127 printf("%3d ", minor(st.st_rdev));
128 } else {
129 printf("%7d ", (int) st.st_size);
130 }
131
132 /* date */
133 date = ctime(&st.st_mtime);
134 date += 4; /* skip day-of-week */
135 date[12] = '\0'; /* to the minute */
136 printf("%s ", date);
137
138 /* name */
139 printf("%s", fname);
140
141 if (ifmt_c == 'L') {
142 char linkto[256];
143 int n;
144
145 n = readlink(fname, linkto, sizeof(linkto)-1);
146 if (n < 0) {
147 perror(fname);
148 return;
149 }
150 linkto[n] = '\0';
151 printf(" -> %s", linkto);
152 }
153 printf("\n");
154 }
155