Operating System - Linux
1751746 Members
5514 Online
108781 Solutions
New Discussion юеВ

Re: Problems with exec families

 
Jesus Ruiz_1
New Member

Problems with exec families

i wrote this program
------------------------
#include
#include
#include
#include
#include
int main(int argc, char *argv[])
{
char cadena[2048];
sprintf(cadena,"%s",argv[1]);
cadena[strlen(cadena)]='\0';
printf("finger %s\n",cadena);
if (execl("/usr/bin/","finger",cadena)<0)
{
perror("Creacion:");
}
}
-----------------------
but when i running this is the result
[jruiz@dns cgi]$ a.out jruiz
finger jruiz
Creacion:: Permission denied
[jruiz@dns cgi]$
-------------------------
what is the problem i know i have permition to see myself, is the same problem when the root own the program
2 REPLIES 2
Felix J. Liu
Advisor

Re: Problems with exec families

The first arguement for execl is the pathname of a *FILE* which is to be executed. This means you need to write

if (execl("/usr/bin/finger","finger",cadena)<0) {
/* ... */
}

Also, what is the purpose of the line

cadena[strlen(cadena)]='\0';

If you can get string length, it is properly null-terminated. If it is not terminated or terminated, say, at 2040, this line is not going to help.

Regards,

Fellix
Ahmed Masud
New Member

Re: Problems with exec families

#include
#include
#include
#include
#include
int main(int argc, char *argv[])
{
char cadena[2048];
sprintf(cadena,"%s",argv[1]);
cadena[strlen(cadena)]='\0';
printf("finger %s\n",cadena);
/* the first parameter to any exec is
* the name/path of the program you wish to
* execute, thus:
* execl("/usr/bin/finger", "finger", cadena);
*/
if (execl("/usr/bin/","finger",cadena)<0)
{
perror("Creacion:");
}
}

Now ... as for argv[0], which is the second parameter, this is actually passed as a the zeroth argument to the program. While with something like finger it is not evident what the use may be, but some software programs use the zeroth argumnet rather niftily (is that a word yet?)

For example, gzip examines its own name (i.e, contents of argv[0]) and figures out if it is actually being called as 'gzip' or 'gunzip' and accordingly zips or unzips the subsequent arguments.

For instance, the attached example implements a slightly different version of the system utils 'basename' and 'dirname'