Help req on CD command...... to be implemented in C
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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");
}
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...!)
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.