home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!charon.amdahl.com!pacbell.com!sgiblab!sdd.hp.com!zaphod.mps.ohio-state.edu!cis.ohio-state.edu!rutgers!njitgw.njit.edu!hertz.njit.edu!jxv3790
- From: jxv3790@hertz.njit.edu (Jake Vogelaar)
- Newsgroups: comp.unix.programmer
- Subject: Problems redirecting a child's output
- Message-ID: <1992Nov6.233505.4588@njitgw.njit.edu>
- Date: 6 Nov 92 23:35:05 GMT
- Sender: news@njit.edu
- Organization: New Jersey Institute of Technology, Newark, N.J.
- Lines: 35
- Nntp-Posting-Host: hertz.njit.edu
-
- I have to implement a simple shell for one of my classes, I copied the
- following piece of code from a textbook to do redirection. Problem is,
- it doesn't work. Two things go wrong. One: "outfile" stays a file of
- lenght zero after the redirect, Two: after one redirect everything is
- redirected. The redirect input does not work either. Can anybody help ??
-
- //JV
-
- -------------------------------------------
- if ((frk=fork())==0) {
- /*********** CHILD ***********/
- if (/* redirect output */) {
- if ((fd=creat(outfile))==-1) {
- fprintf(stderr,"Error: cannot open %s\n",outfile);
- exit(1);
- }
- close(stdout); dup(fd); close(fd);
- }
- if (/* redirect input */) {
- if ((fd=open(infile,O_RDONLY))==-1) {
- fprintf(stderr,"Error: cannot open %s\n",infile);
- exit(1);
- }
- close(stdin); dup(fd); close(fd);
- }
- execlp(cmd,cmd,NULL);
- exit(1);
- }
- parent ...
- ---------------------------------------
- --
- /////////////////////////////////////////////////////////////////////////
- Jake Vogelaar
- Computer Vision Research
- Warner-Lambert Manufacturing Development Laboratory
-