home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / unix / programm / 5197 < prev    next >
Encoding:
Internet Message Format  |  1992-11-08  |  1.6 KB

  1. 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
  2. From: jxv3790@hertz.njit.edu (Jake Vogelaar)
  3. Newsgroups: comp.unix.programmer
  4. Subject: Problems redirecting a child's output
  5. Message-ID: <1992Nov6.233505.4588@njitgw.njit.edu>
  6. Date: 6 Nov 92 23:35:05 GMT
  7. Sender: news@njit.edu
  8. Organization: New Jersey Institute of Technology, Newark, N.J.
  9. Lines: 35
  10. Nntp-Posting-Host: hertz.njit.edu
  11.  
  12. I have to implement a simple shell for one of my classes, I copied the 
  13. following piece of code from a textbook to do redirection.  Problem is,
  14. it doesn't work.  Two things go wrong.  One: "outfile" stays a file of
  15. lenght zero after the redirect, Two: after one redirect everything is
  16. redirected.  The redirect input does not work either. Can anybody help ??  
  17.  
  18. //JV
  19.  
  20. -------------------------------------------
  21.   if ((frk=fork())==0) {
  22.    /*********** CHILD ***********/
  23.    if (/* redirect output */) {
  24.     if ((fd=creat(outfile))==-1) {
  25.      fprintf(stderr,"Error: cannot open %s\n",outfile);
  26.      exit(1);
  27.     }
  28.     close(stdout);  dup(fd);  close(fd);
  29.    }
  30.    if (/* redirect input */) {
  31.     if ((fd=open(infile,O_RDONLY))==-1) {
  32.      fprintf(stderr,"Error: cannot open %s\n",infile);
  33.      exit(1);
  34.     }
  35.     close(stdin); dup(fd); close(fd);
  36.    }
  37.    execlp(cmd,cmd,NULL);
  38.    exit(1);
  39.   }
  40.   parent ...
  41. ---------------------------------------
  42. -- 
  43. /////////////////////////////////////////////////////////////////////////
  44. Jake Vogelaar
  45. Computer Vision Research 
  46. Warner-Lambert Manufacturing Development Laboratory
  47.