* Do not define _BSD_SOURCE but define _DEFAULT_SOURCE instead.
[alpine.git] / imap / src / osdep / unix / sslstdio.c
blob3d3b1e0620440445f965fa0c67ff0d530f46f562
1 /* ========================================================================
2 * Copyright 2008 Mark Crispin
3 * ========================================================================
4 */
6 /*
7 * Program: SSL standard I/O routines for server use
9 * Author: Mark Crispin
11 * Date: 22 September 1998
12 * Last Edited: 19 November 2008
14 * Previous versions of this file were
16 * Copyright 1988-2006 University of Washington
18 * Licensed under the Apache License, Version 2.0 (the "License");
19 * you may not use this file except in compliance with the License.
20 * You may obtain a copy of the License at
22 * http://www.apache.org/licenses/LICENSE-2.0
26 /* Get character
27 * Returns: character or EOF
30 int PBIN (void)
32 if (!sslstdio) {
33 int ret;
34 do {
35 clearerr (stdin);
36 ret = getchar ();
37 } while ((ret == EOF) && !feof (stdin) && ferror (stdin) &&
38 (errno == EINTR));
39 return ret;
41 if (!ssl_getdata (sslstdio->sslstream)) return EOF;
42 /* one last byte available */
43 sslstdio->sslstream->ictr--;
44 return (int) *(sslstdio->sslstream->iptr)++;
48 /* Get string
49 * Accepts: destination string pointer
50 * number of bytes available
51 * Returns: destination string pointer or NIL if EOF
54 char *PSIN (char *s,int n)
56 int i,c;
57 if (start_tls) { /* doing a start TLS? */
58 ssl_server_init (start_tls);/* enter the mode */
59 start_tls = NIL; /* don't do this again */
61 if (!sslstdio) {
62 char *ret;
63 do {
64 clearerr (stdin);
65 ret = fgets (s,n,stdin);
66 } while (!ret && !feof (stdin) && ferror (stdin) && (errno == EINTR));
67 return ret;
69 for (i = c = 0, n-- ; (c != '\n') && (i < n); sslstdio->sslstream->ictr--) {
70 if ((sslstdio->sslstream->ictr <= 0) && !ssl_getdata (sslstdio->sslstream))
71 return NIL; /* read error */
72 c = s[i++] = *(sslstdio->sslstream->iptr)++;
74 s[i] = '\0'; /* tie off string */
75 return s;
79 /* Get record
80 * Accepts: destination string pointer
81 * number of bytes to read
82 * Returns: T if success, NIL otherwise
85 long PSINR (char *s,unsigned long n)
87 unsigned long i;
88 if (start_tls) { /* doing a start TLS? */
89 ssl_server_init (start_tls);/* enter the mode */
90 start_tls = NIL; /* don't do this again */
92 if (sslstdio) return ssl_getbuffer (sslstdio->sslstream,n,s);
93 /* non-SSL case */
94 while (n && ((i = fread (s,1,n,stdin)) || (errno == EINTR))) s += i,n -= i;
95 return n ? NIL : LONGT;
99 /* Wait for stdin input
100 * Accepts: timeout in seconds
101 * Returns: T if have input on stdin, else NIL
104 long INWAIT (long seconds)
106 return (sslstdio ? ssl_server_input_wait : server_input_wait) (seconds);
109 /* Put character
110 * Accepts: character
111 * Returns: character written or EOF
114 int PBOUT (int c)
116 if (!sslstdio) return putchar (c);
117 /* flush buffer if full */
118 if (!sslstdio->octr && PFLUSH ()) return EOF;
119 sslstdio->octr--; /* count down one character */
120 *sslstdio->optr++ = c; /* write character */
121 return c; /* return that character */
125 /* Put string
126 * Accepts: destination string pointer
127 * Returns: 0 or EOF if error
130 int PSOUT (char *s)
132 if (!sslstdio) return fputs (s,stdout);
133 while (*s) { /* flush buffer if full */
134 if (!sslstdio->octr && PFLUSH ()) return EOF;
135 *sslstdio->optr++ = *s++; /* write one more character */
136 sslstdio->octr--; /* count down one character */
138 return 0; /* success */
141 /* Put record
142 * Accepts: source sized text
143 * Returns: 0 or EOF if error
146 int PSOUTR (SIZEDTEXT *s)
148 unsigned char *t = s->data;
149 unsigned long i = s->size;
150 unsigned long j;
151 if (sslstdio) while (i) { /* until request satisfied */
152 /* flush buffer if full */
153 if (!sslstdio->octr && PFLUSH ()) break;
154 /* blat as big a chucnk as we can */
155 memcpy (sslstdio->optr,t,j = min (i,sslstdio->octr));
156 sslstdio->optr += j; /* account for chunk */
157 sslstdio->octr -= j;
158 t += j;
159 i -= j;
161 else while (i && ((j = fwrite (t,1,i,stdout)) || (errno == EINTR)))
162 t += j,i -= j;
163 return i ? EOF : NIL;
167 /* Flush output
168 * Returns: 0 or EOF if error
171 int PFLUSH (void)
173 if (!sslstdio) return fflush (stdout);
174 /* force out buffer */
175 if (!ssl_sout (sslstdio->sslstream,sslstdio->obuf,
176 SSLBUFLEN - sslstdio->octr)) return EOF;
177 /* renew output buffer */
178 sslstdio->optr = sslstdio->obuf;
179 sslstdio->octr = SSLBUFLEN;
180 return 0; /* success */