LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Help req on CD command...... to be implemented in C (https://www.linuxquestions.org/questions/programming-9/help-req-on-cd-command-to-be-implemented-in-c-723087/)

nikh.jas 05-02-2009 01:17 AM

Help req on CD command...... to be implemented in C
 
I am facing some problem in implementing CD command using C language.
This a small code that I have written but it is not changing the directory....

please suggest something


#include <unistd.h>
#include<stdio.h>
int main(int argc, char** argv)
{
if (argc == 1) // no args
{
const char* home = getenv("HOME");
chdir(home ? home : "."); // in case HOME is not defined :-)
}
else // arg given (could it be a path?)
{
}
char *directory = "/tr02/mar09ft1/t401579/nik";
int ret;

ret = chdir (directory);
if (ret ==0) // just to check if chdir() is working or not//
printf("good");
else
printf("bad");
}

rylan76 05-04-2009 04:10 AM

Hmm why are you trying to do this?

I.e. as far as I know the directory you are currently "in" is something that is held in the terminal environment, i. e. inside BASH or CSH.

In a C programming running in a terminal emulator or from the command prompt, the "pwd" directory should be the directory that the executable started up in, right? So if you need files in sub-directories, or files from elsewhere on the filing system, why not just use absolute paths?

Then there is no logical need to "change directories"... or am I completely misunderstanding you?? (I Suspect the latter...!)

wje_lq 05-04-2009 05:48 AM

Quote:

Originally Posted by rylan76 (Post 3529279)
I.e. as far as I know the directory you are currently "in" is something that is held in the terminal environment, i. e. inside BASH or CSH.

Not so. The environment variable PWD is a mere convenience, and is not necessarily accurate. If you run the following bash script:
Code:

cat > 1.c <<EOD; gcc -Wall 1.c -o 1; ./1
#include <sys/stat.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

void
where(void)
{
  char buffer[1024];

  if(getcwd(buffer,sizeof(buffer))==NULL)
  {
    abort();
  }

  printf("current directory is      %s\n",
        buffer
        );

  printf("environment variable says %s\n",
        getenv("PWD")
        );

} /* where() */

int
main(void)
{
  system("rm -rf subdirectory");

  if(mkdir("subdirectory",0700))
  {
    abort();
  }

  printf("before changing directory:\n");

  where();

  if(chdir("subdirectory"))
  {
    abort();
  }

  printf("after  changing directory:\n");

  where();

  return 0;

} /* main() */

you should get output something like this:
Code:

before changing directory:
current directory is      /u/wally/monday/2
environment variable says /u/wally/monday/2
after  changing directory:
current directory is      /u/wally/monday/2/subdirectory
environment variable says /u/wally/monday/2

I wrote a C application which I run all the time which changes the current directory; whether to do so is a matter of preference.

The directory is changed for the current process (the one running the C program) and for any descendants of that process (typically started with a fork() call). But it is not changed for the parent process. When you're sitting at the shell prompt, you are running a particular process. When you run a C program, that's a child of this process. So running the C program will change the current directory for its own process, (if you have code in the C program to do that), but the current directory will retain its old value when you return the the shell prompt.

ntubski 05-04-2009 09:56 AM

Quote:

Originally Posted by wje_lq (Post 3529366)
Not so. The environment variable PWD is a mere convenience, and is not necessarily accurate.

Well it's accurate inside shell scripts:
Code:

$ man bash
...
  Shell Variables
      The following variables are set by the shell:
...
      PWD    The current working directory as set by the cd command.


dave.donaghy 05-04-2009 03:02 PM

When chdir fails, it will set errno (man chdir will give full details).

As well as giving useful info if you're just learning about this kind of stuff, errno can also play a useful part in working out how to handle errors: for example, your code may well do different stuff if chdir returns ENOENT or if it returns ENOMEM.

To find out what errno is, I recommend including <errno.h> and <string.h>, and printing out strerror(errno), which is a text string representing the error.

My guess is that you'll get ENOENT, which will come out as text as "No such file or directory"; that will just mean that the directory you're trying to change into doesn't exist.

dave.donaghy 05-05-2009 08:32 AM

Quote:

Originally Posted by rylan76 (Post 3529279)
Hmm why are you trying to do this?

I.e. as far as I know the directory you are currently "in" is something that is held in the terminal environment, i. e. inside BASH or CSH.

In a C programming running in a terminal emulator or from the command prompt, the "pwd" directory should be the directory that the executable started up in, right? So if you need files in sub-directories, or files from elsewhere on the filing system, why not just use absolute paths?

Then there is no logical need to "change directories"... or am I completely misunderstanding you?? (I Suspect the latter...!)

This isn't really the whole truth ...

Rather, a process (not a terminal, or a shell, ...) has a current directory. There could be any number of reasons why a process might want to change directory, and subject to a certain number of practical criteria (see below) this should always be possible.

The practical critera are pretty much that you can only change directory to a directory that exists and is useable by you. (I've glossed over a bit of detail here, but see the chdir man/info page, for example, for more details.)

I think rylan76 has missed quite a lot of the underlying issue here, and a quick reading of his post might suggest that only terminals and shells have current directories: the truth is very different from that.

amysaraantony 05-07-2009 05:58 AM

Googling for this gave me tons of results and some code ... you should try it too !
;)

Debian


All times are GMT -5. The time now is 08:36 PM.