ls.c revision 1.10 1 /* $NetBSD: ls.c,v 1.10 1998/03/03 02:22:40 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. 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 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "from: @(#)ls.c 8.1 (Berkeley) 6/6/93";
40 #else
41 __RCSID("$NetBSD: ls.c,v 1.10 1998/03/03 02:22:40 thorpej Exp $");
42 #endif
43 #endif /* not lint */
44
45 #include <sys/param.h>
46 #include <sys/stat.h>
47
48 #include <err.h>
49 #include <errno.h>
50 #include <fts.h>
51 #include <grp.h>
52 #include <pwd.h>
53 #include <stdio.h>
54 #include <string.h>
55 #include <time.h>
56 #include <tzfile.h>
57 #include <unistd.h>
58 #include <utmp.h>
59
60 #include "find.h"
61
62 /* Derived from the print routines in the ls(1) source code. */
63
64 static void printlink __P((char *));
65 static void printtime __P((time_t));
66
67 void
68 printlong(name, accpath, sb)
69 char *name; /* filename to print */
70 char *accpath; /* current valid path to filename */
71 struct stat *sb; /* stat buffer */
72 {
73 char modep[15];
74
75 (void)printf("%6lu %4qd ", (u_long)sb->st_ino,
76 (long long)sb->st_blocks);
77 (void)strmode(sb->st_mode, modep);
78 (void)printf("%s %3u %-*s %-*s ", modep, sb->st_nlink, UT_NAMESIZE,
79 user_from_uid(sb->st_uid, 0), UT_NAMESIZE,
80 group_from_gid(sb->st_gid, 0));
81
82 if (S_ISCHR(sb->st_mode) || S_ISBLK(sb->st_mode))
83 (void)printf("%3d, %3d ", major(sb->st_rdev),
84 minor(sb->st_rdev));
85 else
86 (void)printf("%8qd ", (long long)sb->st_size);
87 printtime(sb->st_mtime);
88 (void)printf("%s", name);
89 if (S_ISLNK(sb->st_mode))
90 printlink(accpath);
91 (void)putchar('\n');
92 }
93
94 static void
95 printtime(ftime)
96 time_t ftime;
97 {
98 int i;
99 char *longstring;
100
101 longstring = ctime(&ftime);
102 for (i = 4; i < 11; ++i)
103 (void)putchar(longstring[i]);
104
105 #define SIXMONTHS ((DAYSPERNYEAR / 2) * SECSPERDAY)
106 if (ftime + SIXMONTHS > time((time_t *)NULL))
107 for (i = 11; i < 16; ++i)
108 (void)putchar(longstring[i]);
109 else {
110 (void)putchar(' ');
111 for (i = 20; i < 24; ++i)
112 (void)putchar(longstring[i]);
113 }
114 (void)putchar(' ');
115 }
116
117 static void
118 printlink(name)
119 char *name;
120 {
121 int lnklen;
122 char path[MAXPATHLEN + 1];
123
124 if ((lnklen = readlink(name, path, MAXPATHLEN)) == -1) {
125 warn("%s", name);
126 return;
127 }
128 path[lnklen] = '\0';
129 (void)printf(" -> %s", path);
130 }
131