Re: C function woes (more info) - Mailing list pgsql-general

From Marko Kreen
Subject Re: C function woes (more info)
Date
Msg-id 20010221010631.A32477@l-t.ee
Whole thread Raw
In response to C function woes (more info)  (Chris Hayner <hayner80@astro.ocis.temple.edu>)
List pgsql-general
On Tue, Feb 20, 2001 at 04:56:53PM -0500, Chris Hayner wrote:
> I am attempting to create a function which will return data from a
> C-function. This is what i have, and it so far has been successful. Any
> help anyone can give me would be greatly appreciated.

> -----------------------------------------------------------------------
> #include <string.h>
> #include "/usr/local/pgsql/include/postgres.h"
>
> text *
> hello()
>  {
>         char data[] = "hello world";
>         int32 new_text_size = VARHDRSZ + sizeof(data);
>         text *new_text = (text *) palloc(new_text_size);

    VARATT_SIZEP(new_text) = new_text_size;

>         strcpy(VARDATA(new_text), data);

You include the '\0' too, so you see in psql 'right' result coz it
uses libc for printing.  Actually the size is random.

>         return new_text;
>  }

For real 7.1 fmgr interface you should do:

PG_FUNCTION_INFO_V1(hello);
Datum hello(PG_FUNCTION_ARGS)
{
    [...]

    PG_RETURN_TEXT_P(new_text);
}



--
marko


pgsql-general by date:

Previous
From: Tom Lane
Date:
Subject: Re: C function woes
Next
From: Tom Lane
Date:
Subject: Re: C function woes