944,110 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 3570
  • C RSS
Nov 18th, 2006
0

popen and "ls"

Expand Post »
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.
Similar Threads
Reputation Points: 14
Solved Threads: 0
Newbie Poster
Line is offline Offline
12 posts
since Sep 2006
Nov 21st, 2006
0

Re: popen and "ls"

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. }
Reputation Points: 14
Solved Threads: 0
Newbie Poster
Line is offline Offline
12 posts
since Sep 2006
Nov 21st, 2006
0

Re: popen and "ls"

how is buf declared in that client function? If buf is declared as char* then sizeof(buf) is always 4 on 32-bit compilers.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,961 posts
since Aug 2005
Nov 21st, 2006
0

Re: popen and "ls"

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)
Reputation Points: 14
Solved Threads: 0
Newbie Poster
Line is offline Offline
12 posts
since Sep 2006
Nov 21st, 2006
0

Re: popen and "ls"

  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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,961 posts
since Aug 2005
Nov 21st, 2006
0

Re: popen and "ls"

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

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*
Reputation Points: 14
Solved Threads: 0
Newbie Poster
Line is offline Offline
12 posts
since Sep 2006
Nov 21st, 2006
0

Re: popen and "ls"

>>Should I allocate memory for it
not necessary -- what you posted is ok.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,961 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: urgent help needed for a beginner
Next Thread in C Forum Timeline: Regexp?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC