[BACK]Return to ls.c CVS log [TXT][DIR] Up to [local] / prex-old / usr / bin / ls

Annotation of prex-old/usr/bin/ls/ls.c, Revision 1.1

1.1     ! nbrk        1: /*
        !             2:  * Copyright (c) 2005-2006, Kohsuke Ohtani
        !             3:  * All rights reserved.
        !             4:  *
        !             5:  * Redistribution and use in source and binary forms, with or without
        !             6:  * modification, are permitted provided that the following conditions
        !             7:  * are met:
        !             8:  * 1. Redistributions of source code must retain the above copyright
        !             9:  *    notice, this list of conditions and the following disclaimer.
        !            10:  * 2. Redistributions in binary form must reproduce the above copyright
        !            11:  *    notice, this list of conditions and the following disclaimer in the
        !            12:  *    documentation and/or other materials provided with the distribution.
        !            13:  * 3. Neither the name of the author nor the names of any co-contributors
        !            14:  *    may be used to endorse or promote products derived from this software
        !            15:  *    without specific prior written permission.
        !            16:  *
        !            17:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
        !            18:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
        !            19:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
        !            20:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
        !            21:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
        !            22:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
        !            23:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
        !            24:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
        !            25:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
        !            26:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
        !            27:  * SUCH DAMAGE.
        !            28:  */
        !            29:
        !            30: #include <sys/stat.h>
        !            31:
        !            32: #include <unistd.h>
        !            33: #include <err.h>
        !            34: #include <string.h>
        !            35: #include <stdlib.h>
        !            36: #include <dirent.h>
        !            37: #include <limits.h>
        !            38: #include <stdio.h>
        !            39: #include <errno.h>
        !            40:
        !            41: #ifdef CMDBOX
        !            42: #define main(argc, argv)       ls_main(argc, argv)
        !            43: #endif
        !            44:
        !            45: static void print_entry(char *name, struct stat *sp);
        !            46: static int do_ls(char *path);
        !            47:
        !            48: /* Flags */
        !            49: #define LSF_DOT                0x01    /* List files begining with . */
        !            50: #define LSF_LONG       0x02    /* Long format */
        !            51: #define LSF_SINGLE     0x04    /* Single column */
        !            52: #define LSF_TYPE       0x08    /* Add /(dir) and @(symlink) with file name */
        !            53: #define LSF_ALL                0x10    /* List hidden files */
        !            54:
        !            55: #define LSF_RECURSIVE  0x20    /* List Subdirectory */
        !            56: #define LSF_TIMESORT   0x40    /* Sort by time */
        !            57:
        !            58: static unsigned int ls_flags;
        !            59:
        !            60: int
        !            61: main(int argc, char *argv[])
        !            62: {
        !            63:        int ch, rc;
        !            64:
        !            65:        ls_flags = 0;
        !            66:
        !            67:        while ((ch = getopt(argc, argv, "1ClFaA")) != -1) {
        !            68:                switch(ch) {
        !            69:                case '1':
        !            70:                        ls_flags |= LSF_SINGLE;
        !            71:                        ls_flags &= ~LSF_LONG;
        !            72:                        break;
        !            73:                case 'C':
        !            74:                        ls_flags &= ~LSF_SINGLE;
        !            75:                        ls_flags &= ~LSF_LONG;
        !            76:                        break;
        !            77:                case 'l':
        !            78:                        ls_flags |= LSF_LONG;
        !            79:                        ls_flags &= ~LSF_SINGLE;
        !            80:                        break;
        !            81:                case 'F':
        !            82:                        ls_flags |= LSF_TYPE;
        !            83:                        break;
        !            84:                case 'a':
        !            85:                        ls_flags |= LSF_DOT;
        !            86:                        /* FALLTHROUGH */
        !            87:                case 'A':
        !            88:                        ls_flags |= LSF_ALL;
        !            89:                        break;
        !            90:                default:
        !            91:                case '?':
        !            92:                        fprintf(stderr, "usage: ls [-1CFAal] [file ...]\n");
        !            93:                        exit(1);
        !            94:                }
        !            95:        }
        !            96:        argc -= optind;
        !            97:        argv += optind;
        !            98:
        !            99:        if (argc == 0)
        !           100:                rc = do_ls(".");
        !           101:        else {
        !           102:                do {
        !           103:                        rc = do_ls(*argv);
        !           104:                        ++argv;
        !           105:                } while (*argv);
        !           106:        }
        !           107:        if (rc)
        !           108:                err(1, NULL);
        !           109:        return 0;
        !           110: }
        !           111:
        !           112: static void
        !           113: print_entry(char *name, struct stat *sp)
        !           114: {
        !           115:        int color;
        !           116:        int dot = 0;
        !           117:
        !           118:        if (name[0] == '.') {
        !           119:                if ((ls_flags & LSF_DOT) == 0)
        !           120:                        return;
        !           121:                dot = 1;
        !           122:        }
        !           123:
        !           124:        /* set color */
        !           125:        color = 0;
        !           126:        if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode))
        !           127:                color = 35;  /* magenta */
        !           128:        else if (S_ISDIR(sp->st_mode))
        !           129:                color = 36;  /* cyan */
        !           130:        else if (S_ISLNK(sp->st_mode))
        !           131:                color = 33;  /* yellow */
        !           132:
        !           133:        if (ls_flags & LSF_LONG) {
        !           134:                /* print mode */
        !           135:                if (S_ISDIR(sp->st_mode))
        !           136:                        putchar('d');
        !           137:                else if (S_ISLNK(sp->st_mode))
        !           138:                        putchar('|');
        !           139:                else
        !           140:                        putchar('-');
        !           141:
        !           142:                printf("rw");
        !           143:                if (sp->st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))
        !           144:                        putchar('x');
        !           145:                else
        !           146:                        putchar('-');
        !           147:                printf("------");
        !           148:
        !           149:                /* print link */
        !           150:                printf("  1 ");
        !           151:
        !           152:                /* print owner */
        !           153:                printf("prex   ");
        !           154:
        !           155:                /* print date/time */
        !           156:                printf("%s 12:00 ", __DATE__);
        !           157:
        !           158:                /* print size */
        !           159:                printf("%7d ", (int)sp->st_size);
        !           160:        }
        !           161:
        !           162:        /* print name */
        !           163:        printf("\033[%dm", color);
        !           164:        printf("%s", name);
        !           165:
        !           166:        /* print type */
        !           167:        if (!dot && (ls_flags & LSF_TYPE)) {
        !           168:                switch (sp->st_mode & S_IFMT) {
        !           169:                case S_IFDIR:
        !           170:                        putchar('/');
        !           171:                        break;
        !           172:                case S_IFIFO:
        !           173:                        putchar('|');
        !           174:                        break;
        !           175:                case S_IFLNK:
        !           176:                        putchar('@');
        !           177:                        break;
        !           178:                case S_IFSOCK:
        !           179:                        putchar('=');
        !           180:                        break;
        !           181:                case S_IFWHT:
        !           182:                        putchar('%');
        !           183:                        break;
        !           184:                }
        !           185:                if (sp->st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))
        !           186:                        putchar('*');
        !           187:        }
        !           188:        printf("\033[0m");
        !           189:        if (ls_flags & LSF_LONG || ls_flags & LSF_SINGLE) {
        !           190:                putchar('\n');
        !           191:        } else {
        !           192:                putchar(' ');
        !           193:        }
        !           194: }
        !           195:
        !           196: static int
        !           197: do_ls(char *path)
        !           198: {
        !           199:        struct stat st;
        !           200:        int nr_file = 0;
        !           201:        char buf[PATH_MAX];
        !           202:        DIR *dir;
        !           203:        struct dirent *entry;
        !           204:
        !           205:        if (stat(path, &st) == -1)
        !           206:                return ENOTDIR;
        !           207:
        !           208:        if (S_ISDIR(st.st_mode)) {
        !           209:
        !           210:                dir = opendir(path);
        !           211:                if (dir == NULL)
        !           212:                        return ENOTDIR;
        !           213:
        !           214:                for (;;) {
        !           215:                        entry = readdir(dir);
        !           216:                        if (entry == NULL)
        !           217:                                break;
        !           218:                        buf[0] = 0;
        !           219:                        strlcpy(buf, path, sizeof(buf));
        !           220:                        buf[sizeof(buf) - 1] = '\0';
        !           221:                        if (!strcmp(entry->d_name, "."))
        !           222:                                ;       /* Do nothing */
        !           223:                        else if (!strcmp(entry->d_name, ".."))
        !           224:                                ;       /* Do nothing */
        !           225:                        else {
        !           226:                                strlcat(buf, "/", sizeof(buf));
        !           227:                                strlcat(buf, entry->d_name, sizeof(buf));
        !           228:                        }
        !           229:                        if (stat(buf, &st) == -1)
        !           230:                                break;
        !           231:                        print_entry(entry->d_name, &st);
        !           232:                        nr_file++;
        !           233:                }
        !           234:                closedir(dir);
        !           235:                if (ls_flags & LSF_LONG)
        !           236:                        printf("total %d\n", nr_file);
        !           237:                else
        !           238:                        putchar('\n');
        !           239:        } else {
        !           240:                print_entry(path, &st);
        !           241:        }
        !           242:        return 0;
        !           243: }

CVSweb