imapext-2007

view src/osdep/unix/sslstdio.c @ 0:ada5e610ab86

imap-2007e
author yuuji@gentei.org
date Mon, 14 Sep 2009 15:17:45 +0900
parents
children
line source
1 /* ========================================================================
2 * Copyright 1988-2006 University of Washington
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 *
11 * ========================================================================
12 */
14 /*
15 * Program: SSL standard I/O routines for server use
16 *
17 * Author: Mark Crispin
18 * Networks and Distributed Computing
19 * Computing & Communications
20 * University of Washington
21 * Administration Building, AG-44
22 * Seattle, WA 98195
23 * Internet: MRC@CAC.Washington.EDU
24 *
25 * Date: 22 September 1998
26 * Last Edited: 30 August 2006
27 */
29 /* Get character
30 * Returns: character or EOF
31 */
33 int PBIN (void)
34 {
35 if (!sslstdio) return getchar ();
36 if (!ssl_getdata (sslstdio->sslstream)) return EOF;
37 /* one last byte available */
38 sslstdio->sslstream->ictr--;
39 return (int) *(sslstdio->sslstream->iptr)++;
40 }
43 /* Get string
44 * Accepts: destination string pointer
45 * number of bytes available
46 * Returns: destination string pointer or NIL if EOF
47 */
49 char *PSIN (char *s,int n)
50 {
51 int i,c;
52 if (start_tls) { /* doing a start TLS? */
53 ssl_server_init (start_tls);/* enter the mode */
54 start_tls = NIL; /* don't do this again */
55 }
56 if (!sslstdio) return fgets (s,n,stdin);
57 for (i = c = 0, n-- ; (c != '\n') && (i < n); sslstdio->sslstream->ictr--) {
58 if ((sslstdio->sslstream->ictr <= 0) && !ssl_getdata (sslstdio->sslstream))
59 return NIL; /* read error */
60 c = s[i++] = *(sslstdio->sslstream->iptr)++;
61 }
62 s[i] = '\0'; /* tie off string */
63 return s;
64 }
67 /* Get record
68 * Accepts: destination string pointer
69 * number of bytes to read
70 * Returns: T if success, NIL otherwise
71 */
73 long PSINR (char *s,unsigned long n)
74 {
75 unsigned long i;
76 if (start_tls) { /* doing a start TLS? */
77 ssl_server_init (start_tls);/* enter the mode */
78 start_tls = NIL; /* don't do this again */
79 }
80 if (sslstdio) return ssl_getbuffer (sslstdio->sslstream,n,s);
81 /* non-SSL case */
82 while (n && ((i = fread (s,1,n,stdin)) || (errno == EINTR))) s += i,n -= i;
83 return n ? NIL : LONGT;
84 }
87 /* Wait for stdin input
88 * Accepts: timeout in seconds
89 * Returns: T if have input on stdin, else NIL
90 */
92 long INWAIT (long seconds)
93 {
94 return (sslstdio ? ssl_server_input_wait : server_input_wait) (seconds);
95 }
97 /* Put character
98 * Accepts: character
99 * Returns: character written or EOF
100 */
102 int PBOUT (int c)
103 {
104 if (!sslstdio) return putchar (c);
105 /* flush buffer if full */
106 if (!sslstdio->octr && PFLUSH ()) return EOF;
107 sslstdio->octr--; /* count down one character */
108 *sslstdio->optr++ = c; /* write character */
109 return c; /* return that character */
110 }
113 /* Put string
114 * Accepts: destination string pointer
115 * Returns: 0 or EOF if error
116 */
118 int PSOUT (char *s)
119 {
120 if (!sslstdio) return fputs (s,stdout);
121 while (*s) { /* flush buffer if full */
122 if (!sslstdio->octr && PFLUSH ()) return EOF;
123 *sslstdio->optr++ = *s++; /* write one more character */
124 sslstdio->octr--; /* count down one character */
125 }
126 return 0; /* success */
127 }
129 /* Put record
130 * Accepts: source sized text
131 * Returns: 0 or EOF if error
132 */
134 int PSOUTR (SIZEDTEXT *s)
135 {
136 unsigned char *t = s->data;
137 unsigned long i = s->size;
138 unsigned long j;
139 if (sslstdio) while (i) { /* until request satisfied */
140 /* flush buffer if full */
141 if (!sslstdio->octr && PFLUSH ()) break;
142 /* blat as big a chucnk as we can */
143 memcpy (sslstdio->optr,t,j = min (i,sslstdio->octr));
144 sslstdio->optr += j; /* account for chunk */
145 sslstdio->octr -= j;
146 t += j;
147 i -= j;
148 }
149 else while (i && ((j = fwrite (t,1,i,stdout)) || (errno == EINTR)))
150 t += j,i -= j;
151 return i ? EOF : NIL;
152 }
155 /* Flush output
156 * Returns: 0 or EOF if error
157 */
159 int PFLUSH (void)
160 {
161 if (!sslstdio) return fflush (stdout);
162 /* force out buffer */
163 if (!ssl_sout (sslstdio->sslstream,sslstdio->obuf,
164 SSLBUFLEN - sslstdio->octr)) return EOF;
165 /* renew output buffer */
166 sslstdio->optr = sslstdio->obuf;
167 sslstdio->octr = SSLBUFLEN;
168 return 0; /* success */
169 }

UW-IMAP'd extensions by yuuji