pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Re: Registring a C function in PostgreSQL II
3+ messages / 2 participants
[nested] [flat]

* Re: Registring a C function in PostgreSQL II
@ 1970-01-01 00:00 Haller Christoph <ch@rodos.fzk.de>
  0 siblings, 0 replies; 3+ messages in thread

From: Haller Christoph @ 1970-01-01 00:00 UTC (permalink / raw)
  To: iafmgc@unileon.es; +Cc: pgsql-sql

I'm working on a HP-UX system, so some of the 
following has to be adapted, but in principle 
it's the same on every system and it works. 
First piece of code is a standalone program, 
which you should always write and test before 
you start creating C functions inside PostgreSQL. 
Second piece is your sendemail function slightly 
modified to make it run on my system. 
When I do a 
select sendemail('ch', 'was soll das?') ;
I'm receiving an email from postgres. 
Regards, Christoph 

First piece:

/*
cc -Aa -g -I/opt/pgsql/include/ -c sendemtest.c
cc sendemail.o sendemtest.o -o sendemtest
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "postgres.h"

void main() {
   char buf[1024];
   int ln;
   text *res;
   text *to;
   int sendemail(text *email,text *message);
   
   strcpy(buf, "Kissenminister Aussinger \n");
   ln = strlen(buf);
   
   res = (text *) malloc(VARHDRSZ + ln);
   memset(res, 0, VARHDRSZ + ln);
   res->vl_len = VARHDRSZ + ln;
   memcpy(res->vl_dat, buf, (int) ln);
   
   strcpy(buf, "ch");
   ln = strlen(buf);
   
   to = (text *) malloc(VARHDRSZ + ln);
   memset(to, 0, VARHDRSZ + ln);
   to->vl_len = VARHDRSZ + ln;
   memcpy(to->vl_dat, buf, (int) ln);
   
   sendemail(to, res);
}

Second piece: 

/*
cc -Aa -g -I/opt/pgsql/include/ +z -c sendemail.c
ld -b -o sendemail.sl sendemail.o

CREATE FUNCTION sendemail(text,text) RETURNS int4
 AS '/fdsk2/users/ch/tools/pgsql.mydoc/sendemail.sl' LANGUAGE 'c';
DROP FUNCTION sendemail(text,text);
*/
 #include <stdio.h>
 #include <stdlib.h>
 #include "postgres.h"

 int sendemail(text *email,text *message)
 {
     int result = 0 ; 

     char string_tosend [300];

     sprintf(string_tosend,"/usr/bin/echo \"%s\" >/tmp/mailtmp.txt\n",VARDATA(message));

     result += system(string_tosend);

     sprintf(string_tosend,"/usr/bin/mail -dt %s </tmp/mailtmp.txt \n",
	     VARDATA(email));

     result += system(string_tosend);

     result += system("/usr/bin/rm /tmp/mailtmp.txt");

     return result;


 }




^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Registring a C function in PostgreSQL II
@ 2001-09-19 18:23 Miguel González <iafmgc@unileon.es>
  1970-01-01 00:00 ` Re: Registring a C function in PostgreSQL II Haller Christoph <ch@rodos.fzk.de>
  0 siblings, 1 reply; 3+ messages in thread

From: Miguel González @ 2001-09-19 18:23 UTC (permalink / raw)
  To: ; +Cc: pgsql-sql

 Thank you very much for your help.

  After being testing the program first from my prompt (it worked fine) and
 then within PostgreSQL, I realized that I was using aliases for the system
 users emails (for instance I used postgres instead of
 postgres@myhost.mydomain.com

 once that i tried the "complete" email address it worked fine. Is it
 possible to use aliases from within PostgreSQL (outside works fine, which
is
 something weird that I dont understand).

  Many thanks

  Miguel


> ----- Original Message -----
> From: "Haller Christoph" <ch@rodos.fzk.de>
> To: "Miguel González" <iafmgc@unileon.es>
> Cc: <pgsql-sql@postgresql.org>
> Sent: Wednesday, September 19, 2001 4:41 PM
> Subject: Re: [SQL] Registring a C function in PostgreSQL II
>
>
> > I'm working on a HP-UX system, so some of the
> > following has to be adapted, but in principle
> > it's the same on every system and it works.
> > First piece of code is a standalone program,
> > which you should always write and test before
> > you start creating C functions inside PostgreSQL.
> > Second piece is your sendemail function slightly
> > modified to make it run on my system.
> > When I do a
> > select sendemail('ch', 'was soll das?') ;
> > I'm receiving an email from postgres.
> > Regards, Christoph
> >
> > First piece:
> >
> > /*
> > cc -Aa -g -I/opt/pgsql/include/ -c sendemtest.c
> > cc sendemail.o sendemtest.o -o sendemtest
> > */
> > #include <stdio.h>
> > #include <stdlib.h>
> > #include <string.h>
> > #include "postgres.h"
> >
> > void main() {
> >    char buf[1024];
> >    int ln;
> >    text *res;
> >    text *to;
> >    int sendemail(text *email,text *message);
> >
> >    strcpy(buf, "Kissenminister Aussinger \n");
> >    ln = strlen(buf);
> >
> >    res = (text *) malloc(VARHDRSZ + ln);
> >    memset(res, 0, VARHDRSZ + ln);
> >    res->vl_len = VARHDRSZ + ln;
> >    memcpy(res->vl_dat, buf, (int) ln);
> >
> >    strcpy(buf, "ch");
> >    ln = strlen(buf);
> >
> >    to = (text *) malloc(VARHDRSZ + ln);
> >    memset(to, 0, VARHDRSZ + ln);
> >    to->vl_len = VARHDRSZ + ln;
> >    memcpy(to->vl_dat, buf, (int) ln);
> >
> >    sendemail(to, res);
> > }
> >
> > Second piece:
> >
> > /*
> > cc -Aa -g -I/opt/pgsql/include/ +z -c sendemail.c
> > ld -b -o sendemail.sl sendemail.o
> >
> > CREATE FUNCTION sendemail(text,text) RETURNS int4
> >  AS '/fdsk2/users/ch/tools/pgsql.mydoc/sendemail.sl' LANGUAGE 'c';
> > DROP FUNCTION sendemail(text,text);
> > */
> >  #include <stdio.h>
> >  #include <stdlib.h>
> >  #include "postgres.h"
> >
> >  int sendemail(text *email,text *message)
> >  {
> >      int result = 0 ;
> >
> >      char string_tosend [300];
> >
> >      sprintf(string_tosend,"/usr/bin/echo \"%s\"
> >/tmp/mailtmp.txt\n",VARDATA(message));
> >
> >      result += system(string_tosend);
> >
> >      sprintf(string_tosend,"/usr/bin/mail -dt %s </tmp/mailtmp.txt \n",
> >      VARDATA(email));
> >
> >      result += system(string_tosend);
> >
> >      result += system("/usr/bin/rm /tmp/mailtmp.txt");
> >
> >      return result;
> >
> >
> >  }
> >
> >
> > ---------------------------(end of broadcast)---------------------------
> > TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org
> >
>




^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Registring a C function in PostgreSQL II
  2001-09-19 18:23 Re: Registring a C function in PostgreSQL II Miguel González <iafmgc@unileon.es>
@ 1970-01-01 00:00 ` Haller Christoph <ch@rodos.fzk.de>
  0 siblings, 0 replies; 3+ messages in thread

From: Haller Christoph @ 1970-01-01 00:00 UTC (permalink / raw)
  To: iafmgc@unileon.es; +Cc: pgsql-sql

My understanding is, if you have system calls from within 
postgres, the child processes invoked are run in the 
postgres user's environment. 
So, login as user postgres and have a look what aliases 
are set. My idea is, because postgres is not a human user, 
these aliases are not set. I think, if you copy the 
aliases from your environment to the postgres environment, 
you should be able to use them. 
But, I do not think it's a good idea to do so, because 
using such cross dependencies from the postgres environment 
to databank internals is prone to hard-to-find errors 
and makes porting applications to quite a hazard. 
Regards, Christoph 
> 
>  Thank you very much for your help.
> 
>   After being testing the program first from my prompt (it worked fine) and
>  then within PostgreSQL, I realized that I was using aliases for the system
>  users emails (for instance I used postgres instead of
>  postgres@myhost.mydomain.com
> 
>  once that i tried the "complete" email address it worked fine. Is it
>  possible to use aliases from within PostgreSQL (outside works fine, which
> is
>  something weird that I dont understand).
> 
>   Many thanks
> 
>   Miguel
> 
> 



^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2001-09-19 18:23 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
1970-01-01 00:00 Re: Registring a C function in PostgreSQL II Haller Christoph <ch@rodos.fzk.de>
2001-09-19 18:23 Re: Registring a C function in PostgreSQL II Miguel González <iafmgc@unileon.es>
1970-01-01 00:00 ` Haller Christoph <ch@rodos.fzk.de>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox