/* Copyright (c) 2008, Tim Post * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * Neither the name of the original program's authors nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ /* NOTE: * This is a bit of an ugly hack, working around the absence of fstat / etc. * As more stuff is completed and exposed in libc, this will improve */ #include #include #include #include #include #include #include #include #include #include #include "errors.h" #include "config.h" #include "util.h" #include "entry.h" #include "cmds.h" /* Various values that can be returned by ls_scope() */ #define LS_BOGUS 0 #define LS_FILE 1 #define LS_DIR 2 /** Structure to represent a directory entry. * * Useful to keep together important information * for sorting directory entries. */ struct dir_elem_t { char *name; struct stat s; }; static const char *cmdname = "ls"; static struct option const long_options[] = { { "help", no_argument, 0, 'h' }, { "unsort", no_argument, 0, 'u' }, { 0, 0, 0, 0 } }; /** Print an entry. * * 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. * * @param de Directory element. */ static void ls_print(struct dir_elem_t *de) { if (de->s.is_file) printf("%-40s\t%llu\n", de->name, (long long) de->s.size); else if (de->s.is_directory) printf("%-40s\t\n", de->name); else printf("%-40s\n", de->name); } /** Compare 2 directory elements. * * It compares 2 elements of a directory : a file is considered * as bigger than a directory, and if they have the same type, * they are compared alphabetically. * * @param a Pointer to the structure of the first element. * @param b Pointer to the structure of the second element. * @param arg Pointer for an other and optionnal argument. * * @return -1 if a < b, 1 otherwise. */ static int ls_cmp(void *a, void *b, void *arg) { struct dir_elem_t *da = a; struct dir_elem_t *db = b; if ((da->s.is_directory && db->s.is_file) || ((da->s.is_directory == db->s.is_directory) && str_cmp(da->name, db->name) < 0)) return -1; else return 1; } /** Scan a directory. * * Scan the content of a directory and print it. * * @param d Name of the directory. * @param dirp Directory stream. * @param sort 1 if the output must be sorted, * 0 otherwise. */ static void ls_scan_dir(const char *d, DIR *dirp, int sort) { int alloc_blocks = 20; int i; int nbdirs = 0; int rc; int len; char *buff; struct dir_elem_t *tmp; struct dir_elem_t *tosort; struct dirent *dp; if (!dirp) return; buff = (char *) malloc(PATH_MAX); if (!buff) { cli_error(CL_ENOMEM, "ls: failed to scan %s", d); return; } tosort = (struct dir_elem_t *) malloc(alloc_blocks * sizeof(*tosort)); if (!tosort) { cli_error(CL_ENOMEM, "ls: failed to scan %s", d); free(buff); return; } while ((dp = readdir(dirp))) { if (nbdirs + 1 > alloc_blocks) { alloc_blocks += alloc_blocks; tmp = (struct dir_elem_t *) realloc(tosort, alloc_blocks * sizeof(struct dir_elem_t)); if (!tmp) { cli_error(CL_ENOMEM, "ls: failed to scan %s", d); goto out; } tosort = tmp; } /* fill the name field */ tosort[nbdirs].name = (char *) malloc(str_length(dp->d_name) + 1); if (!tosort[nbdirs].name) { cli_error(CL_ENOMEM, "ls: failed to scan %s", d); goto out; } str_cpy(tosort[nbdirs].name, str_length(dp->d_name) + 1, dp->d_name); len = snprintf(buff, PATH_MAX - 1, "%s/%s", d, tosort[nbdirs].name); buff[len] = '\0'; rc = stat(buff, &tosort[nbdirs++].s); if (rc != 0) { printf("ls: skipping bogus node %s\n", buff); printf("rc=%d\n", rc); goto out; } } if (sort) { if (!qsort(&tosort[0], nbdirs, sizeof(struct dir_elem_t), ls_cmp, NULL)) { printf("Sorting error.\n"); } } for (i = 0; i < nbdirs; i++) ls_print(&tosort[i]); out: for(i = 0; i < nbdirs; i++) free(tosort[i].name); free(tosort); free(buff); } void help_cmd_ls(unsigned int level) { if (level == HELP_SHORT) { printf("`%s' lists files and directories.\n", cmdname); } else { help_cmd_ls(HELP_SHORT); printf( "Usage: %s [options] [path]\n" "If not path is given, the current working directory is used.\n" "Options:\n" " -h, --help A short option summary\n" " -u, --unsort Do not sort directory entries\n", cmdname); } return; } int cmd_ls(char **argv) { unsigned int argc; struct dir_elem_t de; DIR *dirp; int c, opt_ind; int sort = 1; argc = cli_count_args(argv); for (c = 0, optind = 0, opt_ind = 0; c != -1;) { c = getopt_long(argc, argv, "hu", long_options, &opt_ind); switch (c) { case 'h': help_cmd_ls(HELP_LONG); return CMD_SUCCESS; case 'u': sort = 0; break; } } argc -= optind; de.name = (char *) malloc(PATH_MAX); if (!de.name) { cli_error(CL_ENOMEM, "%s: ", cmdname); return CMD_FAILURE; } memset(de.name, 0, sizeof(PATH_MAX)); if (argc == 0) getcwd(de.name, PATH_MAX); else str_cpy(de.name, PATH_MAX, argv[optind]); if (stat(de.name, &de.s)) { cli_error(CL_ENOENT, de.name); free(de.name); return CMD_FAILURE; } if (de.s.is_file) { ls_print(&de); } else { dirp = opendir(de.name); if (!dirp) { /* May have been deleted between scoping it and opening it */ cli_error(CL_EFAIL, "Could not stat %s", de.name); free(de.name); return CMD_FAILURE; } ls_scan_dir(de.name, dirp, sort); closedir(dirp); } free(de.name); return CMD_SUCCESS; }