I have a question, here is my original code in the testchdir.c file:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc,char **argv)
{
if (argc < 2)
{
printf("Usage: %s <pathname\n",argv[0]);
exit(1);
}
if (chdir(argv[1]) == 0)
{
printf("success in chdir\n");
return 0;
}
else
{
printf("error happened");
exit(1);
}
}
In my Linux system, my original path is /home/Tom3543, then when I compile my codes above using gcc -o testchdir testchdir.c, it looks good. Later on, I want to change my path and execute the program, so I type
./testchdir /home/tom3543/C++
"success in chdir" appeared in my terminal, but my path is still /home/Tom3543 in my terminal. Can someone help me explain why? I am confused about that!
It's because the shell starts a new process for your program, and you only change the current directory in that new process. The shells process will be unaffected.
Unfortunately (for you) there's no real good (or legal) way to change the working directory of the parent process (the process of the shell).
Related
I am using cmake to build my project from the command line. The problem is, as soon as the executable finishes running it disappear immediately and I have mere milliseconds the contents that are printed on screen. I can't read much in milliseconds. Is there anything I can do so that the console screen doesnt disappear as soon as it has finished executing?
here is my CMakeLists.txt:
cmake_minimum_required(VERSION 2.8)
project(TotallyFree C)
add_executable(Acrolibre acrolibre.c)
set(LIBSRC acrodict.c acrodict.h)
add_library(acrodict ${LIBSRC})
add_executable(Acrodictlibre acrolibre.c)
target_link_libraries(Acrodictlibre acrodict)
set_target_properties(Acrodictlibre PROPERTIES COMPILE_FLAGS "-DUSE_ACRODICT")
and here is my equivalent of main.cpp:
//#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifdef USE_ACRODICT
#include "acrodict.h"
#endif
int main(int argc, char* argv[])
{
//int waitTime = 10000000;
const char* name;
#ifdef USE_ACRODICT
const acroItem_t* item;
#endif
if(argc < 2)
{
fprintf(stderr, "%s: you need one argument\n", argv[0]);
fprintf(stderr, "%s <name>\n", argv[0]);
exit(EXIT_FAILURE);
}
name = argv[1];
#ifndef USE_ACRODICT
//if(strcasecmp(name, "toulibre")==0){
if(_stricmp(name, "toulibre")==0){
printf("Toulibre is a French organization promoting FLOSS.\n");
}
#else
item = acrodict_get(name);
if(NULL != item){
printf("%s: %s\n", item->name, item->description);
}else if(item = acrodict_get(name)){
printf("<%s> is unknown maybe you mean:\n", name);
printf("%s: %s\n", item->name, item->description);
}
#endif
else{
printf("Sorry, I don't know: <%s>\n", name);
//Sleep(waitTime);
return EXIT_FAILURE;
}
//Sleep(waitTime);
return EXIT_SUCCESS;
}
As you can see I even used Sleep() and #include to delay this shutting down of the console. But it doesn't work. Is there anything anyone can recommend?
The problem is not in the make files here. Your program exits, and the console window disappears. Try using getch() before returning from main() for the program to wait for any key before exiting. Or, you can run your program from a console.
This has nothing to do with CMake, and everything to do with the way you're running your application.
When you double-click a console EXE in Windows, it opens a new console, runs the program, and closes the console, lie it should.
You have two options:
Run the program from the command-line of an already open console (cmd.exe)
Add this code to the end of main:
printf("Press Enter to exit\n");
getchar();
I am attempting to write a simple program that calls git checkout -- . on a Github repo that would be a command line argument. I would like to call it like > clearRepo repoName. I keep all my repos in the same Github directory.
The code is as follows:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
#include <errno.h>
void print_error()
{
fprintf(stderr, "Error executing: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
void print_usage(char* this)
{
printf("SYNTAX ERROR:\n%s [directoryName]\n", this);
exit(EXIT_FAILURE);
}
int main(int argc, char **argv)
{
if(argc != 2)
{
print_usage(argv[0]);
}
pid_t pid = fork();
if (pid == 0)
{
static char* params[] = {"git", "checkout", "--", ".", NULL};
char s[50], s2[50];
strcpy(s, "/home/myname/Documents/Github/");
strcpy(s2, argv[1]);
strcat(s, s2);
printf("s: %s\n", s);
int err = execv(s, params);
if(err == -1)
{
print_error();
}
exit(127);
}
else
{
waitpid(pid, 0, 0);
}
return 0;
}
It compiles fine, but print_error() will spit out Error executing: Permission denied every time I run it. I am not too familiar with writing programs for Linux, so it is probably a simple mistake. Information on what I'm doing wrong is appreciated. Thanks.
The first argument you're passing to execv is a directory, but execv expects a program. The error "Permission denied" is slightly misleading, because there is no such thing as "permission to execute directories".
To change the current directory, call chdir. Then, call whichever one of the exec* functions you like to invoke git.
chmod u+x filename for changing file permission.
If you want to checkout of a branch, but save the changes, use git stash. You can use git stash pop or git stash apply when you come back to the branch.
https://git-scm.com/docs/git-stash
Git commands and programs are quite tricky.
Follow these steps and you might debug your problem.
Enter the git commands written in the program into a terminal and check if the logic actually works. (Suggested this as you said you were new to Linux)
If it works, change the permission of your file by typing "chmod +x filename.extention".
My code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(void)
{
printf("entering main process---\n");
int ret;
char *argv[] = {"history",NULL};
ret = execvp("history",argv);
if(ret == -1)
perror("execl error");
printf("exiting main process ----\n");
return 0;
}
Output:
entering main process---
execl error: No such file or directory
exiting main process ----
Question:
Can I get executed commands not using history or ~/.bash_history?
It seems that something is wrong using function like execvp .
I've tried system function.
Code:
#include <stdio.h>
int main()
{
system("history");
return 0 ;
}
Nothing output.
If you try a man history you will get into the BASH_BUILTINS(1) General Commands Manual page. This means history is part of the bash shell internals. In order to have something executed via execvp() you need to have an actual executable somewhere in your PATH.
It's unclear why reading ~/.bash_history is not enough. Is it perhaps because you want the history of the currently running shell?
The short answer is no, you can't get it.
The long answer is you could attach with ptrace or through /proc/pid/mem, find the history in memory, and print it.
Probably not worth the effort.
You can pipe the output of the history builtin if you wish, by running your program with
history | ./myprog
I have the following c code. I want to display my file with less by calling execv()
however the following seems never work. The program terminates and noting pop out.
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
int main(void){
int pid;
if(pid=fork()>0){
//read in from stdin and pass to pipe
}else if(pid==0){
//read from pipe
//write to out.txt
//everything up to here works fine
char* para[]={"less","/Desktop/out.txt"};
execv("/bin/less",para);
}
return 0;
}
(The original code contained execv("bin/less", para);.) Unless the current directory is the root directory, /, or unless there is a program less in the subdirectory ./bin/less, then one of your problems is that you have a probable typo in the name of the executable. That assumes the program is /bin/less and not /usr/bin/less. You might even use execvp() to do a PATH-based search for the program.
There's an additional problem: you need to include a null pointer to mark the end of the argument list.
Finally, you can print an error message after the execv() returns. The mere fact that it returns tells you it failed.
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
int main(void)
{
int pid;
if ((pid = fork()) != 0)
{
// read in from stdin and pass to pipe
// Need to test for fork() error here too
}
else
{
// read from pipe
// write to out.txt
// everything up to here works fine
char *para[] = { "/bin/less", "Desktop/out.txt", 0 };
execv(para[0], para);
fprintf(stderr, "Failed to execute %s\n", para[0]);
exit(1);
}
return 0;
}
Or:
char *para[] = { "less", "Desktop/out.txt", 0 };
execvp(para[0], para);
fprintf(stderr, "Failed to execute %s\n", para[0]);
The remarks in the code about pipes are puzzling since there is no sign of pipes other than in the comments. As it stands, less will read the file it is told to read. Note that less will not paginate its output if the output is not going to a terminal. Since we can see no I/O redirection, we have to assume, then, that less will ignore anything the program tries to write to it, and will not send any data back to the program.
char* para[]={"less","/Desktop/out.txt"};
execv("/bin/less",para);
How does execv know when to stop reading parameters?
I think if you'd put code in there to handle execv() returning an error you'd have found this. You're also not testing for errors from fork().
How do I programmatically open a file in its default program in Linux (im using Ubuntu 10.10).
For example, opening *.mp3 will open the file in Movie Player (or something else).
You need to run gnome-open, kde-open, or exo-open, depending on which desktop you are using.
I believe there is a project called xdg-utils that attempts to provide a unified interface to the local desktop.
So, something like:
snprintf(s, sizeof s, "%s %s", "xdg-open", the_file);
system(s);
Beware of code injection. It's safer to bypass scripting layers with user input, so consider something like:
pid = fork();
if (pid == 0) {
execl("/usr/bin/xdg-open", "xdg-open", the_file, (char *)0);
exit(1);
}
// parent will usually wait for child here
Ubuntu 10.10 is based on GNOME. So, it would be good idea to use
g_app_info_launch_default_for_uri().
Something like this should work.
#include <stdio.h>
#include <gio/gio.h>
int main(int argc, char *argv[])
{
gboolean ret;
GError *error = NULL;
g_type_init();
ret = g_app_info_launch_default_for_uri("file:///etc/passwd",
NULL,
&error);
if (ret)
g_message("worked");
else
g_message("nop: %s", error->message);
return 0;
}
BTW, xdg-open, a shell script, tries to determin your desktop environment and call a known helper like gvfs-open for GNOME, kde-open for KDE, or something else. gvfs-open ends up calling g_app_info_launch_default_for_uri().
A simple solution with less coding:
I've tested this program on my Ubuntu and it is working fine, and if I am not wrong you are looking for something like this
#include <stdio.h>
#include <stdlib.h>
int main()
{
system("firefox file:///dox/song.mp3");
return 0;
}