Exemplo n.º 1
0
void handleChild(char *line) {
  char** options = getOptionsFromLine(line);
  int success = executeCommand(options);
  freeOptions(options);
  if (!success) perror("execv");
  exit(success ? EXIT_SUCCESS : EXIT_FAILURE);
}
Exemplo n.º 2
0
void terminateProgram(int info_fd, int log_fd, options* opt)
{
    // Close info file, SMC and clear resources
    CHECK_ERROR( CloseFile(info_fd) );
    CHECK_ERROR( CloseFile(log_fd) );
    smcClose();
    freeOptions(opt);
}
Exemplo n.º 3
0
int parse_options(int argc, char *argv[])
{
   /*
    * parses command line options
    */
   int j;
   static struct option longOpts[] = {
      // name, has_arg, flag, val
      {"help", 0, 0, 'h'},
      {"version", 0, 0, 'v'},
      {0, 0, 0, 0}
   };
   OPT_ASCII = 0; // sort by using locale collation order
   OPT_IGNORE_CASE = 0; // default is case sensitive
   OPT_INFO = 0; // no info by default
   OPT_MODIFICATION = 0; // sort by last modification time
   OPT_MORE_INFO = 0;   
   OPT_NATURAL_SORT = 0; // natural sort
   OPT_ORDER = 0; // default order (directories first)
   OPT_RANDOM = 0; // random sort order
   OPT_REVERSE = 1; // default (1) is normal order, use -1 for reverse order.
   OPT_VERSION = 0; // no version information by default

   // empty string lists for inclusion and exclusion of dirs
   OPT_INCL_DIRS = newStringList();
   if (!OPT_INCL_DIRS)
   {
      myerror("Could not create stringList!");
      return -1;
   }
   OPT_INCL_DIRS_REC = newStringList();
   if (!OPT_INCL_DIRS_REC)
   {
      myerror("Could not create stringList!");
      freeOptions();
      return -1;
   }
   OPT_EXCL_DIRS = newStringList();
   if (!OPT_EXCL_DIRS)
   {
      myerror("Could not create stringList!");
      freeOptions();
      return -1;
   }
   OPT_EXCL_DIRS_REC = newStringList();
   if (!OPT_EXCL_DIRS_REC)
   {
      myerror("Could not create stringList!");
      freeOptions();
      return -1;
   }

   // empty string list for to be ignored prefixes
   OPT_IGNORE_PREFIXES_LIST = newStringList();
   if (!OPT_IGNORE_PREFIXES_LIST)
   {
      myerror("Could not create stringList!");
      freeOptions();
      return -1;
   }
   opterr = 0;
   while
   (
      -1 != j = getopt_long(argc, argv, "imvhco:lrRnd:D:x:X:I:ta", longOpts, 0)
   )
   {
      switch (j) {
      case 'a':
         OPT_ASCII = 1;
         break;
      case 'c':
         OPT_IGNORE_CASE = 1;
         break;
      case 'h':
         OPT_HELP = 1;
         break;
      case 'i':
         OPT_INFO = 1;
         break;
      case 'm':
         OPT_MORE_INFO = 1;
         break;
      case 'l':
         OPT_LIST = 1;
         break;
      case 'o':
         switch (optarg[0]) {
         case 'd':
            OPT_ORDER = 0;
            break;
         case 'f':
            OPT_ORDER = 1;
            break;
         case 'a':
            OPT_ORDER = 2;
            break;
         default:
            myerror("Unknown flag '%c' for option 'o'.", optarg[0]);
            myerror("Use -h for more help.");
            freeOptions();
            return -1;
         }
         break;
      case 'd':
         if (addDirPathToStringList(
            OPT_INCL_DIRS, (const char (*)[PATH_MAX + 1]) optarg
         ))
         {
            myerror("Could not add directory path to dirPathList");
            freeOptions();
            return -1;
         }
         break;
      case 'D':
         if (addDirPathToStringList(
            OPT_INCL_DIRS_REC, (const char (*)[PATH_MAX + 1]) optarg
         ))
         {
            myerror("Could not add directory path to string list");
            freeOptions();
            return -1;
         }
         break;
      case 'x':
         if (addDirPathToStringList(
            OPT_EXCL_DIRS, (const char (*)[PATH_MAX + 1]) optarg
         ))
         {
            myerror("Could not add directory path to string list");
            freeOptions();
            return -1;
         }
         break;
      case 'X':
         if (addDirPathToStringList(
            OPT_EXCL_DIRS_REC, (const char (*)[PATH_MAX + 1]) optarg
         ))
         {
            myerror("Could not add directory path to string list");
            freeOptions();
            return -1;
         }
         break;
      case 'I':
         if (addStringToStringList(OPT_IGNORE_PREFIXES_LIST, optarg))
         {
            myerror("Could not add directory path to string list");
            freeOptions();
            return -1;
         }
         break;
      case 'n':
         OPT_NATURAL_SORT = 1;
         break;
      case 'r':
         OPT_REVERSE = -1;
         break;
      case 'R':
         OPT_RANDOM = 1;
         break;
      case 't':
         OPT_MODIFICATION = 1;
         break;
      case 'v':
         OPT_VERSION = 1;
         break;
      default:
         myerror("Unknown option '%c'.", optopt);
         myerror("Use -h for more help.");
         freeOptions();
         return -1;
      }
   }
   return 0;
}