home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / k / ksh48.zip / sh / io.c < prev    next >
C/C++ Source or Header  |  1993-01-12  |  4KB  |  229 lines

  1. /*
  2.  * shell buffered IO and formatted output
  3.  */
  4.  
  5. #ifndef lint
  6. static char *RCSid = "$Id: io.c,v 1.4 1992/12/05 13:15:39 sjg Exp $";
  7. #endif
  8.  
  9. #include "stdh.h"
  10. #include <errno.h>
  11. #include <unistd.h>
  12. #include <fcntl.h>
  13. #include <signal.h>
  14. #include <setjmp.h>
  15. #ifdef __STDC__
  16. #include <stdarg.h>
  17. #else
  18. #include <varargs.h>
  19. #endif
  20. #include "sh.h"
  21.  
  22. #if 0
  23. /* fputc with ^ escaping */
  24. static void
  25. fzotc(c, f)
  26.     register int c;
  27.     register FILE *f;
  28. {
  29.     if ((c&0x60) == 0) {        /* C0|C1 */
  30.         putc((c&0x80) ? '$' : '^', f);
  31.         putc((c&0x7F|0x40), f);
  32.     } else if ((c&0x7F) == 0x7F) {    /* DEL */
  33.         putc((c&0x80) ? '$' : '^', f);
  34.         putc('?', f);
  35.     } else
  36.         putc(c, f);
  37. }
  38. #endif
  39.  
  40. /*
  41.  * formatted output functions
  42.  */
  43.  
  44. /* shellf(...); error() */
  45. int
  46. #ifdef __STDC__
  47. errorf(const char *fmt, ...) {
  48. #else
  49. errorf(va_alist) va_dcl
  50. {
  51.     char *fmt;
  52. #endif
  53.     va_list va;
  54.  
  55. #ifdef __STDC__
  56.     va_start(va, fmt);
  57. #else
  58.     va_start(va);
  59.     fmt = va_arg(va, char *);
  60. #endif
  61.     vfprintf(shlout, fmt, va);
  62.     va_end(va);
  63.     /*fflush(shlout);*/
  64.     error();
  65. }
  66.  
  67. /* printf to shlout (stderr) */
  68. int
  69. #ifdef __STDC__
  70. shellf(const char *fmt, ...) {
  71. #else
  72. shellf(va_alist) va_dcl
  73. {
  74.     char *fmt;
  75. #endif
  76.     va_list va;
  77.  
  78. #ifdef __STDC__
  79.     va_start(va, fmt);
  80. #else
  81.     va_start(va);
  82.     fmt = va_arg(va, char *);
  83. #endif
  84.     vfprintf(shlout, fmt, va);
  85.     va_end(va);
  86.     return 0;
  87. }
  88.  
  89. /*
  90.  * We have a stdio stream for any open shell file descriptors (0-9)
  91.  */
  92. FILE *    shf [NUFILE];        /* map shell fd to FILE * */
  93.  
  94. /* open stream for shell fd */
  95. void
  96. fopenshf(fd)
  97.     int fd;
  98. {
  99.     if (shf[fd] != NULL)
  100.         return;
  101.     if (fd <= 2)
  102. #ifdef __EMX__
  103.         /* ??? _streamv[fd].flags = 0; /* re-use stdin, stdout, stderr */
  104. #else
  105. #ifndef _BSDI
  106. #ifdef _MINIX
  107.         /* ? */;
  108. #else
  109.         _iob[fd]._flag = 0; /* re-use stdin, stdout, stderr */
  110. #endif
  111. #else
  112.         /* Chris Torek's stdio replacement */
  113.         __sF[fd]._flags = 0;
  114. #endif
  115. #endif
  116.     shf[fd] = fdopen(fd, "r+");
  117.     if (shf[fd] == NULL)
  118.         return;
  119.     setvbuf(shf[fd], (char*)NULL, _IOFBF, (size_t)BUFSIZ);
  120. }
  121.  
  122. /* flush stream assoc with fd */
  123. /* this must invalidate input and output buffers */
  124. void
  125. flushshf(fd)
  126.     int fd;
  127. {
  128.     if (shf[fd] != NULL) {
  129. #ifndef _BSDI
  130.         /* Chris Torek's stdio replacement */
  131.         fseek(shf[fd], 0L, 1); /* V7 derived */
  132. #endif
  133.         fflush(shf[fd]);    /* standard C */
  134.     }
  135. }
  136.  
  137. /*
  138.  * move fd from user space (0<=fd<10) to shell space (fd>=10)
  139.  */
  140. int
  141. savefd(fd)
  142.     int fd;
  143. {
  144.     int nfd;
  145.  
  146.     if (fd < FDBASE) {
  147.         flushshf(fd);
  148. #ifdef OS2
  149.         nfd = getdup(fd, FDBASE);
  150. #else
  151.         nfd = fcntl(fd, F_DUPFD, FDBASE);
  152. #endif
  153.         if (nfd < 0)
  154.             if (errno == EBADF)
  155.                 return -1;
  156.             else
  157.                 errorf("too many files open in shell\n");
  158.         (void) fd_clexec(nfd);
  159.         close(fd);
  160.     } else
  161.         nfd = fd;
  162.     return nfd;
  163. }
  164.  
  165. void
  166. restfd(fd, ofd)
  167.     int fd, ofd;
  168. {
  169.     if (ofd == 0)        /* not saved (e.savefd) */
  170.         return;
  171.     flushshf(fd);
  172.     close(fd);
  173.     if (ofd < 0)        /* original fd closed */
  174.         return;
  175. #ifdef OS2
  176.         dup2(ofd, fd);
  177. #else
  178.     (void) fcntl(ofd, F_DUPFD, fd);
  179. #endif
  180.     close(ofd);
  181. }
  182.  
  183. void
  184. openpipe(pv)
  185.     register int *pv;
  186. {
  187.     if (pipe(pv) < 0)
  188.         errorf("can't create pipe - try again\n");
  189.     pv[0] = savefd(pv[0]);
  190.     pv[1] = savefd(pv[1]);
  191. }
  192.  
  193. void
  194. closepipe(pv)
  195.     register int *pv;
  196. {
  197.     close(pv[0]);
  198.     close(pv[1]);
  199. }
  200.  
  201. /*
  202.  * temporary files
  203.  */
  204.  
  205. struct temp *
  206. maketemp(ap)
  207.     Area *ap;
  208. {
  209.     register struct temp *tp;
  210.     static unsigned int inc = 0;
  211.     char path [PATH];
  212.  
  213. #ifdef OS2
  214.         extern char *getenv();
  215.         char *tmp = getenv("TMP");
  216.         sprintf(path, "%s/shXXXXXX", tmp ? tmp : "");
  217.         mktemp(path);
  218. #else
  219.     sprintf(path, "/tmp/sh%05u%02u", (unsigned)getpid(), inc++);
  220. #if defined(_SYSV) || defined(_BSD)
  221.     close(creat(path, 0600));    /* to get appropriate permissions */
  222. #endif
  223. #endif
  224.     tp = (struct temp *) alloc(sizeof(struct temp), ap);
  225.     tp->next = NULL;
  226.     tp->name = strsave(path, ap);
  227.     return tp;
  228. }
  229.