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