How can I return a NULL value from a function? - Mailing list pgsql-hackers

From Chongbing Liu
Subject How can I return a NULL value from a function?
Date
Msg-id Pine.LNX.4.58.0312182240130.15450@hongkong
Whole thread Raw
Responses Re: How can I return a NULL value from a function?
List pgsql-hackers
Hello, can you please tell me how to return a NULL value
from a function? I am running postgresql 7.3.2. and the
following is my case. Thank you very much.

Chongbing

============ header file ===============

#include "postgres.h"
#include "fmgr.h"
#include <string.h>
#include <sys/types.h>

#ifndef pname_h
#define pname_h 1



struct pname
{ int  size; char name[1];
};

typedef struct pname pname;

pname * pname_in( char * in);

char * pname_out( pname * nm);

#endif

============ C code =============
#include "postgres.h"
#include "fmgr.h"
#include "pname.h"
#include <ctype.h>
#include <string.h>

#ifndef pname_c
#define pname_c 1

pname * pname_in(char * in) { int i; pname * n; n = (pname *) palloc(strlen(in)+VARHDRSZ); n->size=strlen(in)+VARHDRSZ;
memcpy(n->name,in, strlen(in)); if(strlen(in)<4)   return (NULL); return n;
 
}


char * pname_out(pname* nm) {  char *t;  if(nm==NULL)    return (NULL);  t = (char *) palloc(VARSIZE(nm)-VARHDRSZ+1);
strcpy(t,nm->name); return t;
 
}

#endif


============== psql command for function installation =============
CREATE FUNCTION pname_in (opaque)   RETURNS pname   AS '/home/cliu/types_lib/pname.so', 'pname_in'   LANGUAGE 'c';

CREATE FUNCTION pname_out(opaque)   RETURNS opaque   AS '/home/cliu/types_lib/pname.so','pname_out'   LANGUAGE 'c';


CREATE TYPE pname (   input = pname_in,   output = pname_out,   internallength = VARIABLE,   externallength = VARIABLE,
 default = " "
 
);

================== test cases ===============

cliu=# select pname('abcdefghijk');
NOTICE:  ok2:abcdefghijk
   pname
-------------abcdefghijk
(1 row)

cliu=# select pname('ab');
NOTICE:  ok2:ab

server closed the connection unexpectedly       This probably means the server terminated abnormally       before or
whileprocessing the request.
 
The connection to the server was lost. Attempting reset: Failed.
!#



pgsql-hackers by date:

Previous
From: Tom Lane
Date:
Subject: Re: replace all with * in pg_hba.conf
Next
From: Karel Zak
Date:
Subject: Re: Dates BC.