1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
/*-------------------------------------------------------------------------
*
* FILE
* fe-misc.c
*
* DESCRIPTION
* miscellaneous useful functions
* these routines are analogous to the ones in libpq/pqcomm.c
*
* Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-misc.c,v 1.8 1997/09/08 21:55:44 momjian Exp $
*
*-------------------------------------------------------------------------
*/
#include <stdlib.h>
#include <stdio.h>
#include "postgres.h"
#include "libpq-fe.h"
/* --------------------------------------------------------------------- */
/* pqGetc:
get a character from stream f
if debug is set, also echo the character fetched
*/
int
pqGetc(FILE *fin, FILE *debug)
{
int c;
c = getc(fin);
if (debug && c != EOF)
fprintf(debug, "From backend> %c\n", c);
return c;
}
/* --------------------------------------------------------------------- */
/* pqPutnchar:
send a string of exactly len length into stream f
returns 1 if there was an error, 0 otherwise.
*/
int
pqPutnchar(const char *s, int len, FILE *f, FILE *debug)
{
if (f == NULL)
return 1;
if (debug)
fprintf(debug, "To backend> %s\n", s);
if (fwrite(s, 1, len, f) != len)
return 1;
return 0;
}
/* --------------------------------------------------------------------- */
/* pqGetnchar:
get a string of exactly len length from stream f
*/
int
pqGetnchar(char *s, int len, FILE *f, FILE *debug)
{
int cnt;
if (f == NULL)
return 1;
cnt = fread(s, 1, len, f);
s[cnt] = '\0';
/* mjl: actually needs up to len+1 bytes, is this okay? XXX */
if (debug)
fprintf(debug, "From backend (%d)> %s\n", len, s);
return 0;
}
/* --------------------------------------------------------------------- */
/* pqGets:
get a string of up to length len from stream f
*/
int
pqGets(char *s, int len, FILE *f, FILE *debug)
{
int c;
const char *str = s;
if (f == NULL)
return 1;
while (len-- && (c = getc(f)) != EOF && c)
*s++ = c;
*s = '\0';
/* mjl: actually needs up to len+1 bytes, is this okay? XXX */
if (debug)
fprintf(debug, "From backend> \"%s\"\n", str);
return 0;
}
/* --------------------------------------------------------------------- */
/* pgPutInt
send an integer of 2 or 4 bytes to the file stream, compensate
for host endianness.
returns 0 if successful, 1 otherwise
*/
int
pqPutInt(const int integer, int bytes, FILE *f, FILE *debug)
{
int retval = 0;
switch (bytes)
{
case 2:
retval = pqPutShort(integer, f);
break;
case 4:
retval = pqPutLong(integer, f);
break;
default:
fprintf(stderr, "** int size %d not supported\n", bytes);
retval = 1;
}
if (debug)
fprintf(debug, "To backend (%d#)> %d\n", bytes, integer);
return retval;
}
/* --------------------------------------------------------------------- */
/* pgGetInt
read a 2 or 4 byte integer from the stream and swab it around
to compensate for different endianness
returns 0 if successful
*/
int
pqGetInt(int *result, int bytes, FILE *f, FILE *debug)
{
int retval = 0;
switch (bytes)
{
case 2:
retval = pqGetShort(result, f);
break;
case 4:
retval = pqGetLong(result, f);
break;
default:
fprintf(stderr, "** int size %d not supported\n", bytes);
retval = 1;
}
if (debug)
fprintf(debug, "From backend (#%d)> %d\n", bytes, *result);
return retval;
}
/* --------------------------------------------------------------------- */
int
pqPuts(const char *s, FILE *f, FILE *debug)
{
if (f == NULL)
return 1;
if (fputs(s, f) == EOF)
return 1;
fputc('\0', f); /* important to send an ending \0 since
* backend expects it */
fflush(f);
if (debug)
{
fprintf(debug, "To backend> %s\n", s);
}
return 0;
}
/* --------------------------------------------------------------------- */
void
pqFlush(FILE *f, FILE *debug)
{
if (f)
fflush(f);
if (debug)
fflush(debug);
}
/* --------------------------------------------------------------------- */
|