Functions

util.c File Reference

#include <unistd.h>
#include <string.h>
#include "echo.h"
Include dependency graph for util.c:

Go to the source code of this file.

Functions

static ssize_t my_read (int fd, char *ptr)
ssize_t readline (int fd, void *vptr, size_t maxlen)
ssize_t Readline (int fd, void *ptr, size_t maxlen)
ssize_t writen (int fd, const void *vptr, size_t n)
void Writen (int fd, void *ptr, size_t nbytes)

Function Documentation

static ssize_t my_read ( int  fd,
char *  ptr 
) [static]

Definition at line 6 of file util.c.

{
  static int read_cnt = 0;
  static char *read_ptr;
  static char read_buf[MAXLINE];

  if (read_cnt <= 0) {
  again:
    if ((read_cnt = read(fd, read_buf, sizeof(read_buf))) < 0) {
      if (errno == EINTR)
    goto again;
      return (-1);
    } else if (read_cnt == 0)
      return (0);
    read_ptr = read_buf;
  }

  read_cnt--;
  *ptr = *read_ptr++;
  return (1);
}

Here is the caller graph for this function:

ssize_t readline ( int  fd,
void *  vptr,
size_t  maxlen 
)

Definition at line 28 of file util.c.

{
  int n, rc;
  char c, *ptr;

  ptr = vptr;
  for (n = 1; n < maxlen; n++) {
    if ((rc = my_read(fd, &c)) == 1) {
      *ptr++ = c;
      if (c == '\n')
    break;          /* newline is stored, like fgets() */
    } else if (rc == 0) {
      if (n == 1)
    return (0);     /* EOF, no data read */
      else
    break;          /* EOF, some data was read */
    } else
      return (-1);      /* error, errno set by read() */
  }

  *ptr = 0;         /* null terminate like fgets() */
  return (n);
}

Here is the call graph for this function:

Here is the caller graph for this function:

ssize_t Readline ( int  fd,
void *  ptr,
size_t  maxlen 
)

Definition at line 54 of file util.c.

{
  ssize_t n;

  if ((n = readline(fd, ptr, maxlen)) < 0)
    perror("readline error");
  return (n);
}

Here is the call graph for this function:

Here is the caller graph for this function:

ssize_t writen ( int  fd,
const void *  vptr,
size_t  n 
)

Definition at line 64 of file util.c.

{
  size_t nleft;
  ssize_t nwritten;
  const char *ptr;

  ptr = vptr;
  nleft = n;
  while (nleft > 0) {
    if ((nwritten = write(fd, ptr, nleft)) <= 0) {
      if (errno == EINTR)
    nwritten = 0;       /* and call write() again */
      else
    return (-1);        /* error */
    }

    nleft -= nwritten;
    ptr += nwritten;
  }
  return (n);
}

Here is the caller graph for this function:

void Writen ( int  fd,
void *  ptr,
size_t  nbytes 
)

Definition at line 88 of file util.c.

{
  if (writen(fd, ptr, nbytes) != nbytes)
    perror("writen error");
}

Here is the call graph for this function:

Here is the caller graph for this function: