]> git.8kb.co.uk Git - pgpool-ii/pgpool-ii_2.2.5/blob - pool_params.c
Attempt to send a proper failure message to frontend when authentication
[pgpool-ii/pgpool-ii_2.2.5] / pool_params.c
1 /* -*-pgsql-c-*- */
2 /*
3  * $Header: /cvsroot/pgpool/pgpool-II/pool_params.c,v 1.3.2.1 2009/08/22 04:19:49 t-ishii Exp $
4  *
5  * pgpool: a language independent connection pool server for PostgreSQL
6  * written by Tatsuo Ishii
7  *
8  * Copyright (c) 2003-2008      PgPool Global Development Group
9  *
10  * Permission to use, copy, modify, and distribute this software and
11  * its documentation for any purpose and without fee is hereby
12  * granted, provided that the above copyright notice appear in all
13  * copies and that both that copyright notice and this permission
14  * notice appear in supporting documentation, and that the name of the
15  * author not be used in advertising or publicity pertaining to
16  * distribution of the software without specific, written prior
17  * permission. The author makes no representations about the
18  * suitability of this software for any purpose.  It is provided "as
19  * is" without express or implied warranty.
20  *
21  * params.c: Paramter Status handling routines
22  *
23  */
24 #include "config.h"
25
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include "pool.h"
30
31 #define MAX_PARAM_ITEMS 128
32
33 /*
34  * initialize parameter structure
35  */
36 int pool_init_params(ParamStatus *params)
37 {
38     params->num = 0;
39     params->names = malloc(MAX_PARAM_ITEMS*sizeof(char *));
40         if (params->names == NULL)
41         {
42                 pool_error("pool_init_params: cannot allocate memory");
43                 return -1;
44         }
45     params->values = malloc(MAX_PARAM_ITEMS*sizeof(char *));
46         if (params->values == NULL)
47         {
48                 pool_error("pool_init_params: cannot allocate memory");
49                 return -1;
50         }
51         return 0;
52 }
53
54 /*
55  * discard parameter structure
56  */
57 void pool_discard_params(ParamStatus *params)
58 {
59     int i;
60
61     for (i=0;i<params->num;i++)
62     {
63                 free(params->names[i]);
64                 free(params->values[i]);
65     }
66     free(params->names);
67     free(params->values);
68 }
69
70 /*
71  * find param value by name. if found, its value is returned
72  * also, pos is set
73  * if not found, NULL is returned
74  */
75 char *pool_find_name(ParamStatus *params, char *name, int *pos)
76 {
77     int i;
78
79     for (i=0;i<params->num;i++)
80     {
81                 if (!strcmp(name, params->names[i]))
82                 {
83                         *pos = i;
84                         return params->values[i];
85                 }
86     }
87     return NULL;
88 }
89
90 /*
91  * return name and value by index.
92  */
93 int pool_get_param(ParamStatus *params, int index, char **name, char **value)
94 {
95         if (index < 0 || index >= params->num)
96                 return -1;
97
98         *name = params->names[index];
99         *value = params->values[index];
100
101         return 0;
102 }
103
104 /*
105  * add or replace name/value pair
106  */
107 int pool_add_param(ParamStatus *params, char *name, char *value)
108 {
109     int pos;
110
111     if (pool_find_name(params, name, &pos))
112     {
113                 /* name already exists */
114                 if (strlen(params->values[pos]) < strlen(value))
115                 {
116                         params->values[pos] = realloc(params->values[pos], strlen(value) + 1);
117                         if (params->values[pos] == NULL)
118                         {
119                                 pool_error("pool_init_params: cannot allocate memory");
120                                 return -1;
121                         }
122                 }
123                 strcpy(params->values[pos], value);
124     }
125     else
126     {
127                 int num;
128
129                 /* add name/value pair */
130                 if (params->num >= MAX_PARAM_ITEMS)
131                 {
132                         pool_error("pool_add_param: no more room for num");
133                         return -1;
134                 }
135                 num = params->num;
136                 params->names[num] = strdup(name);
137                 if (params->names[num] == NULL)
138                 {
139                         pool_error("pool_init_params: cannot allocate memory");
140                         return -1;
141                 }
142                 params->values[num] = strdup(value);
143                 if (params->values[num] == NULL)
144                 {
145                         pool_error("pool_init_params: cannot allocate memory");
146                         return -1;
147                 }
148                 params->num++;
149     }
150         return 0;
151 }
152
153 void pool_param_debug_print(ParamStatus *params)
154 {
155         int i;
156
157     for (i=0;i<params->num;i++)
158     {
159                 pool_debug("No.%d: name: %s value: %s", i, params->names[i], params->values[i]);
160         }
161 }