popen and "ls"

Reply

Join Date: Sep 2006
Posts: 12
Reputation: Line is an unknown quantity at this point 
Solved Threads: 0
Line's Avatar
Line Line is offline Offline
Newbie Poster

popen and "ls"

 
0
  #1
Nov 18th, 2006
Hey!

Writing a simple client/server in C and I'm currently stuck with getting the results after calling system("ls") so I can send it from server to client.

Since system() returns an int I tried popen() -->

  1. char buffer[1000];
  2. FILE* myPipe = popen(buf, "r");
  3. if(myPipe==NULL){
  4. //errorstuff
  5. }
  6. while(fgets(buffer, 1000, myPipe)!=NULL){
  7. (void) printf("%s \n", buffer);
  8. }
  9. pclose(myPipe);

But buffer only contains the last filename listed, not the rest of them. How do I get fgets to copy everything into the buffer?

As always; all help is appreciated! :o)
Last edited by Line; Nov 18th, 2006 at 10:21 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 12
Reputation: Line is an unknown quantity at this point 
Solved Threads: 0
Line's Avatar
Line Line is offline Offline
Newbie Poster

Re: popen and "ls"

 
0
  #2
Nov 21st, 2006
Aki, now I'm one step further, using scandir + I've written in the code for sending one filename at the time to the client, who reads in a loop.

The result from this is the server reading the directory and printing it perfectly, while the client writes the name of the first file in the direcory minus the file type. One the new line all the other filenames are put together, but not with complete names, just some letters...! Example -->
! fil (should be fil.txt)
! klieklieold" (this is the files klient, klient.c + directory old)

The server clearly sends a termination to the client as I've tester just printing to the screen when it enters that method... So I know the client should be able to exit the while-loop...

What's causing this? I'm adding my methods for reading a directory, and hoping someone can find an error there, but I'm starting to worry it might be outside these functions...

  1. /*
  2. ** SERVER **
  3. *******************/
  4.  
  5. void skrivLs(int sock){
  6. char *buffer;
  7. int teller,i;
  8. struct direct **files;
  9. int file_select();
  10.  
  11. teller = scandir(".", &files, file_select, alphasort);
  12.  
  13. // Dersom ingen filer blir funnet */
  14. if(teller < 0){
  15. perror("Socket");
  16. strcpy(buffer, "No such file or directory\n");
  17. printf("BUF; %s\n", buffer);
  18. rebruk = write(sock, buffer, sizeof(files));
  19. if(rebruk < 0){
  20. perror("write() failed");
  21. close(lytte_sock);
  22. close(sock);
  23. exit(1);
  24. }
  25. skrivSlutt();
  26. exit(0);
  27. }
  28.  
  29. i=0;
  30. while(i<teller){
  31. i++;
  32. //Test to see this actually works...
  33. //printf("%s ",files[i-1]->d_name);
  34. //printf("\n");
  35. buffer=files[i-1]->d_name;
  36. rebruk = write(sock, buffer, sizeof(files));
  37. if(rebruk < 0){
  38. perror("write() failed");
  39. close(lytte_sock);
  40. close(sock);
  41. exit(1);
  42. }
  43. }
  44. skrivSlutt(); //method that sends "-1" to the client
  45. nullBuf(buffer);
  46. }

  1. /*
  2. ** CLIENT **
  3. *******************/
  4.  
  5. void les(){
  6. nullBuf(buf); //writing a bunch of nulls to buf if it isn't empty
  7. while(buf){
  8. rebruk = read(sock, buf, sizeof(buf));
  9. if(rebruk<0){
  10. perror("Read");
  11. close(sock);
  12. exit(1);
  13. }
  14. if(strcmp(buf, slutt)!=0){ //if server sends "-1"
  15. printf("! %s\n",buf);
  16. }else{
  17. break;
  18. }
  19. }
  20. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,348
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1462
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: popen and "ls"

 
0
  #3
Nov 21st, 2006
how is buf declared in that client function? If buf is declared as char* then sizeof(buf) is always 4 on 32-bit compilers.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 12
Reputation: Line is an unknown quantity at this point 
Solved Threads: 0
Line's Avatar
Line Line is offline Offline
Newbie Poster

Re: popen and "ls"

 
0
  #4
Nov 21st, 2006
Yes, it is a char*... I tried writing 1000 instead of sizeof(buf), but I got the same result... :o/

Thank you so much for taking a look! :o)
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,348
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1462
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: popen and "ls"

 
0
  #5
Nov 21st, 2006
  1. buffer=files[i-1]->d_name;
  2. rebruk = write(sock, buffer, sizeof(files));
I think this is also wrong. buffer is a pointer to d_name, but attempting to write sizeof(files) number of bytes. If files is an array of pointers to structures, then sizeof(files) will be 4 * number of pointers in that array. You probably mean this, unless files[i-1]->d_name is also a pointer.
  1. buffer=files[i-1]->d_name;
  2. rebruk = write(sock, buffer, sizeof(files[i-1]->d_name));

In that case why use buffer at all
  1. rebruk = write(sock, files[i-1]->d_name, sizeof(files[i-1]->d_name));

If d_name is also a pointer, then you should probably replace the sizeof() with strlen(d_name)+1
In that case why use buffer at all
  1. rebruk = write(sock, files[i-1]->d_name, strlen(files[i-1]->d_name)+1);

Now for the client side:
Since buf is a pointer, did you allocate memory for it, or what does it point to? please post code for this.
Last edited by Ancient Dragon; Nov 21st, 2006 at 10:26 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 12
Reputation: Line is an unknown quantity at this point 
Solved Threads: 0
Line's Avatar
Line Line is offline Offline
Newbie Poster

Re: popen and "ls"

 
0
  #6
Nov 21st, 2006
Aki, looked into this and I'm now using

  1. rebruk = write(sock, files[i-1]->d_name, strlen(files[i-1]->d_name)+1);

Originally Posted by Ancient Dragon View Post
Now for the client side:
Since buf is a pointer, did you allocate memory for it, or what does it point to? please post code for this.
char buf[1000]; --> That's all I have with buf, the rest is done by the les() method...

Should I allocate memory for it? if so, how and more importantly; Where would it be natural to do it? *clueless*
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,348
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1462
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: popen and "ls"

 
0
  #7
Nov 21st, 2006
>>Should I allocate memory for it
not necessary -- what you posted is ok.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC