xref: /original-bsd/usr.bin/find/ls.c (revision c180b88f)
1 /*
2  * Copyright (c) 1989 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)ls.c	5.8 (Berkeley) 05/01/93";
10 #endif /* not lint */
11 
12 #include <sys/param.h>
13 #include <sys/stat.h>
14 
15 #include <err.h>
16 #include <errno.h>
17 #include <stdio.h>
18 #include <string.h>
19 #include <time.h>
20 #include <tzfile.h>
21 #include <unistd.h>
22 #include <utmp.h>
23 
24 /* Derived from the print routines in the ls(1) source code. */
25 
26 static void printlink __P((char *));
27 static void printtime __P((time_t));
28 
29 void
30 printlong(name, accpath, sb)
31 	char *name;			/* filename to print */
32 	char *accpath;			/* current valid path to filename */
33 	struct stat *sb;		/* stat buffer */
34 {
35 	char modep[15], *user_from_uid(), *group_from_gid();
36 
37 	(void)printf("%6lu %4qd ", sb->st_ino, sb->st_blocks);
38 	(void)strmode(sb->st_mode, modep);
39 	(void)printf("%s %3u %-*s %-*s ", modep, sb->st_nlink, UT_NAMESIZE,
40 	    user_from_uid(sb->st_uid, 0), UT_NAMESIZE,
41 	    group_from_gid(sb->st_gid, 0));
42 
43 	if (S_ISCHR(sb->st_mode) || S_ISBLK(sb->st_mode))
44 		(void)printf("%3d, %3d ", major(sb->st_rdev),
45 		    minor(sb->st_rdev));
46 	else
47 		(void)printf("%8qd ", sb->st_size);
48 	printtime(sb->st_mtime);
49 	(void)printf("%s", name);
50 	if (S_ISLNK(sb->st_mode))
51 		printlink(accpath);
52 	(void)putchar('\n');
53 }
54 
55 static void
56 printtime(ftime)
57 	time_t ftime;
58 {
59 	int i;
60 	char *longstring, *ctime();
61 	time_t time();
62 
63 	longstring = ctime((long *)&ftime);
64 	for (i = 4; i < 11; ++i)
65 		(void)putchar(longstring[i]);
66 
67 #define	SIXMONTHS	((DAYSPERNYEAR / 2) * SECSPERDAY)
68 	if (ftime + SIXMONTHS > time((time_t *)NULL))
69 		for (i = 11; i < 16; ++i)
70 			(void)putchar(longstring[i]);
71 	else {
72 		(void)putchar(' ');
73 		for (i = 20; i < 24; ++i)
74 			(void)putchar(longstring[i]);
75 	}
76 	(void)putchar(' ');
77 }
78 
79 static void
80 printlink(name)
81 	char *name;
82 {
83 	int lnklen;
84 	char path[MAXPATHLEN + 1];
85 
86 	if ((lnklen = readlink(name, path, MAXPATHLEN)) == -1) {
87 		warn("%s", name);
88 		return;
89 	}
90 	path[lnklen] = '\0';
91 	(void)printf(" -> %s", path);
92 }
93