|
11 | 11 | *
|
12 | 12 | *
|
13 | 13 | * IDENTIFICATION
|
14 |
| - * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-misc.c,v 1.4 1996/12/31 07:29:17 bryanh Exp $ |
| 14 | + * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-misc.c,v 1.5 1997/03/16 18:51:29 scrappy Exp $ |
15 | 15 | *
|
16 | 16 | *-------------------------------------------------------------------------
|
17 | 17 | */
|
|
23 | 23 |
|
24 | 24 | #include"libpq-fe.h"
|
25 | 25 |
|
| 26 | +/* --------------------------------------------------------------------- */ |
26 | 27 | /* pqGetc:
|
27 | 28 | get a character from stream f
|
28 | 29 |
|
29 | 30 | if debug is set, also echo the character fetched
|
30 | 31 | */
|
31 |
| -int |
32 |
| -pqGetc(FILE*fin,FILE*debug) |
| 32 | +intpqGetc(FILE*fin,FILE*debug) |
33 | 33 | {
|
34 | 34 | intc;
|
35 | 35 |
|
36 | 36 | c=getc(fin);
|
| 37 | + |
37 | 38 | if (debug&&c!=EOF)
|
38 | 39 | fprintf(debug,"From backend> %c\n",c);
|
| 40 | + |
39 | 41 | returnc;
|
40 | 42 | }
|
41 | 43 |
|
| 44 | +/* --------------------------------------------------------------------- */ |
42 | 45 | /* pqPutnchar:
|
43 | 46 | send a string of exactly len length into stream f
|
44 | 47 |
|
45 | 48 | returns 1 if there was an error, 0 otherwise.
|
46 | 49 | */
|
47 |
| -int |
48 |
| -pqPutnchar(constchar*s,intlen,FILE*f,FILE*debug) |
| 50 | +intpqPutnchar(constchar*s,intlen,FILE*f,FILE*debug) |
49 | 51 | {
|
50 |
| -intstatus; |
51 |
| - |
52 | 52 | if (f==NULL)
|
53 | 53 | return1;
|
54 | 54 |
|
55 |
| -if (debug)fputs("To backend>",debug); |
56 |
| -while (len--) { |
57 |
| -status=fputc(*s,f); |
58 |
| -if (debug) |
59 |
| -fputc(*s,debug); |
60 |
| -s++; |
61 |
| -if (status==EOF) |
| 55 | +if(debug) |
| 56 | +fprintf(debug,"To backend> %s\n",s); |
| 57 | + |
| 58 | +if(fwrite(s,1,len,f)!=len) |
62 | 59 | return1;
|
63 |
| - } |
64 |
| -if (debug)fputc('\n',debug); |
| 60 | + |
65 | 61 | return0;
|
66 | 62 | }
|
67 | 63 |
|
| 64 | +/* --------------------------------------------------------------------- */ |
68 | 65 | /* pqGetnchar:
|
69 | 66 | get a string of exactly len length from stream f
|
70 | 67 | */
|
71 |
| -int |
72 |
| -pqGetnchar(char*s,intlen,FILE*f,FILE*debug) |
| 68 | +intpqGetnchar(char*s,intlen,FILE*f,FILE*debug) |
73 | 69 | {
|
74 |
| -intc; |
| 70 | +intcnt; |
75 | 71 |
|
76 | 72 | if (f==NULL)
|
77 | 73 | return1;
|
78 | 74 |
|
79 |
| -while (len--&& (c=getc(f))!=EOF) |
80 |
| -*s++=c; |
81 |
| -*s='\0'; |
| 75 | +cnt=fread(s,1,len,f); |
| 76 | +s[cnt]='\0'; |
| 77 | +/* mjl: actually needs up to len+1 bytes, is this okay? XXX */ |
| 78 | + |
| 79 | +if (debug) |
| 80 | +fprintf(debug,"From backend (%d)> %s\n",len,s); |
82 | 81 |
|
83 |
| -if (debug) { |
84 |
| -fprintf(debug,"From backend> %s\n",s); |
85 |
| - } |
86 | 82 | return0;
|
87 | 83 | }
|
88 | 84 |
|
| 85 | +/* --------------------------------------------------------------------- */ |
89 | 86 | /* pqGets:
|
90 | 87 | get a string of up to length len from stream f
|
91 | 88 | */
|
92 |
| -int |
93 |
| -pqGets(char*s,intlen,FILE*f,FILE*debug) |
| 89 | +intpqGets(char*s,intlen,FILE*f,FILE*debug) |
94 | 90 | {
|
95 | 91 | intc;
|
| 92 | +constchar*str=s; |
96 | 93 |
|
97 | 94 | if (f==NULL)
|
98 | 95 | return1;
|
99 | 96 |
|
100 | 97 | while (len--&& (c=getc(f))!=EOF&&c)
|
101 | 98 | *s++=c;
|
102 | 99 | *s='\0';
|
| 100 | +/* mjl: actually needs up to len+1 bytes, is this okay? XXX */ |
| 101 | + |
| 102 | +if (debug) |
| 103 | +fprintf(debug,"From backend> \"%s\"\n",str); |
103 | 104 |
|
104 |
| -if (debug) { |
105 |
| -fprintf(debug,"From backend> %s\n",s); |
106 |
| - } |
107 | 105 | return0;
|
108 | 106 | }
|
109 | 107 |
|
110 |
| - |
| 108 | +/* --------------------------------------------------------------------- */ |
111 | 109 | /* pgPutInt
|
112 |
| - send an integer of up to 4 bytesto the file stream |
113 |
| - do this one byte at at time. |
114 |
| - This insures that machines with different ENDIANness can talk to each other |
115 |
| - get a n-byte integer from the stream into result |
| 110 | + send an integer of 2 or 4 bytes to the file stream, compensate |
| 111 | + for host endianness. |
116 | 112 | returns 0 if successful, 1 otherwise
|
117 | 113 | */
|
118 |
| -int |
119 |
| -pqPutInt(constintinteger,intbytes,FILE*f,FILE*debug) |
| 114 | +intpqPutInt(constintinteger,intbytes,FILE*f,FILE*debug) |
120 | 115 | {
|
121 |
| -inti; |
122 |
| -intstatus; |
123 |
| - |
124 |
| -i=integer; |
125 |
| - |
126 |
| -if (bytes>4) |
127 |
| -bytes=4; |
128 |
| - |
129 |
| -while (bytes--) { |
130 |
| -status=fputc(i&0xff,f); |
131 |
| -i >>=8; |
132 |
| -if (status==EOF) { |
133 |
| -return1; |
134 |
| -} |
135 |
| - } |
136 |
| -if (debug)fprintf(debug,"To backend (#)> %d\n",integer); |
137 |
| -return0; |
| 116 | +intretval=0; |
| 117 | + |
| 118 | +switch(bytes) |
| 119 | + { |
| 120 | +case2: |
| 121 | +retval=pqPutShort(integer,f); |
| 122 | +break; |
| 123 | +case4: |
| 124 | +retval=pqPutLong(integer,f); |
| 125 | +break; |
| 126 | +default: |
| 127 | +fprintf(stderr,"** int size %d not supported\n",bytes); |
| 128 | +retval=1; |
| 129 | + } |
| 130 | + |
| 131 | +if (debug)fprintf(debug,"To backend (%d#)> %d\n",bytes,integer); |
| 132 | + |
| 133 | +returnretval; |
138 | 134 | }
|
139 | 135 |
|
| 136 | +/* --------------------------------------------------------------------- */ |
140 | 137 | /* pgGetInt
|
141 |
| - reconstructs the integer one byte at a time. |
142 |
| - This insures that machines with different ENDIANness can talk to each other |
143 |
| - get a n-byte integer from the stream into result |
| 138 | + read a 2 or 4 byte integer from the stream and swab it around |
| 139 | + to compensate for different endianness |
144 | 140 | returns 0 if successful
|
145 | 141 | */
|
146 |
| -int |
147 |
| -pqGetInt(int*result,intbytes,FILE*f,FILE*debug) |
| 142 | +intpqGetInt(int*result,intbytes,FILE*f,FILE*debug) |
148 | 143 | {
|
149 |
| -intc; |
150 |
| -intp; |
151 |
| -intn; |
152 |
| - |
153 |
| -if (f==NULL) |
154 |
| -return1; |
| 144 | +intretval=0; |
155 | 145 |
|
156 |
| -p=0; |
157 |
| -n=0; |
158 |
| -while (bytes&& (c=getc(f))!=EOF) |
| 146 | +switch(bytes) |
159 | 147 | {
|
160 |
| -n |= (c&0xff) <<p; |
161 |
| -p+=8; |
162 |
| -bytes--; |
| 148 | +case2: |
| 149 | +retval=pqGetShort(result,f); |
| 150 | +break; |
| 151 | +case4: |
| 152 | +retval=pqGetLong(result,f); |
| 153 | +break; |
| 154 | +default: |
| 155 | +fprintf(stderr,"** int size %d not supported\n",bytes); |
| 156 | +retval=1; |
163 | 157 | }
|
164 | 158 |
|
165 |
| -if (bytes!=0) |
166 |
| -return1; |
167 |
| - |
168 |
| -*result=n; |
169 | 159 | if (debug)
|
170 |
| -fprintf(debug,"From backend (#)> %d\n",*result); |
171 |
| -return0; |
172 |
| -} |
| 160 | +fprintf(debug,"From backend (#%d)> %d\n",bytes,*result); |
173 | 161 |
|
| 162 | +returnretval; |
| 163 | +} |
174 | 164 |
|
175 |
| -int |
176 |
| -pqPuts(constchar*s,FILE*f,FILE*debug) |
| 165 | +/* --------------------------------------------------------------------- */ |
| 166 | +intpqPuts(constchar*s,FILE*f,FILE*debug) |
177 | 167 | {
|
178 | 168 | if (f==NULL)
|
179 | 169 | return1;
|
180 | 170 |
|
181 |
| -if (fputs(s,f)==EOF) |
| 171 | +if (fputs(s,f)==EOF) |
182 | 172 | return1;
|
183 | 173 |
|
184 |
| -fputc('\0',f);/* important to send an endingEOF since backend expects it */ |
| 174 | +fputc('\0',f);/* important to send an ending\0 since backend expects it */ |
185 | 175 | fflush(f);
|
186 | 176 |
|
187 | 177 | if (debug) {
|
188 | 178 | fprintf(debug,"To backend> %s\n",s);
|
189 | 179 | }
|
| 180 | + |
190 | 181 | return0;
|
191 | 182 | }
|
192 | 183 |
|
193 |
| - |
194 |
| -void |
195 |
| -pqFlush(FILE*f,FILE*debug) |
| 184 | +/* --------------------------------------------------------------------- */ |
| 185 | +voidpqFlush(FILE*f,FILE*debug) |
196 | 186 | {
|
197 | 187 | if (f)
|
198 | 188 | fflush(f);
|
| 189 | + |
199 | 190 | if (debug)
|
200 | 191 | fflush(debug);
|
201 | 192 | }
|
202 | 193 |
|
203 |
| - |
| 194 | +/* --------------------------------------------------------------------- */ |