473,403 Members | 2,354 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

First argument lost when doing exec

I'm currently using execvp() for my lab program and I realise that the
first argument (args[0]) I pass to execvp is always not passed to the
file ("/bin/ls") being executed.

I'm using gcc compiler (3.3.1) running under Cygwin from Redhat.

I've done a test program which looks like this:

main(){
char* args[3];

args[0] = "--color=auto";
args[1] = "-l";
args[2] = NULL;

execvp("/bin/ls", args);
}

The purpose of the --color=auto switch is to enable a color listing of
the files and directories. As it turns out, the output is the same as
/bin/ls -l. Minus the --color=auto.

main(){
char* args[4];

args[0] = "-dummy";
args[1] = "--color=auto";
args[2] = "-l";
args[3] = NULL;

execvp("/bin/ls", args);
}

If I add in a dummy for the first argument (-dummy at args[0]) then
the output is equivalent to /bin/ls --color=auto -l. Minus the -dummy.
We can see that args[0] is always not passed to /bin/ls.

Is this a bug or is my understanding of exec* parameters wrong? Why is
this happening?

---
Goh, Yong Kwang
Singapore
Nov 14 '05 #1
3 3436
Goh, Yong Kwang wrote:

<snip>
Is this a bug or is my understanding of exec* parameters wrong?


Your understanding of exec* parameters is wrong. Since the exec* functions
are not part of ISO C, however, they're not topical here in the comp.lang.c
newsgroup. Ask in comp.unix.programmer and you'll get a superb answer, I'm
sure.

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 14 '05 #2
go**********@hotmail.com (Goh, Yong Kwang) wrote:
# I'm currently using execvp() for my lab program and I realise that the
# first argument (args[0]) I pass to execvp is always not passed to the
# file ("/bin/ls") being executed.
#
# I'm using gcc compiler (3.3.1) running under Cygwin from Redhat.
#
# I've done a test program which looks like this:
#
# main(){
# char* args[3];
#
# args[0] = "--color=auto";
# args[1] = "-l";
# args[2] = NULL;
#
# execvp("/bin/ls", args);
# }

The args passed in are the argc/argv passed to executed program without
modification. By convention, the args[0] -> argv[0] is the name of the
executable, but that is not required. The path or open file number or other
identifier of the executable file is passed separately from the argv,
and it can be a completely different executable than the one indicated
in the argv.

# args[0] = "-dummy";

To follow the conventions programs expect, it is your responsibility to do
args[0] = "ls"
or
args[0] = "/bin/ls";

--
Derk Gwen http://derkgwen.250free.com/html/index.html
Elvis was an artist. But that didn't stop him from joining the service
in time of war. That's why he is the king, and you're a shmuck.
Nov 14 '05 #3
Goh, Yong Kwang wrote:
I'm currently using execvp() for my lab program and I realise that the
first argument (args[0]) I pass to execvp is always not passed to the
file ("/bin/ls") being executed.

I'm using gcc compiler (3.3.1) running under Cygwin from Redhat.

I've done a test program which looks like this:

main(){
char* args[3];

args[0] = "--color=auto";
args[1] = "-l";
args[2] = NULL;

execvp("/bin/ls", args);
}

This is equivalent to calling ls via a symbolic link named `--color=auto'.
The purpose of the --color=auto switch is to enable a color listing of
the files and directories. As it turns out, the output is the same as
/bin/ls -l. Minus the --color=auto.

main(){
char* args[4];

args[0] = "-dummy";
args[1] = "--color=auto";
args[2] = "-l";
args[3] = NULL;

execvp("/bin/ls", args);
}
Now you're calling ls a `-dummy'

If I add in a dummy for the first argument (-dummy at args[0]) then
the output is equivalent to /bin/ls --color=auto -l. Minus the -dummy.
We can see that args[0] is always not passed to /bin/ls.

Is this a bug or is my understanding of exec* parameters wrong? Why is
this happening?

args[0] is the name that the program calls another program.
Let's take "grep" as an example.
If args[0] is "grep", then /bin/grep will act as the standard grep.
If args[0] is "egrep", then /bin/grep will act as the extended grep.
If args[0] is "fgrep", then /bin/grep will act as the fixed-string grep.

To make my point:
The program:
#include <unistd.h>

int main (int argc, char **argv){
if (argc < 2){
return 1;
} else {
execvp (argv[0], &argv[1]);
}
return 2;
}
The testing:
$ printf "hello\nbye\nhe|by\nhe|b[y]\n" | ./execvp grep grep 'he|b[y]'
he|by
$ printf "hello\nbye\nhe|by\nhe|b[y]\n" | ./execvp grep egrep 'he|b[y]'
hello
bye
he|by
he|b[y]
$ printf "hello\nbye\nhe|by\nhe|b[y]\n" | ./execvp grep fgrep 'he|b[y]'
he|b[y]

---
Goh, Yong Kwang
Singapore


Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Arjan | last post by:
I've got the following error: Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/wbdfdart/public_html/wbdfforum/verwijder.php on line 13 verwijder.php ...
1
by: aa | last post by:
Anybody know the solution to trailing space in argument of Runtime.getRuntime().exec(arg), Process p = Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler C:\\Documents and...
3
by: Frank Bechmann | last post by:
Eventually most of you will not learn much from this because it's just another event in the 'default argument value gotcha' series, but because it cost me some hours yesterday to spot this 'error'...
6
by: Sean Berry | last post by:
I don't know that this partilarily bad programming, but I was interested in doing the following. def functionname( module, var1, var2 ): import module I would like to be able to pass the...
0
by: Mathieu Drapeau | last post by:
Hi, I would like to do something like below but I lost my arguments passed via an html web page when I reload python within my script: /import os,sys dirName = "/opt/oracle/product/9.2.0/lib"...
7
by: Mark DuPrey | last post by:
I've got a script in an ASP page that is supposed to extract certain files from a zip file, move them, create a new zip with the moved files and then make a self-extracting archive out of the new...
44
by: user | last post by:
Hi, Let's say I have 2 dates in the b/m format: Date 1 and date 2 How do I check whether Date2 is later than Date 1? Date1. 21-Nov-2006 09:00:00 PM
3
by: jeremyfee | last post by:
The spawn* and exec* functions appear to escape asterisks, I'm guessing all shell characters too, before the spawn/exec'ed process sees them. Is there a way around this? Not sure if this is a...
7
by: mirandacascade | last post by:
The questions are toward the bottom of this post. Situation is this: 1) Access 97 2) Multi-user appplication 3) SQL Server 2000 4) Sporadically (i.e. less than 1% of the time) encounter the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.