Beispiellösung

Content

#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <afblib/strlist.h>

int main(int argc, char** argv) {
   strlist env = {0};
   const char* cmdname = *argv++; --argc;
   while (argc > 0 && strchr(*argv, '=')) {
      if (!strlist_push(&env, *argv)) {
	 perror(""); exit(1);
      }
      --argc; ++argv;
   }
   if (argc == 0) {
      fprintf(stderr, "Usage: %s param=value ... path arg...\n", cmdname);
      exit(1);
   }
   if (!strlist_push0(&env)) {
      perror(""); exit(1);
   }
   execve(argv[0], argv, env.list);
   perror(argv[0]);
   exit(1);
}

Übersetzung und Ausführung

theon$ gcc -Wall -o environ environ.c -lafb
environ.c:6:10: fatal error: afblib/strlist.h: No such file or directory
    6 | #include <afblib/strlist.h>
      |          ^~~~~~~~~~~~~~~~~~
compilation terminated.
theon$ ./environ /usr/bin/date
Tue Nov 15 23:15:30 CET 2022
theon$ ./environ LANG=de TZ=EST /usr/bin/date
Dienstag, 15. November 2022 um 17:15:30 Uhr EST
theon$ ./environ date
date: No such file or directory
theon$