Subversion Repositories HelenOS

Rev

Rev 3425 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3425 Rev 4377
Line 48... Line 48...
48
#include "ls.h"
48
#include "ls.h"
49
#include "cmds.h"
49
#include "cmds.h"
50
 
50
 
51
static char *cmdname = "ls";
51
static char *cmdname = "ls";
52
 
52
 
-
 
53
static inline off_t flen(const char *f)
-
 
54
{
-
 
55
    int fd;
-
 
56
    off_t size;
-
 
57
 
-
 
58
    fd = open(f, O_RDONLY);
-
 
59
    if (fd == -1)
-
 
60
        return 0;
-
 
61
 
-
 
62
    size = lseek(fd, 0, SEEK_END);
-
 
63
    close(fd);
-
 
64
 
-
 
65
    if (size < 0)
-
 
66
        size = 0;
-
 
67
 
-
 
68
    return size;
-
 
69
}
-
 
70
 
53
static unsigned int ls_scope(const char *path)
71
static unsigned int ls_scope(const char *path)
54
{
72
{
55
    int fd;
73
    int fd;
56
    DIR *dirp;
74
    DIR *dirp;
57
 
75
 
Line 94... Line 112...
94
        switch (scope) {
112
        switch (scope) {
95
        case LS_DIR:
113
        case LS_DIR:
96
            ls_print_dir(dp->d_name);
114
            ls_print_dir(dp->d_name);
97
            break;
115
            break;
98
        case LS_FILE:
116
        case LS_FILE:
99
            ls_print_file(dp->d_name);
117
            ls_print_file(dp->d_name, buff);
100
            break;
118
            break;
101
        case LS_BOGUS:
119
        case LS_BOGUS:
102
            /* Odd chance it was deleted from the time readdir() found
120
            /* Odd chance it was deleted from the time readdir() found
103
             * it and the time that it was scoped */
121
             * it and the time that it was scoped */
104
            printf("ls: skipping bogus node %s\n", dp->d_name);
122
            printf("ls: skipping bogus node %s\n", dp->d_name);
Line 118... Line 136...
118
 *
136
 *
119
 * Now we just print basic DOS style lists */
137
 * Now we just print basic DOS style lists */
120
 
138
 
121
static void ls_print_dir(const char *d)
139
static void ls_print_dir(const char *d)
122
{
140
{
123
    printf("%-40s\t<DIR>\n", d);
141
    printf("%-40s\t<dir>\n", d);
124
 
142
 
125
    return;
143
    return;
126
}
144
}
127
 
145
 
128
static void ls_print_file(const char *f)
146
static void ls_print_file(const char *name, const char *pathname)
129
{
147
{
130
    printf("%-40s\n", f);
148
    printf("%-40s\t%llu\n", name, (long long) flen(pathname));
131
 
149
 
132
    return;
150
    return;
133
}
151
}
134
 
152
 
135
void help_cmd_ls(unsigned int level)
153
void help_cmd_ls(unsigned int level)
Line 162... Line 180...
162
    memset(buff, 0, sizeof(buff));
180
    memset(buff, 0, sizeof(buff));
163
 
181
 
164
    if (argc == 1)
182
    if (argc == 1)
165
        getcwd(buff, PATH_MAX);
183
        getcwd(buff, PATH_MAX);
166
    else
184
    else
167
        strncpy(buff, argv[1], PATH_MAX);
185
        str_cpy(buff, PATH_MAX, argv[1]);
168
 
186
 
169
    scope = ls_scope(buff);
187
    scope = ls_scope(buff);
170
 
188
 
171
    switch (scope) {
189
    switch (scope) {
172
    case LS_BOGUS:
190
    case LS_BOGUS:
173
        cli_error(CL_ENOENT, buff);
191
        cli_error(CL_ENOENT, buff);
174
        free(buff);
192
        free(buff);
175
        return CMD_FAILURE;
193
        return CMD_FAILURE;
176
    case LS_FILE:
194
    case LS_FILE:
177
        ls_print_file(buff);
195
        ls_print_file(buff, buff);
178
        break;
196
        break;
179
    case LS_DIR:
197
    case LS_DIR:
180
        dirp = opendir(buff);
198
        dirp = opendir(buff);
181
        if (! dirp) {
199
        if (! dirp) {
182
            /* May have been deleted between scoping it and opening it */
200
            /* May have been deleted between scoping it and opening it */