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