Subversion Repositories HelenOS

Rev

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

  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.  
  9. /* Extremely primitive 'ls' implementation. Does not sort or stat files,
  10.  * just here for convenience */
  11.  
  12. #include <stdio.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"
  24. #include "entry.h"
  25. #include "ls.h"
  26. #include "cmds.h"
  27.  
  28. static char *cmdname = "ls";
  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.  
  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);
  64.     return CMD_VOID;
  65. }
  66.  
  67. int * cmd_ls(char **argv)
  68. {
  69.     unsigned int argc;
  70.     unsigned int scope;
  71.     char *buff;
  72.  
  73.     DIR *dirp;
  74.     struct dirent *dp;
  75.  
  76.     /* Count the arguments */
  77.     for (argc = 0; argv[argc] != NULL; argc ++);
  78.  
  79.     if (argc > 2) {
  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) {
  87.         cli_error(CL_ENOMEM, "%s: ", cmdname);
  88.         return CMD_FAILURE;
  89.     }
  90.     memset(buff, 0, sizeof(buff));
  91.  
  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:
  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.    
  119. }
  120.  
  121.