Back to index

glibc  2.9
Defines | Functions
devstream.c File Reference
#include <stdio.h>
#include <mach.h>
#include <device/device.h>
#include <errno.h>
#include <string.h>

Go to the source code of this file.

Defines

#define cookie_io_functions_t   __io_functions
#define write   __write
#define read   __read
#define close   __close

Functions

static ssize_t devstream_write (void *cookie, const char *buffer, size_t n)
static ssize_t devstream_read (void *cookie, char *buffer, size_t to_read)
static int dealloc_ref (void *cookie)
FILEmach_open_devstream (mach_port_t dev, const char *mode)

Define Documentation

#define close   __close

Definition at line 127 of file devstream.c.

#define cookie_io_functions_t   __io_functions

Definition at line 124 of file devstream.c.

#define read   __read

Definition at line 126 of file devstream.c.

#define write   __write

Definition at line 125 of file devstream.c.


Function Documentation

static int dealloc_ref ( void *  cookie) [static]

Definition at line 113 of file devstream.c.

{
  if (mach_port_deallocate (mach_task_self (), (mach_port_t) cookie))
    {
      errno = EINVAL;
      return -1;
    }
  return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static ssize_t devstream_read ( void *  cookie,
char *  buffer,
size_t  to_read 
) [static]

Definition at line 84 of file devstream.c.

{
  const device_t dev = (device_t) cookie;

  kern_return_t err;
  mach_msg_type_number_t nread = to_read;

  err = device_read_inband (dev, 0, 0, to_read, buffer, &nread);
  if (err)
    {
      errno = err;
      return -1;
    }

  /* Translate CR to LF.  */
  {
    char *p;
    for (p = memchr (buffer, '\r', nread); p;
        p = memchr (p + 1, '\r', (buffer + nread) - (p + 1)))
      *p = '\n';
  }

  /* Echo back what we read.  */
  (void) devstream_write (cookie, buffer, nread);

  return nread;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static ssize_t devstream_write ( void *  cookie,
const char *  buffer,
size_t  n 
) [static]

Definition at line 29 of file devstream.c.

{
  const device_t dev = (device_t) cookie;

  int write_some (const char *p, size_t to_write)
    {
      kern_return_t err;
      int wrote;
      int thiswrite;

      while (to_write > 0)
       {
         thiswrite = to_write;
         if (thiswrite > IO_INBAND_MAX)
           thiswrite = IO_INBAND_MAX;

         if (err = device_write_inband (dev, 0, 0, p, thiswrite, &wrote))
           {
             errno = err;
             return 0;
           }
         p += wrote;
         to_write -= wrote;
       }
      return 1;
    }
  int write_crlf (void)
    {
      static const char crlf[] = "\r\n";
      return write_some (crlf, 2);
    }

  /* Search for newlines (LFs) in the buffer.  */

  const char *start = buffer, *p;
  while ((p = memchr (start, '\n', n)) != NULL)
    {
      /* Found one.  Write out through the preceding character,
        and then write a CR/LF pair.  */

      if ((p > start && !write_some (start, p - start))
         || !write_crlf ())
       return (start - buffer) ?: -1;

      n -= p + 1 - start;
      start = p + 1;
    }

  /* Write the remainder of the buffer.  */
  if (write_some (start, n))
    start += n;
  return (start - buffer) ?: -1;
}

Here is the call graph for this function:

Here is the caller graph for this function:

FILE* mach_open_devstream ( mach_port_t  dev,
const char *  mode 
)

Definition at line 131 of file devstream.c.

{
  FILE *stream;

  if (mach_port_mod_refs (mach_task_self (), dev, MACH_PORT_RIGHT_SEND, 1))
    {
      errno = EINVAL;
      return NULL;
    }

  stream = fopencookie ((void *) dev, mode,
                     (cookie_io_functions_t) { write: devstream_write,
                                            read: devstream_read,
                                            close: dealloc_ref });
  if (stream == NULL)
    {
      mach_port_deallocate (mach_task_self (), dev);
      return NULL;
    }

  return stream;
}

Here is the call graph for this function: