1 /* $OpenBSD: print.c,v 1.41 2024/03/27 14:44:52 millert 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 for (p = dp->list; p; p = p->fts_link) { 75 if (IS_NOPRINT(p)) 76 continue; 77 (void)printaname(p, dp->s_inode, dp->s_block); 78 (void)putchar('\n'); 79 } 80 } 81 82 void 83 printlong(DISPLAY *dp) 84 { 85 struct stat *sp; 86 FTSENT *p; 87 NAMES *np; 88 char buf[20]; 89 90 if ((dp->list == NULL || dp->list->fts_level != FTS_ROOTLEVEL) && 91 (f_longform || f_size)) 92 (void)printf("total %llu\n", howmany(dp->btotal, blocksize)); 93 94 for (p = dp->list; p; p = p->fts_link) { 95 if (IS_NOPRINT(p)) 96 continue; 97 sp = p->fts_statp; 98 if (f_inode) 99 (void)printf("%*llu ", dp->s_inode, 100 (unsigned long long)sp->st_ino); 101 if (f_size) 102 (void)printf("%*lld ", dp->s_block, 103 howmany((long long)sp->st_blocks, blocksize)); 104 (void)strmode(sp->st_mode, buf); 105 np = p->fts_pointer; 106 (void)printf("%s %*u ", buf, dp->s_nlink, sp->st_nlink); 107 if (!f_grouponly) 108 (void)printf("%-*s ", dp->s_user, np->user); 109 (void)printf("%-*s ", dp->s_group, np->group); 110 if (f_flags) 111 (void)printf("%-*s ", dp->s_flags, np->flags); 112 if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode)) 113 (void)printf("%*u, %*u ", 114 dp->s_major, major(sp->st_rdev), 115 dp->s_minor, minor(sp->st_rdev)); 116 else 117 printsize(dp->s_size, sp->st_size); 118 if (f_accesstime) 119 printtime(sp->st_atime); 120 else if (f_statustime) 121 printtime(sp->st_ctime); 122 else 123 printtime(sp->st_mtime); 124 (void)mbsprint(p->fts_name, 1); 125 if (f_type || (f_typedir && S_ISDIR(sp->st_mode))) 126 (void)printtype(sp->st_mode); 127 if (S_ISLNK(sp->st_mode)) 128 printlink(p); 129 (void)putchar('\n'); 130 } 131 } 132 133 static int 134 compute_columns(DISPLAY *dp, int *pnum) 135 { 136 int colwidth; 137 extern int termwidth; 138 int mywidth; 139 140 colwidth = dp->maxlen; 141 if (f_inode) 142 colwidth += dp->s_inode + 1; 143 if (f_size) 144 colwidth += dp->s_block + 1; 145 if (f_type || f_typedir) 146 colwidth += 1; 147 148 colwidth += 1; 149 mywidth = termwidth + 1; /* no extra space for last column */ 150 151 if (mywidth < 2 * colwidth) { 152 printscol(dp); 153 return (0); 154 } 155 156 *pnum = mywidth / colwidth; 157 return (mywidth / *pnum); /* spread out if possible */ 158 } 159 160 void 161 printcol(DISPLAY *dp) 162 { 163 static FTSENT **array; 164 static int lastentries = -1; 165 FTSENT *p; 166 int base, chcnt, col, colwidth, num; 167 int numcols, numrows, row; 168 169 if ((colwidth = compute_columns(dp, &numcols)) == 0) 170 return; 171 /* 172 * Have to do random access in the linked list -- build a table 173 * of pointers. 174 */ 175 if (dp->entries > lastentries) { 176 FTSENT **a; 177 178 if ((a = reallocarray(array, dp->entries, sizeof(FTSENT *))) == 179 NULL) { 180 free(array); 181 array = NULL; 182 dp->entries = 0; 183 lastentries = -1; 184 warn(NULL); 185 printscol(dp); 186 return; 187 } 188 lastentries = dp->entries; 189 array = a; 190 } 191 for (p = dp->list, num = 0; p; p = p->fts_link) 192 if (p->fts_number != NO_PRINT) 193 array[num++] = p; 194 195 numrows = num / numcols; 196 if (num % numcols) 197 ++numrows; 198 199 if ((dp->list == NULL || dp->list->fts_level != FTS_ROOTLEVEL) && 200 (f_longform || f_size)) 201 (void)printf("total %llu\n", howmany(dp->btotal, blocksize)); 202 for (row = 0; row < numrows; ++row) { 203 for (base = row, col = 0;;) { 204 chcnt = printaname(array[base], dp->s_inode, dp->s_block); 205 if ((base += numrows) >= num) 206 break; 207 if (++col == numcols) 208 break; 209 while (chcnt++ < colwidth) 210 putchar(' '); 211 } 212 (void)putchar('\n'); 213 } 214 } 215 216 /* 217 * print [inode] [size] name 218 * return # of characters printed, no trailing characters. 219 */ 220 static int 221 printaname(FTSENT *p, int inodefield, int sizefield) 222 { 223 struct stat *sp; 224 int chcnt; 225 226 sp = p->fts_statp; 227 chcnt = 0; 228 if (f_inode) 229 chcnt += printf("%*llu ", inodefield, 230 (unsigned long long)sp->st_ino); 231 if (f_size) 232 chcnt += printf("%*lld ", sizefield, 233 howmany((long long)sp->st_blocks, blocksize)); 234 chcnt += mbsprint(p->fts_name, 1); 235 if (f_type || (f_typedir && S_ISDIR(sp->st_mode))) 236 chcnt += printtype(sp->st_mode); 237 return (chcnt); 238 } 239 240 static void 241 printtime(time_t ftime) 242 { 243 char f_date[DATELEN]; 244 struct tm *tm; 245 static time_t now; 246 static int now_set = 0; 247 248 if (! now_set) { 249 now = time(NULL); 250 now_set = 1; 251 } 252 253 /* 254 * convert time to string, and print 255 */ 256 if ((tm = localtime(&ftime)) == NULL) { 257 /* Invalid time stamp, just display the epoch. */ 258 ftime = 0; 259 tm = localtime(&ftime); 260 } 261 if (strftime(f_date, sizeof(f_date), f_sectime ? "%b %e %H:%M:%S %Y" : 262 (ftime <= now - SIXMONTHS || ftime > now) ? "%b %e %Y" : 263 "%b %e %H:%M", tm) == 0) 264 f_date[0] = '\0'; 265 266 printf("%s ", f_date); 267 } 268 269 void 270 printacol(DISPLAY *dp) 271 { 272 FTSENT *p; 273 int chcnt, col, colwidth; 274 int numcols; 275 276 if ( (colwidth = compute_columns(dp, &numcols)) == 0) 277 return; 278 279 if ((dp->list == NULL || dp->list->fts_level != FTS_ROOTLEVEL) && 280 (f_longform || f_size)) 281 (void)printf("total %llu\n", howmany(dp->btotal, blocksize)); 282 col = 0; 283 for (p = dp->list; p; p = p->fts_link) { 284 if (IS_NOPRINT(p)) 285 continue; 286 if (col >= numcols) { 287 col = 0; 288 (void)putchar('\n'); 289 } 290 chcnt = printaname(p, dp->s_inode, dp->s_block); 291 col++; 292 if (col < numcols) 293 while (chcnt++ < colwidth) 294 (void)putchar(' '); 295 } 296 (void)putchar('\n'); 297 } 298 299 void 300 printstream(DISPLAY *dp) 301 { 302 extern int termwidth; 303 FTSENT *p; 304 int col; 305 int extwidth; 306 307 extwidth = 0; 308 if (f_inode) 309 extwidth += dp->s_inode + 1; 310 if (f_size) 311 extwidth += dp->s_block + 1; 312 if (f_type) 313 extwidth += 1; 314 315 for (col = 0, p = dp->list; p != NULL; p = p->fts_link) { 316 if (IS_NOPRINT(p)) 317 continue; 318 if (col > 0) { 319 (void)putchar(','), col++; 320 if (col + 1 + extwidth + mbsprint(p->fts_name, 0) >= 321 termwidth) 322 (void)putchar('\n'), col = 0; 323 else 324 (void)putchar(' '), col++; 325 } 326 col += printaname(p, dp->s_inode, dp->s_block); 327 } 328 (void)putchar('\n'); 329 } 330 331 static int 332 printtype(mode_t mode) 333 { 334 switch (mode & S_IFMT) { 335 case S_IFDIR: 336 (void)putchar('/'); 337 return (1); 338 case S_IFIFO: 339 (void)putchar('|'); 340 return (1); 341 case S_IFLNK: 342 (void)putchar('@'); 343 return (1); 344 case S_IFSOCK: 345 (void)putchar('='); 346 return (1); 347 } 348 if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) { 349 (void)putchar('*'); 350 return (1); 351 } 352 return (0); 353 } 354 355 static void 356 printlink(FTSENT *p) 357 { 358 int lnklen; 359 char name[PATH_MAX], path[PATH_MAX]; 360 361 if (p->fts_level == FTS_ROOTLEVEL) 362 (void)snprintf(name, sizeof(name), "%s", p->fts_name); 363 else 364 (void)snprintf(name, sizeof(name), 365 "%s/%s", p->fts_parent->fts_accpath, p->fts_name); 366 if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) { 367 (void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno)); 368 return; 369 } 370 path[lnklen] = '\0'; 371 (void)printf(" -> "); 372 (void)mbsprint(path, 1); 373 } 374 375 static void 376 printsize(int width, off_t bytes) 377 { 378 char ret[FMT_SCALED_STRSIZE]; 379 380 if ((f_humanval) && (fmt_scaled(bytes, ret) != -1)) { 381 (void)printf("%*s ", width, ret); 382 return; 383 } 384 (void)printf("%*lld ", width, (long long)bytes); 385 } 386