[hackers] [farbfeld] arg.h: Do not modify the array elements of argv[] || Laslo Hunhold

From: <git_AT_suckless.org>
Date: Fri, 4 Aug 2017 16:01:55 +0200 (CEST)

commit 26c092599447284fe85e11d322daf034ca96eeb3
Author: Laslo Hunhold <dev_AT_frign.de>
AuthorDate: Fri Aug 4 15:59:29 2017 +0200
Commit: Laslo Hunhold <dev_AT_frign.de>
CommitDate: Fri Aug 4 16:01:50 2017 +0200

    arg.h: Do not modify the array elements of argv[]
    
    The C99 standard explicitly allows to modify argc, argv, but leaves it
    open what happens if you modify the content of argv. Under OpenBSD, this
    actually has an effect on how the program is listed (e.g. in ps). To
    prevent this, we just add a counter variable and use that for iteration.
    
    While at it, this commit also includes a few style changes.
    
    Thanks Hiltjo for reporting this!

diff --git a/arg.h b/arg.h
index 2ac48ea..81127a2 100644
--- a/arg.h
+++ b/arg.h
_AT_@ -24,15 +24,15 @@ extern char *argv0;
 /* int main(int argc, char *argv[]) */
 #define ARGBEGIN for (argv0 = *argv, *argv ? (argc--, argv++) : ((void *)0); \
                       *argv && (*argv)[0] == '-' && (*argv)[1]; argc--, argv++) { \
- int argused; \
- if ((*argv)[1] == '-' && (*argv)[2] == '\0') { \
+ int i, argused; \
+ if ((*argv)[1] == '-' && !(*argv)[2]) { \
                                  argc--, argv++; \
                                  break; \
                          } \
- for (argused = 0, (*argv)++; (*argv)[0]; (*argv)++) { \
- switch((*argv)[0])
+ for (i = 1, argused = 0; (*argv)[i]; i++) { \
+ switch((*argv)[i])
 #define ARGEND if (argused) { \
- if ((*argv)[1] != '\0') { \
+ if ((*argv)[i + 1]) { \
                                                  break; \
                                          } else { \
                                                  argc--, argv++; \
_AT_@ -41,13 +41,13 @@ extern char *argv0;
                                  } \
                          } \
                  }
-#define ARGC() *argv[0]
-#define ARGF_(x) (((*argv)[1] == '\0' && !*(argv + 1)) ? \
- (x) : \
- (argused = 1, ((*argv)[1] != '\0') ? \
- (&(*argv)[1]) : \
- (*(argv + 1)) \
- ) \
+#define ARGC() (*argv)[i]
+#define ARGF_(x) ((!(*argv)[i + 1] && !*(argv + 1)) ? \
+ (x) : \
+ (argused = 1, ((*argv)[i + 1]) ? \
+ (&(*argv)[i + 1]) : \
+ (*(argv + 1)) \
+ ) \
                  )
 #define EARGF(x) ARGF_(((x), exit(1), (char *)0))
 #define ARGF() ARGF_((char *)0)
Received on Fri Aug 04 2017 - 16:01:55 CEST

This archive was generated by hypermail 2.3.0 : Fri Aug 04 2017 - 16:13:32 CEST