home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / glibc-1.06 / sysdeps / mach / hurd / sysd-stdio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-01  |  4.9 KB  |  195 lines

  1. /* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3.  
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8.  
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB.  If
  16. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17. Cambridge, MA 02139, USA.  */
  18.  
  19. #include <ansidecl.h>
  20. #include <errno.h>
  21. #include <stdio.h>
  22. #include <hurd.h>
  23.  
  24.  
  25. /* XXX FILE * locking */
  26.  
  27.  
  28. /* Read up to N chars into BUF from COOKIE.
  29.    Return how many chars were read, 0 for EOF or -1 for error.  */
  30. int
  31. DEFUN(__stdio_read, (cookie, buf, n),
  32.       PTR cookie AND register char *buf AND register size_t n)
  33. {
  34.   size_t nread;
  35.   error_t err;
  36.   char *bufp = buf;
  37.  
  38.   if (err = __io_read ((io_t) cookie, /* XXX ctty */,
  39.                &bufp, &nread, -1, n))
  40.     return __hurd_fail (err);
  41.  
  42.   if (bufp != buf)
  43.     {
  44.       memcpy (buf, bufp, nread);
  45.       __vm_deallocate (bufp, nread);
  46.     }
  47.  
  48.   return nread;
  49. }
  50.  
  51. /* Write up to N chars from BUF to COOKIE.
  52.    Return how many chars were written or -1 for error.  */
  53. int
  54. DEFUN(__stdio_write, (cookie, buf, n),
  55.       PTR cookie AND register CONST char *buf AND register size_t n)
  56. {
  57.   size_t wrote;
  58.   error_t err;
  59.  
  60.   if (err = __io_write ((io_t) cookie, /* XXX ctty */,
  61.             buf, n, -1, &wrote))
  62.     return __hurd_fail (err);
  63.  
  64.   return wrote;
  65. }
  66.  
  67. /* Move COOKIE's file position *POS bytes, according to WHENCE.
  68.    The current file position is stored in *POS.
  69.    Returns zero if successful, nonzero if not.  */
  70. int
  71. DEFUN(__stdio_seek, (cookie, pos, whence),
  72.       PTR cookie AND fpos_t *pos AND int whence)
  73. {
  74.   error_t error = __file_seek ((file_t) cookie, *pos, whence, pos);
  75.   if (error)
  76.     return __hurd_fail (error);
  77.   return 0;
  78. }
  79.  
  80. /* Close the file associated with COOKIE.
  81.    Return 0 for success or -1 for failure.  */
  82. int
  83. DEFUN(__stdio_close, (cookie), PTR cookie)
  84. {
  85.   if (__mach_port_deallocate (__mach_task_self (), (mach_port_t) cookie))
  86.     {
  87.       errno = EINVAL;        /* ? */
  88.       return -1;
  89.     }
  90.   return 0;
  91. }
  92.  
  93.  
  94. /* Open FILENAME with the mode in M.
  95.    Return the magic cookie associated with the opened file
  96.    or NULL which specifies that an integral descriptor may be
  97.    found in *FDPTR.  This descriptor is negative for errors.  */
  98. PTR
  99. DEFUN(__stdio_open, (filename, m, fdptr),
  100.       CONST char *filename AND __io_mode m AND int *fdptr)
  101. {
  102.   int error;
  103.   int flags;
  104.   file_t file;
  105.  
  106.   flags = 0;
  107.   if (m.__read)
  108.     flags |= FS_LOOKUP_READ;
  109.   if (m.__write)
  110.     flags |= FS_LOOKUP_WRITE;
  111.   if (m.__append)
  112.     flags |= FS_LOOKUP_APPEND;
  113.   if (m.__create)
  114.     flags |= FS_LOOKUP_CREATE;
  115.   if (m.__truncate)
  116.     flags |= FS_LOOKUP_TRUNCATE;
  117.   if (m.__exclusive)
  118.     flags |= FS_LOOKUP_EXCL;
  119.  
  120.   file = __path_lookup (filename, flags, 0666 & ~_hurd_umask);
  121.   if (file == MACH_PORT_NULL)
  122.     {
  123.       *fdptr = -1;
  124.       return NULL;
  125.     }
  126.   return (PTR) file;
  127. }
  128.  
  129.  
  130. /* Write a message to the error output.
  131.    Try hard to make it really get out.  */
  132. void
  133. DEFUN(__stdio_errmsg, (msg, len), CONST char *msg AND size_t len)
  134. {
  135.   io_t server;
  136.   size_t wrote;
  137.  
  138.   server = __getdport (2);
  139.   __io_write (server, 0, 0, 0,    /* XXX ctty? */
  140.           buf, n, -1, &wrote);
  141.   __mach_port_deallocate (__mach_task_self (), server);
  142. }
  143.  
  144.  
  145. /* Generate a (hopefully) unique temporary filename
  146.    in DIR (if applicable), using prefix PFX.
  147.    If DIR_SEARCH is nonzero, perform directory searching
  148.    malarky as per the SVID for tempnam.
  149.    Return the generated filename or NULL if one could not
  150.    be generated, putting the length of the string in *LENPTR.  */
  151. char *
  152. DEFUN(__stdio_gen_tempname, (dir, pfx, dir_search, lenptr),
  153.       CONST char *dir AND CONST char *pfx AND
  154.       int dir_search AND size_t *lenptr)
  155. {
  156.   char *name;
  157.   error_t err;
  158.   static file_t tmpserv = MACH_PORT_NULL;
  159.  
  160.   if (tmpserv == MACH_PORT_NULL)
  161.     {
  162.       tmpserv = __path_lookup (_SERVERS_TMPFILE, 0, 0);
  163.       if (tmpserv == MACH_PORT_NULL)
  164.     return NULL;
  165.     }
  166.  
  167.   if (err = __tmpfile_generate_name (tmpserv, dir, pfx, getenv ("TMPDIR"),
  168.                      &name, lenptr))
  169.     {
  170.       errno = __hurd_errno (err);
  171.       return NULL;
  172.     }
  173.   return name;
  174. }
  175.  
  176. /* XXX _hurd_fork_hook to install stream ports.  */
  177.  
  178. static error_t
  179. fork_stdio (task_t newtask)
  180. {
  181.   FILE *f;
  182.  
  183.   for (f = __stdio_head; f != NULL; f = f->next)
  184.     if (f->__io_funcs.__close == __stdio_close &&
  185.     (io_t) f->__cookie != MACH_PORT_NULL)
  186.       {
  187.     error_t err = __mach_port_insert_right (newtask,
  188.                         (io_t) f->__cookie,
  189.                         (io_t) f->__cookie,
  190.                         MACH_PORT_COPY_SEND);
  191.     if (err)
  192.       return err;
  193.       }
  194. }
  195.