1 /* $MirOS: src/bin/ls/print.c,v 1.4 2007/02/18 03:16:45 tg Exp $ */
2 /* $OpenBSD: print.c,v 1.24 2005/06/15 17:47:17 millert Exp $ */
3 /* $NetBSD: print.c,v 1.15 1996/12/11 03:25:39 thorpej Exp $ */
4
5 /*
6 * Copyright (c) 1989, 1993, 1994
7 * The Regents of the University of California. All rights reserved.
8 *
9 * This code is derived from software contributed to Berkeley by
10 * Michael Fischbein.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. 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 #include <sys/param.h>
38 #include <sys/stat.h>
39
40 #include <err.h>
41 #include <errno.h>
42 #include <fts.h>
43 #include <grp.h>
44 #include <pwd.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <time.h>
49 #include <tzfile.h>
50 #include <unistd.h>
51
52 #include "ls.h"
53 #include "extern.h"
54
55 __SCCSID("@(#)print.c 8.5 (Berkeley) 7/28/94");
56 __RCSID("$MirOS: src/bin/ls/print.c,v 1.4 2007/02/18 03:16:45 tg Exp $");
57
58 extern int termwidth;
59
60 static int printaname(FTSENT *, u_long, u_long);
61 static void printlink(FTSENT *);
62 static void printtime(time_t);
63 static int printtype(u_int);
64 static int compute_columns(DISPLAY *, int *);
65
66 #define IS_NOPRINT(p) ((p)->fts_number == NO_PRINT)
67
68 void
printscol(DISPLAY * dp)69 printscol(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
printlong(DISPLAY * dp)82 printlong(DISPLAY *dp)
83 {
84 struct stat *sp;
85 FTSENT *p;
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("%*u ", dp->s_inode, sp->st_ino);
98 if (f_size)
99 (void)printf("%*lld ",
100 dp->s_block, howmany(sp->st_blocks, blocksize));
101 (void)strmode(sp->st_mode, buf);
102 np = p->fts_pointer;
103 (void)printf("%s %*u %-*s %-*s ", buf, dp->s_nlink,
104 sp->st_nlink, dp->s_user, np->user, dp->s_group,
105 np->group);
106 if (f_flags)
107 (void)printf("%-*s ", dp->s_flags, np->flags);
108 if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode))
109 (void)printf("%3d, %3d ",
110 major(sp->st_rdev), minor(sp->st_rdev));
111 else if (dp->bcfile)
112 (void)printf("%*s%*lld ",
113 8 - dp->s_size, "", dp->s_size, sp->st_size);
114 else
115 (void)printf("%*lld ", dp->s_size, sp->st_size);
116 if (f_accesstime)
117 printtime(sp->st_atime);
118 else if (f_statustime)
119 printtime(sp->st_ctime);
120 else
121 printtime(sp->st_mtime);
122 (void)putname(p->fts_name);
123 if (f_type || (f_typedir && S_ISDIR(sp->st_mode)))
124 (void)printtype(sp->st_mode);
125 if (S_ISLNK(sp->st_mode))
126 printlink(p);
127 (void)putchar('\n');
128 }
129 }
130
131 static int
compute_columns(DISPLAY * dp,int * pnum)132 compute_columns(DISPLAY *dp, int *pnum)
133 {
134 int colwidth;
135 int mywidth;
136
137 colwidth = dp->maxlen;
138 if (f_inode)
139 colwidth += dp->s_inode + 1;
140 if (f_size)
141 colwidth += dp->s_block + 1;
142 if (f_type || f_typedir)
143 colwidth += 1;
144
145 colwidth += 1;
146 mywidth = termwidth + 1; /* no extra space for last column */
147
148 if (mywidth < 2 * colwidth) {
149 printscol(dp);
150 return (0);
151 }
152
153 *pnum = mywidth / colwidth;
154 return (mywidth / *pnum); /* spread out if possible */
155 }
156
157 void
printcol(DISPLAY * dp)158 printcol(DISPLAY *dp)
159 {
160 static FTSENT **array;
161 static int lastentries = -1;
162 FTSENT *p;
163 int base, chcnt, col, colwidth, num;
164 int numcols, numrows, row;
165
166 if ((colwidth = compute_columns(dp, &numcols)) == 0)
167 return;
168 /*
169 * Have to do random access in the linked list -- build a table
170 * of pointers.
171 */
172 if (dp->entries > lastentries) {
173 FTSENT **a;
174
175 if ((a = realloc(array, dp->entries * sizeof(FTSENT *))) ==
176 NULL) {
177 free(array);
178 array = NULL;
179 dp->entries = 0;
180 lastentries = -1;
181 warn(NULL);
182 printscol(dp);
183 return;
184 }
185 lastentries = dp->entries;
186 array = a;
187 }
188 for (p = dp->list, num = 0; p; p = p->fts_link)
189 if (p->fts_number != NO_PRINT)
190 array[num++] = p;
191
192 numrows = num / numcols;
193 if (num % numcols)
194 ++numrows;
195
196 if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
197 (void)printf("total %lu\n", howmany(dp->btotal, blocksize));
198 for (row = 0; row < numrows; ++row) {
199 for (base = row, col = 0;;) {
200 chcnt = printaname(array[base], dp->s_inode, dp->s_block);
201 if ((base += numrows) >= num)
202 break;
203 if (++col == numcols)
204 break;
205 while (chcnt++ < colwidth)
206 putchar(' ');
207 }
208 (void)putchar('\n');
209 }
210 }
211
212 /*
213 * print [inode] [size] name
214 * return # of characters printed, no trailing characters.
215 */
216 static int
printaname(FTSENT * p,u_long inodefield,u_long sizefield)217 printaname(FTSENT *p, u_long inodefield, u_long sizefield)
218 {
219 struct stat *sp;
220 int chcnt;
221
222 sp = p->fts_statp;
223 chcnt = 0;
224 if (f_inode)
225 chcnt += printf("%*u ", (int)inodefield, sp->st_ino);
226 if (f_size)
227 chcnt += printf("%*lld ",
228 (int)sizefield, howmany(sp->st_blocks, blocksize));
229 chcnt += putname(p->fts_name);
230 if (f_type || (f_typedir && S_ISDIR(sp->st_mode)))
231 chcnt += printtype(sp->st_mode);
232 return (chcnt);
233 }
234
235 static void
printtime(time_t ftime)236 printtime(time_t ftime)
237 {
238 int i;
239 char *longstring;
240
241 longstring = ctime(&ftime);
242 for (i = 4; i < 11; ++i)
243 (void)putchar(longstring[i]);
244
245 #define SIXMONTHS ((DAYSPERNYEAR / 2) * SECSPERDAY)
246 if (f_sectime)
247 for (i = 11; i < 24; i++)
248 (void)putchar(longstring[i]);
249 else if (ftime + SIXMONTHS > time(NULL))
250 for (i = 11; i < 16; ++i)
251 (void)putchar(longstring[i]);
252 else {
253 (void)putchar(' ');
254 for (i = 20; i < 24; ++i)
255 (void)putchar(longstring[i]);
256 }
257 (void)putchar(' ');
258 }
259
260 void
printacol(DISPLAY * dp)261 printacol(DISPLAY *dp)
262 {
263 FTSENT *p;
264 int chcnt, col, colwidth;
265 int numcols;
266
267 if ( (colwidth = compute_columns(dp, &numcols)) == 0)
268 return;
269
270 if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
271 (void)printf("total %lu\n", howmany(dp->btotal, blocksize));
272 col = 0;
273 for (p = dp->list; p; p = p->fts_link) {
274 if (IS_NOPRINT(p))
275 continue;
276 if (col >= numcols) {
277 col = 0;
278 (void)putchar('\n');
279 }
280 chcnt = printaname(p, dp->s_inode, dp->s_block);
281 col++;
282 if (col < numcols)
283 while (chcnt++ < colwidth)
284 (void)putchar(' ');
285 }
286 (void)putchar('\n');
287 }
288
289 void
printstream(DISPLAY * dp)290 printstream(DISPLAY *dp)
291 {
292 FTSENT *p;
293 int col;
294 int extwidth;
295
296 extwidth = 0;
297 if (f_inode)
298 extwidth += dp->s_inode + 1;
299 if (f_size)
300 extwidth += dp->s_block + 1;
301 if (f_type)
302 extwidth += 1;
303
304 for (col = 0, p = dp->list; p != NULL; p = p->fts_link) {
305 if (IS_NOPRINT(p))
306 continue;
307 if (col > 0) {
308 (void)putchar(','), col++;
309 if (col + 1 + extwidth + p->fts_namelen >=
310 (size_t)termwidth)
311 (void)putchar('\n'), col = 0;
312 else
313 (void)putchar(' '), col++;
314 }
315 col += printaname(p, dp->s_inode, dp->s_block);
316 }
317 (void)putchar('\n');
318 }
319
320 static int
printtype(u_int mode)321 printtype(u_int mode)
322 {
323 switch (mode & S_IFMT) {
324 case S_IFDIR:
325 (void)putchar('/');
326 return (1);
327 case S_IFIFO:
328 (void)putchar('|');
329 return (1);
330 case S_IFLNK:
331 (void)putchar('@');
332 return (1);
333 case S_IFSOCK:
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
printlink(FTSENT * p)345 printlink(FTSENT *p)
346 {
347 int lnklen;
348 char name[MAXPATHLEN], path[MAXPATHLEN];
349
350 if (p->fts_level == FTS_ROOTLEVEL)
351 (void)snprintf(name, sizeof(name), "%s", p->fts_name);
352 else
353 (void)snprintf(name, sizeof(name),
354 "%s/%s", p->fts_parent->fts_accpath, p->fts_name);
355 if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) {
356 (void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno));
357 return;
358 }
359 path[lnklen] = '\0';
360 (void)printf(" -> ");
361 (void)putname(path);
362 }
363