Re: Problems with a C function, pg_uname(), and String concatenation. - Mailing list pgsql-general

From Rafael Martinez
Subject Re: Problems with a C function, pg_uname(), and String concatenation.
Date
Msg-id 486A1875.9050304@usit.uio.no
Whole thread Raw
In response to Re: Problems with a C function, pg_uname(), and String concatenation.  ("Pavel Stehule" <pavel.stehule@gmail.com>)
List pgsql-general
Pavel Stehule wrote:
> 2008/7/1 Rafael Martinez <r.m.guerrero@usit.uio.no>:
>> Pavel Stehule wrote:
>>> hello
>>> try
>>>
>>>
>>> memcpy(VARDATA(result),uname_pointer.release,strlen(uname_pointer.release));
>>> SET_VARSIZE(result, strlen(uname_pointer.release) + VARHDRSZ);
>>>
>> [.........]
>>
>> This a 8.2.x system. SET_VARSIZE is not available.
>>
>
> VARATT_SIZEP(result) = strlen(uname_pointer.release) + VARHDRSZ;
>

Hello and thank you for your help.

Everything works perfect now with the new version of the function. Here
it is just in case somebody wants to use it:

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


#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif


PG_FUNCTION_INFO_V1(pg_uname);

Datum
pg_uname(PG_FUNCTION_ARGS)
{
     text *argument = PG_GETARG_TEXT_P(0);
     size_t argumentlen = VARSIZE(argument)-VARHDRSZ;

     text *result = (text *) palloc(256);
     char *option = (char *) palloc(argumentlen+1);

     char sysname[] = "sysname";
     char nodename[] = "nodename";
     char release[] = "release";
     char version[] = "version";
     char machine[] = "machine";
     char null[] = "null";

     struct utsname uname_pointer;
     uname(&uname_pointer);

     memcpy(option,VARDATA(argument),argumentlen);
     option[argumentlen] = '\0';

     if (strcmp(option,sysname) == 0){
       VARATT_SIZEP(result) = strlen(uname_pointer.sysname) + VARHDRSZ;

memcpy(VARDATA(result),uname_pointer.sysname,strlen(uname_pointer.sysname));
     }
     else if (strcmp(option,nodename) == 0){
       VARATT_SIZEP(result) = strlen(uname_pointer.nodename) + VARHDRSZ;

memcpy(VARDATA(result),uname_pointer.nodename,strlen(uname_pointer.nodename));
     }
     else if (strcmp(option,release) == 0){
       VARATT_SIZEP(result) = strlen(uname_pointer.release) + VARHDRSZ;

memcpy(VARDATA(result),uname_pointer.release,strlen(uname_pointer.release));
     }
     else if (strcmp(option,version) == 0){
       VARATT_SIZEP(result) = strlen(uname_pointer.version) + VARHDRSZ;

memcpy(VARDATA(result),uname_pointer.version,strlen(uname_pointer.version));
     }
     else if (strcmp(option,machine) == 0){
       VARATT_SIZEP(result) = strlen(uname_pointer.machine) + VARHDRSZ;

memcpy(VARDATA(result),uname_pointer.machine,strlen(uname_pointer.machine));
     }
     else{
       memcpy(VARDATA(result),null,sizeof(null));
     }

     pfree(option);
     PG_RETURN_TEXT_P(result);
}

-------------------------------------------------------


--
  Rafael Martinez, <r.m.guerrero@usit.uio.no>
  Center for Information Technology Services
  University of Oslo, Norway

  PGP Public Key: http://folk.uio.no/rafael/

pgsql-general by date:

Previous
From: Glyn Astill
Date:
Subject: Re: Problems with a C function, pg_uname(), and String concatenation.
Next
From: "Dave Coventry"
Date:
Subject: SAST FATAL: could not access private key file "server.key"