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