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.
Distribution: Debian testing 64bit at home, EL5 32/64bit at work.
Posts: 196
Rep:
execlp command syntax help
I'm forking a process to run a backup command. I've been trying to figure out how to use one of the "exec" commands to do the equivalent of the code below:
Code:
tar czf /tmp/2004Sep01.tar.gz `find /data/local/2004/Sep/01 -daystart -cmin +1420 -cmin -2290`
. . . but I'm having trouble figuring this out.
To start out, I've tried to figure out how to just do the find command part, but this attempt doesn't work:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <glib.h>
int main () {
g_print("Executing command\n");
execlp("find", "find", "/data/local/2004/Sep/01", "-daystart", "-cmin", "+1000", "-cmin", "+2000", ">/tmp/find.text", NULL);
g_print("End of command\n");
return EXIT_SUCCESS;
}
Could you please help me figure out how to properly use one of the "exec" commands to do what I want?
Possibly running a tar/find command isn't what I should be doing? Is there a "C" way to do it?
--Tony
Last edited by tonyfreeman; 09-02-2004 at 11:21 AM.
One thing I noticed is that the first arguement of execlp() is the filename of the file to be executed, maybe use the full pathname (/usr/bin/find). And I don't believe you need the "find" twice, just once will do.
One thing I noticed is that the first arguement of execlp() is the filename of the file to be executed, maybe use the full pathname (/usr/bin/find). And I don't believe you need the "find" twice, just once will do.
You do need to have find twice, once for the program to execute and the second for the the zero argument (i.e. the name of the program).
And you don't need to use the full pathname if the command is in your path. The p family does it for you.
If you need to redirect the output then use dup2 to change the stdout to that of a file descriptor of a file.
Then mybin is executed with only two arguments (agrv[0]="mybin" and argv[1]="-myarg") and the shell takes care of the rest. You were sending "> mypath/myfile" as an argument to "mybin" who doesn't understand that. You should redirect the output before or in the case of find, send a parameter like "-fprintf", look at the man page for find.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.