Re: __progname in base
Theo Buehler wrote: > ping. > ok > On Tue, Dec 08, 2015 at 07:15:39PM +0100, Theo Buehler wrote: > > On Sat, Nov 07, 2015 at 12:20:42PM +0100, Tobias Stoeckmann wrote: > > > Based on Todd's patch for at and cron, I did a grep through our base > > > tree to see if there are more occurrences of self-made __progname > > > handling. > > > > A few more of those: > > > > Index: caesar/caesar.c > > === > > RCS file: /var/cvs/src/games/caesar/caesar.c,v > > retrieving revision 1.17 > > diff -u -p -r1.17 caesar.c > > --- caesar/caesar.c 14 Oct 2015 08:12:12 - 1.17 > > +++ caesar/caesar.c 8 Dec 2015 17:40:00 - > > @@ -69,7 +69,8 @@ int > > main(int argc, char *argv[]) > > { > > int ch, i, nread; > > - char *inbuf, *p, **av; > > + extern char *__progname; > > + char *inbuf; > > int obs[26], try, winner; > > double dot, winnerdot; > > > > @@ -77,11 +78,7 @@ main(int argc, char *argv[]) > > err(1, "pledge"); > > > > /* check to see if we were called as rot13 */ > > - av = argv; > > - p = strrchr(*av, '/'); > > - if (p++ == NULL) > > - p = *av; > > - if (strcmp(p,"rot13") == 0) > > + if (strcmp(__progname, "rot13") == 0) > > printit(13); > > > > if (argc > 1) { > > Index: hack/hack.end.c > > === > > RCS file: /var/cvs/src/games/hack/hack.end.c,v > > retrieving revision 1.13 > > diff -u -p -r1.13 hack.end.c > > --- hack/hack.end.c 24 Oct 2015 18:26:13 - 1.13 > > +++ hack/hack.end.c 8 Dec 2015 17:53:00 - > > @@ -613,7 +613,7 @@ charcat(char *s, char c) > > void > > prscore(int argc, char **argv) > > { > > - extern char *hname; > > + extern char *__progname; > > char **players; > > int playerct; > > int rank; > > @@ -699,7 +699,7 @@ prscore(int argc, char **argv) > > if(playerct > 1) printf("any of "); > > for(i=0; i> printf("%s%s", players[i], (i > - printf("Call is: %s -s [playernames]\n", hname); > > + printf("Call is: %s -s [playernames]\n", __progname); > > } > > } > > return; > > Index: hack/hack.main.c > > === > > RCS file: /var/cvs/src/games/hack/hack.main.c,v > > retrieving revision 1.18 > > diff -u -p -r1.18 hack.main.c > > --- hack/hack.main.c4 Nov 2015 21:22:10 - 1.18 > > +++ hack/hack.main.c8 Dec 2015 17:54:14 - > > @@ -90,7 +90,6 @@ int locknum; /* max num of > > players */ > > char *catmore; /* default pager */ > > #endif > > char SAVEF[PL_NSIZ + 11] = "save/";/* save/9player */ > > -char *hname; /* name of the game (argv[0] of call) */ > > char obuf[BUFSIZ]; /* BUFSIZ is defined in stdio.h */ > > > > extern char *nomovemsg; > > @@ -103,12 +102,12 @@ static void chdirx(char *, boolean); > > int > > main(int argc, char **argv) > > { > > + extern char *__progname; > > int fd; > > #ifdef CHDIR > > char *dir; > > #endif > > > > - hname = argv[0]; > > hackpid = getpid(); > > > > #ifdef CHDIR /* otherwise no chdir() */ > > @@ -187,7 +186,7 @@ main(int argc, char **argv) > > * Find the creation date of this game, > > * so as to avoid restoring outdated savefiles. > > */ > > - gethdate(hname); > > + gethdate(__progname); > > > > /* > > * We cannot do chdir earlier, otherwise gethdate will fail. > > Index: hunt/huntd/driver.c > > === > > RCS file: /var/cvs/src/games/hunt/huntd/driver.c,v > > retrieving revision 1.22 > > diff -u -p -r1.22 driver.c > > --- hunt/huntd/driver.c 25 Sep 2015 17:50:53 - 1.22 > > +++ hunt/huntd/driver.c 8 Dec 2015 18:07:36 - > > @@ -52,7 +52,6 @@ > > #include "conf.h" > > #include "server.h" > > > > -char *First_arg; /* pointer to argv[0] */ > > u_int16_t Server_port; > > intServer_socket; /* test socket to answer datagrams */ > > FLAG should_announce = TRUE; /* true if listening on standard port */ > > @@ -88,6 +87,7 @@ main(ac, av) > > static FLAG server = FALSE; > > extern int optind; > > extern char *optarg; > > + extern char *__progname; > > int c; > > static struct timeval linger = { 0, 0 }; > > static struct timeval timeout = { 0, 0 }, *to; > > @@ -97,8 +97,6 @@ main(ac, av) > > int fd; > > int background = 0; > > > > - First_arg = av[0]; > > - > > config(); > > > > while ((c = getopt(ac, av, "bsp:a:D:")) != -1) { > > @@ -127,7 +125,7 @@ erred: > > fprintf(stderr, > >
Re: __progname in base
On Sat, Nov 07, 2015 at 12:20:42PM +0100, Tobias Stoeckmann wrote: > Based on Todd's patch for at and cron, I did a grep through our base > tree to see if there are more occurrences of self-made __progname > handling. A few more of those: Index: caesar/caesar.c === RCS file: /var/cvs/src/games/caesar/caesar.c,v retrieving revision 1.17 diff -u -p -r1.17 caesar.c --- caesar/caesar.c 14 Oct 2015 08:12:12 - 1.17 +++ caesar/caesar.c 8 Dec 2015 17:40:00 - @@ -69,7 +69,8 @@ int main(int argc, char *argv[]) { int ch, i, nread; - char *inbuf, *p, **av; + extern char *__progname; + char *inbuf; int obs[26], try, winner; double dot, winnerdot; @@ -77,11 +78,7 @@ main(int argc, char *argv[]) err(1, "pledge"); /* check to see if we were called as rot13 */ - av = argv; - p = strrchr(*av, '/'); - if (p++ == NULL) - p = *av; - if (strcmp(p,"rot13") == 0) + if (strcmp(__progname, "rot13") == 0) printit(13); if (argc > 1) { Index: hack/hack.end.c === RCS file: /var/cvs/src/games/hack/hack.end.c,v retrieving revision 1.13 diff -u -p -r1.13 hack.end.c --- hack/hack.end.c 24 Oct 2015 18:26:13 - 1.13 +++ hack/hack.end.c 8 Dec 2015 17:53:00 - @@ -613,7 +613,7 @@ charcat(char *s, char c) void prscore(int argc, char **argv) { - extern char *hname; + extern char *__progname; char **players; int playerct; int rank; @@ -699,7 +699,7 @@ prscore(int argc, char **argv) if(playerct > 1) printf("any of "); for(i=0; i
Re: __progname in base
Tobias Stoeckmann wrote: > Opinions, thoughts? looks good, but you've got some mostly unrelated changes in here. this should be separate, but ok for the rest. > > Index: sbin/newfs_ext2fs/newfs_ext2fs.c > > === > > RCS file: /cvs/src/sbin/newfs_ext2fs/newfs_ext2fs.c,v > > retrieving revision 1.17 > > diff -u -p -u -p -r1.17 newfs_ext2fs.c > > --- sbin/newfs_ext2fs/newfs_ext2fs.c14 Oct 2015 15:54:49 - > > 1.17 > > +++ sbin/newfs_ext2fs/newfs_ext2fs.c7 Nov 2015 11:16:27 - > > @@ -519,7 +519,8 @@ getpartition(int fsi, const char *specia > > if (!S_ISCHR(st.st_mode)) > > warnx("%s: not a character-special device", special); > > cp = strchr(argv[0], '\0') - 1; > > - if (cp == NULL || ((*cp < 'a' || *cp > ('a' + getmaxpartitions() - 1)) > > + if (cp == NULL || cp < argv[0] || > > + ((*cp < 'a' || *cp > ('a' + getmaxpartitions() - 1)) > > && !isdigit((unsigned char)*cp))) > > errx(EXIT_FAILURE, "%s: can't figure out file system > > partition", argv[0]); > > lp = getdisklabel(special, fsi);
Re: __progname in base
On Sat, Dec 05, 2015 at 03:29:06AM -0500, Ted Unangst wrote: > looks good, but you've got some mostly unrelated changes in here. this should > be separate, but ok for the rest. It started with a "check argv" code review and ended up with __progname adjustments, so I agree here and removed the newfs parts for a separate commit. Also the gomoku KNF is a bit distracting, so it's gone now, too. Theo pointed out that tradcpp and lex should stay as they are, because they are upstream projects. Joerg mentioned the getprogname function previously, so I left id and nl alone, too. The patch touches these programs now: mt, pax, gomoku, telnet, crunchgen, pppd, and pdisk As I have removed the concerning parts and got OKs for the rest, I will commit it later this day if nobody objects. Index: bin/mt/mt.c === RCS file: /cvs/src/bin/mt/mt.c,v retrieving revision 1.36 diff -u -p -u -p -r1.36 mt.c --- bin/mt/mt.c 12 Nov 2013 04:36:02 - 1.36 +++ bin/mt/mt.c 5 Dec 2015 10:01:55 - @@ -88,6 +88,8 @@ int _rmtmtioctop(int fd, struct mtop *c struct mtget *_rmtstatus(int fd); void _rmtclose(void); +extern char*__progname; + char *host = NULL; /* remote host (if any) */ int @@ -133,7 +135,6 @@ _rmtclose(void) #endif } -char *progname; inteject = 0; int @@ -145,12 +146,7 @@ main(int argc, char *argv[]) char *p, *tape, *realtape, *opts; size_t len; - if ((progname = strrchr(argv[0], '/'))) - progname++; - else - progname = argv[0]; - - if (strcmp(progname, "eject") == 0) { + if (strcmp(__progname, "eject") == 0) { opts = "t"; eject = 1; tape = NULL; @@ -320,9 +316,9 @@ void usage(void) { if (eject) - (void)fprintf(stderr, "usage: %s [-t] device\n", progname); + (void)fprintf(stderr, "usage: %s [-t] device\n", __progname); else (void)fprintf(stderr, - "usage: %s [-f device] command [count]\n", progname); + "usage: %s [-f device] command [count]\n", __progname); exit(X_USAGE); } Index: bin/pax/options.c === RCS file: /cvs/src/bin/pax/options.c,v retrieving revision 1.91 diff -u -p -u -p -r1.91 options.c --- bin/pax/options.c 18 May 2015 20:26:16 - 1.91 +++ bin/pax/options.c 5 Dec 2015 10:01:56 - @@ -184,14 +184,12 @@ char *chdname = NULL; void options(int argc, char **argv) { + extern char *__progname; /* * Are we acting like pax, tar or cpio (based on argv[0]) */ - if ((argv0 = strrchr(argv[0], '/')) != NULL) - argv0++; - else - argv0 = argv[0]; + argv0 = __progname; if (strcmp(NM_TAR, argv0) == 0) { tar_options(argc, argv); Index: games/gomoku/main.c === RCS file: /cvs/src/games/gomoku/main.c,v retrieving revision 1.29 diff -u -p -u -p -r1.29 main.c --- games/gomoku/main.c 30 Nov 2015 08:44:51 - 1.29 +++ games/gomoku/main.c 5 Dec 2015 10:01:56 - @@ -45,10 +45,11 @@ #define PROGRAM1 /* get input from program */ #define INPUTF 2 /* get input from a file */ +extern char *__progname; /* name of program */ + intinteractive = 1;/* true if interactive */ intdebug; /* true if debugging */ inttest; /* both moves come from 1: input, 2: computer */ -char *prog; /* name of program */ FILE *debugfp; /* file for debug output */ FILE *inputfp; /* file for debug input */ @@ -84,12 +85,6 @@ main(argc, argv) if (pledge("stdio rpath wpath cpath tty", NULL) == -1) err(1, "pledge"); - prog = strrchr(argv[0], '/'); - if (prog) - prog++; - else - prog = argv[0]; - if ((tmpname = getlogin()) != NULL) strlcpy(you, tmpname, sizeof(you)); else @@ -117,7 +112,7 @@ main(argc, argv) default: fprintf(stderr, "usage: %s [-bcdu] [-D debugfile] [inputfile]\n", - prog); + __progname); exit(1); } } @@ -194,8 +189,8 @@ again: } } if (interactive) { - plyr[BLACK] = input[BLACK] == USER ? you : prog; - plyr[WHITE] = input[WHITE] == USER ? you : prog; + plyr[BLACK] = input[BLACK] == USER ? you : __progname; + plyr[WHITE] = input[WHITE] == USER ? you : __progname; bdwho(1); } @@ -222,8 +217,8 @@ again:
Re: __progname in base
Tobias Stoeckmann wrote: > Opinions, thoughts? > > > [...] > > > > Index: sbin/newfs/newfs.c > > === > > RCS file: /cvs/src/sbin/newfs/newfs.c,v > > retrieving revision 1.100 > > diff -u -p -u -p -r1.100 newfs.c > > --- sbin/newfs/newfs.c 29 Sep 2015 03:19:24 - 1.100 > > +++ sbin/newfs/newfs.c 7 Nov 2015 11:16:26 - > > @@ -410,7 +410,7 @@ main(int argc, char *argv[]) > > special); > > } > > cp = strchr(argv[0], '\0') - 1; > > - if (cp == NULL || > > + if (cp == NULL || cp < argv[0] || > > ((*cp < 'a' || *cp > ('a' + maxpartitions - 1)) > > && !isdigit((unsigned char)*cp))) > > fatal("%s: can't figure out file system partition", Why not just use *argv[0] == '\0' in the condition? Seems clearer to me. ok mmcc@ for that hunk either way. It could also be nicer to break that out into its own condition and make the error message something like "Empty partition name supplied".
Re: __progname in base
On Sat, Nov 07, 2015 at 12:20:42PM +0100, Tobias Stoeckmann wrote: > Index: bin/mt/mt.c > === > RCS file: /cvs/src/bin/mt/mt.c,v > retrieving revision 1.36 > diff -u -p -u -p -r1.36 mt.c > --- bin/mt/mt.c 12 Nov 2013 04:36:02 - 1.36 > +++ bin/mt/mt.c 7 Nov 2015 11:16:25 - > @@ -88,6 +88,8 @@ int _rmtmtioctop(int fd, struct mtop *c > struct mtget *_rmtstatus(int fd); > void _rmtclose(void); > > +extern char *__progname; > + > char *host = NULL; /* remote host (if any) */ > > int > @@ -133,7 +135,6 @@ _rmtclose(void) > #endif > } > > -char *progname; > int eject = 0; > > int > @@ -145,12 +146,7 @@ main(int argc, char *argv[]) > char *p, *tape, *realtape, *opts; > size_t len; > > - if ((progname = strrchr(argv[0], '/'))) > - progname++; > - else > - progname = argv[0]; > - > - if (strcmp(progname, "eject") == 0) { > + if (strcmp(__progname, "eject") == 0) { > opts = "t"; > eject = 1; > tape = NULL; As Ingo recently reminded me, OpenBSD actually did add getprogname() at some point, which needs need a actually manual forward definition. Joerg
Re: __progname in base
On Sat, Nov 07, 2015 at 03:32:11PM +0100, Joerg Sonnenberger wrote: > As Ingo recently reminded me, OpenBSD actually did add getprogname() at > some point, which needs need a actually manual forward definition. Too early for writing. It does *not* need such an ugly manual extern declaration. Joerg