Search Postgresql Archives

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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Pavel Stehule wrote:
2008/7/1 Rafael Martinez <r.m.guerrero@xxxxxxxxxxx>:
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@xxxxxxxxxxx>
 Center for Information Technology Services
 University of Oslo, Norway

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


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux