| | |
popen and "ls"
![]() |
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() -->
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)
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() -->
C Syntax (Toggle Plain Text)
char buffer[1000]; FILE* myPipe = popen(buf, "r"); if(myPipe==NULL){ //errorstuff } while(fgets(buffer, 1000, myPipe)!=NULL){ (void) printf("%s \n", buffer); } 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.
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...
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...
C Syntax (Toggle Plain Text)
/* ** SERVER ** *******************/ void skrivLs(int sock){ char *buffer; int teller,i; struct direct **files; int file_select(); teller = scandir(".", &files, file_select, alphasort); // Dersom ingen filer blir funnet */ if(teller < 0){ perror("Socket"); strcpy(buffer, "No such file or directory\n"); printf("BUF; %s\n", buffer); rebruk = write(sock, buffer, sizeof(files)); if(rebruk < 0){ perror("write() failed"); close(lytte_sock); close(sock); exit(1); } skrivSlutt(); exit(0); } i=0; while(i<teller){ i++; //Test to see this actually works... //printf("%s ",files[i-1]->d_name); //printf("\n"); buffer=files[i-1]->d_name; rebruk = write(sock, buffer, sizeof(files)); if(rebruk < 0){ perror("write() failed"); close(lytte_sock); close(sock); exit(1); } } skrivSlutt(); //method that sends "-1" to the client nullBuf(buffer); }
C Syntax (Toggle Plain Text)
/* ** CLIENT ** *******************/ void les(){ nullBuf(buf); //writing a bunch of nulls to buf if it isn't empty while(buf){ rebruk = read(sock, buf, sizeof(buf)); if(rebruk<0){ perror("Read"); close(sock); exit(1); } if(strcmp(buf, slutt)!=0){ //if server sends "-1" printf("! %s\n",buf); }else{ break; } } }
C Syntax (Toggle Plain Text)
buffer=files[i-1]->d_name; rebruk = write(sock, buffer, sizeof(files));
C Syntax (Toggle Plain Text)
buffer=files[i-1]->d_name; rebruk = write(sock, buffer, sizeof(files[i-1]->d_name));
In that case why use buffer at all
C Syntax (Toggle Plain Text)
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
C Syntax (Toggle Plain Text)
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.
Aki, looked into this and I'm now using
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*
C Syntax (Toggle Plain Text)
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.
Should I allocate memory for it? if so, how and more importantly; Where would it be natural to do it? *clueless*
![]() |
Similar Threads
- google "keyword" question (Search Engine Optimization)
- Windows 2000 Adv Server and "Printing Subsystem" (Windows NT / 2000 / XP)
Other Threads in the C Forum
- Previous Thread: urgent help needed for a beginner
- Next Thread: Regexp?
| Thread Tools | Search this Thread |
#include * ansi array arrays asterisks binarysearch calculate centimeter changingto char character convert copyanyfile copyimagefile cprogramme creafecopyofanytypeoffileinc createprocess() database dynamic execv feet fgets file floatingpointvalidation fork function getlogicaldrivestrin givemetehcodez grade gtkwinlinux hacking histogram ide inches include incrementoperators infiniteloop input interest intmain() iso kernel keyboard kilometer km license linked linkedlist linux looping lowest matrix meter microsoft number oddnumber open opendocumentformat openwebfoundation owf pdf pointer posix power probleminc process program programming pyramidusingturboccodes radix read recursion recv recvblocked research reversing scheduling segmentationfault send sequential single socket socketprogramming standard strchr string suggestions systemcall test threads turboc unix urboc user variable wab whythiscodecausesegmentationfault win32api windowsapi






