-
Exec functions
Hi, i just want to know what can i do for open a directory using exec functions.
Example, i want to open the Documents/Downloads folder
so im on linux he is on "cd /home/Documents/Downloads"
Is there any way by using exec functions to do that? To open that directory?
-
Yes.
More information please, would you like to open the folder in their file manager? Or get the contents etc?
-
so, on the shell linux if i do "cd /home/Documents/Downloads" i go to Downloads Folder, and shows something like this on the shell
user@userlocalhost:~home/documents/downloads:
i just want to do a exec that do the same that this command "cd /home/Documents/Downloads"
-
Why not just use a system call instead?
-
How i do that with System call? There's no chance with exec functions?
-
Well, the exec family expects a file to execute, not a system command; also exec doesn't come back, so if there's more things you want done, well, that's too bad.
-
Code:
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
int main(void)
{
char new_dir[] = "/home/tmp/";
if (chdir(new_dir) != 0) {
perror("Call to chdir failed! ");
exit(EXIT_FAILURE);
}
printf("chdir to %s successful!\n", new_dir);
return 0;
}
kermit@fastbox ~/cprogs/board $ gcc -Wall -o my_cd my_cd.c
kermit@fastbox ~/cprogs/board $ ./my_cd
Call to chdir failed! : No such file
Change one line to
Code:
char new_dir[] = "/home/kermit/tmp/";
kermit@fastbox ~/cprogs/board $ gcc -Wall -o my_cd my_cd.c
kermit@fastbox ~/cprogs/board $ ./my_cd
chdir to /home/kermit/tmp/ successful!
kermit@fastbox ~/cprogs/board $
There you go. There is an awful lot more you can (and probably should) do with that to make it more robust, but that is the basics of the system call. You can get all of that from the man page for chdir.
-
thank you for that information but what i really want is that after doing that chdir the shell is poiting to that directory like:
kermit@fastbox ~/cprogs/board $ gcc -Wall -o my_cd my_cd.c
kermit@fastbox ~/cprogs/board $ ./my_cd
chdir to /home/kermit/tmp/ successful!
kermit@fastbox ~/home/kermit/tmp $
its that what im thinking about...
-
Is there some reason why you cannot just do this with a shell script? As zacs7 already noted, perhaps you could give a little more context as to what it is you are trying to achieve, and how you want to achieve it.
-
I want to change the directory wiith the objective to get the names of the files that are inside that directory.
-
That still sounds exactly like a shell script.
Even if for some unearthly reason, it isn't a shell script, then there's no reason why you wouldn't use system commands.
-
So then after you have changed into that directory, you want your program to read the names of the files to be used by the program then? If so, you don't even need to do a change of directory, but do some system calls to read a given directory's contents...
-
That still sounds exactly like a shell script.
Even if for some unearthly reason, it isn't a shell script, then there's no reason why you wouldn't use system commands.
-
I agree; some input from the OP to provide some context, in order to determine what is necessary/helpful would be useful. As it is, we are kind of guessing here. There seems to be a bit of that gong around lately.... :(
-
so using system calls i can read my files? can you give an example how to use please?