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