Subversion Repositories HelenOS

Rev

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

Rev 3310 Rev 3326
Line 68... Line 68...
68
    }
68
    }
69
 
69
 
70
    return LS_BOGUS;
70
    return LS_BOGUS;
71
}
71
}
72
 
72
 
73
void ls_print(const char *f)
73
void ls_scan_dir(const char *d, DIR *dirp)
74
{
74
{
-
 
75
    struct dirent *dp;
75
    unsigned int scope;
76
    unsigned int scope;
-
 
77
    char *buff;
76
 
78
 
77
    scope = ls_scope(f);
79
    if (! dirp)
-
 
80
        return;
78
 
81
 
79
    /* If this function is called in a readdir loop, LS_BOGUS
82
    buff = (char *)malloc(PATH_MAX);
80
     * can be treated as LS_FILE, since we aren't presenting
83
    if (NULL == buff) {
81
     * the full path to open(), just pointer->d_name as obtained
-
 
82
     * from opendir(). This will not happen if ../path/to/file
84
        cli_error(CL_ENOMEM, "ls: failed to scan %s", d);
83
     * is passed, as may be the argument to ls */
85
        return;
-
 
86
    }
84
 
87
 
-
 
88
    while ((dp = readdir(dirp))) {
-
 
89
        memset(buff, 0, sizeof(buff));
-
 
90
        /* Don't worry if inserting a double slash, this will be fixed by
-
 
91
         * absolutize() later with subsequent calls to open() or readdir() */
-
 
92
        snprintf(buff, PATH_MAX - 1, "%s/%s", d, dp->d_name);
-
 
93
        scope = ls_scope(buff);
85
    switch (scope) {
94
        switch (scope) {
86
    case LS_DIR:
95
        case LS_DIR:
87
        printf("%-40s <DIR>\n", f);
96
            ls_print_dir(dp->d_name);
88
        break;
97
            break;
89
    case LS_FILE:
98
        case LS_FILE:
90
        printf("%-40s\n", f);
99
            ls_print_file(dp->d_name);
91
        break;
100
            break;
92
    /* This is never reached unless in a readdir() loop */
-
 
93
    case LS_BOGUS:
101
        case LS_BOGUS:
-
 
102
            /* Odd chance it was deleted from the time readdir() found
-
 
103
             * it and the time that it was scoped */
94
        printf("%-40s\n", f);
104
            printf("ls: skipping bogus node %s\n", dp->d_name);
95
        break;
105
            break;
-
 
106
        }
96
    }
107
    }
-
 
108
 
-
 
109
    free(buff);
-
 
110
 
-
 
111
    return;
-
 
112
}
-
 
113
 
-
 
114
/* ls_print_* currently does nothing more than print the entry.
-
 
115
 * in the future, we will likely pass the absolute path, and
-
 
116
 * some sort of ls_options structure that controls how each
-
 
117
 * entry is printed and what is printed about it.
-
 
118
 *
-
 
119
 * Now we just print basic DOS style lists */
-
 
120
 
-
 
121
void ls_print_dir(const char *d)
-
 
122
{
-
 
123
    printf("%-40s\t<DIR>\n", d);
-
 
124
 
-
 
125
    return;
-
 
126
}
-
 
127
 
-
 
128
void ls_print_file(const char *f)
-
 
129
{
-
 
130
    printf("%-40s\n", f);
-
 
131
 
97
    return;
132
    return;
98
}
133
}
99
 
134
 
100
/* Dispays help for ls in various levels */
-
 
101
void * help_cmd_ls(unsigned int level)
135
void * help_cmd_ls(unsigned int level)
102
{
136
{
103
    if (level == HELP_SHORT) {
137
    if (level == HELP_SHORT) {
104
        printf("`%s' lists files and directories.\n", cmdname);
138
        printf("`%s' lists files and directories.\n", cmdname);
105
    } else {
139
    } else {
Line 114... Line 148...
114
int * cmd_ls(char **argv)
148
int * cmd_ls(char **argv)
115
{
149
{
116
    unsigned int argc;
150
    unsigned int argc;
117
    unsigned int scope;
151
    unsigned int scope;
118
    char *buff;
152
    char *buff;
119
 
-
 
120
    DIR *dirp;
153
    DIR *dirp;
121
    struct dirent *dp;
-
 
122
 
154
 
123
    /* Count the arguments */
155
    /* Count the arguments */
124
    for (argc = 0; argv[argc] != NULL; argc ++);
156
    for (argc = 0; argv[argc] != NULL; argc ++);
125
 
157
 
126
    if (argc > 2) {
-
 
127
        printf("%s - Too many arguments. Try `help %s extended'\n",
-
 
128
            cmdname, cmdname);
-
 
129
        return CMD_FAILURE;
-
 
130
    }
-
 
131
 
-
 
132
    buff = (char *) malloc(PATH_MAX);
158
    buff = (char *) malloc(PATH_MAX);
133
    if (NULL == buff) {
159
    if (NULL == buff) {
134
        cli_error(CL_ENOMEM, "%s: ", cmdname);
160
        cli_error(CL_ENOMEM, "%s: ", cmdname);
135
        return CMD_FAILURE;
161
        return CMD_FAILURE;
136
    }
162
    }
Line 147... Line 173...
147
    case LS_BOGUS:
173
    case LS_BOGUS:
148
        cli_error(CL_ENOENT, buff);
174
        cli_error(CL_ENOENT, buff);
149
        free(buff);
175
        free(buff);
150
        return CMD_FAILURE;
176
        return CMD_FAILURE;
151
    case LS_FILE:
177
    case LS_FILE:
152
        ls_print(buff);
178
        ls_print_file(buff);
153
        break;
179
        break;
154
    case LS_DIR:
180
    case LS_DIR:
155
        dirp = opendir(buff);
181
        dirp = opendir(buff);
156
        while ((dp = readdir(dirp)))
182
        if (! dirp) {
-
 
183
            /* May have been deleted between scoping it and opening it */
-
 
184
            cli_error(CL_EFAIL, "Could not stat %s", buff);
-
 
185
            free(buff);
-
 
186
            return CMD_FAILURE;
-
 
187
        }
157
            ls_print(dp->d_name);
188
        ls_scan_dir(buff, dirp);
158
        closedir(dirp);
189
        closedir(dirp);
159
        break;
190
        break;
160
    }
191
    }
161
 
192
 
162
    free(buff);
193
    free(buff);
163
 
194
 
164
    return CMD_SUCCESS;
195
    return CMD_SUCCESS;
165
   
-
 
166
}
196
}
167
 
197