1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* FILE
|
|
|
|
* fe-misc.c
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* DESCRIPTION
|
|
|
|
* miscellaneous useful functions
|
|
|
|
* these routines are analogous to the ones in libpq/pqcomm.c
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
1997-09-08 23:56:23 +02:00
|
|
|
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-misc.c,v 1.8 1997/09/08 21:55:44 momjian Exp $
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
1996-11-03 08:14:32 +01:00
|
|
|
|
|
|
|
#include "postgres.h"
|
|
|
|
|
1996-08-06 18:16:50 +02:00
|
|
|
#include "libpq-fe.h"
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-03-16 19:51:29 +01:00
|
|
|
/* --------------------------------------------------------------------- */
|
1996-07-09 08:22:35 +02:00
|
|
|
/* pqGetc:
|
|
|
|
get a character from stream f
|
|
|
|
|
|
|
|
if debug is set, also echo the character fetched
|
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
int
|
1997-09-08 23:56:23 +02:00
|
|
|
pqGetc(FILE *fin, FILE *debug)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
int c;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
c = getc(fin);
|
1997-03-16 19:51:29 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
if (debug && c != EOF)
|
|
|
|
fprintf(debug, "From backend> %c\n", c);
|
|
|
|
|
|
|
|
return c;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1997-03-16 19:51:29 +01:00
|
|
|
/* --------------------------------------------------------------------- */
|
1996-07-09 08:22:35 +02:00
|
|
|
/* pqPutnchar:
|
1997-09-07 07:04:48 +02:00
|
|
|
send a string of exactly len length into stream f
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
returns 1 if there was an error, 0 otherwise.
|
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
int
|
1997-09-08 23:56:23 +02:00
|
|
|
pqPutnchar(const char *s, int len, FILE *f, FILE *debug)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
if (f == NULL)
|
|
|
|
return 1;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
if (debug)
|
|
|
|
fprintf(debug, "To backend> %s\n", s);
|
1997-03-16 19:51:29 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
if (fwrite(s, 1, len, f) != len)
|
|
|
|
return 1;
|
1997-03-16 19:51:29 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
return 0;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1997-03-16 19:51:29 +01:00
|
|
|
/* --------------------------------------------------------------------- */
|
1996-07-09 08:22:35 +02:00
|
|
|
/* pqGetnchar:
|
1997-09-07 07:04:48 +02:00
|
|
|
get a string of exactly len length from stream f
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
int
|
1997-09-08 23:56:23 +02:00
|
|
|
pqGetnchar(char *s, int len, FILE *f, FILE *debug)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
int cnt;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
if (f == NULL)
|
|
|
|
return 1;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
cnt = fread(s, 1, len, f);
|
|
|
|
s[cnt] = '\0';
|
|
|
|
/* mjl: actually needs up to len+1 bytes, is this okay? XXX */
|
1997-03-16 19:51:29 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
if (debug)
|
|
|
|
fprintf(debug, "From backend (%d)> %s\n", len, s);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
return 0;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1997-03-16 19:51:29 +01:00
|
|
|
/* --------------------------------------------------------------------- */
|
1996-07-09 08:22:35 +02:00
|
|
|
/* pqGets:
|
|
|
|
get a string of up to length len from stream f
|
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
int
|
1997-09-08 23:56:23 +02:00
|
|
|
pqGets(char *s, int len, FILE *f, FILE *debug)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
int c;
|
|
|
|
const char *str = s;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
if (f == NULL)
|
|
|
|
return 1;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
while (len-- && (c = getc(f)) != EOF && c)
|
|
|
|
*s++ = c;
|
|
|
|
*s = '\0';
|
|
|
|
/* mjl: actually needs up to len+1 bytes, is this okay? XXX */
|
1997-03-16 19:51:29 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
if (debug)
|
|
|
|
fprintf(debug, "From backend> \"%s\"\n", str);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
return 0;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1997-03-16 19:51:29 +01:00
|
|
|
/* --------------------------------------------------------------------- */
|
1996-07-09 08:22:35 +02:00
|
|
|
/* pgPutInt
|
1997-03-16 19:51:29 +01:00
|
|
|
send an integer of 2 or 4 bytes to the file stream, compensate
|
|
|
|
for host endianness.
|
1996-07-09 08:22:35 +02:00
|
|
|
returns 0 if successful, 1 otherwise
|
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
int
|
1997-09-08 23:56:23 +02:00
|
|
|
pqPutInt(const int integer, int bytes, FILE *f, FILE *debug)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
int retval = 0;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
switch (bytes)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
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;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (debug)
|
|
|
|
fprintf(debug, "To backend (%d#)> %d\n", bytes, integer);
|
|
|
|
|
|
|
|
return retval;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1997-03-16 19:51:29 +01:00
|
|
|
/* --------------------------------------------------------------------- */
|
1997-09-07 07:04:48 +02:00
|
|
|
/* pgGetInt
|
1997-03-16 19:51:29 +01:00
|
|
|
read a 2 or 4 byte integer from the stream and swab it around
|
|
|
|
to compensate for different endianness
|
1997-09-07 07:04:48 +02:00
|
|
|
returns 0 if successful
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
int
|
1997-09-08 23:56:23 +02:00
|
|
|
pqGetInt(int *result, int bytes, FILE *f, FILE *debug)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
int retval = 0;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
switch (bytes)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
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;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (debug)
|
|
|
|
fprintf(debug, "From backend (#%d)> %d\n", bytes, *result);
|
|
|
|
|
|
|
|
return retval;
|
1997-03-16 19:51:29 +01:00
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-03-16 19:51:29 +01:00
|
|
|
/* --------------------------------------------------------------------- */
|
1997-09-07 07:04:48 +02:00
|
|
|
int
|
1997-09-08 23:56:23 +02:00
|
|
|
pqPuts(const char *s, FILE *f, FILE *debug)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
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;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1997-03-16 19:51:29 +01:00
|
|
|
/* --------------------------------------------------------------------- */
|
1997-09-07 07:04:48 +02:00
|
|
|
void
|
1997-09-08 23:56:23 +02:00
|
|
|
pqFlush(FILE *f, FILE *debug)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
if (f)
|
|
|
|
fflush(f);
|
1997-03-16 19:51:29 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
if (debug)
|
|
|
|
fflush(debug);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1996-12-31 08:29:17 +01:00
|
|
|
|
1997-03-16 19:51:29 +01:00
|
|
|
/* --------------------------------------------------------------------- */
|