Added support of user namespaces
[untie.git] / cmdline.c
blob535870e244fde83a7a56f2d31138ed0fbc7f197a
1 /*
2 File autogenerated by gengetopt version 2.19.1
3 generated with the following command:
4 gengetopt -uCOMMAND --set-version=0.4
6 The developers of gengetopt consider the fixed text that goes in all
7 gengetopt output files to be in the public domain:
8 we make no copyright claims on it.
9 */
11 /* If we use autoconf. */
12 #ifdef HAVE_CONFIG_H
13 #include "config.h"
14 #endif
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
20 #include "getopt.h"
22 #include "cmdline.h"
24 const char *gengetopt_args_info_purpose = "";
26 const char *gengetopt_args_info_usage = "Usage: untie [OPTIONS]... [COMMAND]...";
28 const char *gengetopt_args_info_description = "";
30 const char *gengetopt_args_info_help[] = {
31 " -h, --help Print help and exit",
32 " -V, --version Print version and exit",
33 " --mount New mount namespace (default=off)",
34 " --uname New uname (default=off)",
35 " --ipc New IPC context (default=off)",
36 " --userns New user namespace (default=off)",
37 " --mask=MASK Pass user defined flag",
38 " --chroot=DIRECTORY Chroot in the specified directory",
39 " --uid=UID Change to the specified UID",
40 " --username=USERNAME Change to the specified username",
41 " --gid=GID Add the specified GID",
42 " --groupname=GROUP Add the specified group",
43 " --daemonize Run as daemon (default=off)",
44 " --alarm=SECONDS Set an alarm after the specified delay",
45 " --nice=NICE Run as the specified nice level",
46 " --sched=SCHED Use the specified (possibly RT) scheduling policy",
47 " --schedprio=PRIO Set the priority according to the given scheduler",
48 " --timeout-term=SECONDS Delay before sending a SIGTERM to the process",
49 " --timeout-kill=SECONDS Delay before sending a SIGKILL to the process",
53 static
54 void clear_given (struct gengetopt_args_info *args_info);
55 static
56 void clear_args (struct gengetopt_args_info *args_info);
58 static int
59 cmdline_parser_internal (int argc, char * const *argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required, const char *additional_error);
61 static int
62 cmdline_parser_required2 (struct gengetopt_args_info *args_info, const char *prog_name, const char *additional_error);
64 struct int_list
66 int arg;
67 char *orig;
68 struct int_list * next;
71 struct string_list
73 char * arg;
74 char *orig;
75 struct string_list * next;
78 static char *
79 gengetopt_strdup (const char *s);
81 static
82 void clear_given (struct gengetopt_args_info *args_info)
84 args_info->help_given = 0 ;
85 args_info->version_given = 0 ;
86 args_info->mount_given = 0 ;
87 args_info->uname_given = 0 ;
88 args_info->ipc_given = 0 ;
89 args_info->userns_given = 0 ;
90 args_info->mask_given = 0 ;
91 args_info->chroot_given = 0 ;
92 args_info->uid_given = 0 ;
93 args_info->username_given = 0 ;
94 args_info->gid_given = 0 ;
95 args_info->groupname_given = 0 ;
96 args_info->daemonize_given = 0 ;
97 args_info->alarm_given = 0 ;
98 args_info->nice_given = 0 ;
99 args_info->sched_given = 0 ;
100 args_info->schedprio_given = 0 ;
101 args_info->timeout_term_given = 0 ;
102 args_info->timeout_kill_given = 0 ;
105 static
106 void clear_args (struct gengetopt_args_info *args_info)
108 args_info->mount_flag = 0;
109 args_info->uname_flag = 0;
110 args_info->ipc_flag = 0;
111 args_info->userns_flag = 0;
112 args_info->mask_arg = NULL;
113 args_info->mask_orig = NULL;
114 args_info->chroot_arg = NULL;
115 args_info->chroot_orig = NULL;
116 args_info->uid_orig = NULL;
117 args_info->username_arg = NULL;
118 args_info->username_orig = NULL;
119 args_info->gid_arg = NULL;
120 args_info->gid_orig = NULL;
121 args_info->groupname_arg = NULL;
122 args_info->groupname_orig = NULL;
123 args_info->daemonize_flag = 0;
124 args_info->alarm_orig = NULL;
125 args_info->nice_orig = NULL;
126 args_info->sched_arg = NULL;
127 args_info->sched_orig = NULL;
128 args_info->schedprio_orig = NULL;
129 args_info->timeout_term_orig = NULL;
130 args_info->timeout_kill_orig = NULL;
134 static
135 void init_args_info(struct gengetopt_args_info *args_info)
137 args_info->help_help = gengetopt_args_info_help[0] ;
138 args_info->version_help = gengetopt_args_info_help[1] ;
139 args_info->mount_help = gengetopt_args_info_help[2] ;
140 args_info->uname_help = gengetopt_args_info_help[3] ;
141 args_info->ipc_help = gengetopt_args_info_help[4] ;
142 args_info->userns_help = gengetopt_args_info_help[5] ;
143 args_info->mask_help = gengetopt_args_info_help[6] ;
144 args_info->mask_min = -1;
145 args_info->mask_max = -1;
146 args_info->chroot_help = gengetopt_args_info_help[7] ;
147 args_info->uid_help = gengetopt_args_info_help[8] ;
148 args_info->username_help = gengetopt_args_info_help[9] ;
149 args_info->gid_help = gengetopt_args_info_help[10] ;
150 args_info->gid_min = -1;
151 args_info->gid_max = -1;
152 args_info->groupname_help = gengetopt_args_info_help[11] ;
153 args_info->groupname_min = -1;
154 args_info->groupname_max = -1;
155 args_info->daemonize_help = gengetopt_args_info_help[12] ;
156 args_info->alarm_help = gengetopt_args_info_help[13] ;
157 args_info->nice_help = gengetopt_args_info_help[14] ;
158 args_info->sched_help = gengetopt_args_info_help[15] ;
159 args_info->schedprio_help = gengetopt_args_info_help[16] ;
160 args_info->timeout_term_help = gengetopt_args_info_help[17] ;
161 args_info->timeout_kill_help = gengetopt_args_info_help[18] ;
165 void
166 cmdline_parser_print_version (void)
168 printf ("%s %s\n", CMDLINE_PARSER_PACKAGE, CMDLINE_PARSER_VERSION);
171 void
172 cmdline_parser_print_help (void)
174 int i = 0;
175 cmdline_parser_print_version ();
177 if (strlen(gengetopt_args_info_purpose) > 0)
178 printf("\n%s\n", gengetopt_args_info_purpose);
180 printf("\n%s\n\n", gengetopt_args_info_usage);
182 if (strlen(gengetopt_args_info_description) > 0)
183 printf("%s\n", gengetopt_args_info_description);
185 while (gengetopt_args_info_help[i])
186 printf("%s\n", gengetopt_args_info_help[i++]);
189 void
190 cmdline_parser_init (struct gengetopt_args_info *args_info)
192 clear_given (args_info);
193 clear_args (args_info);
194 init_args_info (args_info);
196 args_info->inputs = NULL;
197 args_info->inputs_num = 0;
200 static void
201 cmdline_parser_release (struct gengetopt_args_info *args_info)
204 unsigned int i;
205 if (args_info->mask_arg)
207 for (i = 0; i < args_info->mask_given; ++i)
209 if (args_info->mask_orig [i])
211 free (args_info->mask_orig [i]); /* free previous argument */
212 args_info->mask_orig [i] = 0;
215 free (args_info->mask_arg); /* free previous argument */
216 args_info->mask_arg = 0;
217 free (args_info->mask_orig); /* free previous argument */
218 args_info->mask_orig = 0;
220 if (args_info->chroot_arg)
222 free (args_info->chroot_arg); /* free previous argument */
223 args_info->chroot_arg = 0;
225 if (args_info->chroot_orig)
227 free (args_info->chroot_orig); /* free previous argument */
228 args_info->chroot_orig = 0;
230 if (args_info->uid_orig)
232 free (args_info->uid_orig); /* free previous argument */
233 args_info->uid_orig = 0;
235 if (args_info->username_arg)
237 free (args_info->username_arg); /* free previous argument */
238 args_info->username_arg = 0;
240 if (args_info->username_orig)
242 free (args_info->username_orig); /* free previous argument */
243 args_info->username_orig = 0;
245 if (args_info->gid_arg)
247 for (i = 0; i < args_info->gid_given; ++i)
249 if (args_info->gid_orig [i])
251 free (args_info->gid_orig [i]); /* free previous argument */
252 args_info->gid_orig [i] = 0;
255 free (args_info->gid_arg); /* free previous argument */
256 args_info->gid_arg = 0;
257 free (args_info->gid_orig); /* free previous argument */
258 args_info->gid_orig = 0;
260 if (args_info->groupname_arg)
262 for (i = 0; i < args_info->groupname_given; ++i)
264 if (args_info->groupname_arg [i])
266 free (args_info->groupname_arg [i]); /* free previous argument */
267 args_info->groupname_arg [i] = 0;
269 if (args_info->groupname_orig [i])
271 free (args_info->groupname_orig [i]); /* free previous argument */
272 args_info->groupname_orig [i] = 0;
275 if (args_info->groupname_arg [0])
276 free (args_info->groupname_arg [0]); /* free default string */
277 free (args_info->groupname_arg); /* free previous argument */
278 args_info->groupname_arg = 0;
279 free (args_info->groupname_orig); /* free previous argument */
280 args_info->groupname_orig = 0;
282 if (args_info->alarm_orig)
284 free (args_info->alarm_orig); /* free previous argument */
285 args_info->alarm_orig = 0;
287 if (args_info->nice_orig)
289 free (args_info->nice_orig); /* free previous argument */
290 args_info->nice_orig = 0;
292 if (args_info->sched_arg)
294 free (args_info->sched_arg); /* free previous argument */
295 args_info->sched_arg = 0;
297 if (args_info->sched_orig)
299 free (args_info->sched_orig); /* free previous argument */
300 args_info->sched_orig = 0;
302 if (args_info->schedprio_orig)
304 free (args_info->schedprio_orig); /* free previous argument */
305 args_info->schedprio_orig = 0;
307 if (args_info->timeout_term_orig)
309 free (args_info->timeout_term_orig); /* free previous argument */
310 args_info->timeout_term_orig = 0;
312 if (args_info->timeout_kill_orig)
314 free (args_info->timeout_kill_orig); /* free previous argument */
315 args_info->timeout_kill_orig = 0;
318 for (i = 0; i < args_info->inputs_num; ++i)
319 free (args_info->inputs [i]);
321 if (args_info->inputs_num)
322 free (args_info->inputs);
324 clear_given (args_info);
328 cmdline_parser_file_save(const char *filename, struct gengetopt_args_info *args_info)
330 FILE *outfile;
331 int i = 0;
333 outfile = fopen(filename, "w");
335 if (!outfile)
337 fprintf (stderr, "%s: cannot open file for writing: %s\n", CMDLINE_PARSER_PACKAGE, filename);
338 return EXIT_FAILURE;
341 if (args_info->help_given) {
342 fprintf(outfile, "%s\n", "help");
344 if (args_info->version_given) {
345 fprintf(outfile, "%s\n", "version");
347 if (args_info->mount_given) {
348 fprintf(outfile, "%s\n", "mount");
350 if (args_info->uname_given) {
351 fprintf(outfile, "%s\n", "uname");
353 if (args_info->ipc_given) {
354 fprintf(outfile, "%s\n", "ipc");
356 if (args_info->userns_given) {
357 fprintf(outfile, "%s\n", "userns");
359 if (args_info->mask_orig)
361 for (i = 0; i < args_info->mask_given; ++i)
363 if (args_info->mask_orig [i])
365 fprintf(outfile, "%s=\"%s\"\n", "mask", args_info->mask_orig [i]);
369 if (args_info->chroot_given) {
370 if (args_info->chroot_orig) {
371 fprintf(outfile, "%s=\"%s\"\n", "chroot", args_info->chroot_orig);
372 } else {
373 fprintf(outfile, "%s\n", "chroot");
376 if (args_info->uid_given) {
377 if (args_info->uid_orig) {
378 fprintf(outfile, "%s=\"%s\"\n", "uid", args_info->uid_orig);
379 } else {
380 fprintf(outfile, "%s\n", "uid");
383 if (args_info->username_given) {
384 if (args_info->username_orig) {
385 fprintf(outfile, "%s=\"%s\"\n", "username", args_info->username_orig);
386 } else {
387 fprintf(outfile, "%s\n", "username");
390 if (args_info->gid_orig)
392 for (i = 0; i < args_info->gid_given; ++i)
394 if (args_info->gid_orig [i])
396 fprintf(outfile, "%s=\"%s\"\n", "gid", args_info->gid_orig [i]);
400 if (args_info->groupname_orig)
402 for (i = 0; i < args_info->groupname_given; ++i)
404 if (args_info->groupname_orig [i])
406 fprintf(outfile, "%s=\"%s\"\n", "groupname", args_info->groupname_orig [i]);
410 if (args_info->daemonize_given) {
411 fprintf(outfile, "%s\n", "daemonize");
413 if (args_info->alarm_given) {
414 if (args_info->alarm_orig) {
415 fprintf(outfile, "%s=\"%s\"\n", "alarm", args_info->alarm_orig);
416 } else {
417 fprintf(outfile, "%s\n", "alarm");
420 if (args_info->nice_given) {
421 if (args_info->nice_orig) {
422 fprintf(outfile, "%s=\"%s\"\n", "nice", args_info->nice_orig);
423 } else {
424 fprintf(outfile, "%s\n", "nice");
427 if (args_info->sched_given) {
428 if (args_info->sched_orig) {
429 fprintf(outfile, "%s=\"%s\"\n", "sched", args_info->sched_orig);
430 } else {
431 fprintf(outfile, "%s\n", "sched");
434 if (args_info->schedprio_given) {
435 if (args_info->schedprio_orig) {
436 fprintf(outfile, "%s=\"%s\"\n", "schedprio", args_info->schedprio_orig);
437 } else {
438 fprintf(outfile, "%s\n", "schedprio");
441 if (args_info->timeout_term_given) {
442 if (args_info->timeout_term_orig) {
443 fprintf(outfile, "%s=\"%s\"\n", "timeout-term", args_info->timeout_term_orig);
444 } else {
445 fprintf(outfile, "%s\n", "timeout-term");
448 if (args_info->timeout_kill_given) {
449 if (args_info->timeout_kill_orig) {
450 fprintf(outfile, "%s=\"%s\"\n", "timeout-kill", args_info->timeout_kill_orig);
451 } else {
452 fprintf(outfile, "%s\n", "timeout-kill");
456 fclose (outfile);
458 i = EXIT_SUCCESS;
459 return i;
462 void
463 cmdline_parser_free (struct gengetopt_args_info *args_info)
465 cmdline_parser_release (args_info);
469 /* gengetopt_strdup() */
470 /* strdup.c replacement of strdup, which is not standard */
471 char *
472 gengetopt_strdup (const char *s)
474 char *result = NULL;
475 if (!s)
476 return result;
478 result = (char*)malloc(strlen(s) + 1);
479 if (result == (char*)0)
480 return (char*)0;
481 strcpy(result, s);
482 return result;
485 static char *
486 get_multiple_arg_token(const char *arg)
488 char *tok, *ret;
489 size_t len, num_of_escape;
490 int i, j;
492 if (!arg)
493 return NULL;
495 tok = strchr (arg, ',');
496 num_of_escape = 0;
498 /* make sure it is not escaped */
499 while (tok)
501 if (*(tok-1) == '\\')
503 /* find the next one */
504 tok = strchr (tok+1, ',');
505 ++num_of_escape;
507 else
508 break;
511 if (tok)
512 len = (size_t)(tok - arg + 1);
513 else
514 len = strlen (arg) + 1;
516 len -= num_of_escape;
518 ret = (char *) malloc (len);
520 i = 0;
521 j = 0;
522 while (arg[i] && (j < len-1))
524 if (arg[i] == '\\')
525 ++i;
527 ret[j++] = arg[i++];
530 ret[len-1] = '\0';
532 return ret;
535 static char *
536 get_multiple_arg_token_next(const char *arg)
538 char *tok;
540 if (!arg)
541 return NULL;
543 tok = strchr (arg, ',');
545 /* make sure it is not escaped */
546 while (tok)
548 if (*(tok-1) == '\\')
550 /* find the next one */
551 tok = strchr (tok+1, ',');
553 else
554 break;
557 if (! tok || strlen(tok) == 1)
558 return 0;
560 return tok+1;
563 static int
564 check_multiple_option_occurrences(const char *prog_name, unsigned int option_given, int min, int max, const char *option_desc);
567 check_multiple_option_occurrences(const char *prog_name, unsigned int option_given, int min, int max, const char *option_desc)
569 int error = 0;
571 if (option_given && ! (min < 0 && max < 0))
573 if (min >= 0 && max >= 0)
575 if (min == max)
577 /* specific occurrences */
578 if (option_given != min)
580 fprintf (stderr, "%s: %s option occurrences must be %d\n",
581 prog_name, option_desc, min);
582 error = 1;
585 else if (option_given < min
586 || option_given > max)
588 /* range occurrences */
589 fprintf (stderr, "%s: %s option occurrences must be between %d and %d\n",
590 prog_name, option_desc, min, max);
591 error = 1;
594 else if (min >= 0)
596 /* at least check */
597 if (option_given < min)
599 fprintf (stderr, "%s: %s option occurrences must be at least %d\n",
600 prog_name, option_desc, min);
601 error = 1;
604 else if (max >= 0)
606 /* at most check */
607 if (option_given > max)
609 fprintf (stderr, "%s: %s option occurrences must be at most %d\n",
610 prog_name, option_desc, max);
611 error = 1;
616 return error;
619 cmdline_parser (int argc, char * const *argv, struct gengetopt_args_info *args_info)
621 return cmdline_parser2 (argc, argv, args_info, 0, 1, 1);
625 cmdline_parser2 (int argc, char * const *argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required)
627 int result;
629 result = cmdline_parser_internal (argc, argv, args_info, override, initialize, check_required, NULL);
631 if (result == EXIT_FAILURE)
633 cmdline_parser_free (args_info);
634 exit (EXIT_FAILURE);
637 return result;
641 cmdline_parser_required (struct gengetopt_args_info *args_info, const char *prog_name)
643 int result = EXIT_SUCCESS;
645 if (cmdline_parser_required2(args_info, prog_name, NULL) > 0)
646 result = EXIT_FAILURE;
648 if (result == EXIT_FAILURE)
650 cmdline_parser_free (args_info);
651 exit (EXIT_FAILURE);
654 return result;
658 cmdline_parser_required2 (struct gengetopt_args_info *args_info, const char *prog_name, const char *additional_error)
660 int error = 0;
662 /* checks for required options */
663 if (check_multiple_option_occurrences(prog_name, args_info->mask_given, args_info->mask_min, args_info->mask_max, "'--mask'"))
664 error = 1;
666 if (check_multiple_option_occurrences(prog_name, args_info->gid_given, args_info->gid_min, args_info->gid_max, "'--gid'"))
667 error = 1;
669 if (check_multiple_option_occurrences(prog_name, args_info->groupname_given, args_info->groupname_min, args_info->groupname_max, "'--groupname'"))
670 error = 1;
673 /* checks for dependences among options */
675 return error;
679 cmdline_parser_internal (int argc, char * const *argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required, const char *additional_error)
681 int c; /* Character of the parsed option. */
682 char *multi_token, *multi_next; /* for multiple options */
684 int i; /* Counter */
686 struct int_list * mask_list = NULL,* mask_new = NULL;
687 struct int_list * gid_list = NULL,* gid_new = NULL;
688 struct string_list * groupname_list = NULL,* groupname_new = NULL;
689 int error = 0;
690 struct gengetopt_args_info local_args_info;
692 if (initialize)
693 cmdline_parser_init (args_info);
695 cmdline_parser_init (&local_args_info);
697 optarg = 0;
698 optind = 0;
699 opterr = 1;
700 optopt = '?';
702 while (1)
704 int option_index = 0;
705 char *stop_char;
707 static struct option long_options[] = {
708 { "help", 0, NULL, 'h' },
709 { "version", 0, NULL, 'V' },
710 { "mount", 0, NULL, 0 },
711 { "uname", 0, NULL, 0 },
712 { "ipc", 0, NULL, 0 },
713 { "userns", 0, NULL, 0 },
714 { "mask", 1, NULL, 0 },
715 { "chroot", 1, NULL, 0 },
716 { "uid", 1, NULL, 0 },
717 { "username", 1, NULL, 0 },
718 { "gid", 1, NULL, 0 },
719 { "groupname", 1, NULL, 0 },
720 { "daemonize", 0, NULL, 0 },
721 { "alarm", 1, NULL, 0 },
722 { "nice", 1, NULL, 0 },
723 { "sched", 1, NULL, 0 },
724 { "schedprio", 1, NULL, 0 },
725 { "timeout-term", 1, NULL, 0 },
726 { "timeout-kill", 1, NULL, 0 },
727 { NULL, 0, NULL, 0 }
730 stop_char = 0;
731 c = getopt_long (argc, argv, "hV", long_options, &option_index);
733 if (c == -1) break; /* Exit from `while (1)' loop. */
735 switch (c)
737 case 'h': /* Print help and exit. */
738 cmdline_parser_print_help ();
739 cmdline_parser_free (&local_args_info);
740 exit (EXIT_SUCCESS);
742 case 'V': /* Print version and exit. */
743 cmdline_parser_print_version ();
744 cmdline_parser_free (&local_args_info);
745 exit (EXIT_SUCCESS);
748 case 0: /* Long option with no short option */
749 /* New mount namespace. */
750 if (strcmp (long_options[option_index].name, "mount") == 0)
752 if (local_args_info.mount_given)
754 fprintf (stderr, "%s: `--mount' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
755 goto failure;
757 if (args_info->mount_given && ! override)
758 continue;
759 local_args_info.mount_given = 1;
760 args_info->mount_given = 1;
761 args_info->mount_flag = !(args_info->mount_flag);
763 /* New uname. */
764 else if (strcmp (long_options[option_index].name, "uname") == 0)
766 if (local_args_info.uname_given)
768 fprintf (stderr, "%s: `--uname' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
769 goto failure;
771 if (args_info->uname_given && ! override)
772 continue;
773 local_args_info.uname_given = 1;
774 args_info->uname_given = 1;
775 args_info->uname_flag = !(args_info->uname_flag);
777 /* New IPC context. */
778 else if (strcmp (long_options[option_index].name, "ipc") == 0)
780 if (local_args_info.ipc_given)
782 fprintf (stderr, "%s: `--ipc' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
783 goto failure;
785 if (args_info->ipc_given && ! override)
786 continue;
787 local_args_info.ipc_given = 1;
788 args_info->ipc_given = 1;
789 args_info->ipc_flag = !(args_info->ipc_flag);
791 /* New user namespace. */
792 else if (strcmp (long_options[option_index].name, "userns") == 0)
794 if (local_args_info.userns_given)
796 fprintf (stderr, "%s: `--userns' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
797 goto failure;
799 if (args_info->userns_given && ! override)
800 continue;
801 local_args_info.userns_given = 1;
802 args_info->userns_given = 1;
803 args_info->userns_flag = !(args_info->userns_flag);
805 /* Pass user defined flag. */
806 else if (strcmp (long_options[option_index].name, "mask") == 0)
808 local_args_info.mask_given++;
810 multi_token = get_multiple_arg_token(optarg);
811 multi_next = get_multiple_arg_token_next (optarg);
813 while (1)
815 mask_new = (struct int_list *) malloc (sizeof (struct int_list));
816 mask_new->next = mask_list;
817 mask_list = mask_new;
818 mask_new->arg = strtol (multi_token, &stop_char, 0);
819 if (!(stop_char && *stop_char == '\0')) {
820 fprintf(stderr, "%s: invalid numeric value: %s\n", argv[0], multi_token);
821 goto failure;
823 mask_new->orig = multi_token;
825 if (multi_next)
827 multi_token = get_multiple_arg_token(multi_next);
828 multi_next = get_multiple_arg_token_next (multi_next);
829 local_args_info.mask_given++;
831 else
832 break;
834 break;
836 /* Chroot in the specified directory. */
837 else if (strcmp (long_options[option_index].name, "chroot") == 0)
839 if (local_args_info.chroot_given)
841 fprintf (stderr, "%s: `--chroot' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
842 goto failure;
844 if (args_info->chroot_given && ! override)
845 continue;
846 local_args_info.chroot_given = 1;
847 args_info->chroot_given = 1;
848 if (args_info->chroot_arg)
849 free (args_info->chroot_arg); /* free previous string */
850 args_info->chroot_arg = gengetopt_strdup (optarg);
851 if (args_info->chroot_orig)
852 free (args_info->chroot_orig); /* free previous string */
853 args_info->chroot_orig = gengetopt_strdup (optarg);
855 /* Change to the specified UID. */
856 else if (strcmp (long_options[option_index].name, "uid") == 0)
858 if (local_args_info.uid_given)
860 fprintf (stderr, "%s: `--uid' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
861 goto failure;
863 if (args_info->uid_given && ! override)
864 continue;
865 local_args_info.uid_given = 1;
866 args_info->uid_given = 1;
867 args_info->uid_arg = strtol (optarg, &stop_char, 0);
868 if (!(stop_char && *stop_char == '\0')) {
869 fprintf(stderr, "%s: invalid numeric value: %s\n", argv[0], optarg);
870 goto failure;
872 if (args_info->uid_orig)
873 free (args_info->uid_orig); /* free previous string */
874 args_info->uid_orig = gengetopt_strdup (optarg);
876 /* Change to the specified username. */
877 else if (strcmp (long_options[option_index].name, "username") == 0)
879 if (local_args_info.username_given)
881 fprintf (stderr, "%s: `--username' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
882 goto failure;
884 if (args_info->username_given && ! override)
885 continue;
886 local_args_info.username_given = 1;
887 args_info->username_given = 1;
888 if (args_info->username_arg)
889 free (args_info->username_arg); /* free previous string */
890 args_info->username_arg = gengetopt_strdup (optarg);
891 if (args_info->username_orig)
892 free (args_info->username_orig); /* free previous string */
893 args_info->username_orig = gengetopt_strdup (optarg);
895 /* Add the specified GID. */
896 else if (strcmp (long_options[option_index].name, "gid") == 0)
898 local_args_info.gid_given++;
900 multi_token = get_multiple_arg_token(optarg);
901 multi_next = get_multiple_arg_token_next (optarg);
903 while (1)
905 gid_new = (struct int_list *) malloc (sizeof (struct int_list));
906 gid_new->next = gid_list;
907 gid_list = gid_new;
908 gid_new->arg = strtol (multi_token, &stop_char, 0);
909 if (!(stop_char && *stop_char == '\0')) {
910 fprintf(stderr, "%s: invalid numeric value: %s\n", argv[0], multi_token);
911 goto failure;
913 gid_new->orig = multi_token;
915 if (multi_next)
917 multi_token = get_multiple_arg_token(multi_next);
918 multi_next = get_multiple_arg_token_next (multi_next);
919 local_args_info.gid_given++;
921 else
922 break;
924 break;
926 /* Add the specified group. */
927 else if (strcmp (long_options[option_index].name, "groupname") == 0)
929 local_args_info.groupname_given++;
931 multi_token = get_multiple_arg_token(optarg);
932 multi_next = get_multiple_arg_token_next (optarg);
934 while (1)
936 groupname_new = (struct string_list *) malloc (sizeof (struct string_list));
937 groupname_new->next = groupname_list;
938 groupname_list = groupname_new;
939 groupname_new->arg = gengetopt_strdup (multi_token);
940 groupname_new->orig = multi_token;
942 if (multi_next)
944 multi_token = get_multiple_arg_token(multi_next);
945 multi_next = get_multiple_arg_token_next (multi_next);
946 local_args_info.groupname_given++;
948 else
949 break;
951 break;
953 /* Run as daemon. */
954 else if (strcmp (long_options[option_index].name, "daemonize") == 0)
956 if (local_args_info.daemonize_given)
958 fprintf (stderr, "%s: `--daemonize' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
959 goto failure;
961 if (args_info->daemonize_given && ! override)
962 continue;
963 local_args_info.daemonize_given = 1;
964 args_info->daemonize_given = 1;
965 args_info->daemonize_flag = !(args_info->daemonize_flag);
967 /* Set an alarm after the specified delay. */
968 else if (strcmp (long_options[option_index].name, "alarm") == 0)
970 if (local_args_info.alarm_given)
972 fprintf (stderr, "%s: `--alarm' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
973 goto failure;
975 if (args_info->alarm_given && ! override)
976 continue;
977 local_args_info.alarm_given = 1;
978 args_info->alarm_given = 1;
979 args_info->alarm_arg = strtol (optarg, &stop_char, 0);
980 if (!(stop_char && *stop_char == '\0')) {
981 fprintf(stderr, "%s: invalid numeric value: %s\n", argv[0], optarg);
982 goto failure;
984 if (args_info->alarm_orig)
985 free (args_info->alarm_orig); /* free previous string */
986 args_info->alarm_orig = gengetopt_strdup (optarg);
988 /* Run as the specified nice level. */
989 else if (strcmp (long_options[option_index].name, "nice") == 0)
991 if (local_args_info.nice_given)
993 fprintf (stderr, "%s: `--nice' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
994 goto failure;
996 if (args_info->nice_given && ! override)
997 continue;
998 local_args_info.nice_given = 1;
999 args_info->nice_given = 1;
1000 args_info->nice_arg = strtol (optarg, &stop_char, 0);
1001 if (!(stop_char && *stop_char == '\0')) {
1002 fprintf(stderr, "%s: invalid numeric value: %s\n", argv[0], optarg);
1003 goto failure;
1005 if (args_info->nice_orig)
1006 free (args_info->nice_orig); /* free previous string */
1007 args_info->nice_orig = gengetopt_strdup (optarg);
1009 /* Use the specified (possibly RT) scheduling policy. */
1010 else if (strcmp (long_options[option_index].name, "sched") == 0)
1012 if (local_args_info.sched_given)
1014 fprintf (stderr, "%s: `--sched' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
1015 goto failure;
1017 if (args_info->sched_given && ! override)
1018 continue;
1019 local_args_info.sched_given = 1;
1020 args_info->sched_given = 1;
1021 if (args_info->sched_arg)
1022 free (args_info->sched_arg); /* free previous string */
1023 args_info->sched_arg = gengetopt_strdup (optarg);
1024 if (args_info->sched_orig)
1025 free (args_info->sched_orig); /* free previous string */
1026 args_info->sched_orig = gengetopt_strdup (optarg);
1028 /* Set the priority according to the given scheduler. */
1029 else if (strcmp (long_options[option_index].name, "schedprio") == 0)
1031 if (local_args_info.schedprio_given)
1033 fprintf (stderr, "%s: `--schedprio' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
1034 goto failure;
1036 if (args_info->schedprio_given && ! override)
1037 continue;
1038 local_args_info.schedprio_given = 1;
1039 args_info->schedprio_given = 1;
1040 args_info->schedprio_arg = strtol (optarg, &stop_char, 0);
1041 if (!(stop_char && *stop_char == '\0')) {
1042 fprintf(stderr, "%s: invalid numeric value: %s\n", argv[0], optarg);
1043 goto failure;
1045 if (args_info->schedprio_orig)
1046 free (args_info->schedprio_orig); /* free previous string */
1047 args_info->schedprio_orig = gengetopt_strdup (optarg);
1049 /* Delay before sending a SIGTERM to the process. */
1050 else if (strcmp (long_options[option_index].name, "timeout-term") == 0)
1052 if (local_args_info.timeout_term_given)
1054 fprintf (stderr, "%s: `--timeout-term' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
1055 goto failure;
1057 if (args_info->timeout_term_given && ! override)
1058 continue;
1059 local_args_info.timeout_term_given = 1;
1060 args_info->timeout_term_given = 1;
1061 args_info->timeout_term_arg = strtod (optarg, &stop_char);
1062 if (!(stop_char && *stop_char == '\0')) {
1063 fprintf(stderr, "%s: invalid numeric value: %s\n", argv[0], optarg);
1064 goto failure;
1066 if (args_info->timeout_term_orig)
1067 free (args_info->timeout_term_orig); /* free previous string */
1068 args_info->timeout_term_orig = gengetopt_strdup (optarg);
1070 /* Delay before sending a SIGKILL to the process. */
1071 else if (strcmp (long_options[option_index].name, "timeout-kill") == 0)
1073 if (local_args_info.timeout_kill_given)
1075 fprintf (stderr, "%s: `--timeout-kill' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
1076 goto failure;
1078 if (args_info->timeout_kill_given && ! override)
1079 continue;
1080 local_args_info.timeout_kill_given = 1;
1081 args_info->timeout_kill_given = 1;
1082 args_info->timeout_kill_arg = strtod (optarg, &stop_char);
1083 if (!(stop_char && *stop_char == '\0')) {
1084 fprintf(stderr, "%s: invalid numeric value: %s\n", argv[0], optarg);
1085 goto failure;
1087 if (args_info->timeout_kill_orig)
1088 free (args_info->timeout_kill_orig); /* free previous string */
1089 args_info->timeout_kill_orig = gengetopt_strdup (optarg);
1092 break;
1093 case '?': /* Invalid option. */
1094 /* `getopt_long' already printed an error message. */
1095 goto failure;
1097 default: /* bug: option not considered. */
1098 fprintf (stderr, "%s: option unknown: %c%s\n", CMDLINE_PARSER_PACKAGE, c, (additional_error ? additional_error : ""));
1099 abort ();
1100 } /* switch */
1101 } /* while */
1104 if (local_args_info.mask_given && mask_list)
1106 struct int_list *tmp;
1107 args_info->mask_arg = (int *) realloc (args_info->mask_arg, (args_info->mask_given + local_args_info.mask_given) * sizeof (int));
1108 args_info->mask_orig = (char **) realloc (args_info->mask_orig, (args_info->mask_given + local_args_info.mask_given) * sizeof (char *));
1109 for (i = (local_args_info.mask_given - 1); i >= 0; --i)
1111 tmp = mask_list;
1112 args_info->mask_arg [i + args_info->mask_given] = mask_list->arg;
1113 args_info->mask_orig [i + args_info->mask_given] = mask_list->orig;
1114 mask_list = mask_list->next;
1115 free (tmp);
1119 if (local_args_info.gid_given && gid_list)
1121 struct int_list *tmp;
1122 args_info->gid_arg = (int *) realloc (args_info->gid_arg, (args_info->gid_given + local_args_info.gid_given) * sizeof (int));
1123 args_info->gid_orig = (char **) realloc (args_info->gid_orig, (args_info->gid_given + local_args_info.gid_given) * sizeof (char *));
1124 for (i = (local_args_info.gid_given - 1); i >= 0; --i)
1126 tmp = gid_list;
1127 args_info->gid_arg [i + args_info->gid_given] = gid_list->arg;
1128 args_info->gid_orig [i + args_info->gid_given] = gid_list->orig;
1129 gid_list = gid_list->next;
1130 free (tmp);
1134 if (local_args_info.groupname_given && groupname_list)
1136 struct string_list *tmp;
1137 args_info->groupname_arg = (char * *) realloc (args_info->groupname_arg, (args_info->groupname_given + local_args_info.groupname_given) * sizeof (char *));
1138 args_info->groupname_orig = (char **) realloc (args_info->groupname_orig, (args_info->groupname_given + local_args_info.groupname_given) * sizeof (char *));
1139 for (i = (local_args_info.groupname_given - 1); i >= 0; --i)
1141 tmp = groupname_list;
1142 args_info->groupname_arg [i + args_info->groupname_given] = groupname_list->arg;
1143 args_info->groupname_orig [i + args_info->groupname_given] = groupname_list->orig;
1144 groupname_list = groupname_list->next;
1145 free (tmp);
1150 args_info->mask_given += local_args_info.mask_given;
1151 local_args_info.mask_given = 0;
1152 args_info->gid_given += local_args_info.gid_given;
1153 local_args_info.gid_given = 0;
1154 args_info->groupname_given += local_args_info.groupname_given;
1155 local_args_info.groupname_given = 0;
1157 if (check_required)
1159 error += cmdline_parser_required2 (args_info, argv[0], additional_error);
1162 cmdline_parser_release (&local_args_info);
1164 if ( error )
1165 return (EXIT_FAILURE);
1167 if (optind < argc)
1169 int i = 0 ;
1170 int found_prog_name = 0;
1171 /* whether program name, i.e., argv[0], is in the remaining args
1172 (this may happen with some implementations of getopt,
1173 but surely not with the one included by gengetopt) */
1175 i = optind;
1176 while (i < argc)
1177 if (argv[i++] == argv[0]) {
1178 found_prog_name = 1;
1179 break;
1181 i = 0;
1183 args_info->inputs_num = argc - optind - found_prog_name;
1184 args_info->inputs =
1185 (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ;
1186 while (optind < argc)
1187 if (argv[optind++] != argv[0])
1188 args_info->inputs[ i++ ] = gengetopt_strdup (argv[optind-1]) ;
1191 return 0;
1193 failure:
1194 if (mask_list)
1196 struct int_list *tmp;
1197 while (mask_list)
1199 tmp = mask_list;
1200 free (mask_list->orig);
1201 mask_list = mask_list->next;
1202 free (tmp);
1205 if (gid_list)
1207 struct int_list *tmp;
1208 while (gid_list)
1210 tmp = gid_list;
1211 free (gid_list->orig);
1212 gid_list = gid_list->next;
1213 free (tmp);
1216 if (groupname_list)
1218 struct string_list *tmp;
1219 while (groupname_list)
1221 tmp = groupname_list;
1222 free (groupname_list->arg);
1223 free (groupname_list->orig);
1224 groupname_list = groupname_list->next;
1225 free (tmp);
1229 cmdline_parser_release (&local_args_info);
1230 return (EXIT_FAILURE);