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