util.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /*
  42. * util.c: Miscellaneous stuffs
  43. *
  44. * All blame to Mike McCool
  45. */
  46. #include "libadmin/libadmin.h"
  47. #include "base/util.h"
  48. #include "private/pprio.h"
  49. #include <base/file.h>
  50. #ifdef XP_UNIX
  51. #include <dirent.h>
  52. #include <sys/types.h>
  53. #include <fcntl.h>
  54. #else
  55. #include <sys/stat.h>
  56. #endif /* WIN32? */
  57. #include <stdio.h>
  58. #include <stdlib.h>
  59. #include <string.h>
  60. #include <sys/stat.h>
  61. #ifdef XP_UNIX
  62. SYS_FILE lf;
  63. #elif defined(XP_WIN32)
  64. HANDLE lf;
  65. #endif
  66. char *get_flock_path(void)
  67. {
  68. char *result="";
  69. char *port=getenv("SERVER_PORT");
  70. #ifdef XP_UNIX
  71. result=(char *) MALLOC(strlen("/tmp/lock.%%s.")+strlen(port)+4);
  72. sprintf(result, "/tmp/lock.%%s.%s", port);
  73. #endif
  74. return result;
  75. }
  76. char *alert_word_wrap(char *str, int width, char *linefeed)
  77. {
  78. char *ans = NULL;
  79. int counter=0;
  80. int lsc=0, lsa=0;
  81. register int strc=0, ansc=0;
  82. register int x=0;
  83. /* assume worst case */
  84. ans = (char *) MALLOC((strlen(str)*strlen(linefeed))+32);
  85. for(strc=0, ansc=0; str[strc]; /*none*/) {
  86. if(str[strc]=='\n') {
  87. counter=0;
  88. lsc=0, lsa=0;
  89. for(x=0; linefeed[x]; x++) {
  90. ans[ansc++]=linefeed[x];
  91. }
  92. strc++;
  93. } else if(str[strc]=='\r') {
  94. strc++;
  95. } else if(str[strc]=='\\') {
  96. ans[ansc++]='\\';
  97. ans[ansc++]=strc++;
  98. } else {
  99. if(counter==width) {
  100. if(lsc && lsa) {
  101. strc=lsc;
  102. ansc=lsa;
  103. counter=0;
  104. lsc=0, lsa=0;
  105. for(x=0; linefeed[x]; x++) {
  106. ans[ansc++]=linefeed[x];
  107. }
  108. strc++;
  109. } else {
  110. /* else, you're a loser, I'm breaking your big word anyway */
  111. counter=0;
  112. lsc=0, lsa=0;
  113. for(x=0; linefeed[x]; x++) {
  114. ans[ansc++]=linefeed[x];
  115. }
  116. strc++;
  117. }
  118. } else {
  119. if(str[strc] == ' ') {
  120. lsc=strc;
  121. lsa=ansc;
  122. }
  123. ans[ansc++]=str[strc++];
  124. counter++;
  125. }
  126. }
  127. }
  128. ans[ansc]='\0';
  129. return ans;
  130. }
  131. /***********************************************************************
  132. ** FUNCTION: cookieValue
  133. ** DESCRIPTION:
  134. ** Get the current value of the cookie variable
  135. ** INPUTS: var - the name of the cookie variable
  136. ** val - if non-NULL, set the in-memory copy of the var
  137. ** OUTPUTS: None
  138. ** RETURN: NULL if the var doesn't exist, else the value
  139. ** SIDE EFFECTS:
  140. ** Eats memory
  141. ** RESTRICTIONS:
  142. ** Don't screw around with the returned string, if anything else wants
  143. ** to use it.
  144. ** MEMORY: This is a memory leak, so only use it in CGIs
  145. ** ALGORITHM:
  146. ** If it's never been called, build a memory structure of the
  147. ** cookie variables.
  148. ** Look for the passed variable, and return its value, or NULL
  149. ***********************************************************************/
  150. NSAPI_PUBLIC char *
  151. cookieValue( char *var, char *val )
  152. {
  153. static char **vars = NULL;
  154. static char **vals = NULL;
  155. static int numVars = -1;
  156. int i;
  157. if ( numVars == -1 ) { /* first time, init the structure */
  158. char *cookie = getenv( "HTTP_COOKIE" );
  159. if ( cookie && *cookie ) {
  160. int len = strlen( cookie );
  161. int foundVal = 0;
  162. cookie = STRDUP( cookie );
  163. numVars = 0;
  164. vars = (char **)MALLOC( sizeof( char * ) );
  165. vals = (char **)MALLOC( sizeof( char * ) );
  166. vars[0] = cookie;
  167. for ( i = 0 ; i < len ; ++i ) {
  168. if ( ( ! foundVal ) && ( cookie[i] == '=' ) ) {
  169. vals[numVars++] = cookie + i + 1;
  170. cookie[i] = '\0';
  171. foundVal = 1;
  172. } else if ( ( cookie[i] == ';' ) && ( cookie[i+1] == ' ' ) ) {
  173. cookie[i] = '\0';
  174. vals = (char **) REALLOC( vals,
  175. sizeof( char * ) * ( numVars + 1 ) );
  176. vars = (char **) REALLOC( vars,
  177. sizeof( char * ) * ( numVars + 1 ) );
  178. vars[numVars] = cookie + i + 2;
  179. i += 2;
  180. foundVal = 0;
  181. }
  182. }
  183. } else { /* no cookie, no vars */
  184. numVars = 0;
  185. }
  186. }
  187. for ( i = 0 ; i < numVars ; ++i ) {
  188. if ( strcmp( vars[i], var ) == 0 ) {
  189. if ( val ) {
  190. vals[i] = STRDUP( val );
  191. } else {
  192. return vals[i];
  193. }
  194. }
  195. }
  196. return NULL;
  197. }
  198. static int adm_initialized=0;
  199. /* Initialize NSPR for all the base functions we use */
  200. NSAPI_PUBLIC int ADM_Init(void)
  201. {
  202. if(!adm_initialized) {
  203. NSPR_INIT("AdminPrograms");
  204. adm_initialized=1;
  205. }
  206. return 0;
  207. }