home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / pipe.c < prev    next >
C/C++ Source or Header  |  1998-06-17  |  5KB  |  131 lines

  1. /***
  2. *pipe.c - create a pipe
  3. *
  4. *       Copyright (c) 1989-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       defines _pipe() - creates a pipe (i.e., an I/O channel for interprocess
  8. *                         communication)
  9. *
  10. *******************************************************************************/
  11.  
  12. #include <cruntime.h>
  13. #include <oscalls.h>
  14. #include <mtdll.h>
  15. #include <io.h>
  16. #include <internal.h>
  17. #include <stdlib.h>
  18. #include <errno.h>
  19. #include <msdos.h>
  20. #include <fcntl.h>
  21.  
  22. /***
  23. *int _pipe(phandles, psize, textmode) - open a pipe
  24. *
  25. *Purpose:
  26. *       Checks if the given handle is associated with a character device
  27. *       (terminal, console, printer, serial port)
  28. *
  29. *       Multi-thread notes: No locking is performed or deemed necessary. The
  30. *       handles returned by DOSCREATEPIPE are newly opened and, therefore,
  31. *       should not be referenced by any thread until after the _pipe call is
  32. *       complete. The function is not protected from some thread of the caller
  33. *       doing, say, output to a previously invalid handle that becomes one of
  34. *       the pipe handles. However, any such program is doomed anyway and
  35. *       protecting the _pipe function such a case would be of little value.
  36. *
  37. *Entry:
  38. *       int phandle[2] - array to hold returned read (phandle[0]) and write
  39. *                        (phandle[1]) handles
  40. *
  41. *       unsigned psize - amount of memory, in bytes, to ask o.s. to reserve
  42. *                        for the pipe
  43. *
  44. *       int textmode   - _O_TEXT, _O_BINARY, _O_NOINHERIT, or 0 (use default)
  45. *
  46. *Exit:
  47. *       returns 0 if successful
  48. *       returns -1 if an error occurs in which case, errno is set to:
  49. *
  50. *Exceptions:
  51. *
  52. *******************************************************************************/
  53.  
  54. int __cdecl _pipe (
  55.         int phandles[2],
  56.         unsigned psize,
  57.         int textmode
  58.         )
  59. {
  60.         ULONG dosretval;                    /* o.s. return value */
  61.  
  62.         HANDLE ReadHandle, WriteHandle;
  63.         SECURITY_ATTRIBUTES SecurityAttributes;
  64.  
  65.         SecurityAttributes.nLength = sizeof(SecurityAttributes);
  66.         SecurityAttributes.lpSecurityDescriptor = NULL;
  67.  
  68.         if (textmode & _O_NOINHERIT) {
  69.                 SecurityAttributes.bInheritHandle = FALSE;
  70.         }
  71.         else {
  72.                 SecurityAttributes.bInheritHandle = TRUE;
  73.         }
  74.  
  75.         if (!CreatePipe(&ReadHandle, &WriteHandle, &SecurityAttributes, psize)) {
  76.                 /* o.s. error */
  77.                 dosretval = GetLastError();
  78.                 _dosmaperr(dosretval);
  79.                 return -1;
  80.         }
  81.  
  82.         /* now we must allocate C Runtime handles for Read and Write handles */
  83.         if ((phandles[0] = _alloc_osfhnd()) != -1) {
  84.  
  85.                 _osfile(phandles[0]) = (char)(FOPEN | FPIPE | FTEXT);
  86.  
  87.                 if ((phandles[1] = _alloc_osfhnd()) != -1) {
  88.  
  89.                         _osfile(phandles[1]) = (char)(FOPEN | FPIPE | FTEXT);
  90.  
  91.                         if ( (textmode & _O_BINARY) ||
  92.                              (((textmode & _O_TEXT) == 0) &&
  93.                               (_fmode == _O_BINARY)) ) {
  94.                                 /* binary mode */
  95.                                 _osfile(phandles[0]) &= ~FTEXT;
  96.                                 _osfile(phandles[1]) &= ~FTEXT;
  97.                         }
  98.  
  99.                         if ( textmode & _O_NOINHERIT ) {
  100.                                 _osfile(phandles[0]) |= FNOINHERIT;
  101.                                 _osfile(phandles[1]) |= FNOINHERIT;
  102.                         }
  103.  
  104.                         _set_osfhnd(phandles[0], (long)ReadHandle);
  105.                         _set_osfhnd(phandles[1], (long)WriteHandle);
  106.                         errno = 0;
  107.  
  108.                         _unlock_fh(phandles[1]);    /* unlock handle */
  109.                 }
  110.                 else {
  111.                         _osfile(phandles[0]) = 0;
  112.                         errno = EMFILE;     /* too many files */
  113.                 }
  114.  
  115.                 _unlock_fh(phandles[0]);    /* unlock handle */
  116.         }
  117.         else {
  118.                 errno = EMFILE;     /* too many files */
  119.         }
  120.  
  121.         /* If error occurred, close Win32 handles and return -1 */
  122.         if (errno != 0) {
  123.                 CloseHandle(ReadHandle);
  124.                 CloseHandle(WriteHandle);
  125.                 _doserrno = 0;      /* not an o.s. error */
  126.                 return -1;
  127.         }
  128.  
  129.         return 0;
  130. }
  131.