• Advertisement
Sign in to follow this  

what is wrong with my very simple codes??

This topic is 4490 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I wrote a very simple C program on linux. But I can not see the result of "ls" on screen but only "************". Who can give me a anwser to see the files list on screen? Thanks! #include <stdio.h> #include <string.h> #include <stdlib.h> #include <unistd.h> void main( void ) { char *cmd[1]= {"ls"}; execvp("ls", cmd); printf("*************"); }

Share this post


Link to post
Share on other sites
Advertisement
Not sure, but can't you do System("ls") (maybe it's a lowercase 's' in "system"). See if that works.

EDIT: Also, with system calls, the buffer is not always flushed automatically. Maybe that's your problem.

EDIT2: If I'm not mistaken, your code is calling "ls" with parameter "ls." Try making cmd "-a" or something.

Share this post


Link to post
Share on other sites
Why are you trying to pass in 'ls' as a parameter to ls? That, I think, would be why you are not seeing anything: you probably don't have something called 'ls' in the current directory. [edit] Forget I said anything.
Try running System() like cowsarenotevil suggested.
By the way, your array should be something like this:
char *cmd[] = { "ls", 0 }; (null-terminated)
[edit2] I tested your code on a *nix system (SunOS 5.8, GCC 3.2.3), and the problem was that your array was not null-terminated. [edit3] Note, also, that main() should return an int.


jfl.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
man, he's instructing the program to print the *'s, that's why the output is a bunch of *'s. pretty logical, isn't it?

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
man, he's instructing the program to print the *'s, that's why the output is a bunch of *'s. pretty logical, isn't it?


You obviously read neither his code, nor his question.

Share this post


Link to post
Share on other sites
Try this:


#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

main()

{
pid_t pid;
char *const parmList[] =
{"/bin/ls", "-l", NULL};

if ((pid = fork()) == -1)
perror("fork() error");
else if (pid == 0) {
execvp("ls", parmList);
printf("Return not expected. Must be an execvp() error.\\n");
}
}





edit: removed '\' from from lines 21 and 29

[Edited by - jdaniel on November 5, 2005 6:51:42 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by jdaniel
Try this:

*** Source Snippet Removed ***


Remove the '\' from lines 21 and 29 ;).

I'm not sure why, but printf's after execvp do nothing. I don't typically program in *nix, unfortunately. [edit] After reading through the man-pages more thoroghly, this seems to be by design. the exec* family of functions *replace* the current process with the one specified, which is why the function does not return on success :).
It works with system(), though.

Share this post


Link to post
Share on other sites
exec replaces the current process with another one, so it will in effect exit your program. You should use a variant of spawn, system or fork and exec.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
why caring, anyways? he'll never be tim sweeney or something

Share this post


Link to post
Share on other sites
Quote:
Original post by jdaniel
Try this:

*** Source Snippet Removed ***

edit: removed '\' from from lines 21 and 29




these codes do work, thanks!!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement