print.c revision 1.19 1 1.19 lukem /* $NetBSD: print.c,v 1.19 1998/01/18 13:30:09 lukem Exp $ */
2 1.13 cgd
3 1.1 cgd /*
4 1.11 mycroft * Copyright (c) 1989, 1993, 1994
5 1.11 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * This code is derived from software contributed to Berkeley by
8 1.1 cgd * Michael Fischbein.
9 1.1 cgd *
10 1.1 cgd * Redistribution and use in source and binary forms, with or without
11 1.1 cgd * modification, are permitted provided that the following conditions
12 1.1 cgd * are met:
13 1.1 cgd * 1. Redistributions of source code must retain the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer.
15 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 cgd * notice, this list of conditions and the following disclaimer in the
17 1.1 cgd * documentation and/or other materials provided with the distribution.
18 1.1 cgd * 3. All advertising materials mentioning features or use of this software
19 1.1 cgd * must display the following acknowledgement:
20 1.1 cgd * This product includes software developed by the University of
21 1.1 cgd * California, Berkeley and its contributors.
22 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
23 1.1 cgd * may be used to endorse or promote products derived from this software
24 1.1 cgd * without specific prior written permission.
25 1.1 cgd *
26 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 1.1 cgd * SUCH DAMAGE.
37 1.1 cgd */
38 1.1 cgd
39 1.16 christos #include <sys/cdefs.h>
40 1.1 cgd #ifndef lint
41 1.13 cgd #if 0
42 1.14 jtc static char sccsid[] = "@(#)print.c 8.5 (Berkeley) 7/28/94";
43 1.13 cgd #else
44 1.19 lukem __RCSID("$NetBSD: print.c,v 1.19 1998/01/18 13:30:09 lukem Exp $");
45 1.13 cgd #endif
46 1.1 cgd #endif /* not lint */
47 1.1 cgd
48 1.1 cgd #include <sys/param.h>
49 1.1 cgd #include <sys/stat.h>
50 1.11 mycroft
51 1.11 mycroft #include <err.h>
52 1.11 mycroft #include <errno.h>
53 1.5 mycroft #include <fts.h>
54 1.1 cgd #include <grp.h>
55 1.1 cgd #include <pwd.h>
56 1.11 mycroft #include <stdio.h>
57 1.5 mycroft #include <stdlib.h>
58 1.5 mycroft #include <string.h>
59 1.11 mycroft #include <time.h>
60 1.11 mycroft #include <tzfile.h>
61 1.11 mycroft #include <unistd.h>
62 1.11 mycroft #include <utmp.h>
63 1.11 mycroft
64 1.1 cgd #include "ls.h"
65 1.5 mycroft #include "extern.h"
66 1.1 cgd
67 1.5 mycroft static int printaname __P((FTSENT *, u_long, u_long));
68 1.5 mycroft static void printlink __P((FTSENT *));
69 1.5 mycroft static void printtime __P((time_t));
70 1.5 mycroft static int printtype __P((u_int));
71 1.5 mycroft
72 1.5 mycroft #define IS_NOPRINT(p) ((p)->fts_number == NO_PRINT)
73 1.5 mycroft
74 1.5 mycroft void
75 1.5 mycroft printscol(dp)
76 1.5 mycroft DISPLAY *dp;
77 1.1 cgd {
78 1.11 mycroft FTSENT *p;
79 1.5 mycroft
80 1.5 mycroft for (p = dp->list; p; p = p->fts_link) {
81 1.5 mycroft if (IS_NOPRINT(p))
82 1.5 mycroft continue;
83 1.5 mycroft (void)printaname(p, dp->s_inode, dp->s_block);
84 1.1 cgd (void)putchar('\n');
85 1.1 cgd }
86 1.1 cgd }
87 1.1 cgd
88 1.5 mycroft void
89 1.5 mycroft printlong(dp)
90 1.5 mycroft DISPLAY *dp;
91 1.5 mycroft {
92 1.11 mycroft struct stat *sp;
93 1.11 mycroft FTSENT *p;
94 1.5 mycroft NAMES *np;
95 1.5 mycroft char buf[20];
96 1.5 mycroft
97 1.5 mycroft if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
98 1.5 mycroft (void)printf("total %lu\n", howmany(dp->btotal, blocksize));
99 1.5 mycroft
100 1.5 mycroft for (p = dp->list; p; p = p->fts_link) {
101 1.5 mycroft if (IS_NOPRINT(p))
102 1.5 mycroft continue;
103 1.5 mycroft sp = p->fts_statp;
104 1.1 cgd if (f_inode)
105 1.16 christos (void)printf("%*u ", dp->s_inode, sp->st_ino);
106 1.1 cgd if (f_size)
107 1.11 mycroft (void)printf("%*qd ",
108 1.17 thorpej dp->s_block,
109 1.17 thorpej (long long)howmany(sp->st_blocks, blocksize));
110 1.5 mycroft (void)strmode(sp->st_mode, buf);
111 1.5 mycroft np = p->fts_pointer;
112 1.5 mycroft (void)printf("%s %*u %-*s %-*s ", buf, dp->s_nlink,
113 1.5 mycroft sp->st_nlink, dp->s_user, np->user, dp->s_group,
114 1.5 mycroft np->group);
115 1.5 mycroft if (f_flags)
116 1.5 mycroft (void)printf("%-*s ", dp->s_flags, np->flags);
117 1.5 mycroft if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode))
118 1.18 mycroft (void)printf("%*u, %*u ",
119 1.18 mycroft dp->s_major, major(sp->st_rdev), dp->s_minor,
120 1.18 mycroft minor(sp->st_rdev));
121 1.1 cgd else
122 1.17 thorpej (void)printf("%*qd ", dp->s_size,
123 1.17 thorpej (long long)sp->st_size);
124 1.1 cgd if (f_accesstime)
125 1.5 mycroft printtime(sp->st_atime);
126 1.1 cgd else if (f_statustime)
127 1.5 mycroft printtime(sp->st_ctime);
128 1.1 cgd else
129 1.5 mycroft printtime(sp->st_mtime);
130 1.5 mycroft (void)printf("%s", p->fts_name);
131 1.1 cgd if (f_type)
132 1.5 mycroft (void)printtype(sp->st_mode);
133 1.5 mycroft if (S_ISLNK(sp->st_mode))
134 1.5 mycroft printlink(p);
135 1.1 cgd (void)putchar('\n');
136 1.1 cgd }
137 1.1 cgd }
138 1.1 cgd
139 1.5 mycroft void
140 1.5 mycroft printcol(dp)
141 1.5 mycroft DISPLAY *dp;
142 1.1 cgd {
143 1.1 cgd extern int termwidth;
144 1.5 mycroft static FTSENT **array;
145 1.5 mycroft static int lastentries = -1;
146 1.11 mycroft FTSENT *p;
147 1.16 christos int base, chcnt, col, colwidth, num;
148 1.15 thorpej int numcols, numrows, row;
149 1.1 cgd
150 1.19 lukem colwidth = dp->maxlen;
151 1.19 lukem if (f_inode)
152 1.19 lukem colwidth += dp->s_inode + 1;
153 1.19 lukem if (f_size)
154 1.19 lukem colwidth += dp->s_block + 1;
155 1.19 lukem if (f_type)
156 1.19 lukem colwidth += 1;
157 1.19 lukem
158 1.19 lukem colwidth += 1;
159 1.19 lukem
160 1.19 lukem if (termwidth < 2 * colwidth) {
161 1.19 lukem printscol(dp);
162 1.19 lukem return;
163 1.19 lukem }
164 1.19 lukem
165 1.5 mycroft /*
166 1.5 mycroft * Have to do random access in the linked list -- build a table
167 1.5 mycroft * of pointers.
168 1.5 mycroft */
169 1.5 mycroft if (dp->entries > lastentries) {
170 1.5 mycroft lastentries = dp->entries;
171 1.5 mycroft if ((array =
172 1.5 mycroft realloc(array, dp->entries * sizeof(FTSENT *))) == NULL) {
173 1.16 christos warn("%s", "");
174 1.5 mycroft printscol(dp);
175 1.5 mycroft }
176 1.5 mycroft }
177 1.5 mycroft for (p = dp->list, num = 0; p; p = p->fts_link)
178 1.5 mycroft if (p->fts_number != NO_PRINT)
179 1.5 mycroft array[num++] = p;
180 1.5 mycroft
181 1.19 lukem numcols = termwidth / colwidth;
182 1.19 lukem colwidth = termwidth / numcols; /* spread out if possible */
183 1.19 lukem numrows = num / numcols;
184 1.19 lukem if (num % numcols)
185 1.19 lukem ++numrows;
186 1.19 lukem
187 1.19 lukem if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
188 1.19 lukem (void)printf("total %lu\n", howmany(dp->btotal, blocksize));
189 1.19 lukem for (row = 0; row < numrows; ++row) {
190 1.19 lukem for (base = row, chcnt = col = 0; col < numcols; ++col) {
191 1.19 lukem chcnt = printaname(array[base], dp->s_inode,
192 1.19 lukem dp->s_block);
193 1.19 lukem if ((base += numrows) >= num)
194 1.19 lukem break;
195 1.19 lukem while (chcnt++ < colwidth)
196 1.19 lukem putchar(' ');
197 1.19 lukem }
198 1.19 lukem (void)putchar('\n');
199 1.19 lukem }
200 1.19 lukem }
201 1.19 lukem
202 1.19 lukem void
203 1.19 lukem printacol(dp)
204 1.19 lukem DISPLAY *dp;
205 1.19 lukem {
206 1.19 lukem extern int termwidth;
207 1.19 lukem FTSENT *p;
208 1.19 lukem int chcnt, col, colwidth;
209 1.19 lukem int numcols;
210 1.19 lukem
211 1.5 mycroft colwidth = dp->maxlen;
212 1.1 cgd if (f_inode)
213 1.5 mycroft colwidth += dp->s_inode + 1;
214 1.1 cgd if (f_size)
215 1.5 mycroft colwidth += dp->s_block + 1;
216 1.1 cgd if (f_type)
217 1.1 cgd colwidth += 1;
218 1.1 cgd
219 1.15 thorpej colwidth += 1;
220 1.15 thorpej
221 1.1 cgd if (termwidth < 2 * colwidth) {
222 1.5 mycroft printscol(dp);
223 1.1 cgd return;
224 1.1 cgd }
225 1.1 cgd
226 1.1 cgd numcols = termwidth / colwidth;
227 1.15 thorpej colwidth = termwidth / numcols; /* spread out if possible */
228 1.1 cgd
229 1.5 mycroft if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
230 1.5 mycroft (void)printf("total %lu\n", howmany(dp->btotal, blocksize));
231 1.19 lukem chcnt = col = 0;
232 1.19 lukem for (p = dp->list; p; p = p->fts_link) {
233 1.19 lukem if (IS_NOPRINT(p))
234 1.19 lukem continue;
235 1.19 lukem if (col >= numcols) {
236 1.19 lukem chcnt = col = 0;
237 1.19 lukem putchar('\n');
238 1.1 cgd }
239 1.19 lukem chcnt = printaname(p, dp->s_inode, dp->s_block);
240 1.19 lukem while (chcnt++ < colwidth)
241 1.19 lukem putchar(' ');
242 1.19 lukem col++;
243 1.1 cgd }
244 1.19 lukem putchar('\n');
245 1.1 cgd }
246 1.1 cgd
247 1.1 cgd /*
248 1.1 cgd * print [inode] [size] name
249 1.5 mycroft * return # of characters printed, no trailing characters.
250 1.1 cgd */
251 1.5 mycroft static int
252 1.5 mycroft printaname(p, inodefield, sizefield)
253 1.11 mycroft FTSENT *p;
254 1.5 mycroft u_long sizefield, inodefield;
255 1.1 cgd {
256 1.5 mycroft struct stat *sp;
257 1.1 cgd int chcnt;
258 1.1 cgd
259 1.5 mycroft sp = p->fts_statp;
260 1.1 cgd chcnt = 0;
261 1.1 cgd if (f_inode)
262 1.16 christos chcnt += printf("%*u ", (int)inodefield, sp->st_ino);
263 1.1 cgd if (f_size)
264 1.11 mycroft chcnt += printf("%*qd ",
265 1.17 thorpej (int)sizefield,
266 1.17 thorpej (long long)howmany(sp->st_blocks, blocksize));
267 1.5 mycroft chcnt += printf("%s", p->fts_name);
268 1.1 cgd if (f_type)
269 1.5 mycroft chcnt += printtype(sp->st_mode);
270 1.5 mycroft return (chcnt);
271 1.1 cgd }
272 1.1 cgd
273 1.5 mycroft static void
274 1.1 cgd printtime(ftime)
275 1.1 cgd time_t ftime;
276 1.1 cgd {
277 1.1 cgd int i;
278 1.5 mycroft char *longstring;
279 1.1 cgd
280 1.5 mycroft longstring = ctime(&ftime);
281 1.1 cgd for (i = 4; i < 11; ++i)
282 1.1 cgd (void)putchar(longstring[i]);
283 1.1 cgd
284 1.1 cgd #define SIXMONTHS ((DAYSPERNYEAR / 2) * SECSPERDAY)
285 1.1 cgd if (f_sectime)
286 1.1 cgd for (i = 11; i < 24; i++)
287 1.1 cgd (void)putchar(longstring[i]);
288 1.5 mycroft else if (ftime + SIXMONTHS > time(NULL))
289 1.1 cgd for (i = 11; i < 16; ++i)
290 1.1 cgd (void)putchar(longstring[i]);
291 1.1 cgd else {
292 1.1 cgd (void)putchar(' ');
293 1.1 cgd for (i = 20; i < 24; ++i)
294 1.1 cgd (void)putchar(longstring[i]);
295 1.1 cgd }
296 1.1 cgd (void)putchar(' ');
297 1.1 cgd }
298 1.1 cgd
299 1.5 mycroft static int
300 1.1 cgd printtype(mode)
301 1.5 mycroft u_int mode;
302 1.1 cgd {
303 1.11 mycroft switch (mode & S_IFMT) {
304 1.1 cgd case S_IFDIR:
305 1.1 cgd (void)putchar('/');
306 1.5 mycroft return (1);
307 1.11 mycroft case S_IFIFO:
308 1.11 mycroft (void)putchar('|');
309 1.11 mycroft return (1);
310 1.1 cgd case S_IFLNK:
311 1.1 cgd (void)putchar('@');
312 1.5 mycroft return (1);
313 1.1 cgd case S_IFSOCK:
314 1.1 cgd (void)putchar('=');
315 1.12 mycroft return (1);
316 1.12 mycroft case S_IFWHT:
317 1.12 mycroft (void)putchar('%');
318 1.6 jtc return (1);
319 1.1 cgd }
320 1.1 cgd if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
321 1.1 cgd (void)putchar('*');
322 1.5 mycroft return (1);
323 1.1 cgd }
324 1.5 mycroft return (0);
325 1.1 cgd }
326 1.1 cgd
327 1.5 mycroft static void
328 1.5 mycroft printlink(p)
329 1.5 mycroft FTSENT *p;
330 1.1 cgd {
331 1.1 cgd int lnklen;
332 1.5 mycroft char name[MAXPATHLEN + 1], path[MAXPATHLEN + 1];
333 1.5 mycroft
334 1.5 mycroft if (p->fts_level == FTS_ROOTLEVEL)
335 1.5 mycroft (void)snprintf(name, sizeof(name), "%s", p->fts_name);
336 1.5 mycroft else
337 1.11 mycroft (void)snprintf(name, sizeof(name),
338 1.11 mycroft "%s/%s", p->fts_parent->fts_accpath, p->fts_name);
339 1.11 mycroft if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) {
340 1.1 cgd (void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno));
341 1.1 cgd return;
342 1.1 cgd }
343 1.1 cgd path[lnklen] = '\0';
344 1.1 cgd (void)printf(" -> %s", path);
345 1.1 cgd }
346