UNIXworkcode

1 /* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 * 4 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 5 * 6 * THE BSD LICENSE 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions are met: 10 * 11 * Redistributions of source code must retain the above copyright notice, this 12 * list of conditions and the following disclaimer. 13 * Redistributions in binary form must reproduce the above copyright notice, 14 * this list of conditions and the following disclaimer in the documentation 15 * and/or other materials provided with the distribution. 16 * 17 * Neither the name of the nor the names of its contributors may be 18 * used to endorse or promote products derived from this software without 19 * specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER 25 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 28 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #ifndef BASE_UTIL_H 35 #define BASE_UTIL_H 36 37 #include "../daemon/netsite.h" 38 #include <cx/string.h> 39 #include "pool.h" 40 41 #ifdef XP_UNIX 42 #include <pwd.h> 43 #endif 44 45 #ifndef NOINTNSAPI 46 #define INTNSAPI 47 #endif /* !NOINTNSAPI */ 48 49 /* 50 * util.h: A hodge podge of utility functions and standard functions which 51 * are unavailable on certain systems 52 * 53 * Rob McCool 54 */ 55 56 /* Needed for various reentrant functions */ 57 #define DEF_CTIMEBUF 26 58 #define DEF_ERRBUF 256 59 #define DEF_PWBUF 1024 60 61 #ifndef BASE_BUFFER_H 62 //include "buffer.h" /* filebuf for getline */ 63 #endif /* !BASE_BUFFER_H */ 64 65 /* 66 * UTIL_ITOA_SIZE is the minimum size for buffers passed to util_itoa(). 67 */ 68 #define UTIL_ITOA_SIZE 12 69 70 /* 71 * UTIL_I64TOA_SIZE is the minimum size for buffers passed to util_i64toa(). 72 */ 73 #define UTIL_I64TOA_SIZE 21 74 75 /* --- Begin common function prototypes --- */ 76 77 #ifdef INTNSAPI 78 79 #ifdef __cplusplus 80 extern "C" { 81 #endif 82 83 size_t util_base64decode(char *bufcoded, size_t codedbytes, char *bufout); 84 char* util_base64encode(char *in, size_t len); 85 86 NSAPI_PUBLIC 87 int INTutil_init_PRNetAddr(PRNetAddr * naddr, char * ipstr, int iplen, int type); 88 89 NSAPI_PUBLIC 90 int INTutil_getline(filebuffer *buf, int lineno, int maxlen, char *l); 91 92 NSAPI_PUBLIC char **INTutil_env_create(char **env, int n, int *pos); 93 94 NSAPI_PUBLIC char *INTutil_env_str(const char *name, const char *value); 95 96 NSAPI_PUBLIC void INTutil_env_replace(char **env, const char *name, const char *value); 97 98 NSAPI_PUBLIC void INTutil_env_free(char **env); 99 100 NSAPI_PUBLIC char **INTutil_env_copy(char **src, char **dst); 101 102 NSAPI_PUBLIC char *INTutil_env_find(char **env, const char *name); 103 104 NSAPI_PUBLIC char **util_argv_parse(const char *cmdline); 105 106 NSAPI_PUBLIC char *INTutil_hostname(void); 107 108 NSAPI_PUBLIC int INTutil_chdir2path(char *path); 109 110 NSAPI_PUBLIC int INTutil_chdir(const char *path); 111 112 NSAPI_PUBLIC char *INTutil_getcwd(void); 113 114 NSAPI_PUBLIC int INTutil_is_mozilla(char *ua, char *major, char *minor); 115 116 NSAPI_PUBLIC int INTutil_is_url(const char *url); 117 118 NSAPI_PUBLIC int INTutil_mstr2num(const char *s); 119 120 NSAPI_PUBLIC int INTutil_later_than(const struct tm *lms, const char *ims); 121 122 NSAPI_PUBLIC int INTutil_time_equal(const struct tm *lms, const char *ims); 123 124 NSAPI_PUBLIC int INTutil_str_time_equal(const char *t1, const char *t2); 125 126 NSAPI_PUBLIC int INTutil_uri_is_evil(const char *t); 127 128 NSAPI_PUBLIC int INTutil_uri_is_evil_internal(const char *t, int, int); 129 130 NSAPI_PUBLIC void INTutil_uri_parse(char *uri); 131 132 #ifdef XP_WIN32 133 NSAPI_PUBLIC int INTutil_uri_unescape_and_normalize(pool_handle_t *pool, char *s, char *unnormalized); 134 #endif /* XP_WIN32 */ 135 136 NSAPI_PUBLIC void INTutil_uri_normalize_slashes(char *s); 137 138 NSAPI_PUBLIC void INTutil_uri_unescape (char *s); 139 140 NSAPI_PUBLIC int INTutil_uri_unescape_strict (char *s); 141 142 NSAPI_PUBLIC int INTutil_uri_unescape_plus (const char *src, char *trg, int len); 143 144 NSAPI_PUBLIC char *INTutil_uri_escape(char *d, const char *s); 145 146 NSAPI_PUBLIC char *INTutil_uri_strip_params(char *uri); 147 148 NSAPI_PUBLIC char* util_canonicalize_uri(pool_handle_t *pool, const char *uri, int len, int *pcanonlen); 149 150 NSAPI_PUBLIC char* util_canonicalize_redirect(pool_handle_t *pool, const char *baseUri, const char *newUri); 151 152 NSAPI_PUBLIC char *INTutil_url_escape(char *d, const char *s); 153 154 NSAPI_PUBLIC char *INTutil_sh_escape(char *s); 155 156 NSAPI_PUBLIC int INTutil_mime_separator(char *sep); 157 158 NSAPI_PUBLIC int INTutil_itoa(int i, char *a); 159 160 NSAPI_PUBLIC int INTutil_i64toa(int64_t i, char *a); 161 162 163 NSAPI_PUBLIC 164 int INTutil_vsprintf(char *s, register const char *fmt, va_list args); 165 166 NSAPI_PUBLIC int INTutil_sprintf(char *s, const char *fmt, ...); 167 168 NSAPI_PUBLIC int INTutil_vsnprintf(char *s, int n, register const char *fmt, 169 va_list args); 170 171 NSAPI_PUBLIC int INTutil_snprintf(char *s, int n, const char *fmt, ...); 172 173 NSAPI_PUBLIC int util_vasprintf(pool_handle_t *pool, char **s, const char *fmt, 174 va_list args); 175 176 NSAPI_PUBLIC int util_asprintf(pool_handle_t *pool, char **s, const char *fmt, ...); 177 178 NSAPI_PUBLIC int util_strlftime(char *dst, size_t dstsize, const char *format, const struct tm *t); 179 180 NSAPI_PUBLIC int INTutil_strftime(char *s, const char *format, const struct tm *t); 181 182 NSAPI_PUBLIC char *INTutil_strtok(char *s1, const char *s2, char **lasts); 183 184 NSAPI_PUBLIC struct tm *INTutil_localtime(const time_t *clock, struct tm *res); 185 186 NSAPI_PUBLIC char *INTutil_ctime(const time_t *clock, char *buf, int buflen); 187 188 NSAPI_PUBLIC char *INTutil_strerror(int errnum, char *msg, int buflen); 189 190 NSAPI_PUBLIC struct tm *INTutil_gmtime(const time_t *clock, struct tm *res); 191 192 NSAPI_PUBLIC char *INTutil_asctime(const struct tm *tm,char *buf, int buflen); 193 194 NSAPI_PUBLIC char *INTutil_cookie_find(char *cookie, const char *name); 195 196 NSAPI_PUBLIC char *INTutil_cookie_next(char *cookie, char **name, char **value); 197 198 NSAPI_PUBLIC char *INTutil_cookie_next_av_pair(char *cookie, char **name, char **value); 199 200 NSAPI_PUBLIC void INTutil_random(void *buf, size_t sz); 201 202 NSAPI_PUBLIC PRBool INTutil_format_http_version(const char *v, int *protv_num, char *buffer, int size); 203 204 NSAPI_PUBLIC int INTutil_getboolean(const char *v, int def); 205 int util_getboolean_s(cxstring s, int def); 206 207 // new 208 NSAPI_PUBLIC int util_strtoint(const char *str, int64_t *value); 209 NSAPI_PUBLIC const char* util_resource_name(const char *url); 210 NSAPI_PUBLIC char* util_parent_path(const char *path); 211 NSAPI_PUBLIC char* util_parent_path_pool(pool_handle_t *pool, const char *path); 212 213 // TODO 214 //NSAPI_PUBLIC PRIntervalTime INTutil_getinterval(const char *v, PRIntervalTime def); 215 216 #ifdef NEED_STRCASECMP 217 NSAPI_PUBLIC int INTutil_strcasecmp(const char *one, const char *two); 218 #endif /* NEED_STRCASECMP */ 219 220 #ifdef NEED_STRNCASECMP 221 NSAPI_PUBLIC int INTutil_strncasecmp(const char *one, const char *two, int n); 222 #endif /* NEED_STRNCASECMP */ 223 224 NSAPI_PUBLIC char *INTutil_strcasestr(char *str, const char *substr); 225 226 NSAPI_PUBLIC size_t util_strlcpy(char *dst, const char *src, size_t dstsize); 227 228 NSAPI_PUBLIC size_t util_strlcat(char *dst, const char *src, size_t dstsize); 229 230 NSAPI_PUBLIC char *INTutil_uuencode(const char *src, int len); 231 232 NSAPI_PUBLIC char *INTutil_uudecode(const char *src); 233 234 NSAPI_PUBLIC char *util_strlower(char *s); 235 236 NSAPI_PUBLIC char *util_decrement_string(char *s); 237 238 NSAPI_PUBLIC int64_t util_atoi64(const char *a); 239 240 NSAPI_PUBLIC int util_qtoi(const char *q, const char **p); 241 242 /* path utils */ 243 NSAPI_PUBLIC 244 cxmutstr util_path_append(pool_handle_t *pool, char *path, char *child); 245 NSAPI_PUBLIC 246 cxmutstr util_path_remove_last(cxmutstr path); 247 NSAPI_PUBLIC 248 void util_add_ppath(cxmutstr root, cxmutstr path, pblock *vars); 249 250 251 int util_isdate(const char *str); 252 253 /* --- End common function prototypes --- */ 254 255 /* --- Begin Unix-only function prototypes --- */ 256 257 #ifdef XP_UNIX 258 259 NSAPI_PUBLIC int INTutil_can_exec(struct stat *finfo, uid_t uid, gid_t gid); 260 261 NSAPI_PUBLIC 262 struct passwd *INTutil_getpwnam(const char *name, struct passwd *result, 263 char *buffer, int buflen); 264 265 NSAPI_PUBLIC 266 struct passwd *INTutil_getpwuid(uid_t uid, struct passwd *result, 267 char *buffer, int buflen); 268 269 NSAPI_PUBLIC pid_t INTutil_waitpid(pid_t pid, int *statptr, int options); 270 271 #endif /* XP_UNIX */ 272 273 /* --- End Unix-only function prototypes --- */ 274 275 /* --- Begin Windows-only function prototypes --- */ 276 277 #ifdef XP_WIN32 278 279 NSAPI_PUBLIC 280 VOID INTutil_delete_directory(char *FileName, BOOL delete_directory); 281 282 #endif /* XP_WIN32 */ 283 284 /* --- End Windows-only function prototypes --- */ 285 286 #ifdef __cplusplus 287 } 288 #endif 289 290 #ifdef __cplusplus 291 292 NSAPI_PUBLIC const char *util_host_port_suffix(const char *h); 293 294 #endif 295 296 #define util_init_PRNetAddr INTutil_init_PRNetAddr 297 #define util_getline INTutil_getline 298 #define util_env_create INTutil_env_create 299 #define util_env_str INTutil_env_str 300 #define util_env_replace INTutil_env_replace 301 #define util_env_free INTutil_env_free 302 #define util_env_copy INTutil_env_copy 303 #define util_env_find INTutil_env_find 304 #define util_hostname INTutil_hostname 305 #define util_chdir2path INTutil_chdir2path 306 #define util_chdir INTutil_chdir 307 #define util_getcwd INTutil_getcwd 308 #define util_is_mozilla INTutil_is_mozilla 309 #define util_is_url INTutil_is_url 310 #define util_mstr2num INTutil_mstr2num 311 #define util_later_than INTutil_later_than 312 #define util_time_equal INTutil_time_equal 313 #define util_str_time_equal INTutil_str_time_equal 314 #define util_uri_is_evil INTutil_uri_is_evil 315 #define util_uri_is_evil_internal INTutil_uri_is_evil_internal 316 #define util_uri_parse INTutil_uri_parse 317 #ifdef XP_WIN32 318 #define util_uri_unescape_and_normalize INTutil_uri_unescape_and_normalize 319 #endif /* XP_WIN32 */ 320 #define util_uri_normalize_slashes INTutil_uri_normalize_slashes 321 #define util_uri_unescape INTutil_uri_unescape 322 #define util_uri_unescape_strict INTutil_uri_unescape_strict 323 #define util_uri_unescape_plus INTutil_uri_unescape_plus 324 325 #define util_uri_escape INTutil_uri_escape 326 #define util_uri_strip_params INTutil_uri_strip_params 327 #define util_url_escape INTutil_url_escape 328 #define util_sh_escape INTutil_sh_escape 329 #define util_mime_separator INTutil_mime_separator 330 #define util_itoa INTutil_itoa 331 #define util_i64toa INTutil_i64toa 332 #define util_vsprintf INTutil_vsprintf 333 #define util_sprintf INTutil_sprintf 334 #define util_vsnprintf INTutil_vsnprintf 335 #define util_snprintf INTutil_snprintf 336 #define util_vasprintf INTutil_vasprintf 337 #define util_asprintf INTutil_asprintf 338 #define util_strftime INTutil_strftime 339 #define util_strcasecmp INTutil_strcasecmp 340 #define util_strncasecmp INTutil_strncasecmp 341 #define util_strcasestr INTutil_strcasestr 342 #define util_strtok INTutil_strtok 343 #define util_localtime INTutil_localtime 344 #define util_ctime INTutil_ctime 345 #define util_strerror INTutil_strerror 346 #define util_gmtime INTutil_gmtime 347 #define util_asctime INTutil_asctime 348 #define util_uuencode INTutil_uuencode 349 #define util_uudecode INTutil_uudecode 350 351 #ifdef XP_UNIX 352 #define util_can_exec INTutil_can_exec 353 #define util_getpwnam INTutil_getpwnam 354 #define util_getpwuid INTutil_getpwuid 355 #define util_waitpid INTutil_waitpid 356 #endif /* XP_UNIX */ 357 358 #ifdef XP_WIN32 359 #define util_delete_directory INTutil_delete_directory 360 #endif /* XP_WIN32 */ 361 362 #ifdef NEED_STRCASECMP 363 #define util_strcasecmp INTutil_strcasecmp 364 #define strcasecmp INTutil_strcasecmp 365 #endif /* NEED_STRCASECMP */ 366 367 #ifdef NEED_STRINGS_H /* usually for strcasecmp */ 368 #include <strings.h> 369 #endif 370 371 #ifdef NEED_STRNCASECMP 372 #define util_strncasecmp INTutil_strncasecmp 373 #define strncasecmp INTutil_strncasecmp 374 #endif /* NEED_STRNCASECMP */ 375 376 #define util_cookie_find INTutil_cookie_find 377 #define util_cookie_next INTutil_cookie_next 378 #define util_cookie_next_av_pair INTutil_cookie_next_av_pair 379 380 #define util_random INTutil_random 381 #define util_format_http_version INTutil_format_http_version 382 #define util_getboolean INTutil_getboolean 383 #define util_getinterval INTutil_getinterval 384 385 #ifdef XP_WIN32 386 void set_fullpathname(PRBool b); 387 #endif /* XP_WIN32 */ 388 #endif /* INTNSAPI */ 389 390 #endif /* !BASE_UTIL_H */ 391 392 393