473,396 Members | 1,914 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Problems implementing popen()

Hello everyone,

I've been trying to roll my own popen(), but I've run into problems.
Would anyone care to look at my code and tell me what I'm doing wrong?

Here's my code:

#include <errno.h>
#include <stdio.h>
#include <unistd.h>

FILE *mypopen(const char *file, const char **argv)
{
int pid;
int filedes[2];

if(pipe(filedes))
return NULL;

pid = fork();
if(pid == -1) {
return NULL;
} else if(! pid) {
close(filedes[1]);
dup2(filedes[0], fileno(stdout));
execvp(file, argv);
} else {
close(filedes[0]);
return fdopen(filedes[1], "r");
}
}

int main(int argc, char **argv)
{
FILE *file;
const char *args[3] = {"hg", "status", NULL};
int inchar;

file = mypopen("hg", args);
if(! file) {
return 1;
} else {
inchar = fgetc(file);
while(inchar != EOF) {
fputc(inchar, stdout);
inchar = fgetc(file);
}
fclose(file);
}
return 0;
}

Thanks,
Rob Hoelz
Oct 15 '07 #1
3 2047
Rob Hoelz <ho***@wisc.eduwrote:
Hello everyone,

I've been trying to roll my own popen(), but I've run into problems.
Would anyone care to look at my code and tell me what I'm doing wrong?

Here's my code:

#include <errno.h>
#include <stdio.h>
#include <unistd.h>

FILE *mypopen(const char *file, const char **argv)
{
int pid;
int filedes[2];

if(pipe(filedes))
return NULL;

pid = fork();
if(pid == -1) {
return NULL;
} else if(! pid) {
close(filedes[1]);
dup2(filedes[0], fileno(stdout));
execvp(file, argv);
} else {
close(filedes[0]);
return fdopen(filedes[1], "r");
}
}

int main(int argc, char **argv)
{
FILE *file;
const char *args[3] = {"hg", "status", NULL};
int inchar;

file = mypopen("hg", args);
if(! file) {
return 1;
} else {
inchar = fgetc(file);
while(inchar != EOF) {
fputc(inchar, stdout);
inchar = fgetc(file);
}
fclose(file);
}
return 0;
}

Thanks,
Rob Hoelz
Nevermind...I need to learn to read man pages more closely...
Oct 15 '07 #2
Rob Hoelz <ho***@wisc.eduwrites:
I've been trying to roll my own popen(), but I've run into problems.
Would anyone care to look at my code and tell me what I'm doing wrong?

Here's my code:

#include <errno.h>
#include <stdio.h>
#include <unistd.h>
[snip]
pipe(), fork(), et al are not defined by the C standard.
Try comp.unix.programmer.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 15 '07 #3
Rob Hoelz wrote:
>
I've been trying to roll my own popen(), but I've run into problems.
Would anyone care to look at my code and tell me what I'm doing wrong?

Here's my code:

#include <errno.h>
#include <stdio.h>
#include <unistd.h>

FILE *mypopen(const char *file, const char **argv)
{
int pid;
int filedes[2];

if(pipe(filedes))
return NULL;

pid = fork();
Wrong newsgroup. So far unistd.h, pipe(), fork() etc. are all
unknown to standard C. Try comp.unix.programmer. My guess is that
you can't write portable system functions.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Oct 19 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Byrom, R (Rob) | last post by:
hi, When I use: os.popen('ssh -l root@something tomcat4 stop') I find the command always hangs. The 'something' host contains the correct key in the authorized_hosts file and works ok on the...
2
by: Stuart McGraw | last post by:
When I run (Python 2.3.3) this script on a MS Windows 2000 machine: import os print "1st popen..." f = os.popen ("echo " + u"\u0054\u0045\u0053\u0054.txt") print f.read () print "2nd popen..."...
7
by: Greg | last post by:
I am trying to implement the UNIX pipe command using C but with the "->" operator. Everything works fine with 1 pipe, but when I try to use 2 or more, it hangs up when reading the pipe_in...
8
by: Henrik Lied | last post by:
Hi there! I'm trying to convert a video in a background process. The scenario I'm after: 1. The user uploads a video 2. The video is saved in my media directory, and the database is populated...
1
by: loial | last post by:
I am having problems with os.rename on AIX The source file definately exists and the target file does not. However the rename always generates an OSerror exception. I can do the rename via a...
2
by: Greg Ercolano | last post by:
When I use os.popen(cmd,'w'), I find that under windows, the stdout of the child process disappears, instead of appearing in the DOS window the script is invoked from. eg: C:\type foo.py import...
3
by: Jesse | last post by:
Hi all, I have a problem using wget and Popen. I hope someone can help. -- Problem -- I want to use the command: wget -nv -O "dir/cpan.txt" "http://search.cpan.org" and capture all it's...
25
by: Jeremy Banks | last post by:
Hi. I wondered if anyone knew the rationale behind the naming of the Popen class in the subprocess module. Popen sounds like the a suitable name for a function that created a subprocess, but the...
1
by: Mark Shewfelt | last post by:
Hello, I am attempting to use Popen() in a Windows service. I have a small Win32 .exe that I normally run through the os.popen2() function. I've written a class to work with the input and output...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.