* exec question
@ 2005-11-04 5:09 ework0
[not found] ` <1131083883.10404.3.camel@mokona>
2005-11-04 17:09 ` Lawrence Bowie
0 siblings, 2 replies; 3+ messages in thread
From: ework0 @ 2005-11-04 5:09 UTC (permalink / raw)
To: linux-c-programming
Hello,
How can I execute a program (without the full path, just the name) using
exec's system calls within a C program? ej: buf[]={"uname",NULL};
Thanks for a brief example,
ework0
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: exec question
[not found] ` <436AF357.5050109@gmail.com>
@ 2005-11-04 16:22 ` davidgn
0 siblings, 0 replies; 3+ messages in thread
From: davidgn @ 2005-11-04 16:22 UTC (permalink / raw)
To: linux-c-programming
Quoting ework0 <ework0@gmail.com>:
> David Eduardo Gómez Noguera wrote:
>
> >On Thu, 2005-11-03 at 23:09 -0600, ework0 wrote:
> >
> >
> >>Hello,
> >>
> >>How can I execute a program (without the full path, just the name)
> using
> >>exec's system calls within a C program? ej: buf[]={"uname",NULL};
> >>
> >>Thanks for a brief example,
> >>
> >>ework0
> >>
> >>
> >>
> >>
> >
> >I think not, not with exec's family of functions anyway.
> >You could try system though, or popen or some other way, but its a bit
> >risky to call programs without the full path.
> >
> >
> >
> are you sure DAVID!!! ???? I cant use system() or popen() is unsecure!
>
>
The reason you can avoid a full path with system and popen is because they first
spawn a shell and then have the shell execute the program.
The shell first looks for the program in the PATH env variable.
Also to use exec you probably want to fork() first.
But then again, what does that buff variable is for? if it's ARGV, then its
right to have the name for the program only, but the filename argument still
needs to include the full path
i.e.
execv("/bin/uname", buff);
would execute /bin/uname, and if it happened to read it's first parameter
(argv[0]) it would find the string "uname"
there would also be no more parameters (argc == 1)
-------------------------------------------------
www.correo.unam.mx
UNAMonos Comunicándonos
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: exec question
2005-11-04 5:09 exec question ework0
[not found] ` <1131083883.10404.3.camel@mokona>
@ 2005-11-04 17:09 ` Lawrence Bowie
1 sibling, 0 replies; 3+ messages in thread
From: Lawrence Bowie @ 2005-11-04 17:09 UTC (permalink / raw)
To: ework0; +Cc: linux-c-programming
I believe this is what you want, right?
In the future, you will get a much more expedient response by posting to
the newsgroup, comp.unix.programmer. :) Just an FYI .. not trying to
scare you away.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int
main(int argc, char *argv[])
{
/* In using execlp you better be sure PATH is set properly and
* also in ceratin situations it might be a security risk.
*/
/* Uncomment below if you want an example with arguments */
//if (execlp("cat", "cat", "/etc/hosts", (char *)0) < 0)
if (execlp("df", "df", (char *)NULL) < 0)
fprintf(stderr, "\nCannot exec!\n");
exit(0);
}
ework0 wrote:
> Hello,
>
> How can I execute a program (without the full path, just the name) using
> exec's system calls within a C program? ej: buf[]={"uname",NULL};
>
> Thanks for a brief example,
>
> ework0
>
>
> -
> To unsubscribe from this list: send the line "unsubscribe
> linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
>
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2005-11-04 17:09 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-11-04 5:09 exec question ework0
[not found] ` <1131083883.10404.3.camel@mokona>
[not found] ` <436AF357.5050109@gmail.com>
2005-11-04 16:22 ` davidgn
2005-11-04 17:09 ` Lawrence Bowie
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).