Subversion Repositories HelenOS

Rev

Rev 3277 | Rev 3294 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3277 post 1
/* Automatically generated by mknewcmd on Mon Aug 11 00:22:09 PHT 2008
2
 * This is machine generated output. The author of mknewcmd claims no
3
 * copyright over the contents of this file. Where legally permitted, the
4
 * contents herein are donated to the public domain.
5
 *
6
 * You should apply any license and copyright that you wish to this file,
7
 * replacing this header in its entirety. */
8
 
3293 post 9
/* Extremely primitive 'ls' implementation. Does not sort or stat files,
10
 * just here for convenience */
11
 
3277 post 12
#include <stdio.h>
13
#include <stdlib.h>
3293 post 14
#include <unistd.h>
15
#include <dirent.h>
16
#include <fcntl.h>
17
#include <sys/types.h>
18
#include <sys/stat.h>
19
#include <string.h>
20
 
21
#include "errors.h"
22
#include "config.h"
23
#include "util.h"
3277 post 24
#include "entry.h"
25
#include "ls.h"
26
#include "cmds.h"
27
 
28
static char *cmdname = "ls";
29
 
3293 post 30
unsigned int ls_scope(const char *path)
31
{
32
    int fd;
33
    DIR *dirp;
34
 
35
    dirp = opendir(path);
36
    if (dirp) {
37
        closedir(dirp);
38
        return LS_DIR;
39
    }
40
 
41
    fd = open(path, O_RDONLY);
42
    if (fd > 0) {
43
        close(fd);
44
        return LS_FILE;
45
    }
46
 
47
    return LS_BOGUS;
48
}
49
 
50
void ls_print(const char *f)
51
{
52
    if (ls_scope(f) == LS_FILE)
53
        printf("%-40s\n", f);
54
    else
55
        printf("%-40s <DIR>\n", f);
56
    return;
57
}
58
 
3277 post 59
/* Dispays help for ls in various levels */
60
void * help_cmd_ls(unsigned int level)
61
{
62
    printf("This is the %s help for '%s'.\n",
63
        level ? EXT_HELP : SHORT_HELP, cmdname);
3293 post 64
    return CMD_VOID;
3277 post 65
}
66
 
67
int * cmd_ls(char **argv)
68
{
69
    unsigned int argc;
3293 post 70
    unsigned int scope;
71
    char *buff;
3277 post 72
 
3293 post 73
    DIR *dirp;
74
    struct dirent *dp;
75
 
3277 post 76
    /* Count the arguments */
77
    for (argc = 0; argv[argc] != NULL; argc ++);
78
 
3293 post 79
    if (argc > 2) {
80
        printf("%s - Too many arguments. Try `help %s extended'\n",
81
            cmdname, cmdname);
82
        return CMD_FAILURE;
83
    }
3277 post 84
 
3293 post 85
    buff = (char *) malloc(PATH_MAX);
86
    if (NULL == buff) {
87
        cli_error(CL_ENOMEM, "%s: ", cmdname);
88
        return CMD_FAILURE;
3277 post 89
    }
3293 post 90
    memset(buff, 0, sizeof(buff));
3277 post 91
 
3293 post 92
    if (argc == 1)
93
        getcwd(buff, PATH_MAX);
94
    else
95
        strncpy(buff, argv[1], PATH_MAX);
3277 post 96
 
3293 post 97
    scope = ls_scope(buff);
98
 
99
    switch (scope) {
100
    case LS_BOGUS:
101
        cli_error(CL_ENOENT, buff);
102
        free(buff);
103
        return CMD_FAILURE;
104
    case LS_FILE:
105
        ls_print(buff);
106
        break;
107
    case LS_DIR:
108
        dirp = opendir(buff);
109
        while ((dp = readdir(dirp)))
110
            ls_print(dp->d_name);
111
        closedir(dirp);
112
        break;
113
    }
114
 
115
    free(buff);
116
 
117
    return CMD_SUCCESS;
118
 
3277 post 119
}
120