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"); } |
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...!) |
Quote:
Code:
cat > 1.c <<EOD; gcc -Wall 1.c -o 1; ./1 Code:
before changing directory: 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. |
Quote:
Code:
$ man bash |
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. |
Quote:
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. |
|
All times are GMT -5. The time now is 08:36 PM. |