Re: system() function



gio wrote:
I have a problem and the solution should works under windows and unix
OS.

Suppose I have a program ex.c in the directory X (so the current
working directory of ex.c is X).

Also suppose I have this code fragment:
...
char otherpath[PATHLEN];
char cmd[CMDLEN];
...
...
otherpath="Y" //where y is a valid path string
...
system(cmd); //where cmd is any command string
...



If in cmd is specified a relative path, it is relative to the current
working directory(in this example is X).

There is a way to execute command wich paths are relatives to the
"otherpath" variable (in this example is Y)?

Build a new cmd, e.g.:
char *newcmd = malloc(3+strlen(otherpath)+2+strlen(cmd)+1);
sprintf(newcmd, "cd %s; $s", otherpath, cmd);
then run that command:
system(newcmd);
free(newcmd);

--
These are my personal views and not those of Fujitsu Siemens Computers!
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize
-- T. Pratchett

.



Relevant Pages

  • Re: system() function and current working directory
    ... working directory of ex.c is X). ... There is a way to execute command wich paths are relatives to the ... This newsgroup is specifically for discussions about the C standard - ...
    (comp.std.c)
  • Re: system() function and current working directory
    ... There is a way to execute command wich paths are relatives to the ... "otherpath" variable? ...
    (comp.unix.programmer)
  • Re: system() function
    ... working directory of ex.c is X). ... char otherpath; ... There is a way to execute command wich paths are relatives to the ...
    (comp.os.linux.development.apps)
  • system() function and current working directory
    ... working directory of ex.c is X). ... char otherpath; ... There is a way to execute command wich paths are relatives to the ...
    (comp.lang.c)
  • system() function
    ... working directory of ex.c is X). ... char otherpath; ... There is a way to execute command wich paths are relatives to the ...
    (comp.os.linux.development.apps)