/** * Everything begins "here" * * @param argc The number of command line arguments * @param argv Command line arguments * @return Zero on and only on success */ int main(int argc, char* argv[]) { int rc = 0; size_t first, diff, end; args_init((char*)"Rotate images in a pattern", (char*)"crazy-rotate [--] <first> <gaps+1> [<last>]", NULL, NULL, 1, 0, args_standard_abbreviations); args_add_option(args_new_argumentless(NULL, 0, (char*)"--help", NULL), (char*)"Prints this help message"); args_parse(argc, argv); args_support_alternatives(); if (args_opts_used((char*)"--help")) { args_help(); goto exit; } if (args_unrecognised_count || (args_files_count < 2) || (args_files_count > 3)) goto invalid_opts; first = parse_size(args_files[0]); diff = parse_size(args_files[1]); end = args_files_count == 3 ? parse_size(args_files[2]) : (SIZE_MAX - 1); if (!first || !diff || !end) goto invalid_opts; if (end++ == SIZE_MAX) t ((errno = ERANGE)); t (perform_rotate(first, diff, end)); exit: args_dispose(); return rc; invalid_opts: args_help(); fail: if (errno) perror(*argv); rc = 1; goto exit; }
ARGPARSEcode subcommand_help(const subcommand *scmd, FILE *stream) { if (!scmd || !stream) return ARGPARSE_PASSED_NULL; fprintf(stream, "%s:\n%s", scmd->name); if (scmd->args) return args_help(scmd->args, stream); return ARGPARSE_PASSED_NULL; }
int args_parse (args_t *args, int argc, char *argv[]) { int c; args->svg_filename = "-"; args->png_filename = "-"; args->scale = 1.0; args->width = -1; args->height = -1; args->flipx = 0; args->flipy = 0; while (1) { c = getopt_long (argc, argv, args_optstring, args_options, NULL); if (c == -1) break; switch (c) { case 'h': args->height = atoi (optarg); break; case 's': args->scale = atof (optarg); break; case 'w': args->width = atoi (optarg); break; case 'V': printf ("%s\n", ARGS_PROGRAM_VERSION); exit (0); break; case ARGS_VAL_FLIPX: args->flipx = 1; break; case ARGS_VAL_FLIPY: args->flipy = 1; break; case ARGS_VAL_HELP: args_help (argv[0]); exit (0); break; case '?': args_help (argv[0]); exit (1); break; default: fprintf (stderr, "Unhandled option: %d\n", c); exit (1); break; } } if (argc - optind >= 1) { args->svg_filename = argv[optind++]; if (argc - optind >= 1) { args->png_filename = argv[optind++]; if (argc - optind > 0) { args_usage (argv[0]); exit (1); } } } return 0; }
int main(int argc, char** argv_) { int found = 0; int first_pid = 1; char* usage_str; ssize_t i, n; argv = argv_; setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); n = (ssize_t)(strlen(_(" [options] [program...]")) + strlen(*argv) + 1); usage_str = alloca((size_t)n * sizeof(char)); sprintf(usage_str, "%s%s", *argv, _(" [options] [program...]")); args_init(!strcmp(argv[0], "dpidof") ? _("epidof with display isolation") : _("pidof with environment constraints"), usage_str, NULL, 0, 1, 0, args_standard_abbreviations); args_add_option(args_new_argumentless(NULL, 0, "-c", "--check-root", NULL), _("Restrict to processes running under the same root")); args_add_option(args_new_argumentless(NULL, 0, "-s", "--single-shot", NULL), _("Return only one process ID")); args_add_option(args_new_argumentless(NULL, 0, "-x", "--scripts", NULL), _("Test the name of scripts")); args_add_option(args_new_argumented (NULL, _("PID"), 0, "-o", "--omit-pid", NULL), _("Do not return a specific process ID")); args_add_option(args_new_argumentless(NULL, 0, "-h", "--help", NULL), _("Display this help information")); args_add_option(args_new_argumentless(NULL, 0, "-V", "--version", NULL), _("Print the name and version of this program")); environment_parse(&argc, argv); args_parse(argc, argv); if (args_unrecognised_count || args_opts_used("-h")) args_help(), fprintf(stderr, "%s\n\n", _(environment_synopsis)); else if (args_opts_used("-V")) printf("%s " VERSION, !strcmp(argv[0], "dpidof") ? "dpidof" : "epidof"); else goto cont; return args_unrecognised_count ? EXIT_FAILURE : EXIT_SUCCESS; cont: /* process command-line options */ if (args_opts_used("-s")) opt_single_shot = 1; if (args_opts_used("-x")) opt_scripts_too = 1; if (args_opts_used("-c") && (geteuid() == 0)) epidof_root = pid_link(getpid(), "root"); if (args_opts_used("-o")) { char** arr = args_opts_get("-o"); for (i = 0, n = (ssize_t)args_opts_get_count("-o"); i < n; i++) add_to_omit_list(arr[i]); } /* main loop */ for (n = 0; n < args_files_count; n++) /* for each program */ { program = args_files[n]; proc_count = 0; select_procs(); /* get the list of matching processes */ if (proc_count > 0) { found = 1; for (i = (ssize_t)proc_count - 1; i >= 0; i--) /* and display their PIDs */ { printf(first_pid ? "%ld" : " %ld", (long)(procs[i])); first_pid = 0; if (opt_single_shot) break; } } } /* final line feed */ if (found) printf("\n"); xfree(procs); xfree(omitted_procs); xfree(epidof_root); args_dispose(); environment_dispose(); return !found; }