[BACK]Return to ls.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / find

Annotation of src/usr.bin/find/ls.c, Revision 1.7

1.7     ! deraadt     1: /*     $OpenBSD: ls.c,v 1.6 1998/10/04 01:05:25 millert Exp $  */
1.2       deraadt     2:
1.1       deraadt     3: /*
                      4:  * Copyright (c) 1989, 1993
                      5:  *     The Regents of the University of California.  All rights reserved.
                      6:  *
                      7:  * Redistribution and use in source and binary forms, with or without
                      8:  * modification, are permitted provided that the following conditions
                      9:  * are met:
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. Redistributions in binary form must reproduce the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer in the
                     14:  *    documentation and/or other materials provided with the distribution.
                     15:  * 3. All advertising materials mentioning features or use of this software
                     16:  *    must display the following acknowledgement:
                     17:  *     This product includes software developed by the University of
                     18:  *     California, Berkeley and its contributors.
                     19:  * 4. 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: #ifndef lint
                     37: /*static char sccsid[] = "from: @(#)ls.c       8.1 (Berkeley) 6/6/93";*/
1.7     ! deraadt    38: static char rcsid[] = "$OpenBSD: ls.c,v 1.6 1998/10/04 01:05:25 millert Exp $";
1.1       deraadt    39: #endif /* not lint */
                     40:
                     41: #include <sys/param.h>
                     42: #include <sys/stat.h>
                     43:
                     44: #include <err.h>
                     45: #include <errno.h>
                     46: #include <stdio.h>
                     47: #include <string.h>
                     48: #include <time.h>
                     49: #include <tzfile.h>
                     50: #include <unistd.h>
                     51: #include <utmp.h>
                     52:
                     53: /* Derived from the print routines in the ls(1) source code. */
                     54:
                     55: static void printlink __P((char *));
                     56: static void printtime __P((time_t));
                     57:
1.7     ! deraadt    58: #define NAME_WIDTH     8
        !            59:
1.1       deraadt    60: void
                     61: printlong(name, accpath, sb)
                     62:        char *name;                     /* filename to print */
                     63:        char *accpath;                  /* current valid path to filename */
                     64:        struct stat *sb;                /* stat buffer */
                     65: {
                     66:        char modep[15], *user_from_uid(), *group_from_gid();
                     67:
1.4       millert    68:        (void)printf("%6u %4qd ", sb->st_ino, sb->st_blocks);
1.1       deraadt    69:        (void)strmode(sb->st_mode, modep);
1.7     ! deraadt    70:        (void)printf("%s %3u %-*.*s %-*.*s ", modep, sb->st_nlink,
        !            71:            NAME_WIDTH, UT_NAMESIZE, user_from_uid(sb->st_uid, 0),
        !            72:            NAME_WIDTH, UT_NAMESIZE, group_from_gid(sb->st_gid, 0));
1.1       deraadt    73:
                     74:        if (S_ISCHR(sb->st_mode) || S_ISBLK(sb->st_mode))
                     75:                (void)printf("%3d, %3d ", major(sb->st_rdev),
                     76:                    minor(sb->st_rdev));
                     77:        else
                     78:                (void)printf("%8qd ", sb->st_size);
                     79:        printtime(sb->st_mtime);
                     80:        (void)printf("%s", name);
                     81:        if (S_ISLNK(sb->st_mode))
                     82:                printlink(accpath);
                     83:        (void)putchar('\n');
                     84: }
                     85:
                     86: static void
                     87: printtime(ftime)
                     88:        time_t ftime;
                     89: {
                     90:        int i;
                     91:        char *longstring;
                     92:
                     93:        longstring = ctime(&ftime);
                     94:        for (i = 4; i < 11; ++i)
                     95:                (void)putchar(longstring[i]);
                     96:
                     97: #define        SIXMONTHS       ((DAYSPERNYEAR / 2) * SECSPERDAY)
1.3       kstailey   98:        if (ftime + SIXMONTHS > time(NULL))
1.1       deraadt    99:                for (i = 11; i < 16; ++i)
                    100:                        (void)putchar(longstring[i]);
                    101:        else {
                    102:                (void)putchar(' ');
                    103:                for (i = 20; i < 24; ++i)
                    104:                        (void)putchar(longstring[i]);
                    105:        }
                    106:        (void)putchar(' ');
                    107: }
                    108:
                    109: static void
                    110: printlink(name)
                    111:        char *name;
                    112: {
                    113:        int lnklen;
1.5       deraadt   114:        char path[MAXPATHLEN];
1.1       deraadt   115:
1.6       millert   116:        if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) {
1.1       deraadt   117:                warn("%s", name);
                    118:                return;
                    119:        }
                    120:        path[lnklen] = '\0';
                    121:        (void)printf(" -> %s", path);
                    122: }