| 1 | /*
|
|---|
| 2 | * Copyright (c) 2008 Tim Post
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | /* NOTE:
|
|---|
| 30 | * This is a bit of an ugly hack, working around the absence of fstat / etc.
|
|---|
| 31 | * As more stuff is completed and exposed in libc, this will improve */
|
|---|
| 32 |
|
|---|
| 33 | #include <errno.h>
|
|---|
| 34 | #include <stdio.h>
|
|---|
| 35 | #include <stdlib.h>
|
|---|
| 36 | #include <unistd.h>
|
|---|
| 37 | #include <dirent.h>
|
|---|
| 38 | #include <fcntl.h>
|
|---|
| 39 | #include <getopt.h>
|
|---|
| 40 | #include <sys/types.h>
|
|---|
| 41 | #include <sys/stat.h>
|
|---|
| 42 | #include <str.h>
|
|---|
| 43 | #include <sort.h>
|
|---|
| 44 |
|
|---|
| 45 | #include "ls.h"
|
|---|
| 46 | #include "errors.h"
|
|---|
| 47 | #include "config.h"
|
|---|
| 48 | #include "util.h"
|
|---|
| 49 | #include "entry.h"
|
|---|
| 50 | #include "cmds.h"
|
|---|
| 51 |
|
|---|
| 52 | static const char *cmdname = "ls";
|
|---|
| 53 |
|
|---|
| 54 | static ls_job_t ls;
|
|---|
| 55 |
|
|---|
| 56 | static struct option const long_options[] = {
|
|---|
| 57 | { "help", no_argument, 0, 'h' },
|
|---|
| 58 | { "unsort", no_argument, 0, 'u' },
|
|---|
| 59 | { "recursive", no_argument, 0, 'r' },
|
|---|
| 60 | { 0, 0, 0, 0 }
|
|---|
| 61 | };
|
|---|
| 62 |
|
|---|
| 63 | /* Prototypes for the ls command, excluding entry points. */
|
|---|
| 64 | static unsigned int ls_start(ls_job_t *);
|
|---|
| 65 | static void ls_print(struct dir_elem_t *);
|
|---|
| 66 | static int ls_cmp(void *, void *, void *);
|
|---|
| 67 | static signed int ls_scan_dir(const char *, DIR *, struct dir_elem_t **);
|
|---|
| 68 | static unsigned int ls_recursive(const char *, DIR *);
|
|---|
| 69 | static unsigned int ls_scope(const char *, struct dir_elem_t *);
|
|---|
| 70 |
|
|---|
| 71 | static unsigned int ls_start(ls_job_t *ls)
|
|---|
| 72 | {
|
|---|
| 73 | ls->recursive = 0;
|
|---|
| 74 | ls->sort = 1;
|
|---|
| 75 |
|
|---|
| 76 | return 1;
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | /** Print an entry.
|
|---|
| 80 | *
|
|---|
| 81 | * ls_print currently does nothing more than print the entry.
|
|---|
| 82 | * In the future, we will likely pass the absolute path, and
|
|---|
| 83 | * some sort of ls_options structure that controls how each
|
|---|
| 84 | * entry is printed and what is printed about it.
|
|---|
| 85 | *
|
|---|
| 86 | * Now we just print basic DOS style lists.
|
|---|
| 87 | *
|
|---|
| 88 | * @param de Directory element.
|
|---|
| 89 | */
|
|---|
| 90 | static void ls_print(struct dir_elem_t *de)
|
|---|
| 91 | {
|
|---|
| 92 | if (de->s.is_file)
|
|---|
| 93 | printf("%-40s\t%llu\n", de->name, (long long) de->s.size);
|
|---|
| 94 | else if (de->s.is_directory)
|
|---|
| 95 | printf("%-40s\t<dir>\n", de->name);
|
|---|
| 96 | else
|
|---|
| 97 | printf("%-40s\n", de->name);
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | /** Compare 2 directory elements.
|
|---|
| 101 | *
|
|---|
| 102 | * It compares 2 elements of a directory : a file is considered
|
|---|
| 103 | * as bigger than a directory, and if they have the same type,
|
|---|
| 104 | * they are compared alphabetically.
|
|---|
| 105 | *
|
|---|
| 106 | * @param a Pointer to the structure of the first element.
|
|---|
| 107 | * @param b Pointer to the structure of the second element.
|
|---|
| 108 | * @param arg Pointer for an other and optionnal argument.
|
|---|
| 109 | *
|
|---|
| 110 | * @return -1 if a < b, 1 otherwise.
|
|---|
| 111 | */
|
|---|
| 112 | static int ls_cmp(void *a, void *b, void *arg)
|
|---|
| 113 | {
|
|---|
| 114 | struct dir_elem_t *da = a;
|
|---|
| 115 | struct dir_elem_t *db = b;
|
|---|
| 116 |
|
|---|
| 117 | if ((da->s.is_directory && db->s.is_file) ||
|
|---|
| 118 | ((da->s.is_directory == db->s.is_directory) &&
|
|---|
| 119 | str_cmp(da->name, db->name) < 0))
|
|---|
| 120 | return -1;
|
|---|
| 121 | else
|
|---|
| 122 | return 1;
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | /** Scan a directory.
|
|---|
| 126 | *
|
|---|
| 127 | * Scan the content of a directory and print it.
|
|---|
| 128 | *
|
|---|
| 129 | * @param d Name of the directory.
|
|---|
| 130 | * @param dirp Directory stream.
|
|---|
| 131 | * @param sort 1 if the output must be sorted,
|
|---|
| 132 | * 0 otherwise.
|
|---|
| 133 | */
|
|---|
| 134 | static signed int ls_scan_dir(const char *d, DIR *dirp,
|
|---|
| 135 | struct dir_elem_t **dir_list_ptr)
|
|---|
| 136 | {
|
|---|
| 137 | int alloc_blocks = 20;
|
|---|
| 138 | int i;
|
|---|
| 139 | int nbdirs = 0;
|
|---|
| 140 | int rc;
|
|---|
| 141 | int len;
|
|---|
| 142 | char *buff;
|
|---|
| 143 | struct dir_elem_t *tmp;
|
|---|
| 144 | struct dir_elem_t *tosort;
|
|---|
| 145 | struct dirent *dp;
|
|---|
| 146 |
|
|---|
| 147 | if (!dirp)
|
|---|
| 148 | return -1;
|
|---|
| 149 |
|
|---|
| 150 | buff = (char *) malloc(PATH_MAX);
|
|---|
| 151 | if (!buff) {
|
|---|
| 152 | cli_error(CL_ENOMEM, "ls: failed to scan %s", d);
|
|---|
| 153 | return -1;
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | tosort = (struct dir_elem_t *) malloc(alloc_blocks * sizeof(*tosort));
|
|---|
| 157 | if (!tosort) {
|
|---|
| 158 | cli_error(CL_ENOMEM, "ls: failed to scan %s", d);
|
|---|
| 159 | free(buff);
|
|---|
| 160 | return -1;
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | while ((dp = readdir(dirp))) {
|
|---|
| 164 | if (nbdirs + 1 > alloc_blocks) {
|
|---|
| 165 | alloc_blocks += alloc_blocks;
|
|---|
| 166 |
|
|---|
| 167 | tmp = (struct dir_elem_t *) realloc(tosort,
|
|---|
| 168 | alloc_blocks * sizeof(struct dir_elem_t));
|
|---|
| 169 | if (!tmp) {
|
|---|
| 170 | cli_error(CL_ENOMEM, "ls: failed to scan %s", d);
|
|---|
| 171 | goto out;
|
|---|
| 172 | }
|
|---|
| 173 | tosort = tmp;
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 | /* fill the name field */
|
|---|
| 177 | tosort[nbdirs].name = (char *) malloc(str_size(dp->d_name) + 1);
|
|---|
| 178 | if (!tosort[nbdirs].name) {
|
|---|
| 179 | cli_error(CL_ENOMEM, "ls: failed to scan %s", d);
|
|---|
| 180 | goto out;
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | str_cpy(tosort[nbdirs].name, str_size(dp->d_name) + 1, dp->d_name);
|
|---|
| 184 | len = snprintf(buff, PATH_MAX - 1, "%s/%s", d, tosort[nbdirs].name);
|
|---|
| 185 | buff[len] = '\0';
|
|---|
| 186 |
|
|---|
| 187 | rc = stat(buff, &tosort[nbdirs++].s);
|
|---|
| 188 | if (rc != 0) {
|
|---|
| 189 | printf("ls: skipping bogus node %s\n", buff);
|
|---|
| 190 | printf("error=%d\n", errno);
|
|---|
| 191 | goto out;
|
|---|
| 192 | }
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | if (ls.sort) {
|
|---|
| 196 | if (!qsort(&tosort[0], nbdirs, sizeof(struct dir_elem_t),
|
|---|
| 197 | ls_cmp, NULL)) {
|
|---|
| 198 | printf("Sorting error.\n");
|
|---|
| 199 | }
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 | for (i = 0; i < nbdirs; i++)
|
|---|
| 203 | ls_print(&tosort[i]);
|
|---|
| 204 |
|
|---|
| 205 | /* Populate the directory list. */
|
|---|
| 206 | if (ls.recursive) {
|
|---|
| 207 | tmp = (struct dir_elem_t *) realloc(*dir_list_ptr,
|
|---|
| 208 | nbdirs * sizeof(struct dir_elem_t));
|
|---|
| 209 | if (!tmp) {
|
|---|
| 210 | cli_error(CL_ENOMEM, "ls: failed to scan %s", d);
|
|---|
| 211 | goto out;
|
|---|
| 212 | }
|
|---|
| 213 | *dir_list_ptr = tmp;
|
|---|
| 214 |
|
|---|
| 215 | for (i = 0; i < nbdirs; i++) {
|
|---|
| 216 | (*dir_list_ptr)[i].name = str_dup(tosort[i].name);
|
|---|
| 217 | if (!(*dir_list_ptr)[i].name) {
|
|---|
| 218 | cli_error(CL_ENOMEM, "ls: failed to scan %s", d);
|
|---|
| 219 | goto out;
|
|---|
| 220 | }
|
|---|
| 221 | }
|
|---|
| 222 | }
|
|---|
| 223 |
|
|---|
| 224 | out:
|
|---|
| 225 | for(i = 0; i < nbdirs; i++)
|
|---|
| 226 | free(tosort[i].name);
|
|---|
| 227 | free(tosort);
|
|---|
| 228 | free(buff);
|
|---|
| 229 |
|
|---|
| 230 | return nbdirs;
|
|---|
| 231 | }
|
|---|
| 232 |
|
|---|
| 233 | /** Visit a directory recursively.
|
|---|
| 234 | *
|
|---|
| 235 | * ls_recursive visits all the subdirectories recursively and
|
|---|
| 236 | * prints the files and directories in them.
|
|---|
| 237 | *
|
|---|
| 238 | * @param path Path the current directory being visited.
|
|---|
| 239 | * @param dirp Directory stream.
|
|---|
| 240 | */
|
|---|
| 241 | static unsigned int ls_recursive(const char *path, DIR *dirp)
|
|---|
| 242 | {
|
|---|
| 243 | int i, nbdirs, ret;
|
|---|
| 244 | unsigned int scope;
|
|---|
| 245 | char *subdir_path;
|
|---|
| 246 | DIR *subdirp;
|
|---|
| 247 | struct dir_elem_t *dir_list;
|
|---|
| 248 |
|
|---|
| 249 | const char * const trailing_slash = "/";
|
|---|
| 250 |
|
|---|
| 251 | nbdirs = 0;
|
|---|
| 252 | dir_list = (struct dir_elem_t *) malloc(sizeof(struct dir_elem_t));
|
|---|
| 253 |
|
|---|
| 254 | printf("\n%s:\n", path);
|
|---|
| 255 |
|
|---|
| 256 | subdir_path = (char *) malloc(PATH_MAX);
|
|---|
| 257 | if (!subdir_path) {
|
|---|
| 258 | ret = CMD_FAILURE;
|
|---|
| 259 | goto out;
|
|---|
| 260 | }
|
|---|
| 261 |
|
|---|
| 262 | nbdirs = ls_scan_dir(path, dirp, &dir_list);
|
|---|
| 263 | if (nbdirs == -1) {
|
|---|
| 264 | ret = CMD_FAILURE;
|
|---|
| 265 | goto out;
|
|---|
| 266 | }
|
|---|
| 267 |
|
|---|
| 268 | for (i = 0; i < nbdirs; ++i) {
|
|---|
| 269 | memset(subdir_path, 0, PATH_MAX);
|
|---|
| 270 |
|
|---|
| 271 | if (str_size(subdir_path) + str_size(path) + 1 <= PATH_MAX)
|
|---|
| 272 | str_append(subdir_path, PATH_MAX, path);
|
|---|
| 273 | if (path[str_size(path)-1] != '/' &&
|
|---|
| 274 | str_size(subdir_path) + str_size(trailing_slash) + 1 <= PATH_MAX)
|
|---|
| 275 | str_append(subdir_path, PATH_MAX, trailing_slash);
|
|---|
| 276 | if (str_size(subdir_path) +
|
|---|
| 277 | str_size(dir_list[i].name) + 1 <= PATH_MAX)
|
|---|
| 278 | str_append(subdir_path, PATH_MAX, dir_list[i].name);
|
|---|
| 279 |
|
|---|
| 280 | scope = ls_scope(subdir_path, &dir_list[i]);
|
|---|
| 281 | switch (scope) {
|
|---|
| 282 | case LS_FILE:
|
|---|
| 283 | break;
|
|---|
| 284 | case LS_DIR:
|
|---|
| 285 | subdirp = opendir(subdir_path);
|
|---|
| 286 | if (!subdirp) {
|
|---|
| 287 | /* May have been deleted between scoping it and opening it */
|
|---|
| 288 | cli_error(CL_EFAIL, "Could not stat %s", dir_list[i].name);
|
|---|
| 289 | ret = CMD_FAILURE;
|
|---|
| 290 | goto out;
|
|---|
| 291 | }
|
|---|
| 292 |
|
|---|
| 293 | ret = ls_recursive(subdir_path, subdirp);
|
|---|
| 294 | closedir(subdirp);
|
|---|
| 295 | if (ret == CMD_FAILURE)
|
|---|
| 296 | goto out;
|
|---|
| 297 | break;
|
|---|
| 298 | case LS_BOGUS:
|
|---|
| 299 | ret = CMD_FAILURE;
|
|---|
| 300 | goto out;
|
|---|
| 301 | }
|
|---|
| 302 | }
|
|---|
| 303 |
|
|---|
| 304 | ret = CMD_SUCCESS;
|
|---|
| 305 |
|
|---|
| 306 | out:
|
|---|
| 307 | for (i = 0; i < nbdirs; i++)
|
|---|
| 308 | free(dir_list[i].name);
|
|---|
| 309 | free(dir_list);
|
|---|
| 310 | free(subdir_path);
|
|---|
| 311 |
|
|---|
| 312 | return ret;
|
|---|
| 313 | }
|
|---|
| 314 |
|
|---|
| 315 | static unsigned int ls_scope(const char *path, struct dir_elem_t *de)
|
|---|
| 316 | {
|
|---|
| 317 | if (stat(path, &de->s) != 0) {
|
|---|
| 318 | cli_error(CL_ENOENT, "%s", path);
|
|---|
| 319 | return LS_BOGUS;
|
|---|
| 320 | }
|
|---|
| 321 |
|
|---|
| 322 | if (de->s.is_file)
|
|---|
| 323 | return LS_FILE;
|
|---|
| 324 | else if (de->s.is_directory)
|
|---|
| 325 | return LS_DIR;
|
|---|
| 326 |
|
|---|
| 327 | return LS_BOGUS;
|
|---|
| 328 | }
|
|---|
| 329 |
|
|---|
| 330 | void help_cmd_ls(unsigned int level)
|
|---|
| 331 | {
|
|---|
| 332 | if (level == HELP_SHORT) {
|
|---|
| 333 | printf("`%s' lists files and directories.\n", cmdname);
|
|---|
| 334 | } else {
|
|---|
| 335 | help_cmd_ls(HELP_SHORT);
|
|---|
| 336 | printf(
|
|---|
| 337 | "Usage: %s [options] [path]\n"
|
|---|
| 338 | "If not path is given, the current working directory is used.\n"
|
|---|
| 339 | "Options:\n"
|
|---|
| 340 | " -h, --help A short option summary\n"
|
|---|
| 341 | " -u, --unsort Do not sort directory entries\n"
|
|---|
| 342 | " -r, --recursive List subdirectories recursively\n",
|
|---|
| 343 | cmdname);
|
|---|
| 344 | }
|
|---|
| 345 |
|
|---|
| 346 | return;
|
|---|
| 347 | }
|
|---|
| 348 |
|
|---|
| 349 | int cmd_ls(char **argv)
|
|---|
| 350 | {
|
|---|
| 351 | unsigned int argc;
|
|---|
| 352 | struct dir_elem_t de;
|
|---|
| 353 | DIR *dirp;
|
|---|
| 354 | int c, opt_ind;
|
|---|
| 355 | int ret = 0;
|
|---|
| 356 | unsigned int scope;
|
|---|
| 357 |
|
|---|
| 358 | if (!ls_start(&ls)) {
|
|---|
| 359 | cli_error(CL_EFAIL, "%s: Could not initialize", cmdname);
|
|---|
| 360 | return CMD_FAILURE;
|
|---|
| 361 | }
|
|---|
| 362 |
|
|---|
| 363 | argc = cli_count_args(argv);
|
|---|
| 364 |
|
|---|
| 365 | for (c = 0, optreset = 1, optind = 0, opt_ind = 0; c != -1;) {
|
|---|
| 366 | c = getopt_long(argc, argv, "hur", long_options, &opt_ind);
|
|---|
| 367 | switch (c) {
|
|---|
| 368 | case 'h':
|
|---|
| 369 | help_cmd_ls(HELP_LONG);
|
|---|
| 370 | return CMD_SUCCESS;
|
|---|
| 371 | case 'u':
|
|---|
| 372 | ls.sort = 0;
|
|---|
| 373 | break;
|
|---|
| 374 | case 'r':
|
|---|
| 375 | ls.recursive = 1;
|
|---|
| 376 | break;
|
|---|
| 377 | }
|
|---|
| 378 | }
|
|---|
| 379 |
|
|---|
| 380 | argc -= optind;
|
|---|
| 381 |
|
|---|
| 382 | de.name = (char *) malloc(PATH_MAX);
|
|---|
| 383 | if (!de.name) {
|
|---|
| 384 | cli_error(CL_ENOMEM, "%s: Out of memory", cmdname);
|
|---|
| 385 | return CMD_FAILURE;
|
|---|
| 386 | }
|
|---|
| 387 | memset(de.name, 0, PATH_MAX);
|
|---|
| 388 |
|
|---|
| 389 | if (argc == 0) {
|
|---|
| 390 | if (getcwd(de.name, PATH_MAX) == NULL) {
|
|---|
| 391 | cli_error(CL_EFAIL, "%s: Failed determining working "
|
|---|
| 392 | "directory", cmdname);
|
|---|
| 393 | return CMD_FAILURE;
|
|---|
| 394 | }
|
|---|
| 395 | } else {
|
|---|
| 396 | str_cpy(de.name, PATH_MAX, argv[optind]);
|
|---|
| 397 | }
|
|---|
| 398 |
|
|---|
| 399 | scope = ls_scope(de.name, &de);
|
|---|
| 400 | switch (scope) {
|
|---|
| 401 | case LS_FILE:
|
|---|
| 402 | ls_print(&de);
|
|---|
| 403 | break;
|
|---|
| 404 | case LS_DIR:
|
|---|
| 405 | dirp = opendir(de.name);
|
|---|
| 406 | if (!dirp) {
|
|---|
| 407 | /* May have been deleted between scoping it and opening it */
|
|---|
| 408 | cli_error(CL_EFAIL, "Could not stat %s", de.name);
|
|---|
| 409 | free(de.name);
|
|---|
| 410 | return CMD_FAILURE;
|
|---|
| 411 | }
|
|---|
| 412 | if (ls.recursive)
|
|---|
| 413 | ret = ls_recursive(de.name, dirp);
|
|---|
| 414 | else
|
|---|
| 415 | ret = ls_scan_dir(de.name, dirp, NULL);
|
|---|
| 416 |
|
|---|
| 417 | closedir(dirp);
|
|---|
| 418 | break;
|
|---|
| 419 | case LS_BOGUS:
|
|---|
| 420 | return CMD_FAILURE;
|
|---|
| 421 | }
|
|---|
| 422 |
|
|---|
| 423 | free(de.name);
|
|---|
| 424 |
|
|---|
| 425 | if (ret == -1 || ret == CMD_FAILURE)
|
|---|
| 426 | return CMD_FAILURE;
|
|---|
| 427 | else
|
|---|
| 428 | return CMD_SUCCESS;
|
|---|
| 429 | }
|
|---|
| 430 |
|
|---|