static void strtotimeval(const char *str, struct timeval *tv) { double user_input; user_input = strtod_or_err(str, "bad number"); tv->tv_sec = (time_t) user_input; tv->tv_usec = (long)((user_input - tv->tv_sec) * 1000000); if ((tv->tv_sec + tv->tv_usec) == 0) errx(EX_USAGE, _("timeout cannot be zero")); }
static double getnum(const char *s) { const double d = strtod_or_err(s, _("failed to parse number")); if (isnan(d)) { errno = EINVAL; err(EXIT_FAILURE, "%s: %s", _("failed to parse number"), s); } return d; }
/*! \brief Start of execution. Parse options, and call other other * functions one after another. At the moment adding threading support * would be difficult, but there does not seem to be valid reason to * consider that. Overall the analysis already quick enough even without * making it parallel. * * \return Return value indicates success or fail or analysis, unless * either --warning or --critical options are in use, which makes the * return value in some cases to match with Nagios expectations about * alarming. */ int main(int argc, char **argv) { int i, sorts = 0; int option_index = 0; char const *tmp; struct range_t *tmp_ranges; enum { OPT_WARN = CHAR_MAX + 1, OPT_CRIT }; int ret_val; /* Options for getopt_long */ static struct option const long_options[] = { {"config", required_argument, NULL, 'c'}, {"leases", required_argument, NULL, 'l'}, {"format", required_argument, NULL, 'f'}, {"sort", required_argument, NULL, 's'}, {"reverse", no_argument, NULL, 'r'}, {"output", required_argument, NULL, 'o'}, {"limit", required_argument, NULL, 'L'}, {"version", no_argument, NULL, 'v'}, {"help", no_argument, NULL, 'h'}, {"warning", required_argument, NULL, OPT_WARN}, {"critical", required_argument, NULL, OPT_CRIT}, {NULL, 0, NULL, 0} }; atexit(close_stdout); /* FIXME: These allocations should be fully dynamic, e.g., grow * if needed. */ config.dhcpdconf_file = xmalloc(sizeof(char) * MAXLEN); config.dhcpdlease_file = xmalloc(sizeof(char) * MAXLEN); config.output_file = xmalloc(sizeof(char) * MAXLEN); /* Make sure string has zero length if there is no * command line option */ config.output_file[0] = '\0'; /* Alarming defaults. */ config.warning = ALARM_WARN; config.critical = ALARM_CRIT; /* File location defaults */ strncpy(config.dhcpdconf_file, DHCPDCONF_FILE, MAXLEN - 1); strncpy(config.dhcpdlease_file, DHCPDLEASE_FILE, MAXLEN - 1); tmp = OUTPUT_LIMIT; config.output_limit[0] = (*tmp - '0'); tmp++; config.output_limit[1] = (*tmp - '0'); config.fullhtml = false; /* Make sure some output format is selected by default */ strncpy(config.output_format, OUTPUT_FORMAT, (size_t)1); /* Default sort order is by IPs small to big */ config.reverse_order = false; config.backups_found = false; /* Parse command line options */ while (1) { int c; c = getopt_long(argc, argv, "c:l:f:o:s:rL:vh", long_options, &option_index); if (c == EOF) break; switch (c) { case 'c': /* config file */ strncpy(config.dhcpdconf_file, optarg, MAXLEN - 1); break; case 'l': /* lease file */ strncpy(config.dhcpdlease_file, optarg, MAXLEN - 1); break; case 'f': /* Output format */ strncpy(config.output_format, optarg, (size_t)1); break; case 's': /* Output sorting option */ sorts = strlen(optarg); if (5 < sorts) { warnx ("main: only first 5 sort orders will be used"); strncpy(config.sort, optarg, (size_t)5); sorts = 5; } else { strncpy(config.sort, optarg, (size_t)sorts); } for (i = 0; i < sorts; i++) { field_selector(config.sort[i]); } break; case 'r': /* What ever sort in reverse order */ config.reverse_order = true; break; case 'o': /* Output file */ strncpy(config.output_file, optarg, MAXLEN - 1); break; case 'L': /* Specification what will be printed */ for (i = 0; i < 2; i++) { if (optarg[i] >= '0' && optarg[i] < '8') { config.output_limit[i] = optarg[i] - '0'; } else { errx(EXIT_FAILURE, "main: output mask `%s' is illegal", optarg); } } break; case OPT_WARN: strcpy(config.output_format, "a"); config.warning = strtod_or_err(optarg, "illegal argument"); break; case OPT_CRIT: strcpy(config.output_format, "a"); config.critical = strtod_or_err(optarg, "illegal argument"); break; case 'v': /* Print version */ print_version(); case 'h': /* Print help */ usage(EXIT_SUCCESS); default: errx(EXIT_FAILURE, "Try `%s --help' for more information.", program_invocation_short_name); } } /* Output function selection */ switch (config.output_format[0]) { case 't': output_analysis = output_txt; break; case 'a': output_analysis = output_alarming; break; case 'h': output_analysis = output_html; break; case 'H': output_analysis = output_html; config.fullhtml = true; break; case 'x': output_analysis = output_xml; break; case 'X': output_analysis = output_xml; break; case 'j': output_analysis = output_json; break; case 'J': output_analysis = output_json; break; case 'c': output_analysis = output_csv; break; default: errx(EXIT_FAILURE, "main: unknown output format `%c'", config.output_format[0]); } /* Do the job */ prepare_memory(); parse_config(true, config.dhcpdconf_file, shared_networks); parse_leases(); prepare_data(); do_counting(); tmp_ranges = xmalloc(sizeof(struct range_t) * num_ranges); if (sorts != 0) { mergesort_ranges(ranges, num_ranges, tmp_ranges); } if (config.reverse_order == true) { flip_ranges(ranges, tmp_ranges); } free(tmp_ranges); ret_val = output_analysis(); clean_up(); return (ret_val); }
int main(int argc, char **argv) { int lines, row, col = 0; int i, opt; double av[3]; static double max_scale = 0, scale_fact; long tmpdly; static const struct option longopts[] = { {"scale", required_argument, NULL, 's'}, {"delay", required_argument, NULL, 'd'}, {"help", no_argument, NULL, 'h'}, {"version", no_argument, NULL, 'V'}, {NULL, 0, NULL, 0} }; #ifdef HAVE_PROGRAM_INVOCATION_NAME program_invocation_name = program_invocation_short_name; #endif setlocale (LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); atexit(close_stdout); while ((opt = getopt_long(argc, argv, "s:d:Vh", longopts, NULL)) != -1) switch (opt) { case 's': max_scale = strtod_or_err(optarg, _("failed to parse argument")); if (max_scale < 0) xerrx(EXIT_FAILURE, _("scale cannot be negative")); break; case 'd': tmpdly = strtol_or_err(optarg, _("failed to parse argument")); if (tmpdly < 1) xerrx(EXIT_FAILURE, _("delay must be positive integer")); else if (UINT_MAX < tmpdly) xerrx(EXIT_FAILURE, _("too large delay value")); dly = tmpdly; break; case 'V': printf(PROCPS_NG_VERSION); return EXIT_SUCCESS; break; case 'h': usage(stdout); default: usage(stderr); } if (argc > optind) if ((fd = open(argv[optind], 1)) == -1) xerr(EXIT_FAILURE, _("can not open tty")); setsize(0); if (max_scale == 0) max_scale = nrows; scale_fact = max_scale; setjmp(jb); col = 0; alrm(0); while (1) { if (scale_fact < max_scale) scale_fact *= 2.0; /* help it drift back up. */ loadavg(&av[0], &av[1], &av[2]); do { lines = av[0] * scale_fact; row = nrows - 1; while (0 <= --lines) { *(screen + row * ncols + col) = '*'; if (--row < 0) { scale_fact /= 2.0; break; } } } while (0 <= lines); while (row >= 0) *(screen + row-- * ncols + col) = ' '; for (i = 1;; ++i) { char *p; row = nrows - (i * scale_fact); if (row < 0) break; if (*(p = screen + row * ncols + col) == ' ') *p = '-'; else *p = '='; } if (++col == ncols) { --col; memmove(screen, screen + 1, scr_size - 1); for (row = nrows - 2; row >= 0; --row) *(screen + row * ncols + col) = ' '; } i = sprintf(screen, " %.2f, %.2f, %.2f", av[0], av[1], av[2]); if (i > 0) screen[i] = ' '; if (write(fd, "\033[H", 3) < 0) xerr(EXIT_FAILURE, _("writing to tty failed")); if (write(fd, screen, scr_size - 1) < 0) xerr(EXIT_FAILURE, _("writing to tty failed")); pause(); } }
static struct libscols_column *parse_column(FILE *f) { size_t len = 0; char *line = NULL; int nlines = 0; struct libscols_column *cl = NULL; while (getline(&line, &len, f) != -1) { char *p = strrchr(line, '\n'); if (p) *p = '\0'; switch (nlines) { case 0: /* NAME */ { struct libscols_cell *hr; cl = scols_new_column(); if (!cl) goto fail; hr = scols_column_get_header(cl); if (!hr || scols_cell_set_data(hr, line)) goto fail; break; } case 1: /* WIDTH-HINT */ { double whint = strtod_or_err(line, "failed to parse column whint"); if (scols_column_set_whint(cl, whint)) goto fail; break; } case 2: /* FLAGS */ { int num_flags = parse_column_flags(line); if (scols_column_set_flags(cl, num_flags)) goto fail; if (strcmp(line, "wrapnl") == 0) { scols_column_set_wrapfunc(cl, scols_wrapnl_chunksize, scols_wrapnl_nextchunk, NULL); scols_column_set_safechars(cl, "\n"); } break; } case 3: /* COLOR */ if (scols_column_set_color(cl, line)) goto fail; break; default: break; } nlines++; } free(line); return cl; fail: free(line); scols_unref_column(cl); return NULL; }