Subversion Repositories HelenOS

Rev

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

Rev Author Line No. Line
3294 post 1
/* Copyright (c) 2008, Tim Post <tinkertim@gmail.com>
2
 * All rights reserved.
3277 post 3
 *
3294 post 4
 * Redistribution and use in source and binary forms, with or without
5
 * modification, are permitted provided that the following conditions are met:
6
 *
7
 * Redistributions of source code must retain the above copyright notice, this
8
 * list of conditions and the following disclaimer.
9
 *
10
 * Redistributions in binary form must reproduce the above copyright notice,
11
 * this list of conditions and the following disclaimer in the documentation
12
 * and/or other materials provided with the distribution.
13
 *
14
 * Neither the name of the original program's authors nor the names of its
15
 * contributors may be used to endorse or promote products derived from this
16
 * software without specific prior written permission.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
 * POSSIBILITY OF SUCH DAMAGE.
29
 */
3277 post 30
 
3294 post 31
/* NOTE:
32
 * This is a bit of an ugly hack, working around the absence of fstat / etc.
33
 * As more stuff is completed and exposed in libc, this will improve */
3293 post 34
 
3277 post 35
#include <stdio.h>
36
#include <stdlib.h>
3293 post 37
#include <unistd.h>
38
#include <dirent.h>
39
#include <fcntl.h>
40
#include <sys/types.h>
41
#include <sys/stat.h>
42
#include <string.h>
43
 
44
#include "errors.h"
45
#include "config.h"
46
#include "util.h"
3277 post 47
#include "entry.h"
48
#include "ls.h"
49
#include "cmds.h"
50
 
51
static char *cmdname = "ls";
52
 
3293 post 53
unsigned int ls_scope(const char *path)
54
{
55
    int fd;
56
    DIR *dirp;
57
 
58
    dirp = opendir(path);
59
    if (dirp) {
60
        closedir(dirp);
61
        return LS_DIR;
62
    }
63
 
64
    fd = open(path, O_RDONLY);
65
    if (fd > 0) {
66
        close(fd);
67
        return LS_FILE;
68
    }
69
 
70
    return LS_BOGUS;
71
}
72
 
73
void ls_print(const char *f)
74
{
75
    if (ls_scope(f) == LS_FILE)
76
        printf("%-40s\n", f);
77
    else
78
        printf("%-40s <DIR>\n", f);
79
    return;
80
}
81
 
3277 post 82
/* Dispays help for ls in various levels */
83
void * help_cmd_ls(unsigned int level)
84
{
3294 post 85
    if (level == HELP_SHORT) {
86
        printf("`%s' lists files and directories.\n", cmdname);
87
    } else {
88
        help_cmd_ls(HELP_SHORT);
89
        printf("  `%s' [path], if no path is given the current "
90
                "working directory is used.\n", cmdname);
91
    }
92
 
3293 post 93
    return CMD_VOID;
3277 post 94
}
95
 
96
int * cmd_ls(char **argv)
97
{
98
    unsigned int argc;
3293 post 99
    unsigned int scope;
100
    char *buff;
3277 post 101
 
3293 post 102
    DIR *dirp;
103
    struct dirent *dp;
104
 
3277 post 105
    /* Count the arguments */
106
    for (argc = 0; argv[argc] != NULL; argc ++);
107
 
3293 post 108
    if (argc > 2) {
109
        printf("%s - Too many arguments. Try `help %s extended'\n",
110
            cmdname, cmdname);
111
        return CMD_FAILURE;
112
    }
3277 post 113
 
3293 post 114
    buff = (char *) malloc(PATH_MAX);
115
    if (NULL == buff) {
116
        cli_error(CL_ENOMEM, "%s: ", cmdname);
117
        return CMD_FAILURE;
3277 post 118
    }
3293 post 119
    memset(buff, 0, sizeof(buff));
3277 post 120
 
3293 post 121
    if (argc == 1)
122
        getcwd(buff, PATH_MAX);
123
    else
124
        strncpy(buff, argv[1], PATH_MAX);
3277 post 125
 
3293 post 126
    scope = ls_scope(buff);
127
 
128
    switch (scope) {
129
    case LS_BOGUS:
130
        cli_error(CL_ENOENT, buff);
131
        free(buff);
132
        return CMD_FAILURE;
133
    case LS_FILE:
134
        ls_print(buff);
135
        break;
136
    case LS_DIR:
137
        dirp = opendir(buff);
138
        while ((dp = readdir(dirp)))
139
            ls_print(dp->d_name);
140
        closedir(dirp);
141
        break;
142
    }
143
 
144
    free(buff);
145
 
146
    return CMD_SUCCESS;
147
 
3277 post 148
}
149