print.c revision 1.1 1 1.1 cgd /*
2 1.1 cgd * Copyright (c) 1989 The Regents of the University of California.
3 1.1 cgd * All rights reserved.
4 1.1 cgd *
5 1.1 cgd * This code is derived from software contributed to Berkeley by
6 1.1 cgd * Michael Fischbein.
7 1.1 cgd *
8 1.1 cgd * Redistribution and use in source and binary forms, with or without
9 1.1 cgd * modification, are permitted provided that the following conditions
10 1.1 cgd * are met:
11 1.1 cgd * 1. Redistributions of source code must retain the above copyright
12 1.1 cgd * notice, this list of conditions and the following disclaimer.
13 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer in the
15 1.1 cgd * documentation and/or other materials provided with the distribution.
16 1.1 cgd * 3. All advertising materials mentioning features or use of this software
17 1.1 cgd * must display the following acknowledgement:
18 1.1 cgd * This product includes software developed by the University of
19 1.1 cgd * California, Berkeley and its contributors.
20 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
21 1.1 cgd * may be used to endorse or promote products derived from this software
22 1.1 cgd * without specific prior written permission.
23 1.1 cgd *
24 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.1 cgd * SUCH DAMAGE.
35 1.1 cgd */
36 1.1 cgd
37 1.1 cgd #ifndef lint
38 1.1 cgd static char sccsid[] = "@(#)print.c 5.24 (Berkeley) 10/19/90";
39 1.1 cgd #endif /* not lint */
40 1.1 cgd
41 1.1 cgd #include <sys/param.h>
42 1.1 cgd #include <sys/stat.h>
43 1.1 cgd #include <stdio.h>
44 1.1 cgd #include <grp.h>
45 1.1 cgd #include <pwd.h>
46 1.1 cgd #include <utmp.h>
47 1.1 cgd #include <tzfile.h>
48 1.1 cgd #include "ls.h"
49 1.1 cgd
50 1.1 cgd printscol(stats, num)
51 1.1 cgd register LS *stats;
52 1.1 cgd register int num;
53 1.1 cgd {
54 1.1 cgd for (; num--; ++stats) {
55 1.1 cgd (void)printaname(stats);
56 1.1 cgd (void)putchar('\n');
57 1.1 cgd }
58 1.1 cgd }
59 1.1 cgd
60 1.1 cgd printlong(stats, num)
61 1.1 cgd LS *stats;
62 1.1 cgd register int num;
63 1.1 cgd {
64 1.1 cgd extern int errno;
65 1.1 cgd char modep[15], *user_from_uid(), *group_from_gid(), *strerror();
66 1.1 cgd
67 1.1 cgd if (f_total)
68 1.1 cgd (void)printf("total %lu\n", f_kblocks ?
69 1.1 cgd howmany(stats[0].lstat.st_btotal, 2) :
70 1.1 cgd stats[0].lstat.st_btotal);
71 1.1 cgd for (; num--; ++stats) {
72 1.1 cgd if (f_inode)
73 1.1 cgd (void)printf("%6lu ", stats->lstat.st_ino);
74 1.1 cgd if (f_size)
75 1.1 cgd (void)printf("%4ld ", f_kblocks ?
76 1.1 cgd howmany(stats->lstat.st_blocks, 2) :
77 1.1 cgd stats->lstat.st_blocks);
78 1.1 cgd (void)strmode(stats->lstat.st_mode, modep);
79 1.1 cgd (void)printf("%s %3u %-*s ", modep, stats->lstat.st_nlink,
80 1.1 cgd UT_NAMESIZE, user_from_uid(stats->lstat.st_uid, 0));
81 1.1 cgd if (f_group)
82 1.1 cgd (void)printf("%-*s ", UT_NAMESIZE,
83 1.1 cgd group_from_gid(stats->lstat.st_gid, 0));
84 1.1 cgd if (S_ISCHR(stats->lstat.st_mode) ||
85 1.1 cgd S_ISBLK(stats->lstat.st_mode))
86 1.1 cgd (void)printf("%3d, %3d ", major(stats->lstat.st_rdev),
87 1.1 cgd minor(stats->lstat.st_rdev));
88 1.1 cgd else
89 1.1 cgd (void)printf("%8ld ", stats->lstat.st_size);
90 1.1 cgd if (f_accesstime)
91 1.1 cgd printtime(stats->lstat.st_atime);
92 1.1 cgd else if (f_statustime)
93 1.1 cgd printtime(stats->lstat.st_ctime);
94 1.1 cgd else
95 1.1 cgd printtime(stats->lstat.st_mtime);
96 1.1 cgd (void)printf("%s", stats->name);
97 1.1 cgd if (f_type)
98 1.1 cgd (void)printtype(stats->lstat.st_mode);
99 1.1 cgd if (S_ISLNK(stats->lstat.st_mode))
100 1.1 cgd printlink(stats->name);
101 1.1 cgd (void)putchar('\n');
102 1.1 cgd }
103 1.1 cgd }
104 1.1 cgd
105 1.1 cgd #define TAB 8
106 1.1 cgd
107 1.1 cgd printcol(stats, num)
108 1.1 cgd LS *stats;
109 1.1 cgd int num;
110 1.1 cgd {
111 1.1 cgd extern int termwidth;
112 1.1 cgd register int base, chcnt, cnt, col, colwidth;
113 1.1 cgd int endcol, numcols, numrows, row;
114 1.1 cgd
115 1.1 cgd colwidth = stats[0].lstat.st_maxlen;
116 1.1 cgd if (f_inode)
117 1.1 cgd colwidth += 6;
118 1.1 cgd if (f_size)
119 1.1 cgd colwidth += 5;
120 1.1 cgd if (f_type)
121 1.1 cgd colwidth += 1;
122 1.1 cgd
123 1.1 cgd colwidth = (colwidth + TAB) & ~(TAB - 1);
124 1.1 cgd if (termwidth < 2 * colwidth) {
125 1.1 cgd printscol(stats, num);
126 1.1 cgd return;
127 1.1 cgd }
128 1.1 cgd
129 1.1 cgd numcols = termwidth / colwidth;
130 1.1 cgd numrows = num / numcols;
131 1.1 cgd if (num % numcols)
132 1.1 cgd ++numrows;
133 1.1 cgd
134 1.1 cgd if (f_size && f_total)
135 1.1 cgd (void)printf("total %lu\n", f_kblocks ?
136 1.1 cgd howmany(stats[0].lstat.st_btotal, 2) :
137 1.1 cgd stats[0].lstat.st_btotal);
138 1.1 cgd for (row = 0; row < numrows; ++row) {
139 1.1 cgd endcol = colwidth;
140 1.1 cgd for (base = row, chcnt = col = 0; col < numcols; ++col) {
141 1.1 cgd chcnt += printaname(stats + base);
142 1.1 cgd if ((base += numrows) >= num)
143 1.1 cgd break;
144 1.1 cgd while ((cnt = (chcnt + TAB & ~(TAB - 1))) <= endcol) {
145 1.1 cgd (void)putchar('\t');
146 1.1 cgd chcnt = cnt;
147 1.1 cgd }
148 1.1 cgd endcol += colwidth;
149 1.1 cgd }
150 1.1 cgd putchar('\n');
151 1.1 cgd }
152 1.1 cgd }
153 1.1 cgd
154 1.1 cgd /*
155 1.1 cgd * print [inode] [size] name
156 1.1 cgd * return # of characters printed, no trailing characters
157 1.1 cgd */
158 1.1 cgd printaname(lp)
159 1.1 cgd LS *lp;
160 1.1 cgd {
161 1.1 cgd int chcnt;
162 1.1 cgd
163 1.1 cgd chcnt = 0;
164 1.1 cgd if (f_inode)
165 1.1 cgd chcnt += printf("%5lu ", lp->lstat.st_ino);
166 1.1 cgd if (f_size)
167 1.1 cgd chcnt += printf("%4ld ", f_kblocks ?
168 1.1 cgd howmany(lp->lstat.st_blocks, 2) : lp->lstat.st_blocks);
169 1.1 cgd chcnt += printf("%s", lp->name);
170 1.1 cgd if (f_type)
171 1.1 cgd chcnt += printtype(lp->lstat.st_mode);
172 1.1 cgd return(chcnt);
173 1.1 cgd }
174 1.1 cgd
175 1.1 cgd printtime(ftime)
176 1.1 cgd time_t ftime;
177 1.1 cgd {
178 1.1 cgd int i;
179 1.1 cgd char *longstring, *ctime();
180 1.1 cgd time_t time();
181 1.1 cgd
182 1.1 cgd longstring = ctime((long *)&ftime);
183 1.1 cgd for (i = 4; i < 11; ++i)
184 1.1 cgd (void)putchar(longstring[i]);
185 1.1 cgd
186 1.1 cgd #define SIXMONTHS ((DAYSPERNYEAR / 2) * SECSPERDAY)
187 1.1 cgd if (f_sectime)
188 1.1 cgd for (i = 11; i < 24; i++)
189 1.1 cgd (void)putchar(longstring[i]);
190 1.1 cgd else if (ftime + SIXMONTHS > time((time_t *)NULL))
191 1.1 cgd for (i = 11; i < 16; ++i)
192 1.1 cgd (void)putchar(longstring[i]);
193 1.1 cgd else {
194 1.1 cgd (void)putchar(' ');
195 1.1 cgd for (i = 20; i < 24; ++i)
196 1.1 cgd (void)putchar(longstring[i]);
197 1.1 cgd }
198 1.1 cgd (void)putchar(' ');
199 1.1 cgd }
200 1.1 cgd
201 1.1 cgd printtype(mode)
202 1.1 cgd mode_t mode;
203 1.1 cgd {
204 1.1 cgd switch(mode & S_IFMT) {
205 1.1 cgd case S_IFDIR:
206 1.1 cgd (void)putchar('/');
207 1.1 cgd return(1);
208 1.1 cgd case S_IFLNK:
209 1.1 cgd (void)putchar('@');
210 1.1 cgd return(1);
211 1.1 cgd case S_IFSOCK:
212 1.1 cgd (void)putchar('=');
213 1.1 cgd return(1);
214 1.1 cgd }
215 1.1 cgd if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
216 1.1 cgd (void)putchar('*');
217 1.1 cgd return(1);
218 1.1 cgd }
219 1.1 cgd return(0);
220 1.1 cgd }
221 1.1 cgd
222 1.1 cgd printlink(name)
223 1.1 cgd char *name;
224 1.1 cgd {
225 1.1 cgd int lnklen;
226 1.1 cgd char path[MAXPATHLEN + 1], *strerror();
227 1.1 cgd
228 1.1 cgd if ((lnklen = readlink(name, path, MAXPATHLEN)) == -1) {
229 1.1 cgd (void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno));
230 1.1 cgd return;
231 1.1 cgd }
232 1.1 cgd path[lnklen] = '\0';
233 1.1 cgd (void)printf(" -> %s", path);
234 1.1 cgd }
235