- use getopt to parse argument instead of home brewed code

- add -M user flag to specify the username to send mail to from the command line
- some snprintf
- sort options.
This commit is contained in:
christos 2008-12-17 17:54:51 +00:00
parent 0f9fc8e8fb
commit bf23612c38
1 changed files with 60 additions and 50 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: supcmain.c,v 1.23 2007/07/20 18:58:14 christos Exp $ */ /* $NetBSD: supcmain.c,v 1.24 2008/12/17 17:54:51 christos Exp $ */
/* /*
* Copyright (c) 1992 Carnegie Mellon University * Copyright (c) 1992 Carnegie Mellon University
@ -155,6 +155,8 @@
* "notify" supfile option, that contains messages * "notify" supfile option, that contains messages
* printed by Sup. * printed by Sup.
* *
* -M "mailto" flag
* Like -m, but send mail to the specified user.
* -o "old files" flag * -o "old files" flag
* Sup will normally only upgrade files that have * Sup will normally only upgrade files that have
* changed on the repository since the last time an * changed on the repository since the last time an
@ -348,7 +350,7 @@ int portdebug; /* network debugging ports */
int main(int, char **); int main(int, char **);
static int checkcoll(TREE *, void *); static int checkcoll(TREE *, void *);
static void doswitch(char *, TREE **, int *, int *); static void doswitch(int *, char ***, TREE **, int *, int *, char *, size_t);
static char *init(int, char **); static char *init(int, char **);
/************************************* /*************************************
@ -458,27 +460,25 @@ main(int argc, char **argv)
#define Tcount Tgid #define Tcount Tgid
static void static void
doswitch(char *argp, TREE ** collTp, int *oflagsp, int *aflagsp) doswitch(int *argc, char ***argv, TREE ** collTp, int *oflagsp, int *aflagsp,
char *username, size_t ulen)
{ {
TREE *t; TREE *t;
char *coll; char *coll, *argp;
int oflags, aflags; int oflags, aflags;
int c;
#define SUPOPTIONS "abBdDeEfkKlmM:NoOPRsStuvXzZ=:"
oflags = aflags = 0; oflags = aflags = 0;
for (;;) { while ((c = getopt(*argc, *argv, SUPOPTIONS)) != -1)
switch (*argp) { switch (c) {
default: default:
logerr("Invalid flag '%c' ignored", *argp); logerr("Invalid flag '%c' ignored", c);
break; break;
case '\0': case '\0':
case '=': case '=':
if (*argp++ == '\0' || *argp == '\0') { argp = optarg;
*oflagsp |= oflags;
*oflagsp &= ~aflags;
*aflagsp |= aflags;
*aflagsp &= ~oflags;
return;
}
do { do {
coll = nxtarg(&argp, ", \t"); coll = nxtarg(&argp, ", \t");
t = Tinsert(collTp, coll, TRUE); t = Tinsert(collTp, coll, TRUE);
@ -488,29 +488,6 @@ doswitch(char *argp, TREE ** collTp, int *oflagsp, int *aflagsp)
t->Taflags &= ~oflags; t->Taflags &= ~oflags;
argp = skipover(argp, ", \t"); argp = skipover(argp, ", \t");
} while (*argp); } while (*argp);
return;
case 'N':
scmdebug++;
break;
case 'P':
portdebug = TRUE;
break;
case 'R':
#if MACH
rpauseflag = TRUE;
#endif /* MACH */
break;
case 'X':
xpatchflag = TRUE;
break;
case 'S':
silent = TRUE;
break;
case 's':
sysflag = TRUE;
break;
case 't':
timeflag = TRUE;
break; break;
case 'a': case 'a':
oflags |= CFALL; oflags |= CFALL;
@ -556,6 +533,14 @@ doswitch(char *argp, TREE ** collTp, int *oflagsp, int *aflagsp)
case 'm': case 'm':
oflags |= CFMAIL; oflags |= CFMAIL;
break; break;
case 'M':
oflags |= CFMAIL;
strncpy(username, optarg, ulen);
username[ulen - 1] = '\0';
break;
case 'N':
scmdebug++;
break;
case 'o': case 'o':
oflags |= CFOLD; oflags |= CFOLD;
aflags &= ~CFOLD; aflags &= ~CFOLD;
@ -564,12 +549,32 @@ doswitch(char *argp, TREE ** collTp, int *oflagsp, int *aflagsp)
oflags &= ~CFOLD; oflags &= ~CFOLD;
aflags |= CFOLD; aflags |= CFOLD;
break; break;
case 'P':
portdebug = TRUE;
break;
case 'R':
#if MACH
rpauseflag = TRUE;
#endif /* MACH */
break;
case 's':
sysflag = TRUE;
break;
case 'S':
silent = TRUE;
break;
case 't':
timeflag = TRUE;
break;
case 'u': case 'u':
noutime = TRUE; noutime = TRUE;
break; break;
case 'v': case 'v':
oflags |= CFVERBOSE; oflags |= CFVERBOSE;
break; break;
case 'X':
xpatchflag = TRUE;
break;
case 'z': case 'z':
oflags |= CFCOMPRESS; oflags |= CFCOMPRESS;
break; break;
@ -577,8 +582,13 @@ doswitch(char *argp, TREE ** collTp, int *oflagsp, int *aflagsp)
oflags &= ~CFCOMPRESS; oflags &= ~CFCOMPRESS;
break; break;
} }
argp++;
} *oflagsp |= oflags;
*oflagsp &= ~aflags;
*aflagsp |= aflags;
*aflagsp &= ~oflags;
*argc -= optind;
*argv += optind;
} }
static char * static char *
@ -601,6 +611,7 @@ init(int argc, char **argv)
void (*oldsigsys) (); void (*oldsigsys) ();
#endif /* MACH */ #endif /* MACH */
username[0] = '\0';
sysflag = FALSE; /* not system upgrade */ sysflag = FALSE; /* not system upgrade */
timeflag = FALSE; /* don't print times */ timeflag = FALSE; /* don't print times */
#if MACH #if MACH
@ -612,11 +623,10 @@ init(int argc, char **argv)
collT = NULL; collT = NULL;
oflags = aflags = 0; oflags = aflags = 0;
while (argc > 1 && argv[1][0] == '-' && argv[1][1] != '\0') {
doswitch(&argv[1][1], &collT, &oflags, &aflags); doswitch(&argc, &argv, &collT, &oflags, &aflags, username,
--argc; sizeof(username));
argv++;
}
if (argc == 1 && !sysflag) if (argc == 1 && !sysflag)
logquit(1, "Need either -s or supfile"); logquit(1, "Need either -s or supfile");
#if MACH #if MACH
@ -627,7 +637,7 @@ init(int argc, char **argv)
(void) signal(SIGSYS, oldsigsys); (void) signal(SIGSYS, oldsigsys);
#endif /* MACH */ #endif /* MACH */
if (sysflag) if (sysflag)
(void) sprintf(supfname = buf, (void) snprintf(supfname = buf, sizeof(buf),
timeflag ? FILESUPTDEFAULT : FILESUPDEFAULT, timeflag ? FILESUPTDEFAULT : FILESUPDEFAULT,
DEFDIR); DEFDIR);
else { else {
@ -644,11 +654,11 @@ init(int argc, char **argv)
--argc; --argc;
argv++; argv++;
} }
if ((u = getlogin()) || if (*username == '\0' && ((u = getlogin()) ||
((pw = getpwuid((int) getuid())) && (u = pw->pw_name))) ((pw = getpwuid((int) getuid())) && (u = pw->pw_name)))) {
(void) strcpy(username, u); (void)strncpy(username, u, sizeof(username));
else username[sizeof(username) - 1] = '\0';
*username = '\0'; }
if (*supfname) { if (*supfname) {
f = fopen(supfname, "r"); f = fopen(supfname, "r");
if (f == NULL) if (f == NULL)