Subversion Repositories HelenOS

Compare Revisions

Ignore whitespace Rev 3325 → Rev 3326

/branches/shell/uspace/app/bdsh/cmds/modules/ls/ls.c
70,34 → 70,68
return LS_BOGUS;
}
 
void ls_print(const char *f)
void ls_scan_dir(const char *d, DIR *dirp)
{
struct dirent *dp;
unsigned int scope;
char *buff;
 
scope = ls_scope(f);
if (! dirp)
return;
 
/* If this function is called in a readdir loop, LS_BOGUS
* can be treated as LS_FILE, since we aren't presenting
* the full path to open(), just pointer->d_name as obtained
* from opendir(). This will not happen if ../path/to/file
* is passed, as may be the argument to ls */
buff = (char *)malloc(PATH_MAX);
if (NULL == buff) {
cli_error(CL_ENOMEM, "ls: failed to scan %s", d);
return;
}
 
switch (scope) {
case LS_DIR:
printf("%-40s <DIR>\n", f);
break;
case LS_FILE:
printf("%-40s\n", f);
break;
/* This is never reached unless in a readdir() loop */
case LS_BOGUS:
printf("%-40s\n", f);
break;
while ((dp = readdir(dirp))) {
memset(buff, 0, sizeof(buff));
/* Don't worry if inserting a double slash, this will be fixed by
* absolutize() later with subsequent calls to open() or readdir() */
snprintf(buff, PATH_MAX - 1, "%s/%s", d, dp->d_name);
scope = ls_scope(buff);
switch (scope) {
case LS_DIR:
ls_print_dir(dp->d_name);
break;
case LS_FILE:
ls_print_file(dp->d_name);
break;
case LS_BOGUS:
/* Odd chance it was deleted from the time readdir() found
* it and the time that it was scoped */
printf("ls: skipping bogus node %s\n", dp->d_name);
break;
}
}
 
free(buff);
 
return;
}
 
/* Dispays help for ls in various levels */
/* ls_print_* currently does nothing more than print the entry.
* in the future, we will likely pass the absolute path, and
* some sort of ls_options structure that controls how each
* entry is printed and what is printed about it.
*
* Now we just print basic DOS style lists */
 
void ls_print_dir(const char *d)
{
printf("%-40s\t<DIR>\n", d);
 
return;
}
 
void ls_print_file(const char *f)
{
printf("%-40s\n", f);
 
return;
}
 
void * help_cmd_ls(unsigned int level)
{
if (level == HELP_SHORT) {
116,19 → 150,11
unsigned int argc;
unsigned int scope;
char *buff;
 
DIR *dirp;
struct dirent *dp;
 
/* Count the arguments */
for (argc = 0; argv[argc] != NULL; argc ++);
 
if (argc > 2) {
printf("%s - Too many arguments. Try `help %s extended'\n",
cmdname, cmdname);
return CMD_FAILURE;
}
 
buff = (char *) malloc(PATH_MAX);
if (NULL == buff) {
cli_error(CL_ENOMEM, "%s: ", cmdname);
149,12 → 175,17
free(buff);
return CMD_FAILURE;
case LS_FILE:
ls_print(buff);
ls_print_file(buff);
break;
case LS_DIR:
dirp = opendir(buff);
while ((dp = readdir(dirp)))
ls_print(dp->d_name);
if (! dirp) {
/* May have been deleted between scoping it and opening it */
cli_error(CL_EFAIL, "Could not stat %s", buff);
free(buff);
return CMD_FAILURE;
}
ls_scan_dir(buff, dirp);
closedir(dirp);
break;
}
162,6 → 193,5
free(buff);
 
return CMD_SUCCESS;
}