src/server/util/util.c

Mon, 09 Sep 2013 11:55:14 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 09 Sep 2013 11:55:14 +0200
changeset 92
382bff43c6eb
parent 91
fac51f87def0
child 96
0185b13bf41f
permissions
-rw-r--r--

fixed some includes

1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * THE BSD LICENSE
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 *
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 * Redistribution and use in source and binary forms, with or without
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * modification, are permitted provided that the following conditions are met:
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 *
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 * Redistributions of source code must retain the above copyright notice, this
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * list of conditions and the following disclaimer.
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * Redistributions in binary form must reproduce the above copyright notice,
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * this list of conditions and the following disclaimer in the documentation
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 * and/or other materials provided with the distribution.
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 *
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * Neither the name of the nor the names of its contributors may be
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * used to endorse or promote products derived from this software without
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * specific prior written permission.
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 *
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32 */
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34 /*
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35 * util.c: A hodge podge of utility functions and standard functions which
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36 * are unavailable on certain systems
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37 *
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38 * Rob McCool
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39 */
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41 #ifdef XP_UNIX
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42 #include <sys/types.h>
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43 #include <sys/wait.h>
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 #include <stdlib.h>
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 #include <unistd.h>
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46 #include <limits.h>
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 #include "prthread.h"
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48 #endif /* XP_UNIX */
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50
24
1a7853a4257e removed some NSPR dependencies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
51 //include "nspr.h"
56
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
52 #include <errno.h>
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
53
24
1a7853a4257e removed some NSPR dependencies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
54 #include "../daemon/netsite.h"
14
b8bf95b39952 New source folder layout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
55 #include "../public/nsapi.h"
91
fac51f87def0 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 81
diff changeset
56 #include <ucx/string.h>
fac51f87def0 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 81
diff changeset
57 #include <ucx/mempool.h>
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59 #include "util.h"
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60
62
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
61
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
62
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
63 /* ------------------------------ _uudecode ------------------------------- */
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
64
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
65 static const unsigned char pr2six[256] = {
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
66 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
67 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,62,64,64,64,63,
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
68 52,53,54,55,56,57,58,59,60,61,64,64,64,64,64,64,64,0,1,2,3,4,5,6,7,8,9,
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
69 10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,64,64,64,64,64,64,26,27,
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
70 28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
71 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
72 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
73 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
74 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
75 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
76 64,64,64,64,64,64,64,64,64,64,64,64,64
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
77 };
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
78
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
79 /** you MUST reserve at least 2 additional bytes for bufout */
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
80 size_t util_base64decode(char *bufcoded, size_t codedbytes, char *bufout) {
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
81 register char *bufin = bufcoded;
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
82 register int nprbytes;
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
83 size_t nbytesdecoded;
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
84
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
85 /* Find the length */
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
86 nprbytes = (int) codedbytes;
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
87 while(pr2six[(int)(bufin[nprbytes-1])] >= 64) {
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
88 nprbytes--;
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
89 }
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
90 nbytesdecoded = ((nprbytes+3)/4) * 3;
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
91
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
92 while (nprbytes > 0) {
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
93 *(bufout++) = (unsigned char)
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
94 (pr2six[(int)(*bufin)] << 2 | pr2six[(int)bufin[1]] >> 4);
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
95 *(bufout++) = (unsigned char)
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
96 (pr2six[(int)bufin[1]] << 4 | pr2six[(int)bufin[2]] >> 2);
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
97 *(bufout++) = (unsigned char)
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
98 (pr2six[(int)bufin[2]] << 6 | pr2six[(int)bufin[3]]);
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
99 bufin += 4;
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
100 nprbytes -= 4;
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
101 }
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
102
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
103 if(nprbytes & 03) {
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
104 if(pr2six[(int)bufin[-2]] > 63)
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
105 nbytesdecoded -= 2;
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
106 else
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
107 nbytesdecoded -= 1;
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
108 }
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
109
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
110 return nbytesdecoded;
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
111 }
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
112
77
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
113
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
114 /* --------------------------- util_env_create ---------------------------- */
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
115
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
116
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
117 NSAPI_PUBLIC char **util_env_create(char **env, int n, int *pos)
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
118 {
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
119 int x;
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
120
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
121 if(!env) {
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
122 *pos = 0;
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
123 return (char **) MALLOC((n + 1)*sizeof(char *));
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
124 }
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
125 else {
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
126 for(x = 0; (env[x]); x++);
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
127 env = (char **) REALLOC(env, (n + x + 1)*(sizeof(char *)));
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
128 *pos = x;
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
129 return env;
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
130 }
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
131 }
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
132
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
133
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
134 /* ---------------------------- util_env_free ----------------------------- */
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
135
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
136
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
137 NSAPI_PUBLIC void util_env_free(char **env)
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
138 {
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
139 register char **ep = env;
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
140
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
141 for(ep = env; *ep; ep++)
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
142 FREE(*ep);
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
143 FREE(env);
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
144 }
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
145
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
146 /* ----------------------------- util_env_str ----------------------------- */
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
147
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
148
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
149 NSAPI_PUBLIC char *util_env_str(const char *name, const char *value) {
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
150 char *t;
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
151
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
152 t = (char *) MALLOC(strlen(name)+strlen(value)+2); /* 2: '=' and '\0' */
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
153
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
154 sprintf(t, "%s=%s", name, value);
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
155
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
156 return t;
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
157 }
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
158
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
159
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
160 /* --------------------------- util_env_replace --------------------------- */
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
161
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
162
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
163 NSAPI_PUBLIC void util_env_replace(char **env, const char *name, const char *value)
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
164 {
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
165 int x, y, z;
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
166 char *i;
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
167
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
168 for(x = 0; env[x]; x++) {
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
169 i = strchr(env[x], '=');
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
170 *i = '\0';
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
171 if(!strcmp(env[x], name)) {
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
172 y = strlen(env[x]);
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
173 z = strlen(value);
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
174
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
175 env[x] = (char *) REALLOC(env[x], y + z + 2);
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
176 util_sprintf(&env[x][y], "=%s", value);
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
177 return;
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
178 }
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
179 *i = '=';
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
180 }
77
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
181 }
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
182
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
183
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
184 /* ---------------------------- util_env_find ----------------------------- */
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
185
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
186
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
187 NSAPI_PUBLIC char *util_env_find(char **env, const char *name)
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
188 {
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
189 char *i;
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
190 int x, r;
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
191
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
192 for(x = 0; env[x]; x++) {
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
193 i = strchr(env[x], '=');
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
194 *i = '\0';
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
195 r = !strcmp(env[x], name);
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
196 *i = '=';
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
197 if(r)
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
198 return i + 1;
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
199 }
77
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
200 return NULL;
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
201 }
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
202
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
203
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
204 /* ---------------------------- util_env_copy ----------------------------- */
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
205
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
206
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
207 NSAPI_PUBLIC char **util_env_copy(char **src, char **dst)
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
208 {
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
209 char **src_ptr;
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
210 int src_cnt;
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
211 int index;
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
212
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
213 if (!src)
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
214 return NULL;
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
215
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
216 for (src_cnt = 0, src_ptr = src; *src_ptr; src_ptr++, src_cnt++);
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
217
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
218 if (!src_cnt)
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
219 return NULL;
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
220
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
221 dst = util_env_create(dst, src_cnt, &index);
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
222
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
223 for (src_ptr = src; *src_ptr; index++, src_ptr++)
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
224 dst[index] = STRDUP(*src_ptr);
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
225 dst[index] = NULL;
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
226
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
227 return dst;
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
228 }
77
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
229
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
230 /* ----------------------------- util_sprintf ----------------------------- */
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
231
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
232 NSAPI_PUBLIC int util_vsnprintf(char *s, int n, register const char *fmt,
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
233 va_list args)
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
234 {
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
235 return vsnprintf(s, n, fmt, args);
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
236 }
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
237
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
238 NSAPI_PUBLIC int util_snprintf(char *s, int n, const char *fmt, ...)
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
239 {
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
240 va_list args;
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
241 va_start(args, fmt);
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
242 return vsnprintf(s, n, fmt, args);
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
243 }
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
244
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
245 NSAPI_PUBLIC int util_vsprintf(char *s, register const char *fmt, va_list args)
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
246 {
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
247 return vsprintf(s, fmt, args);
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
248 }
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
249
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
250 NSAPI_PUBLIC int util_sprintf(char *s, const char *fmt, ...)
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
251 {
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
252 va_list args;
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
253 va_start(args, fmt);
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
254 return vsprintf(s, fmt, args);
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
255 }
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
256
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
257 // TODO: asprintf
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
258
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
259
81
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
260
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
261 /* -------------------------- util_uri_unescape --------------------------- */
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
262
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
263 NSAPI_PUBLIC void util_uri_unescape(char *s)
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
264 {
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
265 char *t, *u;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
266
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
267 for(t = s, u = s; *t; ++t, ++u) {
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
268 if((*t == '%') && t[1] && t[2]) {
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
269 *u = ((t[1] >= 'A' ? ((t[1] & 0xdf) - 'A')+10 : (t[1] - '0'))*16) +
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
270 (t[2] >= 'A' ? ((t[2] & 0xdf) - 'A')+10 : (t[2] - '0'));
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
271 t += 2;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
272 }
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
273 else
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
274 if(u != t)
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
275 *u = *t;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
276 }
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
277 *u = *t;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
278 }
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
279
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
280 /*
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
281 * Same as util_uri_unescape, but returns success/failure
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
282 */
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
283 NSAPI_PUBLIC int util_uri_unescape_strict(char *s)
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
284 {
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
285 char *t, *u, t1, t2;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
286 int rv = 1;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
287
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
288 for(t = s, u = s; *t; ++t, ++u) {
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
289 if (*t == '%') {
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
290 t1 = t[1] & 0xdf; /* [a-f] -> [A-F] */
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
291 if ((t1 < 'A' || t1 > 'F') && (t[1] < '0' || t[1] > '9'))
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
292 rv = 0;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
293
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
294 t2 = t[2] & 0xdf; /* [a-f] -> [A-F] */
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
295 if ((t2 < 'A' || t2 > 'F') && (t[2] < '0' || t[2] > '9'))
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
296 rv = 0;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
297
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
298 *u = ((t[1] >= 'A' ? ((t[1] & 0xdf) - 'A')+10 : (t[1] - '0'))*16) +
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
299 (t[2] >= 'A' ? ((t[2] & 0xdf) - 'A')+10 : (t[2] - '0'));
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
300 t += 2;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
301 }
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
302 else if (u != t)
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
303 *u = *t;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
304 }
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
305 *u = *t;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
306
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
307 return rv;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
308 }
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
309
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
310
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
311 NSAPI_PUBLIC int
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
312 util_uri_unescape_plus (const char *src, char *trg, int len)
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
313 {
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
314 const char *t = src;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
315 char *u = trg == NULL ? (char *)src : trg;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
316 int rlen = 0;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
317
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
318 if (len == -1)
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
319 len = strlen (src);
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
320
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
321 for( ; len && *t; ++t, ++u, len--, rlen++)
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
322 {
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
323 if((*t == '%') && t[1] && t[2])
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
324 {
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
325 *u = ((t[1] >= 'A' ? ((t[1] & 0xdf) - 'A') + 10 : (t[1] - '0')) * 16) +
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
326 (t[2] >= 'A' ? ((t[2] & 0xdf) - 'A') + 10 : (t[2] - '0'));
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
327 t += 2;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
328 len-= 2;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
329 }
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
330 else
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
331 if (*t == '+')
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
332 *u = ' ';
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
333 else
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
334 *u = *t;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
335 }
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
336 *u = 0;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
337 return rlen;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
338 }
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
339
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
340
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
341 NSAPI_PUBLIC int INTutil_getboolean(const char *v, int def) {
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
342 if(v[0] == 'T' || v[0] == 't') {
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
343 return 1;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
344 }
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
345 if(v[0] == 'F' || v[0] == 'f') {
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
346 return 0;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
347 }
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
348 return def;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
349 }
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
350
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
351
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
352 /* ------------------------------ util_itoa ------------------------------- */
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
353 /*
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
354 NSAPI_PUBLIC int util_itoa(int i, char *a)
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
355 {
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
356 int len = util_i64toa(i, a);
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
357
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
358 PR_ASSERT(len < UTIL_ITOA_SIZE);
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
359
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
360 return len;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
361 }
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
362 */
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
363 NSAPI_PUBLIC int INTutil_itoa(int i, char *a) {
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
364 return INTutil_i64toa(i, a);
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
365 }
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
366
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
367
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
368 /* ----------------------------- util_i64toa ------------------------------ */
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
369
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
370 /*
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
371 * Assumption: Reversing the digits will be faster in the general case
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
372 * than doing a log10 or some nasty trick to find the # of digits.
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
373 */
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
374
24
1a7853a4257e removed some NSPR dependencies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
375 NSAPI_PUBLIC int INTutil_i64toa(int64_t i, char *a)
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
376 {
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
377 register int x, y, p;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
378 register char c;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
379 int negative;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
380
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
381 negative = 0;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
382 if(i < 0) {
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
383 *a++ = '-';
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
384 negative = 1;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
385 i = -i;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
386 }
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
387 p = 0;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
388 while(i > 9) {
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
389 a[p++] = (i%10) + '0';
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
390 i /= 10;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
391 }
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
392 a[p++] = i + '0';
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
393
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
394 if(p > 1) {
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
395 for(x = 0, y = p - 1; x < y; ++x, --y) {
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
396 c = a[x];
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
397 a[x] = a[y];
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
398 a[y] = c;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
399 }
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
400 }
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
401 a[p] = '\0';
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
402
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
403 //PR_ASSERT(p + negative < UTIL_I64TOA_SIZE);
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
404
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
405 return p + negative;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
406 }
47
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
407
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
408
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
409
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
410 #ifndef XP_WIN32
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
411 NSAPI_PUBLIC struct passwd *
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
412 util_getpwnam(const char *name, struct passwd *result, char *buffer,
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
413 int buflen)
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
414 {
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
415 struct passwd *rv;
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
416
69
4a10bc0ee80d compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 63
diff changeset
417 #if defined(AIX) || defined(LINUX) || defined(HPUX) || defined(OSX)
47
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
418 errno = getpwnam_r(name, result, buffer, buflen, &rv);
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
419 if (errno != 0)
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
420 rv = NULL;
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
421 #else
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
422 rv = getpwnam_r(name, result, buffer, buflen);
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
423 #endif
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
424
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
425 return rv;
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
426 }
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
427 #endif
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
428
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
429
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
430 #ifndef XP_WIN32
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
431 NSAPI_PUBLIC struct passwd *
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
432 util_getpwuid(uid_t uid, struct passwd *result, char *buffer, int buflen)
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
433 {
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
434 struct passwd *rv;
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
435
69
4a10bc0ee80d compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 63
diff changeset
436 #if defined(AIX) || defined(LINUX) || defined(HPUX) || defined(OSX)
47
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
437 errno = getpwuid_r(uid, result, buffer, buflen, &rv);
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
438 if (errno != 0)
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
439 rv = NULL;
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
440 #else
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
441 rv = getpwuid_r(uid, result, buffer, buflen);
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
442 #endif
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
443
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
444 return rv;
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
445 }
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
446 #endif
56
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
447
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
448
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
449 NSAPI_PUBLIC int util_errno2status(int errno_value) {
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
450 switch(errno_value) {
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
451 case 0: {
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
452 return 200;
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
453 }
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
454 case EACCES: {
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
455 return 403;
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
456 }
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
457 case ENOENT: {
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
458 return 404;
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
459 break;
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
460 }
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
461 }
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
462 return 500;
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
463 }
58
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 56
diff changeset
464
59
ab25c0a231d0 some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 58
diff changeset
465
58
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 56
diff changeset
466
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 56
diff changeset
467 NSAPI_PUBLIC
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 56
diff changeset
468 sstr_t util_path_append(pool_handle_t *pool, char *path, char *ch) {
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 56
diff changeset
469 sstr_t parent = sstr(path);
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 56
diff changeset
470 sstr_t child = sstr(ch);
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 56
diff changeset
471 sstr_t newstr;
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 56
diff changeset
472 sstr_t s;
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 56
diff changeset
473
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 56
diff changeset
474 s.length = 0;
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 56
diff changeset
475 s.ptr = NULL;
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 56
diff changeset
476 newstr.length = parent.length + child.length;
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 56
diff changeset
477 if(parent.ptr[parent.length - 1] != '/') {
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 56
diff changeset
478 s = sstrn("/", 1);
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 56
diff changeset
479 newstr.length++;
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 56
diff changeset
480 }
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 56
diff changeset
481
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 56
diff changeset
482 newstr.ptr = pool_malloc(pool, newstr.length + 1);
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 56
diff changeset
483 if(!newstr.ptr) {
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 56
diff changeset
484 // TODO: error
59
ab25c0a231d0 some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 58
diff changeset
485 newstr.length = 0;
58
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 56
diff changeset
486 return newstr;
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 56
diff changeset
487 }
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 56
diff changeset
488 if(s.length == 1) {
91
fac51f87def0 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 81
diff changeset
489 newstr = sstrncat(newstr, 3, parent, s, child);
58
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 56
diff changeset
490 } else {
91
fac51f87def0 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 81
diff changeset
491 newstr = sstrncat(newstr, 2, parent, child);
58
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 56
diff changeset
492 }
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 56
diff changeset
493 newstr.ptr[newstr.length] = '\0';
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 56
diff changeset
494
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 56
diff changeset
495 return newstr;
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 56
diff changeset
496 }
63
66442f81f823 supports file system ACLs on Solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 62
diff changeset
497
66442f81f823 supports file system ACLs on Solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 62
diff changeset
498 sstr_t util_path_remove_last(sstr_t path) {
66442f81f823 supports file system ACLs on Solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 62
diff changeset
499 int i;
66442f81f823 supports file system ACLs on Solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 62
diff changeset
500 for(i=path.length-1;i>=0;i--) {
66442f81f823 supports file system ACLs on Solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 62
diff changeset
501 char c = path.ptr[i];
66442f81f823 supports file system ACLs on Solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 62
diff changeset
502 if(c == '/') {
66442f81f823 supports file system ACLs on Solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 62
diff changeset
503 path.ptr[i] = 0;
66442f81f823 supports file system ACLs on Solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 62
diff changeset
504 path.length = i;
66442f81f823 supports file system ACLs on Solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 62
diff changeset
505 break;
66442f81f823 supports file system ACLs on Solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 62
diff changeset
506 }
66442f81f823 supports file system ACLs on Solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 62
diff changeset
507 }
66442f81f823 supports file system ACLs on Solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 62
diff changeset
508 if(i < 0) {
66442f81f823 supports file system ACLs on Solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 62
diff changeset
509 path.ptr = NULL;
66442f81f823 supports file system ACLs on Solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 62
diff changeset
510 path.length = 0;
66442f81f823 supports file system ACLs on Solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 62
diff changeset
511 }
66442f81f823 supports file system ACLs on Solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 62
diff changeset
512 return path;
66442f81f823 supports file system ACLs on Solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 62
diff changeset
513 }
81
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
514
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
515
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
516 // new - code in parts from params.cpp
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
517 NSAPI_PUBLIC pblock* util_parse_param(pool_handle_t *pool, char *query) {
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
518 pblock *pb = pblock_create_pool(pool, 32);
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
519 if(!pb) {
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
520 return NULL;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
521 }
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
522 if(!query || !(*query)) {
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
523 return pb;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
524 }
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
525
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
526 int loopFlag = 1;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
527 int nl = 0; // size of the name substring
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
528 int vl = 0; // size of the value substring
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
529 int state = 0;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
530 const char *np = query;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
531 const char *vp = NULL;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
532
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
533 while (loopFlag) {
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
534 char delim = *query++;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
535 switch (delim) {
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
536 case '&':
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
537 case '\0': {
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
538 if(!delim) {
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
539 loopFlag = 0;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
540 }
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
541
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
542 state = 0;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
543
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
544 if(nl > 0) {
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
545 util_uri_unescape_plus(np, NULL, nl);
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
546 util_uri_unescape_plus(vp, NULL, vl);
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
547 pblock_nvlinsert(np, nl, vp, vl, pb);
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
548 }
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
549
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
550 nl = 0;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
551 vl = 0;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
552 vp = NULL;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
553 np = query;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
554 break;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
555 }
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
556 case '=': {
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
557 state = 1;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
558 vp = query;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
559 break;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
560 }
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
561 default: {
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
562 if(state) {
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
563 vl++;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
564 } else {
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
565 nl++;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
566 }
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
567 }
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
568 } /* switch */
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
569 } /* while */
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
570
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
571 return pb;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
572 }
91
fac51f87def0 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 81
diff changeset
573
fac51f87def0 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 81
diff changeset
574 // TODO: remove
fac51f87def0 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 81
diff changeset
575 sstr_t sstrdup_mp(UcxMempool *pool, sstr_t s) {
fac51f87def0 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 81
diff changeset
576 sstr_t newstring;
fac51f87def0 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 81
diff changeset
577 newstring.ptr = (char*)ucx_mempool_malloc(pool, s.length + 1);
fac51f87def0 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 81
diff changeset
578 if (newstring.ptr != NULL) {
fac51f87def0 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 81
diff changeset
579 newstring.length = s.length;
fac51f87def0 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 81
diff changeset
580 newstring.ptr[newstring.length] = 0;
fac51f87def0 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 81
diff changeset
581
fac51f87def0 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 81
diff changeset
582 memcpy(newstring.ptr, s.ptr, s.length);
fac51f87def0 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 81
diff changeset
583 }
fac51f87def0 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 81
diff changeset
584
fac51f87def0 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 81
diff changeset
585 return newstring;
fac51f87def0 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 81
diff changeset
586 }

mercurial