File: pg_wrapper.c

package info (click to toggle)
postgresql 6.5.3-27
  • links: PTS
  • area: main
  • in suites: potato
  • size: 28,360 kB
  • ctags: 20,905
  • sloc: ansic: 204,608; yacc: 10,718; sql: 9,387; sh: 9,379; java: 8,835; tcl: 7,709; makefile: 3,376; lex: 1,642; perl: 1,034; python: 959; cpp: 847; asm: 70; pascal: 42; awk: 10; sed: 8
file content (124 lines) | stat: -rw-r--r-- 3,426 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/* Debian wrapper program for PostgreSQL front-ends.

     Copyright (c) Oliver Elphick 1998
     Licensing: Anyone may use this code under the same terms as the
             Debian PostgreSQL database package code, of which it is
             a part and with which it is released.

     This program is used to set the envelope from
     /etc/postgresql/postmaster.init so that programs know where to find
     the PostgreSQL library and database.
*/

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>

#define    DO_NOT_OVERWRITE    0

int main (int argc, char **argv)
{
    char cmd[128] = "/usr/lib/postgresql/bin/\0";
    char *dbname = NULL, *p, **q;

    /* Write the default values into the environment */
    setenv("PGDATA", "/var/lib/postgres/data", DO_NOT_OVERWRITE);
    setenv("PGLIB", "/usr/lib/postgresql/lib", DO_NOT_OVERWRITE);
    setenv("PGPORT", "5432", DO_NOT_OVERWRITE);

    /* identify the command */
    if ((p = rindex(argv[0], '/')))
    {
        ++p;
    } else {
        p = argv[0];
    }
    strncat (cmd, p, 100);
    argv[0] = rindex(cmd, '/') + 1;

    /* This program should not be called as `pg_wrapper' */
    if (!strcmp(argv[0],"pg_wrapper"))
    {
	    fprintf(stderr,"pg_wrapper cannot be run as itself, but only through a link\nwhose name is that of the real program to be run.");
	    exit(1);
    }

    /* Special processing for  psql */
    if (!strcmp(argv[0],"psql"))
    {
        int ix = 0;
        /* Check the command line */
        char *z[128];
        char newopt[3] = "--";
        int c;

        while ((c = getopt(argc, argv, "a:Ac:d:Eef:F:h:Hlno:p:qsStT:ux")) != EOF)
        {
            switch (c)
            {
                case 'a':
                    fprintf (stderr, 
                             "psql: option -a is obsolete and no longer has any effect.\n");
                    break;
                case 'd':
                    dbname = strdup(argv[optind-1]);
                    break;
                case 'c':
                case 'f':
                case 'F':
                case 'h':
                case 'o':
                case 'p':
                case 'T':
                    /* add option and argument to command line */
                    newopt[1] = c;
                    z[++ix] = strdup(newopt);
                    z[++ix] = strdup(optarg);
                    break;

                case 'A':
                case 'E':
                case 'e':
                case 'H':
                case 'l':
                case 'n':
                case 'q':
                case 's':
                case 'S':
                case 't':
                case 'u':
                case 'x':
                    /* add option to command line */
                    newopt[1] = c;
                    z[++ix] = strdup(newopt);
                    break;

                default:
                     /* an error message has been printed by getopt() */
	             exit(1);
            }
        }

        if (!dbname && argv[optind] != NULL)
        	dbname = strdup(argv[optind]);

	if (!dbname)
		dbname = "template1";

	z[++ix] = "-d";
	z[++ix] = dbname;
	z[++ix] = NULL;
	q = z;
    } else {
        q = argv;
    }

    /* Run the real command */
    q[0] = cmd;
    execv(cmd, q);

    /* If we get here, the execv() failed */
    fprintf(stderr, "Could not execv %s\n", cmd);
    return(255);
}