xref: /netbsd/distrib/utils/tls/tls.c (revision 6550d01e)
1 /*	$NetBSD: tls.c,v 1.4 2010/04/02 15:34:16 christos 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 <stdio.h>
33 #include <stdlib.h>
34 #include <time.h>
35 
36 int iflag;
37 
38 void show_long(char *fname);
39 
40 main(argc, argv)
41 	int argc;
42 	char **argv;
43 {
44 	DIR *dfp;
45 	struct dirent *d;
46 
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);
52 		}
53 	}
54 	if (argc > 2)
55 		fprintf(stderr, "extra args ignored\n");
56 
57 	dfp = opendir(".");
58 	if (dfp == NULL) {
59 		perror("opendir");
60 		return;
61 	}
62 
63 	while ((d = readdir(dfp)) != NULL)
64 		show_long(d->d_name);
65 
66 	closedir(dfp);
67 	exit(0);
68 }
69 
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: ? */
88 };
89 
90 void
91 show_long(fname)
92 	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 ",  st.st_ino);
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