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