aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org>2021-01-29 14:17:48 +0200
committerSergey Poznyakoff <gray@gnu.org>2021-01-29 14:17:48 +0200
commit4d38d3a5b9f86b99db8e1853f5412d9d8d033790 (patch)
tree6e2161ec1f51c81d3767f7f9dd8b9383f01b3ec6
downloadm4env-4d38d3a5b9f86b99db8e1853f5412d9d8d033790.tar.gz
m4env-4d38d3a5b9f86b99db8e1853f5412d9d8d033790.tar.bz2
Initial commit
-rw-r--r--.gitignore5
-rw-r--r--m4env.c56
2 files changed, 61 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..893f7b1
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,5 @@
+m4env
+*~
+\#*#
+.emacs*
+core
diff --git a/m4env.c b/m4env.c
new file mode 100644
index 0000000..0838e86
--- /dev/null
+++ b/m4env.c
@@ -0,0 +1,56 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <limits.h>
+#include <string.h>
+
+extern char **environ;
+
+char *progname;
+
+static void
+enomem(void)
+{
+ fprintf(stderr, "%s: not enough memory\n", progname);
+ exit(1);
+}
+
+int
+main(int argc, char **argv)
+{
+ int xargc;
+ char **xargv;
+ int nenv, i, j;
+
+ progname = argv[0];
+ for (nenv = 0; environ[nenv]; nenv++)
+ ;
+
+ if ((unsigned long) nenv + argc + 1 > INT_MAX) {
+ fprintf(stderr, "%s: environment size too big\n", progname);
+ return 1;
+ }
+
+ xargc = argc + nenv + 1;
+ xargv = calloc(xargc, sizeof(xargv[0]));
+ if (!xargv)
+ enomem();
+
+ xargv[0] = "m4";
+ for (i = 0; i < nenv; i++) {
+ size_t len;
+ char *def;
+
+ len = strlen(environ[i]) + 3;
+ if ((def = malloc(len)) == NULL)
+ enomem();
+ xargv[i + 1] = strcat(strcpy(def, "-D"), environ[i]);
+ }
+ for (j = 1; j <= argc; i++, j++)
+ xargv[i + 1] = argv[j];
+
+ execvp(xargv[0], xargv);
+ perror("execvp");
+ return 2;
+}
+

Return to:

Send suggestions and report system problems to the System administrator.