[pgsql-jp: 26809] C言語関数について

kaneshige kaneshige @ unified.co.jp
2002年 7月 24日 (水) 13:05:30 JST


This is a multi-part message in MIME format.

------=_NextPart_000_005C_01C23312.C978CBC0
Content-Type: text/plain;
	charset="iso-2022-jp"
Content-Transfer-Encoding: 7bit

はじめまして。
本当に初歩的な質問で申し訳がありません。

今、C言語関数のテストプログラムを作ろうとしています。
C言語関数のコンパイル、実行は出来ます。
下記のソースを実行すると、12が戻ってきて正常なのですが、
system関数が実行されていません。
あと、ソース内のファイルの書き込みを有効にするとfopenで
失敗して-1が戻ってきます。

C言語の標準関数であるsqrt()は使えるのですが
fopen(), system()は使えません。
postgresqlのC言語関数では、すべてのC言語標準関数
が使えるのではないのですか?

ご教授ください。
よろしくお願いします。


<ソース(ファイル名:st_test.c>
#include"postgres.h"
#include"fmgr.h"
#include<stdio.h>
#include<stdlib.h>
#include<math.h>

PG_FUNCTION_INFO_V1(st_test);
extern Datum st_test(PG_FUNCTION_ARGS);
Datum st_test(PG_FUNCTION_ARGS)
{
        FILE    *fno;
        int i;

        i = sqrt(16.0);
        /* ファイルオープン */
        /*コメント化します*/
//       if ((fno = fopen("/home/shige/testsrc/st_test.txt", "w")) == NULL)
{
//                printf("error");
//                return(-1);
//        }
//        fprintf(fno, "aaaaa\n");
//        fclose(fno);
      system("/home/shige/testsrc/test_select &");
        return i*PG_GETARG_INT32(0);
}

<コンパイル方法>
$gcc -fpic -I/usr/local/src/postgresql-7.2.1/src/include -c -o st_test.o
st_test.c
$gcc -shared -o st_test.so st_test.o

<実行方法>(DB名:shige)
shige=>CREATE FUNCTION st_test(int4) RETURNS INTEGER AS
shige=>'/home/shige/testsrc/st_test.so'
shige=>LANGUAGE 'c'
shige=>WITH (iscachable);

shige=>select st_test(3);

------=_NextPart_000_005C_01C23312.C978CBC0
Content-Type: text/html;
	charset="iso-2022-jp"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-2022-jp">
<META content=3D"MSHTML 5.50.4916.2300" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT size=3D2><FONT=20
size=3D3>=1B$B$O$8$a$^$7$F!#=1B(B<BR>=1B$BK\Ev$K=3DiJbE*$J<ALd$G?=3D$7Lu$=
,$"$j$^$;$s!#=1B(B<BR><BR>=1B$B:#!"=1B(BC=1B$B8 @ 8l4X?t$N%F%9%H%W%m%0%i%`$=
r:n$m$&$H$7$F$$$^$9!#=1B(B<BR>C=1B$B8 @ 8l4X?t$N%3%s%Q%$%k!"<B9T$O=3DPMh$^$=
9!#=1B(B<BR>=1B$B2<5-$N%=3D!<%9$r<B9T$9$k$H!"=1B(B12=1B$B$,La$C$F$-$F @ 5>o=
$J$N$G$9$,!"=1B(B<BR>system=1B$B4X?t$,<B9T$5$l$F$$$^$;$s!#=1B(B<BR>=1B$B$=
"$H!"%=3D!<%9Fb$N%U%!%$%k$N=3Dq$-9~$_$rM-8z$K$9$k$H=1B(Bfopen=1B$B$G=1B(B=
<BR>=1B$B<:GT$7$F=1B(B-1=1B$B$,La$C$F$-$^$9!#=1B(B<BR><BR>C=1B$B8 @ 8l$NI8=3D=
`4X?t$G$"$k=1B(Bsqrt()=1B$B$O;H$($k$N$G$9$,=1B(B<BR>fopen(),=20
system()=1B$B$O;H$($^$;$s!#=1B(B<BR>postgresql=1B$B$N=1B(BC=1B$B8 @ 8l4X?t$=
G$O!"$9$Y$F$N=1B(BC=1B$B8 @ 8lI8=3D`4X?t=1B(B<BR>=1B$B$,;H$($k$N$G$O$J$$$N$=
G$9$+!)=1B(B<BR><BR>=1B$B$465<x$/$@$5$$!#=1B(B<BR>=1B$B$h$m$7$/$*4j$$$7$^=
$9!#=1B(B<BR><BR><BR>=1B$B!c%=3D!<%9=1B(B(=1B$B%U%!%$%kL>!'=1B(Bst_test.c=
=1B$B!d=1B(B<BR>#include"postgres.h"<BR>#include"fmgr.h"<BR>#include&lt;s=
tdio.h&gt;<BR>#include&lt;stdlib.h&gt;<BR>#include&lt;math.h&gt;<BR><BR>P=
G_FUNCTION_INFO_V1(st_test);<BR>extern=20
Datum st_test(PG_FUNCTION_ARGS);<BR>Datum=20
st_test(PG_FUNCTION_ARGS)<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;=20
FILE&nbsp;&nbsp;&nbsp; =
*fno;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int=20
i;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; i =3D=20
sqrt(16.0);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* =
=1B$B%U%!%$%k%*!<%W%s=1B(B=20
*/<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
/*=1B$B%3%a%s%H2=3D$7$^$9=1B(B*/<BR>//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
; if ((fno =3D=20
fopen("/home/shige/testsrc/st_test.txt", "w")) =3D=3D=20
NULL)<BR>{<BR>//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
printf("error");<BR>//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
return(-1);<BR>//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
}<BR>//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fprintf(fno,=20
"aaaaa\n");<BR>//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
fclose(fno);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
system("/home/shige/testsrc/test_select=20
&amp;");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return=20
i*PG_GETARG_INT32(0);<BR>}<BR><BR>=1B$B!c%3%s%Q%$%kJ}K!!d=1B(B<BR>$gcc =
-fpic=20
-I/usr/local/src/postgresql-7.2.1/src/include -c -o=20
st_test.o<BR>st_test.c<BR>$gcc -shared -o st_test.so=20
st_test.o<BR><BR>=1B$B!c<B9TJ}K!!d=1B(B(DB=1B$BL>!'=1B(Bshige)<BR>shige=3D=
&gt;CREATE FUNCTION st_test(int4)=20
RETURNS INTEGER=20
AS<BR>shige=3D&gt;'/home/shige/testsrc/st_test.so'<BR>shige=3D&gt;LANGUAG=
E=20
'c'<BR>shige=3D&gt;WITH (iscachable);<BR><BR>shige=3D&gt;select=20
st_test(3);</FONT><BR></FONT></DIV></BODY></HTML>

------=_NextPart_000_005C_01C23312.C978CBC0--





pgsql-jp メーリングリストの案内