OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [uclinux/] [userland/] [sash/] [ls.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 199 simons
/*
2
 * Copyright (c) 1993 by David I. Bell
3
 * Permission is granted to use, distribute, or modify this source,
4
 * provided that this copyright notice remains intact.
5
 *
6
 * The "ls" built-in command.
7
 */
8
 
9
#include "sash.h"
10
 
11
#include <sys/types.h>
12
#include <sys/stat.h>
13
#include <dirent.h>
14
#include <pwd.h>
15
#include <grp.h>
16
#include <time.h>
17
 
18
#define LISTSIZE        256
19
 
20
#define COLWIDTH        20
21
 
22
#ifdef  S_ISLNK
23
#define LSTAT   lstat
24
#else
25
#define LSTAT   stat
26
#endif
27
 
28
 
29
/*
30
 * Flags for the LS command.
31
 */
32
#define LSF_LONG        0x01
33
#define LSF_DIR         0x02
34
#define LSF_INODE       0x04
35
#define LSF_MULT        0x08
36
#define LSF_ALL         0x10
37
#define LSF_COMPACT     0x20
38
 
39
 
40
static  char    **list;
41
static  int     listsize;
42
static  int     listused;
43
static  int     linelen = 0;
44
 
45
 
46
static  void    lsfile();
47
 
48
 
49
void
50
do_ls(argc, argv)
51
        int argc;
52
        char    **argv;
53
{
54
        char            *cp;
55
        char            *name;
56
        int             flags;
57
        int             i;
58
        DIR             *dirp;
59
        BOOL            endslash;
60
        char            **newlist;
61
        struct  dirent  *dp;
62
        char            fullname[PATHLEN];
63
        struct  stat    statbuf;
64
        static          char *def[2];
65
 
66
        if (listsize == 0) {
67
                list = (char **) malloc(LISTSIZE * sizeof(char *));
68
                if (list == NULL) {
69
                        fprintf(stderr, "No memory for ls buffer\n");
70
                        return;
71
                }
72
                listsize = LISTSIZE;
73
        }
74
        listused = 0;
75
 
76
        flags = 0;
77
        if ((argc > 1) && (argv[1][0] == '-'))
78
        {
79
                argc--;
80
                cp = *(++argv) + 1;
81
 
82
                while (*cp) switch (*cp++) {
83
                        case 'l':       flags |= LSF_LONG; break;
84
                        case 'd':       flags |= LSF_DIR; break;
85
                        case 'i':       flags |= LSF_INODE; break;
86
                        case 'a':       flags |= LSF_ALL; break;
87
                        case 'C':       flags |= LSF_COMPACT; break;
88
                        default:
89
                                fprintf(stderr, "Unknown option -%c\n", cp[-1]);
90
                                return;
91
                }
92
        }
93
 
94
        if ((flags & LSF_COMPACT) && (flags & ~LSF_COMPACT)) {
95
                fprintf(stderr, "Cannot do compact list with other options\n");
96
                return;
97
        }
98
 
99
        if (argc <= 1) {
100
                argc = 2;
101
                argv = def;
102
                argv[0] = "ls";
103
                argv[1] = ".";
104
        }
105
 
106
        if (argc > 2)
107
                flags |= LSF_MULT;
108
 
109
        while (argc-- > 1) {
110
                name = *(++argv);
111
                endslash = (*name && (name[strlen(name) - 1] == '/'));
112
 
113
                if (LSTAT(name, &statbuf) < 0) {
114
                        perror(name);
115
                        continue;
116
                }
117
 
118
                if ((flags & LSF_DIR) || (!S_ISDIR(statbuf.st_mode))) {
119
                        lsfile(name, &statbuf, flags);
120
                        continue;
121
                }
122
 
123
                /*
124
                 * Do all the files in a directory.
125
                 */
126
                dirp = opendir(name);
127
                if (dirp == NULL) {
128
                        perror(name);
129
                        continue;
130
                }
131
 
132
                if (flags & LSF_MULT)
133
                        printf("\n%s:\n", name);
134
 
135
                while ((dp = readdir(dirp)) != NULL) {
136
 
137
                        if ((dp->d_name[0] == '.') && !(flags & LSF_ALL))
138
                                continue;
139
 
140
                        fullname[0] = '\0';
141
 
142
                        if ((*name != '.') || (name[1] != '\0')) {
143
                                strcpy(fullname, name);
144
                                if (!endslash)
145
                                        strcat(fullname, "/");
146
                        }
147
 
148
                        strcat(fullname, dp->d_name);
149
 
150
                        if (listused >= listsize) {
151
                                newlist = malloc((sizeof(char **)) * (listsize + LISTSIZE));
152
                                if (newlist == NULL) {
153
                                        fprintf(stderr, "No memory for ls buffer\n");
154
                                        break;
155
                                }
156
                                memcpy(newlist, list, sizeof(char**) * listsize);
157
                                free(list);
158
                                listsize += LISTSIZE;
159
                        }
160
 
161
                        list[listused] = strdup(fullname);
162
                        if (list[listused] == NULL) {
163
                                fprintf(stderr, "No memory for filenames\n");
164
                                break;
165
                        }
166
                        listused++;
167
                }
168
 
169
                closedir(dirp);
170
 
171
                /*
172
                 * Sort the files.
173
                 */
174
                qsort((char *) list, listused, sizeof(char *), namesort);
175
 
176
                /*
177
                 * Now finally list the filenames.
178
                 */
179
                for (i = 0; i < listused; i++) {
180
                        name = list[i];
181
 
182
                        if (LSTAT(name, &statbuf) < 0) {
183
                                perror(name);
184
                                free(name);
185
                                continue;
186
                        }
187
 
188
                        cp = strrchr(name, '/');
189
                        if (cp)
190
                                cp++;
191
                        else
192
                                cp = name;
193
 
194
                        lsfile(cp, &statbuf, flags);
195
 
196
                        free(name);
197
                }
198
 
199
                listused = 0;
200
        }
201
 
202
        if (linelen)
203
                fputc('\n', stdout);
204
}
205
 
206
 
207
/*
208
 * Do an LS of a particular file name according to the flags.
209
 */
210
static void
211
lsfile(name, statbuf, flags)
212
        char    *name;
213
        struct  stat    *statbuf;
214
{
215
        char            *cp;
216
        struct  passwd  *pwd;
217
        struct  group   *grp;
218
        int             len;
219
        char            buf[PATHLEN];
220
        static  char    username[12];
221
        static  int     userid;
222
        static  BOOL    useridknown;
223
        static  char    groupname[12];
224
        static  int     groupid;
225
        static  BOOL    groupidknown;
226
 
227
        cp = buf;
228
        *cp = '\0';
229
 
230
        if (flags & LSF_INODE) {
231
                sprintf(cp, "%5d ", statbuf->st_ino);
232
                cp += strlen(cp);
233
        }
234
 
235
        if (flags & LSF_LONG) {
236
                strcpy(cp, modestring(statbuf->st_mode));
237
                cp += strlen(cp);
238
 
239
                sprintf(cp, "%3d ", statbuf->st_nlink);
240
                cp += strlen(cp);
241
 
242
                if (!useridknown || (statbuf->st_uid != userid)) {
243
                        /*pwd = getpwuid(statbuf->st_uid);
244
                        if (pwd)
245
                                strcpy(username, pwd->pw_name);
246
                        else*/
247
                                sprintf(username, "%d", statbuf->st_uid);
248
                        userid = statbuf->st_uid;
249
                        useridknown = TRUE;
250
                }
251
 
252
                sprintf(cp, "%-8s ", username);
253
                cp += strlen(cp);
254
 
255
                if (!groupidknown || (statbuf->st_gid != groupid)) {
256
                        /*grp = getgrgid(statbuf->st_gid);
257
                        if (grp)
258
                                strcpy(groupname, grp->gr_name);
259
                        else*/
260
                                sprintf(groupname, "%d", statbuf->st_gid);
261
                        groupid = statbuf->st_gid;
262
                        groupidknown = TRUE;
263
                }
264
 
265
                sprintf(cp, "%-8s ", groupname);
266
                cp += strlen(cp);
267
 
268
                if (S_ISBLK(statbuf->st_mode) || S_ISCHR(statbuf->st_mode))
269
                        sprintf(cp, "%3d, %3d ", statbuf->st_rdev >> 8,
270
                                statbuf->st_rdev & 0xff);
271
                else
272
                        sprintf(cp, "%8d ", statbuf->st_size);
273
                cp += strlen(cp);
274
 
275
                sprintf(cp, " %-12s ", timestring(statbuf->st_mtime));
276
        }
277
 
278
        fputs(buf, stdout);
279
        fputs(name, stdout);
280
 
281
#ifdef  S_ISLNK
282
        if ((flags & LSF_LONG) && S_ISLNK(statbuf->st_mode)) {
283
                len = readlink(name, buf, PATHLEN - 1);
284
                if (len >= 0) {
285
                        buf[len] = '\0';
286
                        printf(" -> %s", buf);
287
                }
288
        }
289
#endif
290
 
291
        if (flags & LSF_COMPACT) {
292
                len = strlen(name);
293
                if (len < COLWIDTH) {
294
                        for (; (len < COLWIDTH); len++)
295
                                fputc(' ', stdout);
296
                        linelen += COLWIDTH;
297
                } else {
298
                        linelen = 80;
299
                }
300
 
301
                if (linelen >= 80) {
302
                        fputc('\n', stdout);
303
                        linelen = 0;
304
                }
305
        } else {
306
                fputc('\n', stdout);
307
        }
308
}
309
 
310
/* END CODE */

powered by: WebSVN 2.1.0

© copyright 1999-2025 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.