Jump to content

exec problem

- - - - -

  • Please log in to reply
1 reply to this topic

#1
DarkLordofthePenguins

DarkLordofthePenguins

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 409 posts
I was writing a program to pipe data through a FIFO when I ran into a weird problem. I can't figure out what went wrong. The line in question is this:


execlp( "cat", "cat", argv[1] );


It prints the file perfectly fine, but at the end it gives the "File not found" error message:


cat: ????\a: No such file or directory


It looks like cat is running twice, and the second time it runs, it uses the last few characters of the file as its parameter. That's the only explanation I can think of. What could be causing this?
Programming is a journey, not a destination.

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
Ah, you will have to be careful here. The opengroup.org signature for execlp is the following:
int execlp(const char *[I]file, const char *[I]arg0, ... /*, (char *)0 */);[/I][/I]

This hints that the parameter list is both variable and should end in a null pointer (NULL can be useful.)

Quote

"Ye application shall ensure that the last member of this array is a null pointer"

The following should correct this issue:
execlp( "cat", "cat", argv[1], NULL);

I do not believe that the program runs twice, it may be that the garbage is read as a file (i.e. cat file1.txt file2.txt) where file2.txt is in this case garbage ASCII due to lack of termination.

Alexander.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users