Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / distrib / utils / tls / tls.c
blobd58c54e0d53237a64236b37a708b7f01801b0cb7
1 /* $NetBSD: tls.c,v 1.2 2006/06/24 05:12:13 mrg Exp $ */
3 /*
4 * Copyright (c) 1995 Gordon W. Ross
5 * All rights reserved.
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.
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.
28 #include <sys/types.h>
29 #include <sys/stat.h>
31 #include <dirent.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <time.h>
36 int iflag;
38 void show_long(char *fname);
40 main(argc, argv)
41 int argc;
42 char **argv;
44 DIR *dfp;
45 struct dirent *d;
47 /* If given an arg, just cd there first. */
48 if (argc > 1) {
49 if (chdir(argv[1])) {
50 perror(argv[1]);
51 exit(1);
54 if (argc > 2)
55 fprintf(stderr, "extra args ignored\n");
57 dfp = opendir(".");
58 if (dfp == NULL) {
59 perror("opendir");
60 return;
63 while ((d = readdir(dfp)) != NULL)
64 show_long(d->d_name);
66 closedir(dfp);
67 exit(0);
70 /* XXX - This is system dependent... */
71 char ifmt_name[16] = {
72 '?', /* 0: nothing */
73 'P', /* 1: fifo (pipe) */
74 'C', /* 2: chr device */
75 '?', /* 3: ? */
76 'D', /* 4: dir */
77 '?', /* 5: ? */
78 'B', /* 6: blk device */
79 '?', /* 7: ? */
80 'F', /* 8: file */
81 '?', /* 9: ? */
82 'L', /* A: link */
83 '?', /* B: ? */
84 'S', /* C: socket */
85 '?', /* D: ? */
86 'W', /* E: whiteout */
87 '?' /* F: ? */
90 void
91 show_long(fname)
92 char *fname;
94 struct stat st;
95 int ifmt;
96 char ifmt_c;
97 char *date;
99 if (lstat(fname, &st)) {
100 perror(fname);
101 return;
103 ifmt = (st.st_mode >> 12) & 15;
104 ifmt_c = ifmt_name[ifmt];
106 if (iflag) {
107 /* inode number */
108 printf("%6d ", st.st_ino);
111 /* fmt/mode */
112 printf("%c:", ifmt_c);
113 printf("%04o ", st.st_mode & 07777);
115 /* nlinks, uid, gid */
116 printf("%2d ", st.st_nlink);
117 printf("%4d ", st.st_uid);
118 printf("%4d ", st.st_gid);
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);
128 /* date */
129 date = ctime(&st.st_mtime);
130 date += 4; /* skip day-of-week */
131 date[12] = '\0'; /* to the minute */
132 printf("%s ", date);
134 /* name */
135 printf("%s", fname);
137 if (ifmt_c == 'L') {
138 char linkto[256];
139 int n;
141 n = readlink(fname, linkto, sizeof(linkto)-1);
142 if (n < 0) {
143 perror(fname);
144 return;
146 linkto[n] = '\0';
147 printf(" -> %s", linkto);
149 printf("\n");