ls.c revision 17534
11590Srgrimes/* 21590Srgrimes * Copyright (c) 1989, 1993 31590Srgrimes * The Regents of the University of California. All rights reserved. 41590Srgrimes * 51590Srgrimes * Redistribution and use in source and binary forms, with or without 61590Srgrimes * modification, are permitted provided that the following conditions 71590Srgrimes * are met: 81590Srgrimes * 1. Redistributions of source code must retain the above copyright 91590Srgrimes * notice, this list of conditions and the following disclaimer. 101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111590Srgrimes * notice, this list of conditions and the following disclaimer in the 121590Srgrimes * documentation and/or other materials provided with the distribution. 131590Srgrimes * 3. All advertising materials mentioning features or use of this software 141590Srgrimes * must display the following acknowledgement: 151590Srgrimes * This product includes software developed by the University of 161590Srgrimes * California, Berkeley and its contributors. 171590Srgrimes * 4. Neither the name of the University nor the names of its contributors 181590Srgrimes * may be used to endorse or promote products derived from this software 191590Srgrimes * without specific prior written permission. 201590Srgrimes * 211590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 221590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 231590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241590Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 251590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 271590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311590Srgrimes * SUCH DAMAGE. 321590Srgrimes */ 331590Srgrimes 341590Srgrimes#ifndef lint 351590Srgrimesstatic char sccsid[] = "@(#)ls.c 8.1 (Berkeley) 6/6/93"; 361590Srgrimes#endif /* not lint */ 3787628Sdwmalone 3887628Sdwmalone#include <sys/param.h> 3987628Sdwmalone#include <sys/stat.h> 4087628Sdwmalone 4187628Sdwmalone#include <err.h> 4287628Sdwmalone#include <errno.h> 4387231Smarkm#include <stdio.h> 4487231Smarkm#include <string.h> 4587231Smarkm#include <time.h> 4667189Sbrian#include <unistd.h> 471590Srgrimes#include <utmp.h> 481590Srgrimes 491590Srgrimes/* Derived from the print routines in the ls(1) source code. */ 501590Srgrimes 511590Srgrimesstatic void printlink __P((char *)); 521590Srgrimesstatic void printtime __P((time_t)); 531590Srgrimes 541590Srgrimesvoid 551590Srgrimesprintlong(name, accpath, sb) 561590Srgrimes char *name; /* filename to print */ 571590Srgrimes char *accpath; /* current valid path to filename */ 5871326Swill struct stat *sb; /* stat buffer */ 591590Srgrimes{ 6071326Swill char modep[15], *user_from_uid(), *group_from_gid(); 6171326Swill 6270692Swill (void)printf("%6lu %4qd ", sb->st_ino, sb->st_blocks); 631590Srgrimes (void)strmode(sb->st_mode, modep); 6471326Swill (void)printf("%s %3u %-*s %-*s ", modep, sb->st_nlink, UT_NAMESIZE, 6571326Swill user_from_uid(sb->st_uid, 0), UT_NAMESIZE, 6671326Swill group_from_gid(sb->st_gid, 0)); 6771326Swill 681590Srgrimes if (S_ISCHR(sb->st_mode) || S_ISBLK(sb->st_mode)) 691590Srgrimes (void)printf("%3d, %3d ", major(sb->st_rdev), 701590Srgrimes minor(sb->st_rdev)); 711590Srgrimes else 7270672Swill (void)printf("%8qd ", sb->st_size); 731590Srgrimes printtime(sb->st_mtime); 741590Srgrimes (void)printf("%s", name); 751590Srgrimes if (S_ISLNK(sb->st_mode)) 761590Srgrimes printlink(accpath); 7754157Scharnier (void)putchar('\n'); 781590Srgrimes} 791590Srgrimes 801590Srgrimesstatic void 811590Srgrimesprinttime(ftime) 821590Srgrimes time_t ftime; 831590Srgrimes{ 841590Srgrimes int i; 851590Srgrimes char longstring[80]; 861590Srgrimes 8754157Scharnier strftime(longstring, sizeof(longstring), "%c", localtime(&ftime)); 881590Srgrimes for (i = 4; i < 11; ++i) 891590Srgrimes (void)putchar(longstring[i]); 901590Srgrimes 911590Srgrimes#define SIXMONTHS ((365 / 2) * 86400) 921590Srgrimes if (ftime + SIXMONTHS > time((time_t *)NULL)) 931590Srgrimes for (i = 11; i < 16; ++i) 941590Srgrimes (void)putchar(longstring[i]); 951590Srgrimes else { 961590Srgrimes (void)putchar(' '); 971590Srgrimes for (i = 20; i < 24; ++i) 981590Srgrimes (void)putchar(longstring[i]); 991590Srgrimes } 1001590Srgrimes (void)putchar(' '); 1011590Srgrimes} 1021590Srgrimes 1031590Srgrimesstatic void 1041590Srgrimesprintlink(name) 1051590Srgrimes char *name; 1061590Srgrimes{ 1071590Srgrimes int lnklen; 1081590Srgrimes char path[MAXPATHLEN + 1]; 1091590Srgrimes 1101590Srgrimes if ((lnklen = readlink(name, path, MAXPATHLEN)) == -1) { 1111590Srgrimes warn("%s", name); 11270692Swill return; 11370692Swill } 11470692Swill path[lnklen] = '\0'; 11570692Swill (void)printf(" -> %s", path); 11670692Swill} 11770692Swill