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