View mode: basic / threaded / horizontal-split · Log in · Help
November 11, 2005
popen
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;
}
November 15, 2005
Re: popen
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;   
>}   
>   
>
November 29, 2005
Re: popen
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.
Top | Discussion index | About this forum | D home