This question already has answers here:
system("cd <path>") in a C program
(3 answers)
Closed 5 years ago.
I'm able to use popen to run just about any program, but apparently not cd:
#include <stdio.h>
void main() {
FILE *fp = popen("cd", "w");
pclose(fp);
}
I'd expect that to change directory to home but nothing happens. Changing to "r", or changing to e.g. "cd ~", "cd /", does not help. Using system has about the same result, i.e. works for anything but cd. So how is it done? The answers here don't work for me. Thank you.
cd is generally a shell internal command, NOT an executable.
Even if it were, in general no process can change another process's working directory, so it would change the cwd for the "cd" process and then upon exit it'd be gone.
Related
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to stop C++ console application from exiting immediately?
I created an exe file in c. When I run it command prompt opens and then closes quickly and I cannot see the output. The program takes no runtime values from users. It reads data from a file. Is there any way to prevent this?
Run it natively from the command line.
Let's say that your file is in C:\awesomeness.exe
Open the cmd, type cd C:\ and then type awesomeness.exe
One classic way to do it:
#include <stdio.h>
int main() {
puts("hai");
getchar();
}
This will wait for keypress at the end.
Are you in Windows? If so, a quick and dirty way is to use the system() function in stdlib.h (make sure you include it) to execute the PAUSE command in the command prompt.
system("PAUSE");
If you are using Visual Studio, hit CTRL + F5 instead of F5 in order to run your program.
Using 'system("pause");' before ending main() is the most common method.
int main() {
...
...
system("pause");
}
This question already has answers here:
Change directory of parent process after exit [duplicate]
(2 answers)
Closed 7 years ago.
I have a program that calls chdir() to change the cwd. However, after the program finishes the cwd changes back to the directory that called the program instead of staying the in one specified by the call to chdir(). I made a program to test if chdir() is actually changing to the specified directory and found that chdir() is doing what I presumed: changing to the specified directory for the duration of the program then returning to the directory that executed the program.
Here is the code for the test:
#include <stdio.h>
#include <unistd.h>
#define NAME_MAX 100
int main(int argc, char **argv)
{
char buf[NAME_MAX];
char *path = argv[1];
if (chdir(path) == -1) { /* change cwd to path */
fprintf(stderr, "error: could not change to dir %s\n", path);
return 1;
}
getcwd(buf, NAME_MAX);
printf("CWD is: %s\n", buf); /* print cwd as obtained from getcwd() */
return 0;
}
and here is the output from my terminal:
john#ubuntu:~/C/cli$ pwd
/home/john/C/cli
john#ubuntu:~/C/cli$ mkdir foobar
john#ubuntu:~/C/cli$ ./test.c foobar
CWD is: /home/john/C/cli/foobar
john#ubuntu:~/C/cli$ pwd
/home/john/C/cli
So my question is, how can I stay in the directory that I specify in the call to chdir() after the the program finishes? Also, I am on Ubuntu 12.04 and compiling with gcc.
Certain information, including values of environment variables and the current working directory, is propagated from parent processes to child processes but not back to parent processes. If a child process (your program) invokes chdir or sets or modifies an environment variable, that affects that process and any of its children, but cannot affect the parent.
That's why chdir is a built-in command in the shell; it can't be implemented as a separate program.
If you want to have a program change your shell's current directory for you, you'll need to do it indirectly. For example, your program can print a cd command, and you can eval that output in your shell. (You can wrap that in a function.)
For example, if you change:
chdir(path);
to
printf("cd %s\n", path);
you can have a shell function:
my_func() {
eval `your_program`
}
and my_func will change your shell's current directory.
Or you can put a cd command directly in the function, or in a script that you execute via . script-name or source script-name rather than executing it.
All these solutions require your current shell to execute the cd command itself (which internally invokes the chdir system call).
The environment of one process cannot be changed by another process. That includes the current working directory. So no, you can't stay in the directory.
This question already has answers here:
Problem redirecting a C program output in bash
(5 answers)
Closed 9 years ago.
So this is probably a stupid question, but I can't see what I'm doing wrong.
I am running a program that produces output when called like ./ar. The output looks like:
-0.00781 0.02344 0.98828
-0.01172 0.02734 0.98828
-0.01562 0.02344 0.98047
-0.00781 0.02344 1.00000
-0.00391 0.02344 0.98438
A new line of output is written every second.
When I call the code like this ./ar > log and kill the program using ctrl-c after a few seconds, the file log is empty.
I am running this code on an embedded system. The system has a writeable partition which is the partition that I am running in, and I have write access as I am logged in as root.
The reason is the lazy writing concept of UNIX system.
Are you sure you are looking at standard output in you call ./ar? It might be standard error.
So, try ./ar >log 2>err to have 2 files, one for stdout and one for stderr.
Or use ./ar 2>&1 >log to get one file for both streams.
This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
How to delete a directory and its contents in (POSIX) C?
This is in a standard Linux environment.
Thanks!
(I'm aware of rmdir but this isn't what I'm looking for.)
You'll want to traverse the directory tree, using nftw for file tree walk. For the rm -r example, use the flag FTW_DEPTH to process contents first, and check for FTW_D to use rmdir on directories rather than remove or unlink. Of course this doesn't guarantee you're allowed to remove things; that's generally decided by write persmission of the containing directory.
What's wrong with system("rm -rf");?
update: since commenters are showing with their comments that it's easy to miss the point, let me expand to clarify:
if (chdir(dirName) != 0)
return -1;
if (system("rm -rf .") != 0)
return -2;
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to stop C++ console application from exiting immediately?
I created an exe file in c. When I run it command prompt opens and then closes quickly and I cannot see the output. The program takes no runtime values from users. It reads data from a file. Is there any way to prevent this?
Run it natively from the command line.
Let's say that your file is in C:\awesomeness.exe
Open the cmd, type cd C:\ and then type awesomeness.exe
One classic way to do it:
#include <stdio.h>
int main() {
puts("hai");
getchar();
}
This will wait for keypress at the end.
Are you in Windows? If so, a quick and dirty way is to use the system() function in stdlib.h (make sure you include it) to execute the PAUSE command in the command prompt.
system("PAUSE");
If you are using Visual Studio, hit CTRL + F5 instead of F5 in order to run your program.
Using 'system("pause");' before ending main() is the most common method.
int main() {
...
...
system("pause");
}