isc_stdout.c 2.85 KB
Newer Older
Per Cederqvist's avatar
Per Cederqvist committed
1
2
3
/*
** isc_stdout.c                 Some nice-to-have functions for output..
**
Per Cederqvist's avatar
Per Cederqvist committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
** Copyright (C) 1991, 1998-1999 by Peter Eriksson and
** Per Cederqvist of the Lysator Academic Computer Association.
**
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Library General Public
** License as published by the Free Software Foundation; either
** version 2 of the License, or (at your option) any later version.
**
** This library is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
** Library General Public License for more details.
**
** You should have received a copy of the GNU Library General Public
** License along with this library; if not, write to the Free
** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
Per Cederqvist's avatar
Per Cederqvist committed
21
22
23
**
** history:
** 910305 pen      moved into separate file
Per Cederqvist's avatar
Per Cederqvist committed
24
** (See ChangeLog for recent history)
Per Cederqvist's avatar
Per Cederqvist committed
25
26
27
*/

#include <stdio.h>
Per Cederqvist's avatar
Per Cederqvist committed
28
29
30
#ifdef HAVE_STRING_H
#  include <string.h>
#endif
31
32
33
34
35
36
37
#ifdef HAVE_STDARG_H
#  include <stdarg.h>
#endif
#include <time.h>
#include <sys/types.h>
#include <sys/socket.h>

38
39
40
41
#include "oop.h"

#include "s-string.h"

Per Cederqvist's avatar
Per Cederqvist committed
42
#include "isc.h"
43
#include "intern.h"
Per Cederqvist's avatar
Per Cederqvist committed
44
45
46

int
isc_putc(int           chr,
47
	 struct isc_scb *session)
Per Cederqvist's avatar
Per Cederqvist committed
48
{
49
50
  struct isc_scb_internal *scb = (struct isc_scb_internal*)session;

51
  if (scb->state != ISC_STATE_DISABLED &&
Per Cederqvist's avatar
Per Cederqvist committed
52
53
54
55
      scb->state != ISC_STATE_RUNNING)
    return EOF;
  
  while (scb->sendindex == sizeof(scb->sendbuf))
56
    isc_flush(session);
Per Cederqvist's avatar
Per Cederqvist committed
57
58
59
60
61
62
63

  return scb->sendbuf[scb->sendindex++] = chr;
}



int
64
isc_write(struct isc_scb  *session,
Per Cederqvist's avatar
Per Cederqvist committed
65
66
67
68
69
70
71
	  const void  * buffer,
	  size_t        length)
{
  const char  * bp;
  int           len;
  int           blen;
  int           clen;
72
  struct isc_scb_internal *scb = (struct isc_scb_internal*)session;
Per Cederqvist's avatar
Per Cederqvist committed
73
74


75
  if (scb->state != ISC_STATE_DISABLED &&
Per Cederqvist's avatar
Per Cederqvist committed
76
77
78
79
80
81
82
83
84
85
86
87
88
      scb->state != ISC_STATE_RUNNING)
    return EOF;
  
  bp   = (const char *) buffer;
  clen = length;
  
  while (clen > 0)
  {
    blen = sizeof(scb->sendbuf) - scb->sendindex;

    /* Make room in sendbuf */
    while (blen == 0)
    {
89
      isc_flush(&scb->pub);
Per Cederqvist's avatar
Per Cederqvist committed
90
91
92
93
94
95
96
97
98
99
100
101
102
103
      blen = sizeof(scb->sendbuf) - scb->sendindex;
    }

    len  = (clen > blen ? blen : clen);
    
    memcpy(scb->sendbuf+scb->sendindex, bp, len);
    scb->sendindex += len;
    clen -= len;
    bp   += len;
  }

  return length;
}

Per Cederqvist's avatar
Per Cederqvist committed
104
105
int
isc_puts(const char *str,
106
	 struct isc_scb *scb)
Per Cederqvist's avatar
Per Cederqvist committed
107
108
109
110
111
112
{
  return isc_write(scb, str, strlen(str));
}

int
isc_putul(unsigned long nr,
113
	  struct isc_scb *scb)
Per Cederqvist's avatar
Per Cederqvist committed
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
{
  static char   buf[sizeof(unsigned long) * 3 + 1];
  char         *cp;

  if (nr < 10)
    return isc_putc("0123456789"[nr], scb);
  else
  {
    cp = buf + sizeof(buf);
    while (nr > 0)
    {
      *--cp = (nr % 10) + '0';
      nr /= 10;
    }
    return isc_write(scb, cp, buf + sizeof(buf) - cp);
  }
}