Appending environment variable to system calls...

Brendan Kehoe brendan at cs.widener.edu
Tue Apr 2 12:49:05 AEST 1991


In <22200 at yunexus.YorkU.CA>, racine at yunexus.yorku.ca writes:
>I wish to be able to set an environment variable (the path to a file)
>and then to execute a program using the system() function in the
>following fashion:
>
>	char *s;
>	s=getenv("FOO");
>	system("FOO/FUBAR");

  Try:

  #include <stdio.h>
  #include <stdlib.h>
  #include <string.h>

  #define PROGRAM "FUBAR"

  main(argc, argv)
       int argc;
       char **argv;
  {
    char *s, *t;
    if ((s = getenv("FOO")) == (char *)NULL)
      {
	fprintf(stderr, "%s: no such variable FOO\n", *argv);
	exit(1);
      }
    t = (char *) malloc(strlen(s) + strlen(PROGRAM) + 1);
    sprintf(t, "%s/%s", s, PROGRAM);
    if (system(t) < 0)
      {
	perror("system");
	exit(1);
      }
  }

	

  No guarantees -- I just typed it, didn't try it. :-)

Brendan
-- 
     Brendan Kehoe - Widener Sun Network Manager - brendan at cs.widener.edu
  Widener University in Chester, PA                A Bloody Sun-Dec War Zone
 Now that we know he has ID, we could give him an account. finger bush at cs....



More information about the Comp.lang.c mailing list