system() function and current working directory

system() function and current working directory

Post by gio » Thu, 22 Mar 2007 22:20:54


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)?
 
 
 

system() function and current working directory

Post by kuype » Fri, 23 Mar 2007 00:52:10


This newsgroup is specifically for discussions about the C standard -
it's not about how to use C. Furthermore, the C standard doesn't have
anything to say about directories or pathnames. It's meant to be
implementable, and has been implemented, even on systems where
directories are a meaningless concept. The answer to your question
inherently depends upon the operating system you're using. Of course,
that's equally true of any use of the system() function.

On the systems I'm most familiar with, you could set cwd = open(".",
O_RDWR) to open the current directory. Then call chdir(otherpath) to
change the current directory. Finally, when you're done, call
fchdir(cwd) to return to the original directory. Then call close(cwd).
Remember to check each of these function calls for error indications.
Warning: I've never had reason to do this, and therefore can't be sure
that I've correctly interpreted the documentation which explains how
to do it. In any event, there's no guarantee that this will work on
your system - those functions are part of Unix, not the C standard
library itself.

 
 
 

system() function and current working directory

Post by gio » Fri, 23 Mar 2007 01:24:49

This could be a solution:

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);

What do you think about?
 
 
 

system() function and current working directory

Post by gio » Fri, 23 Mar 2007 07:44:19

To be more clear this is my problem:
I'm implementing ftp client-server.
The server is multithread.
Each client can navigates the filesystem beginning on the server
working directory.
For each cliet the server have a path variable to store the client
"virtual" working directory and when a client does cd(change
directory), the server update this variable.
Suppose a client is in the dir "minny" and that wants execute a
command(for example ls) witouth specifing a path.
in this way, if the server' s current directory is "X", the call
system("ls") will list the "x"directory, but it should list the
"minny" directory.

THE PROBLEM IS NOT THE SEARCH OF EXECUTABLE.

THANKS FOR YOUR EFFORTS
 
 
 

system() function and current working directory

Post by kuype » Fri, 23 Mar 2007 09:40:08


Your first message was somewhat off-topic for this newsgroup; this one
is much more so. Please move this to some other newsgroup where it is
on-topic.
 
 
 

system() function and current working directory

Post by John Nagl » Fri, 23 Mar 2007 12:04:02


The reason you're having problems is because "system"
executes the requested command in a subprocess. The
"cd" performed by one call to "system" applies to that
subprocess, and that state change is lost when the subprocess
exits and "system" returns. On some systems, you can execute
multiple commands in a subprocess, with something like this:

system("cd somedir; ls");

If you're having this problem, you're probably trying
to do too much in threads. There's only one current directory
and one set of permissions for a UNIX or Windows process, and those
portions of the context are shared by all threads. Usually, when
you're writing an FTP server, the initial part of the server
forks off a process for each connection, giving that process
its own permissions and current directory. Unless you have
a clear understanding of this, you will probably create a server
with a huge security hole.

Again, this is off-topic for the C standards newsgroup.

John Nagle
 
 
 

system() function and current working directory

Post by gio » Sat, 24 Mar 2007 17:55:41


I'm sorry