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