substdo.c
author tomas@localhost
Thu, 01 Nov 2007 14:46:11 +0100
changeset 0 c045670f36e9
permissions -rw-r--r--
Imported queue-fix-1.4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
     1
#include "substdio.h"
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
     2
#include "str.h"
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
     3
#include "byte.h"
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
     4
#include "error.h"
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
     5
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
     6
static int allwrite(op,fd,buf,len)
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
     7
register int (*op)();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
     8
register int fd;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
     9
register char *buf;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    10
register int len;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    11
{
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    12
  register int w;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    13
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    14
  while (len) {
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    15
    w = op(fd,buf,len);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    16
    if (w == -1) {
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    17
      if (errno == error_intr) continue;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    18
      return -1; /* note that some data may have been written */
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    19
    }
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    20
    if (w == 0) ; /* luser's fault */
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    21
    buf += w;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    22
    len -= w;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    23
  }
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    24
  return 0;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    25
}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    26
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    27
int substdio_flush(s)
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    28
register substdio *s;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    29
{
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    30
  register int p;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    31
 
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    32
  p = s->p;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    33
  if (!p) return 0;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    34
  s->p = 0;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    35
  return allwrite(s->op,s->fd,s->x,p);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    36
}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    37
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    38
int substdio_bput(s,buf,len)
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    39
register substdio *s;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    40
register char *buf;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    41
register int len;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    42
{
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    43
  register int n;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    44
 
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    45
  while (len > (n = s->n - s->p)) {
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    46
    byte_copy(s->x + s->p,n,buf); s->p += n; buf += n; len -= n;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    47
    if (substdio_flush(s) == -1) return -1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    48
  }
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    49
  /* now len <= s->n - s->p */
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    50
  byte_copy(s->x + s->p,len,buf);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    51
  s->p += len;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    52
  return 0;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    53
}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    54
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    55
int substdio_put(s,buf,len)
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    56
register substdio *s;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    57
register char *buf;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    58
register int len;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    59
{
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    60
  register int n;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    61
 
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    62
  n = s->n;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    63
  if (len > n - s->p) {
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    64
    if (substdio_flush(s) == -1) return -1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    65
    /* now s->p == 0 */
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    66
    if (n < SUBSTDIO_OUTSIZE) n = SUBSTDIO_OUTSIZE;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    67
    while (len > s->n) {
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    68
      if (n > len) n = len;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    69
      if (allwrite(s->op,s->fd,buf,n) == -1) return -1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    70
      buf += n;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    71
      len -= n;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    72
    }
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    73
  }
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    74
  /* now len <= s->n - s->p */
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    75
  byte_copy(s->x + s->p,len,buf);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    76
  s->p += len;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    77
  return 0;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    78
}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    79
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    80
int substdio_putflush(s,buf,len)
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    81
register substdio *s;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    82
register char *buf;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    83
register int len;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    84
{
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    85
  if (substdio_flush(s) == -1) return -1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    86
  return allwrite(s->op,s->fd,buf,len);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    87
}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    88
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    89
int substdio_bputs(s,buf)
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    90
register substdio *s;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    91
register char *buf;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    92
{
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    93
  return substdio_bput(s,buf,str_len(buf));
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    94
}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    95
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    96
int substdio_puts(s,buf)
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    97
register substdio *s;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    98
register char *buf;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    99
{
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   100
  return substdio_put(s,buf,str_len(buf));
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   101
}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   102
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   103
int substdio_putsflush(s,buf)
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   104
register substdio *s;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   105
register char *buf;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   106
{
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   107
  return substdio_putflush(s,buf,str_len(buf));
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   108
}