18a16b7a1SPedro F. Giffuni /*-
28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni *
49b50d902SRodney W. Grimes * Copyright (c) 1989, 1993
59b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved.
69b50d902SRodney W. Grimes *
79b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without
89b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions
99b50d902SRodney W. Grimes * are met:
109b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright
119b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer.
129b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright
139b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the
149b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors
169b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software
179b50d902SRodney W. Grimes * without specific prior written permission.
189b50d902SRodney W. Grimes *
199b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
209b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
219b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
229b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
239b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
249b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
259b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
269b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
279b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
289b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
299b50d902SRodney W. Grimes * SUCH DAMAGE.
309b50d902SRodney W. Grimes */
319b50d902SRodney W. Grimes
329b50d902SRodney W. Grimes #include <sys/param.h>
339b50d902SRodney W. Grimes #include <sys/stat.h>
349b50d902SRodney W. Grimes
359b50d902SRodney W. Grimes #include <err.h>
36821df508SXin LI #include <errno.h>
37576541a9SWarner Losh #include <fts.h>
38576541a9SWarner Losh #include <grp.h>
39ef646f18SMark Murray #include <inttypes.h>
40fa2b4915SAndrey A. Chernov #include <langinfo.h>
41576541a9SWarner Losh #include <pwd.h>
429b50d902SRodney W. Grimes #include <stdio.h>
439b50d902SRodney W. Grimes #include <string.h>
449b50d902SRodney W. Grimes #include <time.h>
459b50d902SRodney W. Grimes #include <unistd.h>
469b50d902SRodney W. Grimes
47576541a9SWarner Losh #include "find.h"
48576541a9SWarner Losh
499b50d902SRodney W. Grimes /* Derived from the print routines in the ls(1) source code. */
509b50d902SRodney W. Grimes
51ecca1f1cSMark Murray static void printlink(char *);
52ecca1f1cSMark Murray static void printtime(time_t);
539b50d902SRodney W. Grimes
549b50d902SRodney W. Grimes void
printlong(char * name,char * accpath,struct stat * sb)55ef646f18SMark Murray printlong(char *name, char *accpath, struct stat *sb)
569b50d902SRodney W. Grimes {
57576541a9SWarner Losh char modep[15];
589b50d902SRodney W. Grimes
59b424efd5SMatthew D Fleming (void)printf("%6ju %8"PRId64" ", (uintmax_t)sb->st_ino, sb->st_blocks);
609b50d902SRodney W. Grimes (void)strmode(sb->st_mode, modep);
611c324569SKonstantin Belousov (void)printf("%s %3ju %-*s %-*s ", modep, (uintmax_t)sb->st_nlink,
621c324569SKonstantin Belousov MAXLOGNAME - 1,
636863982aSRobert Drehmel user_from_uid(sb->st_uid, 0), MAXLOGNAME - 1,
649b50d902SRodney W. Grimes group_from_gid(sb->st_gid, 0));
659b50d902SRodney W. Grimes
669b50d902SRodney W. Grimes if (S_ISCHR(sb->st_mode) || S_ISBLK(sb->st_mode))
679f365aa1SEd Schouten (void)printf("%#8jx ", (uintmax_t)sb->st_rdev);
689b50d902SRodney W. Grimes else
695e28140aSDavid E. O'Brien (void)printf("%8"PRId64" ", sb->st_size);
709b50d902SRodney W. Grimes printtime(sb->st_mtime);
719b50d902SRodney W. Grimes (void)printf("%s", name);
729b50d902SRodney W. Grimes if (S_ISLNK(sb->st_mode))
739b50d902SRodney W. Grimes printlink(accpath);
749b50d902SRodney W. Grimes (void)putchar('\n');
759b50d902SRodney W. Grimes }
769b50d902SRodney W. Grimes
779b50d902SRodney W. Grimes static void
printtime(time_t ftime)78ef646f18SMark Murray printtime(time_t ftime)
799b50d902SRodney W. Grimes {
80bf4fce1aSAndrey A. Chernov char longstring[80];
81ef646f18SMark Murray static time_t lnow;
82fa2b4915SAndrey A. Chernov const char *format;
83fa2b4915SAndrey A. Chernov static int d_first = -1;
84*927f8d8bSKirk McKusick struct tm *tm;
859b50d902SRodney W. Grimes
86c3a6ea5bSAlex Richardson #ifdef D_MD_ORDER
87fa2b4915SAndrey A. Chernov if (d_first < 0)
88fa2b4915SAndrey A. Chernov d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
89c3a6ea5bSAlex Richardson #endif
90ef646f18SMark Murray if (lnow == 0)
91ef646f18SMark Murray lnow = time(NULL);
929b50d902SRodney W. Grimes
93656dcd43SGarrett Wollman #define SIXMONTHS ((365 / 2) * 86400)
94ef646f18SMark Murray if (ftime + SIXMONTHS > lnow && ftime < lnow + SIXMONTHS)
95fa2b4915SAndrey A. Chernov /* mmm dd hh:mm || dd mmm hh:mm */
96fa2b4915SAndrey A. Chernov format = d_first ? "%e %b %R " : "%b %e %R ";
97fa2b4915SAndrey A. Chernov else
98fa2b4915SAndrey A. Chernov /* mmm dd yyyy || dd mmm yyyy */
99fa2b4915SAndrey A. Chernov format = d_first ? "%e %b %Y " : "%b %e %Y ";
100*927f8d8bSKirk McKusick if ((tm = localtime(&ftime)) != NULL)
101*927f8d8bSKirk McKusick strftime(longstring, sizeof(longstring), format, tm);
102*927f8d8bSKirk McKusick else
103*927f8d8bSKirk McKusick strlcpy(longstring, "bad date val ", sizeof(longstring));
104fa2b4915SAndrey A. Chernov fputs(longstring, stdout);
1059b50d902SRodney W. Grimes }
1069b50d902SRodney W. Grimes
1079b50d902SRodney W. Grimes static void
printlink(char * name)108ef646f18SMark Murray printlink(char *name)
1099b50d902SRodney W. Grimes {
1109fe7e002SEitan Adler ssize_t lnklen;
111470b24b5SWarner Losh char path[MAXPATHLEN];
1129b50d902SRodney W. Grimes
113c788c9b2SWarner Losh if ((lnklen = readlink(name, path, MAXPATHLEN - 1)) == -1) {
1149b50d902SRodney W. Grimes warn("%s", name);
1159b50d902SRodney W. Grimes return;
1169b50d902SRodney W. Grimes }
1179b50d902SRodney W. Grimes path[lnklen] = '\0';
1189b50d902SRodney W. Grimes (void)printf(" -> %s", path);
1199b50d902SRodney W. Grimes }
120