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