print.c revision 1.37 1 1.37 jschauma /* $NetBSD: print.c,v 1.37 2003/09/22 02:43:20 jschauma 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.35 agc * 3. Neither the name of the University nor the names of its contributors
19 1.1 cgd * may be used to endorse or promote products derived from this software
20 1.1 cgd * without specific prior written permission.
21 1.1 cgd *
22 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 cgd * SUCH DAMAGE.
33 1.1 cgd */
34 1.1 cgd
35 1.16 christos #include <sys/cdefs.h>
36 1.1 cgd #ifndef lint
37 1.13 cgd #if 0
38 1.14 jtc static char sccsid[] = "@(#)print.c 8.5 (Berkeley) 7/28/94";
39 1.13 cgd #else
40 1.37 jschauma __RCSID("$NetBSD: print.c,v 1.37 2003/09/22 02:43:20 jschauma Exp $");
41 1.13 cgd #endif
42 1.1 cgd #endif /* not lint */
43 1.1 cgd
44 1.1 cgd #include <sys/param.h>
45 1.1 cgd #include <sys/stat.h>
46 1.11 mycroft
47 1.11 mycroft #include <err.h>
48 1.11 mycroft #include <errno.h>
49 1.5 mycroft #include <fts.h>
50 1.1 cgd #include <grp.h>
51 1.1 cgd #include <pwd.h>
52 1.11 mycroft #include <stdio.h>
53 1.5 mycroft #include <stdlib.h>
54 1.5 mycroft #include <string.h>
55 1.11 mycroft #include <time.h>
56 1.11 mycroft #include <tzfile.h>
57 1.11 mycroft #include <unistd.h>
58 1.11 mycroft
59 1.1 cgd #include "ls.h"
60 1.5 mycroft #include "extern.h"
61 1.1 cgd
62 1.31 christos extern int termwidth;
63 1.31 christos
64 1.30 lukem static int printaname(FTSENT *, int, int);
65 1.30 lukem static void printlink(FTSENT *);
66 1.30 lukem static void printtime(time_t);
67 1.30 lukem static int printtype(u_int);
68 1.5 mycroft
69 1.20 mycroft static time_t now;
70 1.20 mycroft
71 1.5 mycroft #define IS_NOPRINT(p) ((p)->fts_number == NO_PRINT)
72 1.5 mycroft
73 1.5 mycroft void
74 1.30 lukem printscol(DISPLAY *dp)
75 1.1 cgd {
76 1.11 mycroft FTSENT *p;
77 1.5 mycroft
78 1.5 mycroft for (p = dp->list; p; p = p->fts_link) {
79 1.5 mycroft if (IS_NOPRINT(p))
80 1.5 mycroft continue;
81 1.5 mycroft (void)printaname(p, dp->s_inode, dp->s_block);
82 1.1 cgd (void)putchar('\n');
83 1.1 cgd }
84 1.1 cgd }
85 1.1 cgd
86 1.5 mycroft void
87 1.30 lukem printlong(DISPLAY *dp)
88 1.5 mycroft {
89 1.11 mycroft struct stat *sp;
90 1.11 mycroft FTSENT *p;
91 1.5 mycroft NAMES *np;
92 1.5 mycroft char buf[20];
93 1.5 mycroft
94 1.22 mycroft now = time(NULL);
95 1.20 mycroft
96 1.5 mycroft if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
97 1.24 christos (void)printf("total %llu\n",
98 1.21 lukem (long long)(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.23 christos (void)printf("%*lu ", dp->s_inode,
106 1.23 christos (unsigned long)sp->st_ino);
107 1.1 cgd if (f_size)
108 1.24 christos (void)printf("%*llu ", 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.34 grant (void)printf("%s %*lu ", buf, dp->s_nlink,
113 1.34 grant (unsigned long)sp->st_nlink);
114 1.34 grant if (!f_grouponly)
115 1.34 grant (void)printf("%-*s ", dp->s_user, np->user);
116 1.34 grant (void)printf("%-*s ", dp->s_group, np->group);
117 1.5 mycroft if (f_flags)
118 1.5 mycroft (void)printf("%-*s ", dp->s_flags, np->flags);
119 1.5 mycroft if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode))
120 1.18 mycroft (void)printf("%*u, %*u ",
121 1.18 mycroft dp->s_major, major(sp->st_rdev), dp->s_minor,
122 1.18 mycroft minor(sp->st_rdev));
123 1.1 cgd else
124 1.24 christos (void)printf("%*llu ", dp->s_size,
125 1.17 thorpej (long long)sp->st_size);
126 1.1 cgd if (f_accesstime)
127 1.5 mycroft printtime(sp->st_atime);
128 1.1 cgd else if (f_statustime)
129 1.5 mycroft printtime(sp->st_ctime);
130 1.1 cgd else
131 1.5 mycroft printtime(sp->st_mtime);
132 1.37 jschauma if (f_octal || f_octal_escape)
133 1.36 jschauma (void)safe_print(p->fts_name);
134 1.36 jschauma else if (f_nonprint)
135 1.29 assar (void)printescaped(p->fts_name);
136 1.28 assar else
137 1.28 assar (void)printf("%s", p->fts_name);
138 1.28 assar
139 1.26 kleink if (f_type || (f_typedir && S_ISDIR(sp->st_mode)))
140 1.5 mycroft (void)printtype(sp->st_mode);
141 1.5 mycroft if (S_ISLNK(sp->st_mode))
142 1.5 mycroft printlink(p);
143 1.1 cgd (void)putchar('\n');
144 1.1 cgd }
145 1.1 cgd }
146 1.1 cgd
147 1.5 mycroft void
148 1.30 lukem printcol(DISPLAY *dp)
149 1.1 cgd {
150 1.5 mycroft static FTSENT **array;
151 1.5 mycroft static int lastentries = -1;
152 1.11 mycroft FTSENT *p;
153 1.16 christos int base, chcnt, col, colwidth, num;
154 1.15 thorpej int numcols, numrows, row;
155 1.1 cgd
156 1.19 lukem colwidth = dp->maxlen;
157 1.19 lukem if (f_inode)
158 1.19 lukem colwidth += dp->s_inode + 1;
159 1.19 lukem if (f_size)
160 1.19 lukem colwidth += dp->s_block + 1;
161 1.26 kleink if (f_type || f_typedir)
162 1.19 lukem colwidth += 1;
163 1.19 lukem
164 1.19 lukem colwidth += 1;
165 1.19 lukem
166 1.19 lukem if (termwidth < 2 * colwidth) {
167 1.19 lukem printscol(dp);
168 1.19 lukem return;
169 1.19 lukem }
170 1.19 lukem
171 1.5 mycroft /*
172 1.5 mycroft * Have to do random access in the linked list -- build a table
173 1.5 mycroft * of pointers.
174 1.5 mycroft */
175 1.5 mycroft if (dp->entries > lastentries) {
176 1.5 mycroft lastentries = dp->entries;
177 1.5 mycroft if ((array =
178 1.5 mycroft realloc(array, dp->entries * sizeof(FTSENT *))) == NULL) {
179 1.27 drochner warn(NULL);
180 1.5 mycroft printscol(dp);
181 1.5 mycroft }
182 1.5 mycroft }
183 1.5 mycroft for (p = dp->list, num = 0; p; p = p->fts_link)
184 1.5 mycroft if (p->fts_number != NO_PRINT)
185 1.5 mycroft array[num++] = p;
186 1.5 mycroft
187 1.19 lukem numcols = termwidth / colwidth;
188 1.19 lukem colwidth = termwidth / numcols; /* spread out if possible */
189 1.19 lukem numrows = num / numcols;
190 1.19 lukem if (num % numcols)
191 1.19 lukem ++numrows;
192 1.19 lukem
193 1.19 lukem if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
194 1.24 christos (void)printf("total %llu\n",
195 1.21 lukem (long long)(howmany(dp->btotal, blocksize)));
196 1.19 lukem for (row = 0; row < numrows; ++row) {
197 1.19 lukem for (base = row, chcnt = col = 0; col < numcols; ++col) {
198 1.19 lukem chcnt = printaname(array[base], dp->s_inode,
199 1.19 lukem dp->s_block);
200 1.19 lukem if ((base += numrows) >= num)
201 1.19 lukem break;
202 1.19 lukem while (chcnt++ < colwidth)
203 1.22 mycroft (void)putchar(' ');
204 1.19 lukem }
205 1.19 lukem (void)putchar('\n');
206 1.19 lukem }
207 1.19 lukem }
208 1.19 lukem
209 1.19 lukem void
210 1.30 lukem printacol(DISPLAY *dp)
211 1.19 lukem {
212 1.19 lukem FTSENT *p;
213 1.19 lukem int chcnt, col, colwidth;
214 1.19 lukem int numcols;
215 1.19 lukem
216 1.5 mycroft colwidth = dp->maxlen;
217 1.1 cgd if (f_inode)
218 1.5 mycroft colwidth += dp->s_inode + 1;
219 1.1 cgd if (f_size)
220 1.5 mycroft colwidth += dp->s_block + 1;
221 1.26 kleink if (f_type || f_typedir)
222 1.1 cgd colwidth += 1;
223 1.1 cgd
224 1.15 thorpej colwidth += 1;
225 1.15 thorpej
226 1.1 cgd if (termwidth < 2 * colwidth) {
227 1.5 mycroft printscol(dp);
228 1.1 cgd return;
229 1.1 cgd }
230 1.1 cgd
231 1.1 cgd numcols = termwidth / colwidth;
232 1.15 thorpej colwidth = termwidth / numcols; /* spread out if possible */
233 1.1 cgd
234 1.5 mycroft if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
235 1.33 enami (void)printf("total %llu\n",
236 1.21 lukem (long long)(howmany(dp->btotal, blocksize)));
237 1.19 lukem chcnt = col = 0;
238 1.19 lukem for (p = dp->list; p; p = p->fts_link) {
239 1.19 lukem if (IS_NOPRINT(p))
240 1.19 lukem continue;
241 1.19 lukem if (col >= numcols) {
242 1.19 lukem chcnt = col = 0;
243 1.22 mycroft (void)putchar('\n');
244 1.1 cgd }
245 1.19 lukem chcnt = printaname(p, dp->s_inode, dp->s_block);
246 1.19 lukem while (chcnt++ < colwidth)
247 1.22 mycroft (void)putchar(' ');
248 1.19 lukem col++;
249 1.25 kleink }
250 1.25 kleink (void)putchar('\n');
251 1.25 kleink }
252 1.25 kleink
253 1.25 kleink void
254 1.30 lukem printstream(DISPLAY *dp)
255 1.25 kleink {
256 1.25 kleink FTSENT *p;
257 1.25 kleink int col;
258 1.25 kleink int extwidth;
259 1.25 kleink
260 1.25 kleink extwidth = 0;
261 1.25 kleink if (f_inode)
262 1.25 kleink extwidth += dp->s_inode + 1;
263 1.25 kleink if (f_size)
264 1.25 kleink extwidth += dp->s_block + 1;
265 1.25 kleink if (f_type)
266 1.25 kleink extwidth += 1;
267 1.25 kleink
268 1.25 kleink for (col = 0, p = dp->list; p != NULL; p = p->fts_link) {
269 1.25 kleink if (IS_NOPRINT(p))
270 1.25 kleink continue;
271 1.25 kleink if (col > 0) {
272 1.25 kleink (void)putchar(','), col++;
273 1.25 kleink if (col + 1 + extwidth + p->fts_namelen >= termwidth)
274 1.25 kleink (void)putchar('\n'), col = 0;
275 1.25 kleink else
276 1.25 kleink (void)putchar(' '), col++;
277 1.25 kleink }
278 1.25 kleink col += printaname(p, dp->s_inode, dp->s_block);
279 1.1 cgd }
280 1.22 mycroft (void)putchar('\n');
281 1.1 cgd }
282 1.1 cgd
283 1.1 cgd /*
284 1.1 cgd * print [inode] [size] name
285 1.5 mycroft * return # of characters printed, no trailing characters.
286 1.1 cgd */
287 1.5 mycroft static int
288 1.30 lukem printaname(FTSENT *p, int inodefield, int sizefield)
289 1.1 cgd {
290 1.5 mycroft struct stat *sp;
291 1.1 cgd int chcnt;
292 1.1 cgd
293 1.5 mycroft sp = p->fts_statp;
294 1.1 cgd chcnt = 0;
295 1.1 cgd if (f_inode)
296 1.23 christos chcnt += printf("%*lu ", inodefield, (unsigned long)sp->st_ino);
297 1.1 cgd if (f_size)
298 1.24 christos chcnt += printf("%*llu ", sizefield,
299 1.17 thorpej (long long)howmany(sp->st_blocks, blocksize));
300 1.37 jschauma if (f_octal || f_octal_escape)
301 1.36 jschauma chcnt += safe_print(p->fts_name);
302 1.36 jschauma else if (f_nonprint)
303 1.36 jschauma chcnt += printescaped(p->fts_name);
304 1.29 assar else
305 1.36 jschauma chcnt += printf("%s", p->fts_name);
306 1.26 kleink if (f_type || (f_typedir && S_ISDIR(sp->st_mode)))
307 1.5 mycroft chcnt += printtype(sp->st_mode);
308 1.5 mycroft return (chcnt);
309 1.1 cgd }
310 1.1 cgd
311 1.5 mycroft static void
312 1.30 lukem printtime(time_t ftime)
313 1.1 cgd {
314 1.1 cgd int i;
315 1.5 mycroft char *longstring;
316 1.1 cgd
317 1.5 mycroft longstring = ctime(&ftime);
318 1.1 cgd for (i = 4; i < 11; ++i)
319 1.1 cgd (void)putchar(longstring[i]);
320 1.1 cgd
321 1.1 cgd #define SIXMONTHS ((DAYSPERNYEAR / 2) * SECSPERDAY)
322 1.1 cgd if (f_sectime)
323 1.1 cgd for (i = 11; i < 24; i++)
324 1.1 cgd (void)putchar(longstring[i]);
325 1.20 mycroft else if (ftime + SIXMONTHS > now && ftime - SIXMONTHS < now)
326 1.1 cgd for (i = 11; i < 16; ++i)
327 1.1 cgd (void)putchar(longstring[i]);
328 1.1 cgd else {
329 1.1 cgd (void)putchar(' ');
330 1.1 cgd for (i = 20; i < 24; ++i)
331 1.1 cgd (void)putchar(longstring[i]);
332 1.1 cgd }
333 1.1 cgd (void)putchar(' ');
334 1.1 cgd }
335 1.1 cgd
336 1.5 mycroft static int
337 1.30 lukem printtype(u_int mode)
338 1.1 cgd {
339 1.11 mycroft switch (mode & S_IFMT) {
340 1.1 cgd case S_IFDIR:
341 1.1 cgd (void)putchar('/');
342 1.5 mycroft return (1);
343 1.11 mycroft case S_IFIFO:
344 1.11 mycroft (void)putchar('|');
345 1.11 mycroft return (1);
346 1.1 cgd case S_IFLNK:
347 1.1 cgd (void)putchar('@');
348 1.5 mycroft return (1);
349 1.1 cgd case S_IFSOCK:
350 1.1 cgd (void)putchar('=');
351 1.12 mycroft return (1);
352 1.12 mycroft case S_IFWHT:
353 1.12 mycroft (void)putchar('%');
354 1.6 jtc return (1);
355 1.1 cgd }
356 1.1 cgd if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
357 1.1 cgd (void)putchar('*');
358 1.5 mycroft return (1);
359 1.1 cgd }
360 1.5 mycroft return (0);
361 1.1 cgd }
362 1.1 cgd
363 1.5 mycroft static void
364 1.30 lukem printlink(FTSENT *p)
365 1.1 cgd {
366 1.1 cgd int lnklen;
367 1.5 mycroft char name[MAXPATHLEN + 1], path[MAXPATHLEN + 1];
368 1.5 mycroft
369 1.5 mycroft if (p->fts_level == FTS_ROOTLEVEL)
370 1.5 mycroft (void)snprintf(name, sizeof(name), "%s", p->fts_name);
371 1.33 enami else
372 1.11 mycroft (void)snprintf(name, sizeof(name),
373 1.11 mycroft "%s/%s", p->fts_parent->fts_accpath, p->fts_name);
374 1.11 mycroft if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) {
375 1.1 cgd (void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno));
376 1.1 cgd return;
377 1.1 cgd }
378 1.1 cgd path[lnklen] = '\0';
379 1.28 assar (void)printf(" -> ");
380 1.37 jschauma if (f_octal || f_octal_escape)
381 1.36 jschauma (void)safe_print(path);
382 1.36 jschauma else if (f_nonprint)
383 1.36 jschauma (void)printescaped(path);
384 1.28 assar else
385 1.28 assar (void)printf("%s", path);
386 1.1 cgd }
387