1331722Seadler/*
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: stable/11/usr.bin/find/ls.c 312072 2017-01-13 13:37:09Z kib $");
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);
68312072Skib	(void)printf("%s %3ju %-*s %-*s ", modep, (uintmax_t)sb->st_nlink,
69312072Skib	    MAXLOGNAME - 1,
70100495Srobert	    user_from_uid(sb->st_uid, 0), MAXLOGNAME - 1,
711590Srgrimes	    group_from_gid(sb->st_gid, 0));
721590Srgrimes
731590Srgrimes	if (S_ISCHR(sb->st_mode) || S_ISBLK(sb->st_mode))
74225847Sed		(void)printf("%#8jx ", (uintmax_t)sb->st_rdev);
751590Srgrimes	else
7694792Sobrien		(void)printf("%8"PRId64" ", sb->st_size);
771590Srgrimes	printtime(sb->st_mtime);
781590Srgrimes	(void)printf("%s", name);
791590Srgrimes	if (S_ISLNK(sb->st_mode))
801590Srgrimes		printlink(accpath);
811590Srgrimes	(void)putchar('\n');
821590Srgrimes}
831590Srgrimes
841590Srgrimesstatic void
85116333Smarkmprinttime(time_t ftime)
861590Srgrimes{
8717534Sache	char longstring[80];
88116333Smarkm	static time_t lnow;
8974584Sache	const char *format;
9074584Sache	static int d_first = -1;
911590Srgrimes
9274584Sache	if (d_first < 0)
9374584Sache		d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
94116333Smarkm	if (lnow == 0)
95116333Smarkm		lnow = time(NULL);
961590Srgrimes
979987Swollman#define	SIXMONTHS	((365 / 2) * 86400)
98116333Smarkm	if (ftime + SIXMONTHS > lnow && ftime < lnow + SIXMONTHS)
9974584Sache		/* mmm dd hh:mm || dd mmm hh:mm */
10074584Sache		format = d_first ? "%e %b %R " : "%b %e %R ";
10174584Sache	else
10274584Sache		/* mmm dd  yyyy || dd mmm  yyyy */
10374584Sache		format = d_first ? "%e %b  %Y " : "%b %e  %Y ";
10474584Sache	strftime(longstring, sizeof(longstring), format, localtime(&ftime));
10574584Sache	fputs(longstring, stdout);
1061590Srgrimes}
1071590Srgrimes
1081590Srgrimesstatic void
109116333Smarkmprintlink(char *name)
1101590Srgrimes{
1111590Srgrimes	int lnklen;
11273258Simp	char path[MAXPATHLEN];
1131590Srgrimes
11436787Simp	if ((lnklen = readlink(name, path, MAXPATHLEN - 1)) == -1) {
1151590Srgrimes		warn("%s", name);
1161590Srgrimes		return;
1171590Srgrimes	}
1181590Srgrimes	path[lnklen] = '\0';
1191590Srgrimes	(void)printf(" -> %s", path);
1201590Srgrimes}
121