xref: /original-bsd/usr.bin/find/ls.c (revision 2bd07fe6)
1 /*
2  * Copyright (c) 1989 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #ifndef lint
19 static char sccsid[] = "@(#)ls.c	5.2 (Berkeley) 04/19/90";
20 #endif /* not lint */
21 
22 #include <sys/param.h>
23 #include <sys/stat.h>
24 #include <tzfile.h>
25 #include <utmp.h>
26 #include <stdio.h>
27 
28 /* Derived from the print routines in the ls(1) source code. */
29 
30 extern int errno;
31 
32 void
33 printlong(name, accpath, sb)
34 	char *name;			/* filename to print */
35 	char *accpath;			/* current valid path to filename */
36 	struct stat *sb;		/* stat buffer */
37 {
38 	extern int errno;
39 	static char *modep;
40 	char *user_from_uid(), *group_from_gid(), *strerror();
41 
42 	(void)printf("%6lu %4ld ", sb->st_ino, sb->st_blocks);
43 	if (strmode(sb->st_mode, &modep)) {
44 		(void)fprintf(stderr, "find: %s.\n", strerror(errno));
45 		exit(1);
46 	}
47 	(void)printf("%s %3u %-*s %-*s ", modep, sb->st_nlink, UT_NAMESIZE,
48 	    user_from_uid(sb->st_uid, 0), UT_NAMESIZE,
49 	    group_from_gid(sb->st_gid, 0));
50 
51 	if (S_ISCHR(sb->st_mode) || S_ISBLK(sb->st_mode))
52 		(void)printf("%3d, %3d ", major(sb->st_rdev),
53 		    minor(sb->st_rdev));
54 	else
55 		(void)printf("%8ld ", sb->st_size);
56 	printtime(sb->st_mtime);
57 	(void)printf("%s", name);
58 	if (S_ISLNK(sb->st_mode))
59 		printlink(accpath);
60 	(void)putchar('\n');
61 }
62 
63 printtime(ftime)
64 	time_t ftime;
65 {
66 	int i;
67 	char *longstring, *ctime();
68 	time_t time();
69 
70 	longstring = ctime((long *)&ftime);
71 	for (i = 4; i < 11; ++i)
72 		(void)putchar(longstring[i]);
73 
74 #define	SIXMONTHS	((DAYSPERNYEAR / 2) * SECSPERDAY)
75 	if (ftime + SIXMONTHS > time((time_t *)NULL))
76 		for (i = 11; i < 16; ++i)
77 			(void)putchar(longstring[i]);
78 	else {
79 		(void)putchar(' ');
80 		for (i = 20; i < 24; ++i)
81 			(void)putchar(longstring[i]);
82 	}
83 	(void)putchar(' ');
84 }
85 
86 printlink(name)
87 	char *name;
88 {
89 	int lnklen;
90 	char path[MAXPATHLEN + 1], *strerror();
91 
92 	if ((lnklen = readlink(name, path, MAXPATHLEN)) == -1) {
93 		(void)fprintf(stderr, "\nfind: %s: %s\n", name, strerror(errno));
94 		return;
95 	}
96 	path[lnklen] = '\0';
97 	(void)printf(" -> %s", path);
98 }
99