cpdup custom argument handling to getopt
Craig Dooley
xlnxminusx at gmail.com
Mon Jun 20 07:55:22 PDT 2005
This changes the argument parsing in bin/cpdup to use getopt.
-Craig
--
-----------------------------------------------------------------------
Craig Dooley <xlnxminusx at xxxxxxxxx>
Index: bin/cpdup/cpdup.c
===================================================================
RCS file: /home/dcvs/src/bin/cpdup/cpdup.c,v
retrieving revision 1.8
diff -u -r1.8 cpdup.c
--- bin/cpdup/cpdup.c 25 Aug 2004 01:53:38 -0000 1.8
+++ bin/cpdup/cpdup.c 16 Jun 2005 18:04:05 -0000
@@ -136,94 +136,85 @@
int
main(int ac, char **av)
{
- int i;
+ int i, ch;
char *src = NULL;
char *dst = NULL;
struct timeval start;
gettimeofday(&start, NULL);
- for (i = 1; i < ac; ++i) {
- char *ptr = av[i];
- int v = 1;
-
- if (*ptr != '-') {
- if (src == NULL) {
- src = ptr;
- } else if (dst == NULL) {
- dst = ptr;
- } else {
- fatal("too many arguments");
- /* not reached */
- }
- continue;
- }
- ptr += 2;
-
- if (*ptr)
- v = strtol(ptr, NULL, 0);
-
- switch(ptr[-1]) {
+ while((ch = getopt(ac, av, "fi:ImM:oqus:v:X:x")) != -1) {
+ switch(ch) {
case 'v':
- VerboseOpt = 1;
- while (*ptr == 'v') {
++VerboseOpt;
- ++ptr;
- }
- if (*ptr >= '0' && *ptr <= '9')
- VerboseOpt = strtol(ptr, NULL, 0);
+ if (optarg)
+ VerboseOpt = atoi(optarg);
break;
case 'I':
- SummaryOpt = v;
+ SummaryOpt = 1;
break;
case 'o':
- NoRemoveOpt = v;
+ NoRemoveOpt = 1;
break;
case 'x':
UseCpFile = ".cpignore";
break;
case 'X':
- UseCpFile = (*ptr) ? ptr : av[++i];
+ UseCpFile = optarg;
break;
case 'f':
- ForceOpt = v;
+ ForceOpt = 1;
break;
case 'i':
- AskConfirmation = v;
+ AskConfirmation = atoi(optarg);
+ if (AskConfirmation != 0 || (strlen(optarg) != 1))
+ fatal(NULL);
break;
case 's':
- SafetyOpt = v;
+ SafetyOpt = atoi(optarg);
+ if (SafetyOpt != 0 || (strlen(optarg) != 1))
+ fatal(NULL);
break;
case 'q':
- QuietOpt = v;
+ QuietOpt = 1;
break;
case 'M':
- UseMD5Opt = v;
- MD5CacheFile = av[++i];
+ UseMD5Opt = 1;
+ MD5CacheFile = optarg;
break;
case 'm':
- UseMD5Opt = v;
+ UseMD5Opt = 1;
MD5CacheFile = ".MD5.CHECKSUMS";
break;
case 'u':
setvbuf(stdout, NULL, _IOLBF, 0);
break;
default:
- fatal("illegal option: %s\n", ptr - 2);
+ fatal(NULL);
/* not reached */
break;
}
}
+ ac -= optind;
+ av += optind;
- /*
- * dst may be NULL only if -m option is specified,
- * which forces an update of the MD5 checksums
- */
+ if (ac > 2) {
+ fatal("too many arguments");
+ } else if (ac == 2) {
+ src = av[0];
+ dst = av[1];
+ } else if (ac == 1) {
+ src = av[0];
+ /*
+ * dst may be NULL only if -m option is specified,
+ * which forces an update of the MD5 checksums
+ */
+ if (UseMD5Opt == 0)
+ fatal(NULL);
+ } else {
+ fatal("no arguments");
+ }
- if (dst == NULL && UseMD5Opt == 0) {
- fatal(NULL);
- /* not reached */
- }
- if (dst) {
+ if (dst) {
i = DoCopy(src, dst, (dev_t)-1, (dev_t)-1);
} else {
i = DoCopy(src, NULL, (dev_t)-1, (dev_t)-1);
More information about the Submit
mailing list