Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Changing director inside a program

1 view
Skip to first unread message

root

unread,
Feb 4, 2012, 7:23:43 PM2/4/12
to
Here is an example of a program using system
calls:

/*
tiny little shell
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termio.h>
#include <sys/wait.h>
#include <signal.h>
#include <time.h>
#include <sys/time.h>
#include <string.h>

#define LINESIZE 256
char linebuff[LINESIZE];

main(argc,argv)
int argc;
char *argv[];
{

while(fgets(linebuff,256,stdin)){
system(linebuff);
}
exit(0);
}


Entering, say, cd /tmp
will only cause a child process to cd to the /tmp
directory. Upon return from the system call the
original directory is restored.

Is there any way to actually change the parent
directory for subsequent system calls?

So that, a first ls command will list the
contents of the original directory, but
after cd /tmp subsequent ls calls will list
the contents of /tmp?

Lew Pitcher

unread,
Feb 4, 2012, 7:56:37 PM2/4/12
to
On Saturday 04 February 2012 19:23, in comp.os.linux.misc, NoE...@home.org
wrote:
Not entirely true. Nothing is restored, because nothing (in /that/ process)
was changed.

> Is there any way to actually change the parent
> directory for subsequent system calls?

Yes, of course. By using the chdir(2) syscall.

> So that, a first ls command will list the
> contents of the original directory, but
> after cd /tmp subsequent ls calls will list
> the contents of /tmp?

Certainly not the way you are doing it.

Processes inherit things from their parent processes, including current
working directory. A child process cannot change a parent process' cwd; it
can only change it's own cwd, which can be inherited by /it's/ children.

You've used a complicated way to try to change the cwd. Your system() call
forks off a child process which executes the shell (/bin/sh) to run a
command. That's a parent (which won't be affected), a child running /bin/sh
(which /may/ be affected, if the system() command was a shell builtin
like "cd"), and (possibly) a grandchild process (if the system()
command /wasn't/ a shell builtin).

It'll be the child or grandchild (depending) who changes directory, then
exits.


To change directory /in the parent/ process, you actually have to call the
chdir(2) system call.

HTH
--
Lew Pitcher

root

unread,
Feb 5, 2012, 12:47:22 AM2/5/12
to
Lew Pitcher <lpit...@teksavvy.com> wrote:
>
>
> To change directory /in the parent/ process, you actually have to call the
> chdir(2) system call.
>
> HTH

Thanks, the chdir() does the trick.

Jeroen

unread,
Feb 5, 2012, 4:50:08 PM2/5/12
to
>> Is there any way to actually change the parent
>> directory for subsequent system calls?
>
> Yes, of course. By using the chdir(2) syscall.

And there is no need to include all those *.h files. The first
two will do. OK, the first three if you use chdir().

Jeroen Belleman

root

unread,
Feb 5, 2012, 4:57:33 PM2/5/12
to
Jeroen <jer...@nospam.please> wrote:
>
> And there is no need to include all those *.h files. The first
> two will do. OK, the first three if you use chdir().
>
> Jeroen Belleman
>

I start with a boiler plate of includes for all quick
little programs. All programs, in fact. It costs nothing
and keeps me from searching out what is needed.
0 new messages