some fixes regarding caveats part of article at http://www.postgresql.org/docs/6.3/static/c3102.htm

This commit is contained in:
Miroslav Stampar 2010-02-14 19:36:02 +00:00
parent 8131f9c77c
commit 1d55923c9d

View File

@ -58,7 +58,7 @@ extern DLLIMPORT Datum sys_exec(PG_FUNCTION_ARGS) {
char *command; char *command;
argv0_size = VARSIZE(argv0) - VARHDRSZ; argv0_size = VARSIZE(argv0) - VARHDRSZ;
command = (char *)malloc(argv0_size + 1); command = (char *)palloc(argv0_size + 1);
memcpy(command, VARDATA(argv0), argv0_size); memcpy(command, VARDATA(argv0), argv0_size);
command[argv0_size] = '\0'; command[argv0_size] = '\0';
@ -69,7 +69,7 @@ extern DLLIMPORT Datum sys_exec(PG_FUNCTION_ARGS) {
*/ */
result = system(command); result = system(command);
free(command); pfree(command);
PG_FREE_IF_COPY(argv0, 0); PG_FREE_IF_COPY(argv0, 0);
PG_RETURN_INT32(result); PG_RETURN_INT32(result);
@ -91,7 +91,7 @@ extern DLLIMPORT Datum sys_eval(PG_FUNCTION_ARGS) {
int32 outlen, linelen; int32 outlen, linelen;
argv0_size = VARSIZE(argv0) - VARHDRSZ; argv0_size = VARSIZE(argv0) - VARHDRSZ;
command = (char *)malloc(argv0_size + 1); command = (char *)palloc(argv0_size + 1);
memcpy(command, VARDATA(argv0), argv0_size); memcpy(command, VARDATA(argv0), argv0_size);
command[argv0_size] = '\0'; command[argv0_size] = '\0';
@ -101,7 +101,7 @@ extern DLLIMPORT Datum sys_eval(PG_FUNCTION_ARGS) {
elog(NOTICE, "Command evaluated: %s", command); elog(NOTICE, "Command evaluated: %s", command);
*/ */
result = (char *)malloc(1); result = (char *)palloc(1);
outlen = 0; outlen = 0;
pipe = popen(command, "r"); pipe = popen(command, "r");
@ -119,7 +119,7 @@ extern DLLIMPORT Datum sys_eval(PG_FUNCTION_ARGS) {
result[outlen-1] = 0x00; result[outlen-1] = 0x00;
} }
result_text = (text *)malloc(VARHDRSZ + strlen(result)); result_text = (text *)palloc(VARHDRSZ + strlen(result));
#ifdef SET_VARSIZE #ifdef SET_VARSIZE
SET_VARSIZE(result_text, VARHDRSZ + strlen(result)); SET_VARSIZE(result_text, VARHDRSZ + strlen(result));
#else #else
@ -224,7 +224,7 @@ extern DLLIMPORT Datum sys_fileread(PG_FUNCTION_ARGS) {
FILE *file; FILE *file;
argv0_size = VARSIZE(argv0) - VARHDRSZ; argv0_size = VARSIZE(argv0) - VARHDRSZ;
filename = (char *)malloc(argv0_size + 1); filename = (char *)palloc(argv0_size + 1);
memcpy(filename, VARDATA(argv0), argv0_size); memcpy(filename, VARDATA(argv0), argv0_size);
filename[argv0_size] = '\0'; filename[argv0_size] = '\0';
@ -239,7 +239,7 @@ extern DLLIMPORT Datum sys_fileread(PG_FUNCTION_ARGS) {
len = ftell(file); len = ftell(file);
fseek(file, 0, SEEK_SET); fseek(file, 0, SEEK_SET);
buffer=(char *)malloc(len + 1); buffer=(char *)palloc(len + 1);
if (!buffer) if (!buffer)
{ {
fclose(file); fclose(file);
@ -249,7 +249,7 @@ extern DLLIMPORT Datum sys_fileread(PG_FUNCTION_ARGS) {
fread(buffer, len, 1, file); fread(buffer, len, 1, file);
fclose(file); fclose(file);
result = (char *)malloc(2*len + 1); result = (char *)palloc(2*len + 1);
for (i=0, j=0; i<len; i++) for (i=0, j=0; i<len; i++)
{ {
result[j++] = table[(buffer[i] >> 4) & 0x0f]; result[j++] = table[(buffer[i] >> 4) & 0x0f];
@ -257,7 +257,7 @@ extern DLLIMPORT Datum sys_fileread(PG_FUNCTION_ARGS) {
} }
result[j] = '\0'; result[j] = '\0';
result_text = (text *)malloc(VARHDRSZ + strlen(result)); result_text = (text *)palloc(VARHDRSZ + strlen(result));
#ifdef SET_VARSIZE #ifdef SET_VARSIZE
SET_VARSIZE(result_text, VARHDRSZ + strlen(result)); SET_VARSIZE(result_text, VARHDRSZ + strlen(result));
#else #else
@ -265,9 +265,9 @@ extern DLLIMPORT Datum sys_fileread(PG_FUNCTION_ARGS) {
#endif #endif
memcpy(VARDATA(result_text), result, strlen(result)); memcpy(VARDATA(result_text), result, strlen(result));
free(result); pfree(result);
free(buffer); pfree(buffer);
free(filename); pfree(filename);
PG_RETURN_POINTER(result_text); PG_RETURN_POINTER(result_text);
} }