Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!zaphod.mps.ohio-state.edu!mips!news.cs.indiana.edu!widener!brendan From: brendan@cs.widener.edu (Brendan Kehoe) Newsgroups: comp.lang.c Subject: Re: Appending environment variable to system calls... Message-ID: <+DN_0T#@cs.widener.edu> Date: 2 Apr 91 02:49:05 GMT References: <11697.27f376d8@zeus.unomaha.edu> <22200@yunexus.YorkU.CA> Organization: Widener CS Dept Lines: 45 In <22200@yunexus.YorkU.CA>, racine@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 #include #include #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@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@cs....