www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - popen

reply Charles <Charles_member pathlink.com> writes:
If you happen to use popen don't forget to use pclose ... I just spent hours
trying to figure out why my oh /not/ so elegantly written code was leaving
defunct processes all over the place.

So here's how to do it correctly.

import std.sting;
import std.stream;

extern (C) FILE * popen(char *,char *) ;

int main ()  {
char [] cmd = "ps -A";
FILE * PH;
IN = popen(cmd,"r");
char [] line, tmp_line;
if (!IN) {
//fav error message here
} else {
// read from results
while (fgets(line_tmp,line_tmp.sizeof,IN) ) {
line ~= std.string.chomp(line_tmp);
}
}

if (pclose(IN)!=0 ) {
// error msg here
}
return 0;
}
Nov 11 2005
parent reply someone <someone_member pathlink.com> writes:
Hello,   

i try to compile your programm, but i get some error message.   

<code>   
import std.string;  
import std.stream;  
import std.c.stdio;  
import std.stdio;  

extern (C) FILE* popen(char *,char *);  
extern (C) int pclose(FILE*);  

int main() {  
char [] cmd = "ps -A";  
FILE* IN;  
IN = popen(cmd,"r");  

char [2500] tmp_line;  
char [] line;  
if (!IN) {  
// error message  
} else {  
// read from results  
//while (fgets(tmp_line,tmp_line.sizeof,IN) != null) {  
while (fgets(tmp_line,tmp_line.sizeof,IN) ) {  
line ~= std.string.chomp(tmp_line);  
writefln(line);  
}  
}  

if ( pclose(IN) !=0 ) {  
// error message  
}  
return 0;  
}  

</code>  

I must insert the line "char [2500] tmp_line;"  
only with "char [] tmp_line" i get segmentaution fault  
This line too: extern (C) int pclose(FILE*);  

My output is:  
PID TTY          TIME CMD  
Error: 4invalid UTF-8 sequence  

:-(  

My system is Debian stable and dmd 139 .  


In article <dl2tu1$15ia$1 digitaldaemon.com>, Charles says...   
   
If you happen to use popen don't forget to use pclose ... I just spent hours   
trying to figure out why my oh /not/ so elegantly written code was leaving   
defunct processes all over the place.   
   
So here's how to do it correctly.   
   
import std.sting;   
import std.stream;   
   
extern (C) FILE * popen(char *,char *) ;   
   
int main ()  {   
char [] cmd = "ps -A";   
FILE * PH;   
IN = popen(cmd,"r");   
char [] line, tmp_line;   
if (!IN) {   
//fav error message here   
} else {   
// read from results   
while (fgets(line_tmp,line_tmp.sizeof,IN) ) {   
line ~= std.string.chomp(line_tmp);   
}   
}   
   
if (pclose(IN)!=0 ) {   
// error msg here   
}   
return 0;   
}   
   
   
Nov 15 2005
parent =?ISO-8859-1?Q?Jari-Matti_M=E4kel=E4?= <jmjmak invalid_utu.fi> writes:
someone wrote:
 Hello,   
 
 i try to compile your programm, but i get some error message.   
 
 <code>   
 import std.string;  
 import std.stream;  
 import std.c.stdio;  
 import std.stdio;  
 
 extern (C) FILE* popen(char *,char *);  
 extern (C) int pclose(FILE*);  
 
 int main() {  
 char [] cmd = "ps -A";  
 FILE* IN;  
 IN = popen(cmd,"r");  
 
 char [2500] tmp_line;  
 char [] line;  
 if (!IN) {  
 // error message  
 } else {  
 // read from results  
 //while (fgets(tmp_line,tmp_line.sizeof,IN) != null) {  
 while (fgets(tmp_line,tmp_line.sizeof,IN) ) {  
 line ~= std.string.chomp(tmp_line);  
 writefln(line);  
 }  
 }  
 
 if ( pclose(IN) !=0 ) {  
 // error message  
 }  
 return 0;  
 }  
 
 </code>  
 
 I must insert the line "char [2500] tmp_line;"  
 only with "char [] tmp_line" i get segmentaution fault  
 This line too: extern (C) int pclose(FILE*);  
Many C-functions need a preallocated buffer to store the results.
 My output is:  
 PID TTY          TIME CMD  
 Error: 4invalid UTF-8 sequence
The C-functions used here don't support Unicode. Convert the output to UTF or use printf instead.
Nov 29 2005