~ubuntu-branches/ubuntu/hardy/php5/hardy-updates

« back to all changes in this revision

Viewing changes to ext/yp/yp.c

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-10-09 03:14:32 UTC
  • Revision ID: james.westby@ubuntu.com-20051009031432-kspik3lobxstafv9
Tags: upstream-5.0.5
ImportĀ upstreamĀ versionĀ 5.0.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
   +----------------------------------------------------------------------+
 
3
   | PHP Version 5                                                        |
 
4
   +----------------------------------------------------------------------+
 
5
   | Copyright (c) 1997-2004 The PHP Group                                |
 
6
   +----------------------------------------------------------------------+
 
7
   | This source file is subject to version 3.0 of the PHP license,       |
 
8
   | that is bundled with this package in the file LICENSE, and is        |
 
9
   | available through the world-wide-web at the following url:           |
 
10
   | http://www.php.net/license/3_0.txt.                                  |
 
11
   | If you did not receive a copy of the PHP license and are unable to   |
 
12
   | obtain it through the world-wide-web, please send a note to          |
 
13
   | license@php.net so we can mail you a copy immediately.               |
 
14
   +----------------------------------------------------------------------+
 
15
   | Authors: Stephanie Wehner <_@r4k.net>                                |
 
16
   |          Fredrik Ohrn                                                |
 
17
   +----------------------------------------------------------------------+
 
18
 */
 
19
/* $Id: yp.c,v 1.45 2004/05/31 21:01:18 iliaa Exp $ */
 
20
 
 
21
#ifdef HAVE_CONFIG_H
 
22
#include "config.h"
 
23
#endif
 
24
 
 
25
#include "php.h"
 
26
#include "ext/standard/info.h"
 
27
 
 
28
#if HAVE_YP
 
29
 
 
30
#include "php_yp.h"
 
31
 
 
32
#include <rpcsvc/ypclnt.h>
 
33
 
 
34
/* {{{ thread safety stuff */
 
35
 
 
36
#ifdef ZTS
 
37
int yp_globals_id;
 
38
#else
 
39
PHP_YP_API php_yp_globals yp_globals;
 
40
#endif
 
41
 
 
42
/* }}} */
 
43
 
 
44
function_entry yp_functions[] = {
 
45
        PHP_FE(yp_get_default_domain, NULL)
 
46
        PHP_FE(yp_order, NULL)
 
47
        PHP_FE(yp_master, NULL)
 
48
        PHP_FE(yp_match, NULL)
 
49
        PHP_FE(yp_first, NULL)
 
50
        PHP_FE(yp_next, NULL)
 
51
        PHP_FE(yp_all, NULL)
 
52
        PHP_FE(yp_cat, NULL)
 
53
        PHP_FE(yp_errno, NULL)
 
54
        PHP_FE(yp_err_string, NULL)
 
55
        {NULL, NULL, NULL}
 
56
};
 
57
 
 
58
zend_module_entry yp_module_entry = {
 
59
    STANDARD_MODULE_HEADER,
 
60
        "yp",
 
61
        yp_functions,
 
62
        PHP_MINIT(yp),
 
63
        NULL,
 
64
        PHP_RINIT(yp),
 
65
        NULL,
 
66
        PHP_MINFO(yp),
 
67
    NO_VERSION_YET,
 
68
        STANDARD_MODULE_PROPERTIES
 
69
};
 
70
 
 
71
#ifdef COMPILE_DL_YP
 
72
ZEND_GET_MODULE(yp)
 
73
#endif
 
74
 
 
75
/* {{{ proto string yp_get_default_domain(void)
 
76
   Returns the domain or false */
 
77
PHP_FUNCTION(yp_get_default_domain)
 
78
{
 
79
        char *outdomain;
 
80
 
 
81
        if (ZEND_NUM_ARGS()) {
 
82
                WRONG_PARAM_COUNT;
 
83
        }
 
84
 
 
85
        if((YP(error) = yp_get_default_domain(&outdomain))) {
 
86
                php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", yperr_string (YP(error)));
 
87
                RETURN_FALSE;
 
88
        }
 
89
        RETVAL_STRING(outdomain,1);
 
90
}
 
91
/* }}} */
 
92
 
 
93
/* {{{ proto int yp_order(string domain, string map)            
 
94
   Returns the order number or false */
 
95
PHP_FUNCTION(yp_order)
 
96
{
 
97
        pval **domain, **map;
 
98
 
 
99
#if SOLARIS_YP
 
100
        unsigned long outval;
 
101
#else
 
102
        int outval;
 
103
#endif
 
104
 
 
105
        if((ZEND_NUM_ARGS() != 2) || zend_get_parameters_ex(2,&domain,&map) == FAILURE) {
 
106
                WRONG_PARAM_COUNT;
 
107
        }
 
108
 
 
109
        convert_to_string_ex(domain);
 
110
        convert_to_string_ex(map);
 
111
 
 
112
        if((YP(error) = yp_order(Z_STRVAL_PP (domain), Z_STRVAL_PP (map), &outval))) {
 
113
                php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", yperr_string (YP(error)));
 
114
                RETURN_FALSE;
 
115
        }
 
116
 
 
117
        RETVAL_LONG(outval);
 
118
}
 
119
/* }}} */
 
120
 
 
121
/* {{{ proto string yp_master(string domain, string map)
 
122
   Returns the machine name of the master */
 
123
PHP_FUNCTION(yp_master)
 
124
{
 
125
        pval **domain, **map;
 
126
        char *outname;
 
127
 
 
128
        if((ZEND_NUM_ARGS() != 2) || zend_get_parameters_ex(2,&domain,&map) == FAILURE) {
 
129
                WRONG_PARAM_COUNT;
 
130
        }
 
131
 
 
132
        convert_to_string_ex(domain);
 
133
        convert_to_string_ex(map);
 
134
 
 
135
        if((YP(error) = yp_master(Z_STRVAL_PP (domain), Z_STRVAL_PP (map), &outname))) {
 
136
                php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", yperr_string (YP(error)));
 
137
                RETURN_FALSE;
 
138
        }
 
139
 
 
140
        RETVAL_STRING(outname,1);
 
141
}
 
142
/* }}} */
 
143
 
 
144
/* {{{ proto string yp_match(string domain, string map, string key)
 
145
   Returns the matched line or false */
 
146
PHP_FUNCTION(yp_match)
 
147
{
 
148
        pval **domain, **map, **key;
 
149
        char *outval;
 
150
        int outvallen;
 
151
 
 
152
        if((ZEND_NUM_ARGS() != 3) || zend_get_parameters_ex(3,&domain,&map,&key) == FAILURE) {
 
153
                WRONG_PARAM_COUNT;
 
154
        }
 
155
 
 
156
        convert_to_string_ex(domain);
 
157
        convert_to_string_ex(map);
 
158
        convert_to_string_ex(key);
 
159
 
 
160
        if((YP(error) = yp_match(Z_STRVAL_PP (domain), Z_STRVAL_PP (map), Z_STRVAL_PP (key), Z_STRLEN_PP (key), &outval, &outvallen))) {
 
161
                php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", yperr_string (YP(error)));
 
162
                RETURN_FALSE;
 
163
        }
 
164
 
 
165
        RETVAL_STRINGL(outval,outvallen,1);
 
166
}
 
167
/* }}} */
 
168
 
 
169
/* {{{ proto array yp_first(string domain, string map)
 
170
   Returns the first key as array with $var[$key] and the the line as the value */
 
171
PHP_FUNCTION(yp_first)
 
172
{
 
173
        pval **domain, **map;
 
174
        char *outval, *outkey;
 
175
        int outvallen, outkeylen;
 
176
 
 
177
        if((ZEND_NUM_ARGS() != 2) || zend_get_parameters_ex(2,&domain,&map) == FAILURE) {
 
178
                WRONG_PARAM_COUNT;
 
179
        }
 
180
 
 
181
        convert_to_string_ex(domain);
 
182
        convert_to_string_ex(map);
 
183
 
 
184
        if((YP(error) = yp_first(Z_STRVAL_PP (domain), Z_STRVAL_PP (map), &outkey, &outkeylen, &outval, &outvallen))) {
 
185
                php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", yperr_string (YP(error)));
 
186
                RETURN_FALSE;
 
187
        }
 
188
        array_init(return_value);
 
189
        add_assoc_stringl_ex(return_value,outkey,outkeylen+1,outval,outvallen,1);
 
190
 
 
191
        /* Deprecated */
 
192
        add_assoc_stringl(return_value,"key",outkey,outkeylen,1);
 
193
        add_assoc_stringl(return_value,"value",outval,outvallen,1);
 
194
}
 
195
/* }}} */
 
196
 
 
197
/* {{{ proto array yp_next(string domain, string map, string key)
 
198
   Returns an array with $var[$key] and the the line as the value */
 
199
PHP_FUNCTION(yp_next)
 
200
{
 
201
        pval **domain, **map, **key;
 
202
        char *outval, *outkey;
 
203
        int outvallen, outkeylen;
 
204
 
 
205
        if((ZEND_NUM_ARGS() != 3) || zend_get_parameters_ex(3,&domain,&map,&key) == FAILURE) {
 
206
                WRONG_PARAM_COUNT;
 
207
        }
 
208
 
 
209
        convert_to_string_ex(domain);
 
210
        convert_to_string_ex(map);
 
211
        convert_to_string_ex(key);
 
212
 
 
213
        if((YP(error) = yp_next(Z_STRVAL_PP (domain), Z_STRVAL_PP (map), Z_STRVAL_PP (key), Z_STRLEN_PP (key), &outkey, &outkeylen, &outval, &outvallen))) {
 
214
                php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", yperr_string (YP(error)));
 
215
                RETURN_FALSE;
 
216
        }
 
217
        
 
218
        array_init(return_value);
 
219
        add_assoc_stringl_ex(return_value,outkey,outkeylen+1,outval,outvallen,1);
 
220
}
 
221
/* }}} */
 
222
 
 
223
/* {{{ php_foreach_all
 
224
 */
 
225
static int php_foreach_all (int instatus, char *inkey, int inkeylen, char *inval, int invallen, char *indata)
 
226
{
 
227
        int r;
 
228
        zval *status, *key, *value;
 
229
        zval **args [3];
 
230
        zval *retval;
 
231
        TSRMLS_FETCH();
 
232
 
 
233
        args[0] = &status;
 
234
        args[1] = &key;
 
235
        args[2] = &value;
 
236
 
 
237
        MAKE_STD_ZVAL (status);
 
238
        ZVAL_LONG (status, ypprot_err (instatus));
 
239
 
 
240
        MAKE_STD_ZVAL (key);
 
241
        ZVAL_STRINGL (key, inkey, inkeylen, 1);
 
242
 
 
243
        MAKE_STD_ZVAL (value);
 
244
        ZVAL_STRINGL (value, inval, invallen, 1);
 
245
 
 
246
        if(call_user_function_ex(CG(function_table), NULL, *((zval **)indata), &retval, 3, args, 0, NULL TSRMLS_CC) != SUCCESS)
 
247
        {
 
248
                zval_ptr_dtor(&status);
 
249
                zval_ptr_dtor(&key);
 
250
                zval_ptr_dtor(&value);
 
251
 
 
252
                php_error_docref(NULL TSRMLS_CC, E_ERROR, "Function call failed");
 
253
                return 1;
 
254
        }
 
255
 
 
256
        convert_to_long_ex(&retval);
 
257
        r = Z_LVAL_P (retval);
 
258
 
 
259
        zval_ptr_dtor(&retval);
 
260
 
 
261
        zval_ptr_dtor(&status);
 
262
        zval_ptr_dtor(&key);
 
263
        zval_ptr_dtor(&value);
 
264
 
 
265
        return r;
 
266
}
 
267
/* }}} */
 
268
 
 
269
/* {{{ proto bool yp_all(string domain, string map, string callback)
 
270
   Traverse the map and call a function on each entry */
 
271
PHP_FUNCTION(yp_all)
 
272
{
 
273
        pval **domain, **map, **php_callback;
 
274
        struct ypall_callback callback;
 
275
 
 
276
        if((ZEND_NUM_ARGS() != 3) || zend_get_parameters_ex(3,&domain,&map,&php_callback) == FAILURE) {
 
277
                WRONG_PARAM_COUNT;
 
278
        }
 
279
 
 
280
        convert_to_string_ex(domain);
 
281
        convert_to_string_ex(map);
 
282
 
 
283
        callback.foreach = php_foreach_all;
 
284
        callback.data = (char *) php_callback;
 
285
 
 
286
        yp_all(Z_STRVAL_PP(domain),Z_STRVAL_PP(map),&callback);
 
287
 
 
288
        RETURN_FALSE;
 
289
}
 
290
/* }}} */
 
291
 
 
292
/* {{{ php_foreach_cat
 
293
 */
 
294
static int php_foreach_cat (int instatus, char *inkey, int inkeylen, char *inval, int invallen, char *indata)
 
295
{
 
296
        int err;
 
297
 
 
298
        err = ypprot_err (instatus);
 
299
 
 
300
        if (!err)
 
301
        {
 
302
                if (inkeylen) {
 
303
                        char *key = emalloc(inkeylen+1);
 
304
                        strlcpy(key, inkey, inkeylen+1);
 
305
                        add_assoc_stringl_ex((zval *) indata, key, inkeylen+1, inval, invallen, 1);
 
306
                        efree(key);
 
307
                }
 
308
 
 
309
                return 0;
 
310
        }
 
311
 
 
312
        if (err != YPERR_NOMORE)
 
313
        {
 
314
                TSRMLS_FETCH();
 
315
 
 
316
                YP(error) = err;
 
317
                php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", yperr_string (err));
 
318
        }
 
319
 
 
320
        return 0;
 
321
}
 
322
/* }}} */
 
323
 
 
324
/* {{{ proto array yp_cat(string domain, string map)
 
325
   Return an array containing the entire map */
 
326
PHP_FUNCTION(yp_cat)
 
327
{
 
328
        pval **domain, **map;
 
329
        struct ypall_callback callback;
 
330
 
 
331
        if((ZEND_NUM_ARGS() != 2) || zend_get_parameters_ex(2,&domain,&map) == FAILURE) {
 
332
                WRONG_PARAM_COUNT;
 
333
        }
 
334
 
 
335
        convert_to_string_ex(domain);
 
336
        convert_to_string_ex(map);
 
337
 
 
338
        array_init(return_value);
 
339
 
 
340
        callback.foreach = php_foreach_cat;
 
341
        callback.data = (char *) return_value;
 
342
 
 
343
        yp_all(Z_STRVAL_PP(domain),Z_STRVAL_PP(map),&callback);
 
344
}
 
345
/* }}} */
 
346
 
 
347
/* {{{ proto int yp_errno()
 
348
   Returns the error code from the last call or 0 if no error occured */
 
349
PHP_FUNCTION(yp_errno)
 
350
{
 
351
        if((ZEND_NUM_ARGS() != 0)) {
 
352
                WRONG_PARAM_COUNT;
 
353
        }
 
354
 
 
355
        RETURN_LONG (YP(error));
 
356
}
 
357
/* }}} */
 
358
 
 
359
/* {{{ proto string yp_err_string(int errorcode)
 
360
   Returns the corresponding error string for the given error code */
 
361
PHP_FUNCTION(yp_err_string)
 
362
{
 
363
        pval **error;
 
364
        char *string;
 
365
 
 
366
        if((ZEND_NUM_ARGS() != 1) || zend_get_parameters_ex(1,&error) == FAILURE) {
 
367
                WRONG_PARAM_COUNT;
 
368
        }
 
369
 
 
370
        convert_to_long_ex(error);
 
371
 
 
372
        if((string = yperr_string(Z_LVAL_PP(error))) == NULL) {
 
373
                RETURN_FALSE;
 
374
        }
 
375
 
 
376
        RETVAL_STRING(string,1);
 
377
}
 
378
/* }}} */
 
379
 
 
380
/* {{{ PHP_MINIT_FUNCTION
 
381
 */
 
382
PHP_MINIT_FUNCTION(yp)
 
383
{
 
384
#ifdef ZTS
 
385
        ts_allocate_id(&yp_globals_id, sizeof(php_yp_globals), NULL, NULL);
 
386
#endif
 
387
 
 
388
        REGISTER_LONG_CONSTANT("YPERR_BADARGS", YPERR_BADARGS, CONST_CS | CONST_PERSISTENT);
 
389
        REGISTER_LONG_CONSTANT("YPERR_BADDB", YPERR_BADDB, CONST_CS | CONST_PERSISTENT);
 
390
        REGISTER_LONG_CONSTANT("YPERR_BUSY", YPERR_BUSY, CONST_CS | CONST_PERSISTENT);
 
391
        REGISTER_LONG_CONSTANT("YPERR_DOMAIN", YPERR_DOMAIN, CONST_CS | CONST_PERSISTENT);
 
392
        REGISTER_LONG_CONSTANT("YPERR_KEY", YPERR_KEY, CONST_CS | CONST_PERSISTENT);
 
393
        REGISTER_LONG_CONSTANT("YPERR_MAP", YPERR_MAP, CONST_CS | CONST_PERSISTENT);
 
394
        REGISTER_LONG_CONSTANT("YPERR_NODOM", YPERR_NODOM, CONST_CS | CONST_PERSISTENT);
 
395
        REGISTER_LONG_CONSTANT("YPERR_NOMORE", YPERR_NOMORE, CONST_CS | CONST_PERSISTENT);
 
396
        REGISTER_LONG_CONSTANT("YPERR_PMAP", YPERR_PMAP, CONST_CS | CONST_PERSISTENT);
 
397
        REGISTER_LONG_CONSTANT("YPERR_RESRC", YPERR_RESRC, CONST_CS | CONST_PERSISTENT);
 
398
        REGISTER_LONG_CONSTANT("YPERR_RPC", YPERR_RPC, CONST_CS | CONST_PERSISTENT);
 
399
        REGISTER_LONG_CONSTANT("YPERR_YPBIND", YPERR_YPBIND, CONST_CS | CONST_PERSISTENT);
 
400
        REGISTER_LONG_CONSTANT("YPERR_YPERR", YPERR_YPERR, CONST_CS | CONST_PERSISTENT);
 
401
        REGISTER_LONG_CONSTANT("YPERR_YPSERV", YPERR_YPSERV, CONST_CS | CONST_PERSISTENT);
 
402
        REGISTER_LONG_CONSTANT("YPERR_VERS", YPERR_VERS, CONST_CS | CONST_PERSISTENT);
 
403
 
 
404
        return SUCCESS;
 
405
}
 
406
/* }}} */
 
407
 
 
408
PHP_RINIT_FUNCTION(yp)
 
409
{
 
410
        YP(error) = 0;
 
411
        
 
412
        return SUCCESS;
 
413
}
 
414
 
 
415
PHP_MINFO_FUNCTION(yp)
 
416
{
 
417
        php_info_print_table_start();
 
418
        php_info_print_table_row(2, "YP Support", "enabled");
 
419
        php_info_print_table_end();
 
420
}
 
421
#endif /* HAVE_YP */
 
422
 
 
423
/*
 
424
 * Local variables:
 
425
 * tab-width: 4
 
426
 * c-basic-offset: 4
 
427
 * End:
 
428
 * vim600: sw=4 ts=4 fdm=marker
 
429
 * vim<600: sw=4 ts=4
 
430
 */