print.c revision 1.5 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.5 1993/08/07 03:57:03 mycroft 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 "ls.h"
56 #include "extern.h"
57
58 static int printaname __P((FTSENT *, u_long, u_long));
59 static void printlink __P((FTSENT *));
60 static void printtime __P((time_t));
61 static int printtype __P((u_int));
62
63 #define IS_NOPRINT(p) ((p)->fts_number == NO_PRINT)
64
65 void
66 printscol(dp)
67 DISPLAY *dp;
68 {
69 register FTSENT *p;
70
71 for (p = dp->list; p; p = p->fts_link) {
72 if (IS_NOPRINT(p))
73 continue;
74 (void)printaname(p, dp->s_inode, dp->s_block);
75 (void)putchar('\n');
76 }
77 }
78
79 void
80 printlong(dp)
81 DISPLAY *dp;
82 {
83 register FTSENT *p;
84 register struct stat *sp;
85 NAMES *np;
86 char buf[20];
87
88 if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
89 (void)printf("total %lu\n", howmany(dp->btotal, blocksize));
90
91 for (p = dp->list; p; p = p->fts_link) {
92 if (IS_NOPRINT(p))
93 continue;
94 sp = p->fts_statp;
95 if (f_inode)
96 (void)printf("%*lu ", dp->s_inode, sp->st_ino);
97 if (f_size)
98 #ifdef notyet
99 (void)printf("%*qd ",
100 #else
101 (void)printf("%*ld ",
102 #endif
103 dp->s_block, howmany(sp->st_blocks, blocksize));
104 (void)strmode(sp->st_mode, buf);
105 np = p->fts_pointer;
106 (void)printf("%s %*u %-*s %-*s ", buf, dp->s_nlink,
107 sp->st_nlink, dp->s_user, np->user, dp->s_group,
108 np->group);
109 if (f_flags)
110 (void)printf("%-*s ", dp->s_flags, np->flags);
111 if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode))
112 (void)printf("%3d, %3d ",
113 major(sp->st_rdev), minor(sp->st_rdev));
114 else if (dp->bcfile)
115 #ifdef notyet
116 (void)printf("%*s%*qd ",
117 #else
118 (void)printf("%*s%*ld ",
119 #endif
120 8 - dp->s_size, "", dp->s_size, sp->st_size);
121 else
122 #ifdef notyet
123 (void)printf("%*qd ", dp->s_size, sp->st_size);
124 #else
125 (void)printf("%*ld ", dp->s_size, sp->st_size);
126 #endif
127 if (f_accesstime)
128 printtime(sp->st_atime);
129 else if (f_statustime)
130 printtime(sp->st_ctime);
131 else
132 printtime(sp->st_mtime);
133 (void)printf("%s", p->fts_name);
134 if (f_type)
135 (void)printtype(sp->st_mode);
136 if (S_ISLNK(sp->st_mode))
137 printlink(p);
138 (void)putchar('\n');
139 }
140 }
141
142 #define TAB 8
143
144 void
145 printcol(dp)
146 DISPLAY *dp;
147 {
148 extern int termwidth;
149 static FTSENT **array;
150 static int lastentries = -1;
151 register FTSENT *p;
152 register int base, chcnt, cnt, col, colwidth, num;
153 int endcol, numcols, numrows, row;
154
155 /*
156 * Have to do random access in the linked list -- build a table
157 * of pointers.
158 */
159 if (dp->entries > lastentries) {
160 lastentries = dp->entries;
161 if ((array =
162 realloc(array, dp->entries * sizeof(FTSENT *))) == NULL) {
163 err(0, "%s", strerror(errno));
164 printscol(dp);
165 }
166 }
167 for (p = dp->list, num = 0; p; p = p->fts_link)
168 if (p->fts_number != NO_PRINT)
169 array[num++] = p;
170
171 colwidth = dp->maxlen;
172 if (f_inode)
173 colwidth += dp->s_inode + 1;
174 if (f_size)
175 colwidth += dp->s_block + 1;
176 if (f_type)
177 colwidth += 1;
178
179 colwidth = (colwidth + TAB) & ~(TAB - 1);
180 if (termwidth < 2 * colwidth) {
181 printscol(dp);
182 return;
183 }
184
185 numcols = termwidth / colwidth;
186 numrows = num / numcols;
187 if (num % numcols)
188 ++numrows;
189
190 if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
191 (void)printf("total %lu\n", howmany(dp->btotal, blocksize));
192 for (row = 0; row < numrows; ++row) {
193 endcol = colwidth;
194 for (base = row, chcnt = col = 0; col < numcols; ++col) {
195 chcnt += printaname(array[base], dp->s_inode,
196 dp->s_block);
197 if ((base += numrows) >= num)
198 break;
199 while ((cnt = (chcnt + TAB & ~(TAB - 1))) <= endcol) {
200 (void)putchar('\t');
201 chcnt = cnt;
202 }
203 endcol += colwidth;
204 }
205 (void)putchar('\n');
206 }
207 }
208
209 /*
210 * print [inode] [size] name
211 * return # of characters printed, no trailing characters.
212 */
213 static int
214 printaname(p, inodefield, sizefield)
215 register FTSENT *p;
216 u_long sizefield, inodefield;
217 {
218 struct stat *sp;
219 int chcnt;
220
221 sp = p->fts_statp;
222 chcnt = 0;
223 if (f_inode)
224 chcnt += printf("%*lu ", inodefield, sp->st_ino);
225 if (f_size)
226 #ifdef notyet
227 chcnt += printf("%*qd ",
228 #else
229 chcnt += printf("%*ld ",
230 #endif
231 sizefield, howmany(sp->st_blocks, blocksize));
232 chcnt += printf("%s", p->fts_name);
233 if (f_type)
234 chcnt += printtype(sp->st_mode);
235 return (chcnt);
236 }
237
238 static void
239 printtime(ftime)
240 time_t ftime;
241 {
242 int i;
243 char *longstring;
244
245 longstring = ctime(&ftime);
246 for (i = 4; i < 11; ++i)
247 (void)putchar(longstring[i]);
248
249 #define SIXMONTHS ((DAYSPERNYEAR / 2) * SECSPERDAY)
250 if (f_sectime)
251 for (i = 11; i < 24; i++)
252 (void)putchar(longstring[i]);
253 else if (ftime + SIXMONTHS > time(NULL))
254 for (i = 11; i < 16; ++i)
255 (void)putchar(longstring[i]);
256 else {
257 (void)putchar(' ');
258 for (i = 20; i < 24; ++i)
259 (void)putchar(longstring[i]);
260 }
261 (void)putchar(' ');
262 }
263
264 static int
265 printtype(mode)
266 u_int mode;
267 {
268 switch(mode & S_IFMT) {
269 case S_IFDIR:
270 (void)putchar('/');
271 return (1);
272 case S_IFLNK:
273 (void)putchar('@');
274 return (1);
275 case S_IFSOCK:
276 (void)putchar('=');
277 return (1);
278 }
279 if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
280 (void)putchar('*');
281 return (1);
282 }
283 return (0);
284 }
285
286 static void
287 printlink(p)
288 FTSENT *p;
289 {
290 int lnklen;
291 char name[MAXPATHLEN + 1], path[MAXPATHLEN + 1];
292
293
294 if (p->fts_level == FTS_ROOTLEVEL)
295 (void)snprintf(name, sizeof(name), "%s", p->fts_name);
296 else
297 (void)snprintf(name, sizeof(name),
298 "%s/%s", p->fts_path, p->fts_name);
299 if ((lnklen = readlink(name, path, sizeof(name) - 1)) == -1) {
300 (void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno));
301 return;
302 }
303 path[lnklen] = '\0';
304 (void)printf(" -> %s", path);
305 }
306