Subversion Repositories HelenOS

Rev

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

Rev 3277 Rev 3293
Line 4... Line 4...
4
 * contents herein are donated to the public domain.
4
 * contents herein are donated to the public domain.
5
 *
5
 *
6
 * You should apply any license and copyright that you wish to this file,
6
 * You should apply any license and copyright that you wish to this file,
7
 * replacing this header in its entirety. */
7
 * replacing this header in its entirety. */
8
 
8
 
-
 
9
/* Extremely primitive 'ls' implementation. Does not sort or stat files,
-
 
10
 * just here for convenience */
-
 
11
 
9
#include <stdio.h>
12
#include <stdio.h>
10
#include <stdlib.h>
13
#include <stdlib.h>
-
 
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"
11
#include "entry.h"
24
#include "entry.h"
12
#include "ls.h"
25
#include "ls.h"
13
#include "cmds.h"
26
#include "cmds.h"
14
 
27
 
15
static char *cmdname = "ls";
28
static char *cmdname = "ls";
16
 
29
 
-
 
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
 
17
/* Dispays help for ls in various levels */
59
/* Dispays help for ls in various levels */
18
void * help_cmd_ls(unsigned int level)
60
void * help_cmd_ls(unsigned int level)
19
{
61
{
20
    printf("This is the %s help for '%s'.\n",
62
    printf("This is the %s help for '%s'.\n",
21
        level ? EXT_HELP : SHORT_HELP, cmdname);
63
        level ? EXT_HELP : SHORT_HELP, cmdname);
22
    return 0;
64
    return CMD_VOID;
23
}
65
}
24
 
66
 
25
/* Main entry point for ls, accepts an array of arguments */
-
 
26
int * cmd_ls(char **argv)
67
int * cmd_ls(char **argv)
27
{
68
{
28
    unsigned int argc;
69
    unsigned int argc;
29
    unsigned int i;
70
    unsigned int scope;
-
 
71
    char *buff;
-
 
72
 
-
 
73
    DIR *dirp;
-
 
74
    struct dirent *dp;
30
 
75
 
31
    /* Count the arguments */
76
    /* Count the arguments */
32
    for (argc = 0; argv[argc] != NULL; argc ++);
77
    for (argc = 0; argv[argc] != NULL; argc ++);
33
 
78
 
-
 
79
    if (argc > 2) {
34
    printf("%s %s\n", TEST_ANNOUNCE, cmdname);
80
        printf("%s - Too many arguments. Try `help %s extended'\n",
-
 
81
            cmdname, cmdname);
-
 
82
        return CMD_FAILURE;
-
 
83
    }
-
 
84
 
-
 
85
    buff = (char *) malloc(PATH_MAX);
-
 
86
    if (NULL == buff) {
35
    printf("%d arguments passed to %s", argc - 1, cmdname);
87
        cli_error(CL_ENOMEM, "%s: ", cmdname);
-
 
88
        return CMD_FAILURE;
-
 
89
    }
-
 
90
    memset(buff, 0, sizeof(buff));
36
 
91
 
37
    if (argc < 2) {
92
    if (argc == 1)
-
 
93
        getcwd(buff, PATH_MAX);
-
 
94
    else
-
 
95
        strncpy(buff, argv[1], PATH_MAX);
-
 
96
 
-
 
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:
38
        printf("\n");
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);
39
        return 0;
112
        break;
40
    }
113
    }
41
 
114
 
42
    printf(":\n");
115
    free(buff);
43
    for (i = 1; i < argc; i++)
-
 
44
        printf("[%d] -> %s\n", i, argv[i]);
-
 
45
 
116
 
46
    return 0;
117
    return CMD_SUCCESS;
-
 
118
   
47
}
119
}
48
 
120