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