Debian Codegraph @

libls/print.c

raw
1 /*
2 Copyright (C) 2000-2026 Free Software Foundation, Inc.
3
4 This file is part of GNU Inetutils.
5
6 GNU Inetutils is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or (at
9 your option) any later version.
10
11 GNU Inetutils is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see `http://www.gnu.org/licenses/'. */
18
19 /*
20 * Copyright (c) 1989, 1993, 1994
21 * The Regents of the University of California. All rights reserved.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the above copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. Neither the name of the University nor the names of its contributors
32 * may be used to endorse or promote products derived from this software
33 * without specific prior written permission.
34 *
35 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
36 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
39 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
41 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 * SUCH DAMAGE.
46 */
47
48 /* This code is derived from software contributed to Berkeley by
49 Michael Fischbein. */
50
51 #include <config.h>
52
53 #include <sys/param.h>
54 #include <sys/stat.h>
55
56 #include <errno.h>
57 #include <fts_.h>
58 #include <grp.h>
59 #include <pwd.h>
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <string.h>
63 #include <time.h>
64 #include <unistd.h>
65 #include <filemode.h>
66
67 #ifdef HAVE_SYS_MKDEV_H
68 # include <sys/mkdev.h>
69 #endif
70
71 #define DAYSPERNYEAR 365
72 #define SECSPERDAY 86400
73
74 #include "ls.h"
75 #include "extern.h"
76
77 #ifndef howmany
78 # define howmany(x, y) (((x)+((y)-1))/(y))
79 #endif
80
81 #ifndef major
82 # define major(x) ((int)(((unsigned)(x)>>8)&0377))
83 #endif
84
85 #ifndef minor
86 # define minor(x) ((int)((x)&0377))
87 #endif
88
89 static int printaname (FTSENT *, unsigned long, unsigned long);
90 static void printlink (FTSENT *);
91 static void printtime (time_t);
92 static int printtype (unsigned int);
93 static int compute_columns (DISPLAY *, int *);
94
95 #define IS_NOPRINT(p) ((p)->fts_number == NO_PRINT)
96
97 void
98 printscol (DISPLAY *dp)
99 {
100 FTSENT *p;
101
102 for (p = dp->list; p; p = p->fts_link)
103 {
104 if (IS_NOPRINT (p))
105 continue;
106 printaname (p, dp->s_inode, dp->s_block);
107 putchar ('\n');
108 }
109 }
110
111 void
112 printlong (DISPLAY *dp)
113 {
114 struct stat *sp;
115 FTSENT *p;
116 NAMES *np;
117 char buf[20];
118
119 if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
120 printf ("total %lu\n", howmany (dp->btotal, blocksize));
121
122 for (p = dp->list; p; p = p->fts_link)
123 {
124 if (IS_NOPRINT (p))
125 continue;
126 sp = p->fts_statp;
127 if (f_inode)
128 printf ("%*lu ", dp->s_inode, (unsigned long) sp->st_ino);
129 if (f_size)
130 printf ("%*llu ",
131 dp->s_block, (long long) howmany (sp->st_blocks, blocksize));
132 strmode (sp->st_mode, buf);
133 np = p->fts_pointer;
134 printf ("%s %*d %-*s %-*s ",
135 buf, dp->s_nlink, (int) sp->st_nlink,
136 dp->s_user, np->user, dp->s_group, np->group);
137 if (f_flags)
138 printf ("%-*s ", dp->s_flags, np->flags);
139 if (S_ISCHR (sp->st_mode) || S_ISBLK (sp->st_mode))
140 printf ("%3d, %3d ", (int) major (sp->st_rdev),
141 (int) minor (sp->st_rdev));
142 else if (dp->bcfile)
143 printf ("%*s%*llu ",
144 8 - dp->s_size, "", dp->s_size, (long long) sp->st_size);
145 else
146 printf ("%*llu ", dp->s_size, (long long) sp->st_size);
147 if (f_accesstime)
148 printtime (sp->st_atime);
149 else if (f_statustime)
150 printtime (sp->st_ctime);
151 else
152 printtime (sp->st_mtime);
153 putname (p->fts_name);
154 if (f_type || (f_typedir && S_ISDIR (sp->st_mode)))
155 printtype (sp->st_mode);
156 if (S_ISLNK (sp->st_mode))
157 printlink (p);
158 putchar ('\n');
159 }
160 }
161
162 static int
163 compute_columns (DISPLAY *dp, int *pnum)
164 {
165 int colwidth;
166 extern int termwidth;
167 int mywidth;
168
169 colwidth = dp->maxlen;
170 if (f_inode)
171 colwidth += dp->s_inode + 1;
172 if (f_size)
173 colwidth += dp->s_block + 1;
174 if (f_type || f_typedir)
175 colwidth += 1;
176
177 colwidth += 1;
178 mywidth = termwidth + 1; /* no extra space for last column */
179
180 if (mywidth < 2 * colwidth)
181 {
182 printscol (dp);
183 return (0);
184 }
185
186 *pnum = mywidth / colwidth;
187 return (mywidth / *pnum); /* spread out if possible */
188 }
189
190 void
191 printcol (DISPLAY *dp)
192 {
193 static FTSENT **array;
194 static int lastentries = -1;
195 FTSENT *p;
196 int base, chcnt, col, colwidth, num;
197 int numcols, numrows, row;
198
199 if ((colwidth = compute_columns (dp, &numcols)) == 0)
200 return;
201 /*
202 * Have to do random access in the linked list -- build a table
203 * of pointers.
204 */
205 if (dp->entries > lastentries)
206 {
207 FTSENT **a;
208
209 if ((a = realloc (array, dp->entries * sizeof (FTSENT *))) == NULL)
210 {
211 fprintf (stderr, "realloci: %s \n", strerror (errno));
212 printscol (dp);
213 return;
214 }
215 lastentries = dp->entries;
216 array = a;
217 }
218 for (p = dp->list, num = 0; p; p = p->fts_link)
219 if (p->fts_number != NO_PRINT)
220 array[num++] = p;
221
222 numrows = num / numcols;
223 if (num % numcols)
224 ++numrows;
225
226 if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
227 printf ("total %lu\n", howmany (dp->btotal, blocksize));
228 for (row = 0; row < numrows; ++row)
229 {
230 for (base = row, col = 0;;)
231 {
232 chcnt = printaname (array[base], dp->s_inode, dp->s_block);
233 if ((base += numrows) >= num)
234 break;
235 if (++col == numcols)
236 break;
237 while (chcnt++ < colwidth)
238 putchar (' ');
239 }
240 putchar ('\n');
241 }
242 }
243
244 /*
245 * print [inode] [size] name
246 * return # of characters printed, no trailing characters.
247 */
248 static int
249 printaname (FTSENT *p, unsigned long inodefield, unsigned long sizefield)
250 {
251 struct stat *sp;
252 int chcnt;
253
254 sp = p->fts_statp;
255 chcnt = 0;
256 if (f_inode)
257 chcnt += printf ("%*lu ", (int) inodefield, (unsigned long) sp->st_ino);
258 if (f_size)
259 chcnt += printf ("%*llu ",
260 (int) sizefield, (long long) howmany (sp->st_blocks,
261 blocksize));
262 chcnt += putname (p->fts_name);
263 if (f_type || (f_typedir && S_ISDIR (sp->st_mode)))
264 chcnt += printtype (sp->st_mode);
265 return (chcnt);
266 }
267
268 static void
269 printtime (time_t ftime)
270 {
271 int i;
272 char *longstring;
273
274 longstring = ctime (&ftime);
275 for (i = 4; i < 11; ++i)
276 putchar (longstring[i]);
277
278 #define SIXMONTHS ((DAYSPERNYEAR / 2) * SECSPERDAY)
279 if (f_sectime)
280 for (i = 11; i < 24; i++)
281 putchar (longstring[i]);
282 else if (ftime + SIXMONTHS > time (NULL))
283 for (i = 11; i < 16; ++i)
284 putchar (longstring[i]);
285 else
286 {
287 putchar (' ');
288 for (i = 20; i < 24; ++i)
289 putchar (longstring[i]);
290 }
291 putchar (' ');
292 }
293
294 void
295 printacol (DISPLAY *dp)
296 {
297 FTSENT *p;
298 int chcnt, col, colwidth;
299 int numcols;
300
301 if ((colwidth = compute_columns (dp, &numcols)) == 0)
302 return;
303
304 if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
305 printf ("total %llu\n", (long long) (howmany (dp->btotal, blocksize)));
306 col = 0;
307 for (p = dp->list; p; p = p->fts_link)
308 {
309 if (IS_NOPRINT (p))
310 continue;
311 if (col >= numcols)
312 {
313 col = 0;
314 putchar ('\n');
315 }
316 chcnt = printaname (p, dp->s_inode, dp->s_block);
317 col++;
318 if (col < numcols)
319 while (chcnt++ < colwidth)
320 putchar (' ');
321 }
322 putchar ('\n');
323 }
324
325 void
326 printstream (DISPLAY *dp)
327 {
328 extern int termwidth;
329 FTSENT *p;
330 int col;
331 int extwidth;
332
333 extwidth = 0;
334 if (f_inode)
335 extwidth += dp->s_inode + 1;
336 if (f_size)
337 extwidth += dp->s_block + 1;
338 if (f_type)
339 extwidth += 1;
340
341 for (col = 0, p = dp->list; p != NULL; p = p->fts_link)
342 {
343 if (IS_NOPRINT (p))
344 continue;
345 if (col > 0)
346 {
347 putchar (','), col++;
348 if (col + 1 + extwidth + p->fts_namelen >= termwidth)
349 putchar ('\n'), col = 0;
350 else
351 putchar (' '), col++;
352 }
353 col += printaname (p, dp->s_inode, dp->s_block);
354 }
355 putchar ('\n');
356 }
357
358 static int
359 printtype (unsigned int mode)
360 {
361 switch (mode & S_IFMT)
362 {
363 case S_IFDIR:
364 putchar ('/');
365 return (1);
366 case S_IFIFO:
367 putchar ('|');
368 return (1);
369 case S_IFLNK:
370 putchar ('@');
371 return (1);
372 case S_IFSOCK:
373 putchar ('=');
374 return (1);
375 }
376 if (mode & (S_IXUSR | S_IXGRP | S_IXOTH))
377 {
378 putchar ('*');
379 return (1);
380 }
381 return (0);
382 }
383
384 static void
385 printlink (FTSENT *p)
386 {
387 int lnklen;
388 #ifndef MAXPATHLEN
389 # define MAXPATHLEN 1024
390 #endif
391 char name[MAXPATHLEN], path[MAXPATHLEN];
392
393 if (p->fts_level == FTS_ROOTLEVEL)
394 snprintf (name, sizeof (name), "%s", p->fts_name);
395 else
396 snprintf (name, sizeof (name),
397 "%s/%s", p->fts_parent->fts_accpath, p->fts_name);
398 if ((lnklen = readlink (name, path, sizeof (path) - 1)) == -1)
399 {
400 fprintf (stderr, "\nls: %s: %s\n", name, strerror (errno));
401 return;
402 }
403 path[lnklen] = '\0';
404 printf (" -> ");
405 putname (path);
406 }