1/*	$NetBSD: tls.c,v 1.6 2011/05/20 02:12:39 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/param.h>
30#include <sys/stat.h>
31
32#include <dirent.h>
33#include <err.h>
34#include <stdio.h>
35#include <stdlib.h>
36#include <unistd.h>
37#include <time.h>
38#include <unistd.h>
39
40static int iflag;
41
42static void show_long(const char *);
43
44int
45main(int argc, char *argv[])
46{
47	DIR *dfp;
48	struct dirent *d;
49
50	/* If given an arg, just cd there first. */
51	if (argc > 1) {
52		if (chdir(argv[1]))
53			err(1, "chdir `%s'", argv[1]);
54	}
55	if (argc > 2)
56		fprintf(stderr, "extra args ignored\n");
57
58	dfp = opendir(".");
59	if (dfp == NULL)
60		err(EXIT_FAILURE, "opendir");
61
62	while ((d = readdir(dfp)) != NULL)
63		show_long(d->d_name);
64
65	closedir(dfp);
66	return EXIT_SUCCESS;
67}
68
69/* XXX - This is system dependent... */
70static const char ifmt_name[16] = {
71	'?',	/* 0: nothing */
72	'P',	/* 1: fifo (pipe) */
73	'C',	/* 2: chr device */
74	'?',	/* 3: ? */
75	'D',	/* 4: dir */
76	'?',	/* 5: ? */
77	'B',	/* 6: blk device */
78	'?',	/* 7: ? */
79	'F',	/* 8: file */
80	'?',	/* 9: ? */
81	'L',	/* A: link */
82	'?',	/* B: ? */
83	'S',	/* C: socket */
84	'?',	/* D: ? */
85	'W',	/* E: whiteout */
86	'?' 	/* F: ? */
87};
88
89static void
90show_long(const char *fname)
91{
92	struct stat st;
93	int ifmt;
94	char ifmt_c;
95	char *date;
96
97	if (lstat(fname, &st)) {
98		warn("lstat `%s'", fname);
99		return;
100	}
101	ifmt = (st.st_mode >> 12) & 15;
102	ifmt_c = ifmt_name[ifmt];
103
104	if (iflag) {
105		/* inode number */
106		printf("%6d ",  (int)st.st_ino);	/* assume small fs */
107	}
108
109	/* fmt/mode */
110	printf("%c:",   ifmt_c);
111	printf("%04o ", st.st_mode & 07777);
112
113	/* nlinks, uid, gid */
114	printf("%2d ",   st.st_nlink);
115	printf("%4d ",  st.st_uid);
116	printf("%4d ",  st.st_gid);
117
118	/* size or major/minor */
119	if ((ifmt_c == 'B') || (ifmt_c == 'C')) {
120		printf("%2d, ", major(st.st_rdev));
121		printf("%3d ",  minor(st.st_rdev));
122	} else {
123		printf("%7d ",  (int) st.st_size);
124	}
125
126	/* date */
127	if ((date = ctime(&st.st_mtime)) == NULL)
128		printf("? ");
129	else {
130		date += 4;	/* skip day-of-week */
131		date[12] = '\0';	/* to the minute */
132		printf("%s ", date);
133	}
134
135	/* name */
136	printf("%s", fname);
137
138	if (ifmt_c == 'L') {
139		char linkto[MAXPATHLEN];
140		int n;
141
142		n = readlink(fname, linkto, sizeof(linkto)-1);
143		if (n < 0) {
144			warn("readlink `%s'", fname);
145			return;
146		}
147		linkto[n] = '\0';
148		printf(" -> %s", linkto);
149	}
150	printf("\n");
151}
152