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