Re: Problems with a C function, pg_uname(), and String concatenation.

From: Rafael Martinez <r(dot)m(dot)guerrero(at)usit(dot)uio(dot)no>
To: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
Cc: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: Problems with a C function, pg_uname(), and String concatenation.
Date: 2008-07-01 11:43:49
Message-ID: 486A1875.9050304@usit.uio.no
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Pavel Stehule wrote:
> 2008/7/1 Rafael Martinez <r(dot)m(dot)guerrero(at)usit(dot)uio(dot)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(dot)m(dot)guerrero(at)usit(dot)uio(dot)no>
Center for Information Technology Services
University of Oslo, Norway

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

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Dave Coventry 2008-07-01 12:05:29 SAST FATAL: could not access private key file "server.key"
Previous Message Glyn Astill 2008-07-01 11:38:18 Re: Problems with a C function, pg_uname(), and String concatenation.