Skip to content
This repository
tree: ec65bf48f4
Fetching contributors…

Cannot retrieve contributors at this time

file 52 lines (46 sloc) 1.243 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
#include "xdo_cmd.h"
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>

int cmd_sleep(context_t *context) {
  char *cmd = *context->argv;
  int ret = EXIT_SUCCESS;
  int c;
  double duration_usec;

  typedef enum {
    opt_unused, opt_help
  } optlist_t;
  static struct option longopts[] = {
    { "help", no_argument, NULL, opt_help },
    { 0, 0, 0, 0 },
  };
  static const char *usage =
    "Usage: %s seconds\n" \
    "Sleep a given number of seconds. Fractions of seconds are valid.\n";
  
  int option_index;
  while ((c = getopt_long_only(context->argc, context->argv, "+h",
                               longopts, &option_index)) != -1) {
    switch (c) {
      case 'h':
      case opt_help:
        printf(usage, cmd);
        consume_args(context, context->argc);
        return EXIT_SUCCESS;
        break;
      default:
        fprintf(stderr, usage, cmd);
        return EXIT_FAILURE;
    }
  }

  consume_args(context, optind);

  if (context->argc == 0) {
    fprintf(stderr, "No arguments given.\n");
    fprintf(stderr, usage, cmd);
    return EXIT_FAILURE;
  }

  duration_usec = atof(context->argv[0]) * (1000000);
  usleep(duration_usec);
  consume_args(context, 1);
  return ret;
}
Something went wrong with that request. Please try again.