int main (int argc, char **argv) { /* Recognize --help or --version only if it's the only command-line argument. */ if (argc == 2) { initialize_main (&argc, &argv); set_program_name (argv[0]); setlocale (LC_ALL, ""); bindtextdomain (PACKAGE, LOCALEDIR); textdomain (PACKAGE); atexit (close_stdout); if (STREQ (argv[1], "--help")) usage (EXIT_STATUS); if (STREQ (argv[1], "--version")) version_etc (stdout, PROGRAM_NAME, PACKAGE_NAME, Version, AUTHORS, (char *) NULL); } exit (EXIT_STATUS); }
int main (int argc, char **argv) { initialize_main (&argc, &argv); set_program_name (argv[0]); setlocale (LC_ALL, ""); bindtextdomain (PACKAGE, LOCALEDIR); textdomain (PACKAGE); atexit (close_stdout); parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version, usage, AUTHORS, (char const *) NULL); if (getopt_long (argc, argv, "", NULL, NULL) != -1) usage (EXIT_FAILURE); if (argc < optind + 1) { error (0, 0, _("missing operand")); usage (EXIT_FAILURE); } if (optind + 1 < argc) { error (0, 0, _("extra operand %s"), quote (argv[optind + 1])); usage (EXIT_FAILURE); } if (unlink (argv[optind]) != 0) error (EXIT_FAILURE, errno, _("cannot unlink %s"), quote (argv[optind])); exit (EXIT_SUCCESS); }
int main (int argc, char **argv) { long int id; initialize_main (&argc, &argv); program_name = argv[0]; setlocale (LC_ALL, ""); bindtextdomain (PACKAGE, LOCALEDIR); textdomain (PACKAGE); atexit (close_stdout); parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION, usage, AUTHORS, (char const *) NULL); if (argc > 1) { error (0, 0, _("too many arguments")); usage (EXIT_FAILURE); } id = gethostid (); printf ("%lx\n", id); exit (EXIT_SUCCESS); }
int main (int argc, char **argv) { char limit[1 + MAX (INT_BUFSIZE_BOUND (intmax_t), INT_BUFSIZE_BOUND (uintmax_t))]; initialize_main (&argc, &argv); set_program_name (argv[0]); setlocale (LC_ALL, ""); bindtextdomain (PACKAGE, LOCALEDIR); textdomain (PACKAGE); initialize_exit_failure (EXIT_FAILURE); atexit (close_stdout); parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, VERSION, usage, AUTHORS, (char const *) NULL); #define print_int(TYPE) \ sprintf (limit + 1, "%"PRIuMAX, (uintmax_t) TYPE##_MAX); \ printf (#TYPE"_MAX=%s\n", limit + 1); \ printf (#TYPE"_OFLOW=%s\n", decimal_absval_add_one (limit)); \ if (TYPE##_MIN) \ { \ sprintf (limit + 1, "%"PRIdMAX, (intmax_t) TYPE##_MIN); \ printf (#TYPE"_MIN=%s\n", limit + 1); \ printf (#TYPE"_UFLOW=%s\n", decimal_absval_add_one (limit)); \ } #define print_float(TYPE) \ printf (#TYPE"_MIN=%Le\n", (long double)TYPE##_MIN); \ printf (#TYPE"_MAX=%Le\n", (long double)TYPE##_MAX); /* Variable sized ints */ print_int (CHAR); print_int (SCHAR); print_int (UCHAR); print_int (SHRT); print_int (INT); print_int (UINT); print_int (LONG); print_int (ULONG); print_int (SIZE); print_int (SSIZE); print_int (TIME_T); print_int (UID_T); print_int (GID_T); print_int (PID_T); print_int (OFF_T); print_int (INTMAX); print_int (UINTMAX); /* Variable sized floats */ print_float (FLT); print_float (DBL); print_float (LDBL); }
int main (int argc, char **argv) { int i; double seconds = 0.0; bool ok = true; initialize_main (&argc, &argv); set_program_name (argv[0]); setlocale (LC_ALL, ""); bindtextdomain (PACKAGE, LOCALEDIR); textdomain (PACKAGE); atexit (close_stdout); parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version, usage, AUTHORS, (char const *) NULL); if (getopt_long (argc, argv, "", NULL, NULL) != -1) usage (EXIT_FAILURE); if (argc == 1) { error (0, 0, _("missing operand")); usage (EXIT_FAILURE); } for (i = optind; i < argc; i++) { double s; const char *p; if (! xstrtod (argv[i], &p, &s, c_strtod) /* Nonnegative interval. */ || ! (0 <= s) /* No extra chars after the number and an optional s,m,h,d char. */ || (*p && *(p+1)) /* Check any suffix char and update S based on the suffix. */ || ! apply_suffix (&s, *p)) { error (0, 0, _("invalid time interval %s"), quote (argv[i])); ok = false; } seconds += s; } if (!ok) usage (EXIT_FAILURE); if (xnanosleep (seconds)) error (EXIT_FAILURE, errno, _("cannot read realtime clock")); return EXIT_SUCCESS; }
CHECK_TYPE GLOBAL(PROGRAM_NAME_LOC) { char *hostname; initialize_main (&argc, &argv); set_program_name (argv[0]); setlocale (LC_ALL, ""); bindtextdomain (PACKAGE, LOCALEDIR); textdomain (PACKAGE); atexit (close_stdout); parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version, usage, AUTHORS, (char const *) NULL); if (getopt_long (argc, argv, "", NULL, NULL) != -1) usage (EXIT_FAILURE); if (argc == optind + 1) { #ifdef HAVE_SETHOSTNAME /* Set hostname to operand. */ char const *name = argv[optind]; if (sethostname (name, strlen (name)) != 0) error (EXIT_FAILURE, errno, _("cannot set name to %s"), quote (name)); #else error (EXIT_FAILURE, 0, _("cannot set hostname; this system lacks the functionality")); #endif } if (argc <= optind) { hostname = xgethostname (); if (hostname == NULL) error (EXIT_FAILURE, errno, _("cannot determine hostname")); printf ("%s\n", hostname); } if (optind + 1 < argc) { error (0, 0, _("extra operand %s"), quote (argv[optind + 1])); usage (EXIT_FAILURE); } exit (EXIT_SUCCESS); return 0; }
CHECK_TYPE GLOBAL(PROGRAM_NAME_LOC) { initialize_main (&argc, &argv); set_program_name (argv[0]); setlocale (LC_ALL, ""); bindtextdomain (PACKAGE, LOCALEDIR); textdomain (PACKAGE); atexit (close_stdout); parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version, usage, AUTHORS, (char const *) NULL); if (getopt_long (argc, argv, "", NULL, NULL) != -1) usage (EXIT_FAILURE); if (argc < optind + 1) { error (0, 0, _("missing operand")); usage (EXIT_FAILURE); return 0; } if (optind + 1 < argc) { error (0, 0, _("extra operand %s"), quote (argv[optind + 1])); usage (EXIT_FAILURE); return 0; } // pmr: Annotation. We know all the pertinenet facts about optind at this point, // but we don't know that unlink doesn't modify optind, so we fold it. int pmr_optind = optind; if (unlink (argv[optind]) != 0) error (EXIT_FAILURE, errno, _("cannot unlink %s"), quote (argv[pmr_optind])); exit (EXIT_SUCCESS); return 0; }
int main (int argc, char *argv[]) { int opt; char const *prog; exit_failure = EXIT_TROUBLE; initialize_main (&argc, &argv); program_name = argv[0]; setlocale (LC_ALL, ""); bindtextdomain (PACKAGE, LOCALEDIR); textdomain (PACKAGE); c_stack_action (cleanup); prog = getenv ("EDITOR"); if (prog) editor_program = prog; diffarg (DEFAULT_DIFF_PROGRAM); /* parse command line args */ while ((opt = getopt_long (argc, argv, "abBdEHiI:lo:stvw:W", longopts, 0)) != -1) { switch (opt) { case 'a': diffarg ("-a"); break; case 'b': diffarg ("-b"); break; case 'B': diffarg ("-B"); break; case 'd': diffarg ("-d"); break; case 'E': diffarg ("-E"); break; case 'H': diffarg ("-H"); break; case 'i': diffarg ("-i"); break; case 'I': diffarg ("-I"); diffarg (optarg); break; case 'l': diffarg ("--left-column"); break; case 'o': output = optarg; break; case 's': suppress_common_lines = true; break; case 't': diffarg ("-t"); break; case 'v': version_etc (stdout, "sdiff", PACKAGE_NAME, PACKAGE_VERSION, "Thomas Lord", (char *) 0); check_stdout (); return EXIT_SUCCESS; case 'w': diffarg ("-W"); diffarg (optarg); break; case 'W': diffarg ("-w"); break; case DIFF_PROGRAM_OPTION: diffargv[0] = optarg; break; case HELP_OPTION: usage (); check_stdout (); return EXIT_SUCCESS; case STRIP_TRAILING_CR_OPTION: diffarg ("--strip-trailing-cr"); break; case TABSIZE_OPTION: diffarg ("--tabsize"); diffarg (optarg); break; default: try_help (0, 0); } } if (argc - optind != 2) { if (argc - optind < 2) try_help ("missing operand after `%s'", argv[argc - 1]); else try_help ("extra operand `%s'", argv[optind + 2]); } if (! output) { /* easy case: diff does everything for us */ if (suppress_common_lines) diffarg ("--suppress-common-lines"); diffarg ("-y"); diffarg ("--"); diffarg (argv[optind]); diffarg (argv[optind + 1]); diffarg (0); execvp (diffargv[0], (char **) diffargv); perror_fatal (diffargv[0]); } else { char const *lname, *rname; FILE *left, *right, *out, *diffout; bool interact_ok; struct line_filter lfilt; struct line_filter rfilt; struct line_filter diff_filt; bool leftdir = diraccess (argv[optind]); bool rightdir = diraccess (argv[optind + 1]); if (leftdir & rightdir) fatal ("both files to be compared are directories"); lname = expand_name (argv[optind], leftdir, argv[optind + 1]); left = ck_fopen (lname, "r"); rname = expand_name (argv[optind + 1], rightdir, argv[optind]); right = ck_fopen (rname, "r"); out = ck_fopen (output, "w"); diffarg ("--sdiff-merge-assist"); diffarg ("--"); diffarg (argv[optind]); diffarg (argv[optind + 1]); diffarg (0); trapsigs (); #if ! (HAVE_WORKING_FORK || HAVE_WORKING_VFORK) { size_t cmdsize = 1; char *p, *command; int i; for (i = 0; diffargv[i]; i++) cmdsize += quote_system_arg (0, diffargv[i]) + 1; command = p = xmalloc (cmdsize); for (i = 0; diffargv[i]; i++) { p += quote_system_arg (p, diffargv[i]); *p++ = ' '; } p[-1] = 0; errno = 0; diffout = popen (command, "r"); if (! diffout) perror_fatal (command); free (command); } #else { int diff_fds[2]; # if HAVE_WORKING_VFORK sigset_t procmask; sigset_t blocked; # endif if (pipe (diff_fds) != 0) perror_fatal ("pipe"); # if HAVE_WORKING_VFORK /* Block SIGINT and SIGPIPE. */ sigemptyset (&blocked); sigaddset (&blocked, SIGINT); sigaddset (&blocked, SIGPIPE); sigprocmask (SIG_BLOCK, &blocked, &procmask); # endif diffpid = vfork (); if (diffpid < 0) perror_fatal ("fork"); if (! diffpid) { /* Alter the child's SIGINT and SIGPIPE handlers; this may munge the parent. The child ignores SIGINT in case the user interrupts the editor. The child does not ignore SIGPIPE, even if the parent does. */ if (initial_handler (handler_index_of_SIGINT) != SIG_IGN) signal_handler (SIGINT, SIG_IGN); signal_handler (SIGPIPE, SIG_DFL); # if HAVE_WORKING_VFORK /* Stop blocking SIGINT and SIGPIPE in the child. */ sigprocmask (SIG_SETMASK, &procmask, 0); # endif close (diff_fds[0]); if (diff_fds[1] != STDOUT_FILENO) { dup2 (diff_fds[1], STDOUT_FILENO); close (diff_fds[1]); } execvp (diffargv[0], (char **) diffargv); _exit (errno == ENOENT ? 127 : 126); } # if HAVE_WORKING_VFORK /* Restore the parent's SIGINT and SIGPIPE behavior. */ if (initial_handler (handler_index_of_SIGINT) != SIG_IGN) signal_handler (SIGINT, catchsig); if (initial_handler (handler_index_of_SIGPIPE) != SIG_IGN) signal_handler (SIGPIPE, catchsig); else signal_handler (SIGPIPE, SIG_IGN); /* Stop blocking SIGINT and SIGPIPE in the parent. */ sigprocmask (SIG_SETMASK, &procmask, 0); # endif close (diff_fds[1]); diffout = fdopen (diff_fds[0], "r"); if (! diffout) perror_fatal ("fdopen"); } #endif lf_init (&diff_filt, diffout); lf_init (&lfilt, left); lf_init (&rfilt, right); interact_ok = interact (&diff_filt, &lfilt, lname, &rfilt, rname, out); ck_fclose (left); ck_fclose (right); ck_fclose (out); { int wstatus; int werrno = 0; #if ! (HAVE_WORKING_FORK || HAVE_WORKING_VFORK) wstatus = pclose (diffout); if (wstatus == -1) werrno = errno; #else ck_fclose (diffout); while (waitpid (diffpid, &wstatus, 0) < 0) if (errno == EINTR) checksigs (); else perror_fatal ("waitpid"); diffpid = 0; #endif if (tmpname) { unlink (tmpname); tmpname = 0; } if (! interact_ok) exiterr (); check_child_status (werrno, wstatus, EXIT_FAILURE, diffargv[0]); untrapsig (0); checksigs (); exit (WEXITSTATUS (wstatus)); } } return EXIT_SUCCESS; /* Fool `-Wall'. */ }
int main (int argc, char **argv) { int optc; bool ok = true; gid_t rgid, egid; uid_t ruid; initialize_main (&argc, &argv); set_program_name (argv[0]); setlocale (LC_ALL, ""); bindtextdomain (PACKAGE, LOCALEDIR); textdomain (PACKAGE); atexit (close_stdout); /* Processing the arguments this way makes groups.c behave differently to * groups.sh if one of the arguments is "--". */ while ((optc = getopt_long (argc, argv, "", longopts, NULL)) != -1) { switch (optc) { case_GETOPT_HELP_CHAR; case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS); default: usage (EXIT_FAILURE); } } if (optind == argc) { /* No arguments. Divulge the details of the current process. */ ruid = getuid (); egid = getegid (); rgid = getgid (); if (!print_group_list (NULL, ruid, rgid, egid, true)) ok = false; putchar ('\n'); } else { /* At least one argument. Divulge the details of the specified users. */ while (optind < argc) { struct passwd *pwd = getpwnam (argv[optind]); if (pwd == NULL) error (EXIT_FAILURE, 0, _("%s: No such user"), argv[optind]); ruid = pwd->pw_uid; rgid = egid = pwd->pw_gid; printf ("%s : ", argv[optind]); if (!print_group_list (argv[optind++], ruid, rgid, egid, true)) ok = false; putchar ('\n'); } } exit (ok ? EXIT_SUCCESS : EXIT_FAILURE); }