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 * 4. Neither the name of the University nor the names of its contributors
141590Srgrimes *    may be used to endorse or promote products derived from this software
151590Srgrimes *    without specific prior written permission.
161590Srgrimes *
171590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271590Srgrimes * SUCH DAMAGE.
281590Srgrimes */
291590Srgrimes
30207705Sdelphij#ifndef lint
31207705Sdelphij#if 0
32207705Sdelphijstatic char sccsid[] = "@(#)ls.c	8.1 (Berkeley) 6/6/93";
33207705Sdelphij#endif
34207705Sdelphij#endif /* not lint */
35207705Sdelphij
3693604Sobrien#include <sys/cdefs.h>
3793604Sobrien__FBSDID("$FreeBSD$");
381590Srgrimes
391590Srgrimes#include <sys/param.h>
401590Srgrimes#include <sys/stat.h>
411590Srgrimes
421590Srgrimes#include <err.h>
43200462Sdelphij#include <errno.h>
4490878Simp#include <fts.h>
4590878Simp#include <grp.h>
46116333Smarkm#include <inttypes.h>
4774584Sache#include <langinfo.h>
4890878Simp#include <pwd.h>
491590Srgrimes#include <stdio.h>
501590Srgrimes#include <string.h>
511590Srgrimes#include <time.h>
521590Srgrimes#include <unistd.h>
531590Srgrimes
5490878Simp#include "find.h"
5590878Simp
561590Srgrimes/* Derived from the print routines in the ls(1) source code. */
571590Srgrimes
5892786Smarkmstatic void printlink(char *);
5992786Smarkmstatic void printtime(time_t);
601590Srgrimes
611590Srgrimesvoid
62116333Smarkmprintlong(char *name, char *accpath, struct stat *sb)
631590Srgrimes{
6490878Simp	char modep[15];
651590Srgrimes
66241015Smdf	(void)printf("%6ju %8"PRId64" ", (uintmax_t)sb->st_ino, sb->st_blocks);
671590Srgrimes	(void)strmode(sb->st_mode, modep);
68100495Srobert	(void)printf("%s %3u %-*s %-*s ", modep, sb->st_nlink, MAXLOGNAME - 1,
69100495Srobert	    user_from_uid(sb->st_uid, 0), MAXLOGNAME - 1,
701590Srgrimes	    group_from_gid(sb->st_gid, 0));
711590Srgrimes
721590Srgrimes	if (S_ISCHR(sb->st_mode) || S_ISBLK(sb->st_mode))
73225847Sed		(void)printf("%#8jx ", (uintmax_t)sb->st_rdev);
741590Srgrimes	else
7594792Sobrien		(void)printf("%8"PRId64" ", sb->st_size);
761590Srgrimes	printtime(sb->st_mtime);
771590Srgrimes	(void)printf("%s", name);
781590Srgrimes	if (S_ISLNK(sb->st_mode))
791590Srgrimes		printlink(accpath);
801590Srgrimes	(void)putchar('\n');
811590Srgrimes}
821590Srgrimes
831590Srgrimesstatic void
84116333Smarkmprinttime(time_t ftime)
851590Srgrimes{
8617534Sache	char longstring[80];
87116333Smarkm	static time_t lnow;
8874584Sache	const char *format;
8974584Sache	static int d_first = -1;
901590Srgrimes
9174584Sache	if (d_first < 0)
9274584Sache		d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
93116333Smarkm	if (lnow == 0)
94116333Smarkm		lnow = time(NULL);
951590Srgrimes
969987Swollman#define	SIXMONTHS	((365 / 2) * 86400)
97116333Smarkm	if (ftime + SIXMONTHS > lnow && ftime < lnow + SIXMONTHS)
9874584Sache		/* mmm dd hh:mm || dd mmm hh:mm */
9974584Sache		format = d_first ? "%e %b %R " : "%b %e %R ";
10074584Sache	else
10174584Sache		/* mmm dd  yyyy || dd mmm  yyyy */
10274584Sache		format = d_first ? "%e %b  %Y " : "%b %e  %Y ";
10374584Sache	strftime(longstring, sizeof(longstring), format, localtime(&ftime));
10474584Sache	fputs(longstring, stdout);
1051590Srgrimes}
1061590Srgrimes
1071590Srgrimesstatic void
108116333Smarkmprintlink(char *name)
1091590Srgrimes{
1101590Srgrimes	int lnklen;
11173258Simp	char path[MAXPATHLEN];
1121590Srgrimes
11336787Simp	if ((lnklen = readlink(name, path, MAXPATHLEN - 1)) == -1) {
1141590Srgrimes		warn("%s", name);
1151590Srgrimes		return;
1161590Srgrimes	}
1171590Srgrimes	path[lnklen] = '\0';
1181590Srgrimes	(void)printf(" -> %s", path);
1191590Srgrimes}
120