Example #1
0
int main(int argc, char **argv) {
    int opt;

    while ((opt = sgetopt(argc, argv, "ab:")) != -1) {
        switch (opt) {
        case 'a':
            bio_put_str(bio_1, "a\n");
            break;
        case 'b':
            bio_put_str(bio_1, soptarg);
            bio_put_str(bio_1, "\n");
            break;
        default:
            break;
        }
    }

    bio_flush(bio_1);

    return 0;
}
Example #2
0
int main(int argc, char **argv, char **envp) {
  array_t array;
  bio_t bin;
  char bin_data[8192];
  bio_t *bio;
  int fd;
  char *name;
  int opt;
  ssize_t r;
  struct stat st;
  char *type = 0;
  
  while ((opt = sgetopt(argc, argv, "t:")) != -1) {
    switch (opt) {
      case '?':
        usage();
      case 't':
        type = soptarg;
        break;
    }
  }
  argv += soptind;
  
  if (!type)
    usage();
    
  array_init(&array, 1);
  
  /* Set the initial buffered io pointer to use stdin and set the default name
   * to '-'. */
  bio = bio_0;
  name = "-";
  for (;;) {
    if (*argv) {
      /* If a path was given then check to see if it's a directory and skip
       * over it if it is. */
      if (stat(*argv, &st))
        err(1, "fatal");
      if (S_ISDIR(st.st_mode)) {
        errno = EISDIR;
        warn(*argv);
        argv++;
        continue;
      }
      
      /* Open the file for reading and construct our own buffered io struct to
       * use for reading in the file and hashing it.  Also set the default
       * buffered io pointer to our own and update the name to reflect the
       * current file. */
      fd = open_read(*argv);
      if (fd == -1)
        err(1, "fatal");
      bio_init(&bin, read, fd, bin_data, sizeof(bin_data));
      bio = &bin;
      name = *argv;
    }
    /* If there's no more arguments passed on the command line and the default
     * buffered io pointer doesn't point to stdin then that means all of the
     * files have been hashed and it's time to exit. */
    else if ((bio != bio_0))
      break;
    
    if (!strcmp(type, "md4"))
      handle_md4(&array, bio);
    else if (!strcmp(type, "md5"))
      handle_md5(&array, bio);
    else if (!strcmp(type, "sha1"))
      handle_sha1(&array, bio);
    else if (!strcmp(type, "sha256"))
      handle_sha256(&array, bio);
    else if (!strcmp(type, "sha512"))
      handle_sha512(&array, bio);
    else
      errx(1, "fatal: %s", "unknown type");
      
    bio_put_str(bio_1, array_start(&array));
    bio_put_str(bio_1, "  ");
    bio_put_str(bio_1, name);
    bio_put_str(bio_1, "\n");
    bio_flush(bio_1);
    
    array_trunc(&array);
    
    if (*argv)
      close(fd);
    if ((!*argv) || (!*++argv))
      break;
  }
  
  return 0;
}
Example #3
0
File: svstart.c Project: dpejesh/sv
int main(int argc, char **argv, char **envp) {
  int opt;
  char *path;
  int path_len;
  char *path_log;
  int path_log_size;
  int r;
  sv_stat_t svst;
  
  config_init();
  
  while ((opt = sgetopt(argc, argv, "LlQqv")) != -1) {
    switch (opt) {
      case 'h':
        usage();
      case 'L':
        config.start = 0;
        config.start_log = 1;
        break;
      case 'l':
        config.start_log = 1;
        break;
      case 'Q':
        config.verbosity = 1;
        break;
      case 'q':
        config.verbosity = 0;
        break;
      case 'v':
        config.verbosity = 2;
        break;
      default:
        usage();
    }
  }
  argv += soptind;
  
  if (!*argv)
    usage();
    
  /* Disable buffer_2 so nothing gets outputted. */
  if (!config.verbosity)
    bio_2->fd = -1;
    
  while ((path = *argv++)) {
    if (config.start_log) {
      stdout_log4("Starting ", path, (path[strlen(path) - 1] == '/' ? "" : "/"),
                  "log ... ");
                  
      r = sv_start_log(path);
      if (r) {
        stdout_log("failed\n");
        strerr_die2x(100, FATAL, sv_error_str(r));
      }
      stdout_log("done\n");
    }
    
    if (config.start) {
      stdout_log3("Starting ", path, " ... ");
      r = sv_start(path);
      if (r) {
        stdout_log("failed\n");
        strerr_die2x(100, FATAL, sv_error_str(r));
      }
      stdout_log("done\n");
    }
  }
  
  _exit(0);
}
Example #4
0
File: s6-softlimit.c Project: 8l/s6
int main (int argc, char const *const *argv, char const *const *envp)
{
  PROG = "s6-softlimit" ;
  for (;;)
  {
    register int opt = sgetopt(argc, argv, "a:c:d:f:l:m:o:p:r:s:t:") ;
    if (opt == -1) break ;
    switch (opt)
    {
      case 'a' :
#ifdef RLIMIT_AS
        doit(RLIMIT_AS, subgetopt_here.arg) ;
#endif
#ifdef RLIMIT_VMEM
        doit(RLIMIT_VMEM, subgetopt_here.arg) ;
#endif
        break ;
      case 'c' :
#ifdef RLIMIT_CORE
        doit(RLIMIT_CORE, subgetopt_here.arg) ;
#endif
        break ;
      case 'd' :
#ifdef RLIMIT_DATA
        doit(RLIMIT_DATA, subgetopt_here.arg) ;
#endif
        break ;
      case 'f' :
#ifdef RLIMIT_FSIZE
        doit(RLIMIT_FSIZE, subgetopt_here.arg) ;
#endif
        break ;
      case 'l' :
#ifdef RLIMIT_MEMLOCK
        doit(RLIMIT_MEMLOCK, subgetopt_here.arg) ;
#endif
        break ;
      case 'm' :
#ifdef RLIMIT_DATA
        doit(RLIMIT_DATA, subgetopt_here.arg) ;
#endif
#ifdef RLIMIT_STACK
        doit(RLIMIT_STACK, subgetopt_here.arg) ;
#endif
#ifdef RLIMIT_MEMLOCK
        doit(RLIMIT_MEMLOCK, subgetopt_here.arg) ;
#endif
#ifdef RLIMIT_VMEM
        doit(RLIMIT_VMEM, subgetopt_here.arg) ;
#endif
#ifdef RLIMIT_AS
        doit(RLIMIT_AS, subgetopt_here.arg) ;
#endif
	break ;
      case 'o' :
#ifdef RLIMIT_NOFILE
        doit(RLIMIT_NOFILE, subgetopt_here.arg) ;
#endif
#ifdef RLIMIT_OFILE
        doit(RLIMIT_OFILE, subgetopt_here.arg) ;
#endif
        break ;
      case 'p' :
#ifdef RLIMIT_NPROC
        doit(RLIMIT_NPROC, subgetopt_here.arg) ;
#endif
        break ;
      case 'r' :
#ifdef RLIMIT_RSS
        doit(RLIMIT_RSS, subgetopt_here.arg) ;
#endif
        break ;
      case 's' :
#ifdef RLIMIT_STACK
        doit(RLIMIT_STACK, subgetopt_here.arg) ;
#endif
        break ;
      case 't' :
#ifdef RLIMIT_CPU
        doit(RLIMIT_CPU, subgetopt_here.arg) ;
#endif
        break ;
    }
  }
  argc -= subgetopt_here.ind ; argv += subgetopt_here.ind ;
  if (!argc) strerr_dieusage(100, USAGE) ;
  pathexec_run(argv[0], argv, envp) ;
  strerr_dieexec(111, argv[0]) ;
}
Example #5
0
int main(int argc, char **argv)
{
    /* Program options */
    static int help;
    static int version;
    static double lower_bound = 0./0.; /* NaN */
    static double upper_bound = 0./0.;
    static char stat_type = 'm';
    static int round_output = 0;
    static int floor_output = 0;
    static int minute_output = 0;

    double file_age;
    int check_upper,
        check_lower;
    int i;

    /* TODO: maybe change minutes / round / floor / trunc to -f "1.3m" -f "3.4s", etc */
    struct soption opttable[] = {
        { 'h', "help",    0, capture_presence,    &help },
        { 'v', "version", 0, capture_presence,    &version },
        { 'o', "older",   1, capture_duration,    &lower_bound },
        { 'n', "newer",   1, capture_duration,    &upper_bound },
        { 'a', "access",  0, capture_a,           &stat_type },
        { 'm', "modify",  0, capture_m,           &stat_type },
        { 'c', "change",  0, capture_c,           &stat_type },
        { 0,   "round",   0, capture_presence,    &round_output },
        { 0,   "floor",   0, capture_presence,    &floor_output },
        { 0,   "trunc",   0, capture_presence,    &floor_output },
        { 0,   "minutes", 0, capture_presence,    &minute_output },
        { 0,   0,         0, 0,                   0 }
    };

    if (sgetopt(argc, argv, opttable, argv+1, 0)) {
        fprintf(stderr,"%s: error parsing one of the command line options\n", basename(argv[0]));
        return 1;
    }

    if (help) {
        char *progname = basename(argv[0]);
        printf("Usage: %s [options] parameters...\n"
               "  check source or manpage `man %s' for details.\n", progname, progname);
        return 0;
    }

    if (version) {
        print_version(basename(argv[0]));
        return 0;
    }

    check_upper = upper_bound == upper_bound; /* != NaN */
    check_lower = lower_bound == lower_bound; /* != NaN */

    for (i=1; argv[i]; i++) {
        file_age = stat_age(argv[i],stat_type);
        if (file_age != file_age /* NaN */) {
            fprintf(stderr,"%s: error, unable to retrieve `%s' attributes\n", basename(argv[0]), argv[i]);
            return 1;
        }

        if (check_upper || check_lower) {
            if ((check_upper && file_age > upper_bound) ||
                    (check_lower && file_age < lower_bound))
                return 1;
        } else {
            if (minute_output)
                file_age /= 60.0;
            if (floor_output)
                file_age = floor(file_age);
            else if (round_output)
                file_age = round(file_age);
            printf(round_output || floor_output ? "%.0lf\n" : "%lf\n", file_age);
        }
    }

    return 0;
}