Sign in to follow this  
javajava

what is wrong with my very simple codes??

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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this