util.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. /*
  13. * util.c: Miscellaneous stuffs
  14. *
  15. * All blame to Mike McCool
  16. */
  17. #include "libadmin/libadmin.h"
  18. #include "base/util.h"
  19. #include "private/pprio.h"
  20. #include <base/file.h>
  21. #include <dirent.h>
  22. #include <sys/types.h>
  23. #include <fcntl.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <sys/stat.h>
  28. SYS_FILE lf;
  29. char *get_flock_path(void)
  30. {
  31. char *result="";
  32. char *port=getenv("SERVER_PORT");
  33. result=(char *) MALLOC(strlen("/tmp/lock.%%s.")+strlen(port)+4);
  34. sprintf(result, "/tmp/lock.%%s.%s", port);
  35. return result;
  36. }
  37. char *alert_word_wrap(char *str, int width, char *linefeed)
  38. {
  39. char *ans = NULL;
  40. int counter=0;
  41. int lsc=0, lsa=0;
  42. register int strc=0, ansc=0;
  43. register int x=0;
  44. /* assume worst case */
  45. ans = (char *) MALLOC((strlen(str)*strlen(linefeed))+32);
  46. for(strc=0, ansc=0; str[strc]; /*none*/) {
  47. if(str[strc]=='\n') {
  48. counter=0;
  49. lsc=0, lsa=0;
  50. for(x=0; linefeed[x]; x++) {
  51. ans[ansc++]=linefeed[x];
  52. }
  53. strc++;
  54. } else if(str[strc]=='\r') {
  55. strc++;
  56. } else if(str[strc]=='\\') {
  57. ans[ansc++]='\\';
  58. ans[ansc++]=strc++;
  59. } else {
  60. if(counter==width) {
  61. if(lsc && lsa) {
  62. strc=lsc;
  63. ansc=lsa;
  64. counter=0;
  65. lsc=0, lsa=0;
  66. for(x=0; linefeed[x]; x++) {
  67. ans[ansc++]=linefeed[x];
  68. }
  69. strc++;
  70. } else {
  71. /* else, you're a loser, I'm breaking your big word anyway */
  72. counter=0;
  73. lsc=0, lsa=0;
  74. for(x=0; linefeed[x]; x++) {
  75. ans[ansc++]=linefeed[x];
  76. }
  77. strc++;
  78. }
  79. } else {
  80. if(str[strc] == ' ') {
  81. lsc=strc;
  82. lsa=ansc;
  83. }
  84. ans[ansc++]=str[strc++];
  85. counter++;
  86. }
  87. }
  88. }
  89. ans[ansc]='\0';
  90. return ans;
  91. }
  92. /***********************************************************************
  93. ** FUNCTION: cookieValue
  94. ** DESCRIPTION:
  95. ** Get the current value of the cookie variable
  96. ** INPUTS: var - the name of the cookie variable
  97. ** val - if non-NULL, set the in-memory copy of the var
  98. ** OUTPUTS: None
  99. ** RETURN: NULL if the var doesn't exist, else the value
  100. ** SIDE EFFECTS:
  101. ** Eats memory
  102. ** RESTRICTIONS:
  103. ** Don't screw around with the returned string, if anything else wants
  104. ** to use it.
  105. ** MEMORY: This is a memory leak, so only use it in CGIs
  106. ** ALGORITHM:
  107. ** If it's never been called, build a memory structure of the
  108. ** cookie variables.
  109. ** Look for the passed variable, and return its value, or NULL
  110. ***********************************************************************/
  111. NSAPI_PUBLIC char *
  112. cookieValue( char *var, char *val )
  113. {
  114. static char **vars = NULL;
  115. static char **vals = NULL;
  116. static int numVars = -1;
  117. int i;
  118. if ( numVars == -1 ) { /* first time, init the structure */
  119. char *cookie = getenv( "HTTP_COOKIE" );
  120. if ( cookie && *cookie ) {
  121. int len = strlen( cookie );
  122. int foundVal = 0;
  123. cookie = STRDUP( cookie );
  124. numVars = 0;
  125. vars = (char **)MALLOC( sizeof( char * ) );
  126. vals = (char **)MALLOC( sizeof( char * ) );
  127. vars[0] = cookie;
  128. for ( i = 0 ; i < len ; ++i ) {
  129. if ( ( ! foundVal ) && ( cookie[i] == '=' ) ) {
  130. vals[numVars++] = cookie + i + 1;
  131. cookie[i] = '\0';
  132. foundVal = 1;
  133. } else if ( ( cookie[i] == ';' ) && ( cookie[i+1] == ' ' ) ) {
  134. cookie[i] = '\0';
  135. vals = (char **) REALLOC( vals,
  136. sizeof( char * ) * ( numVars + 1 ) );
  137. vars = (char **) REALLOC( vars,
  138. sizeof( char * ) * ( numVars + 1 ) );
  139. vars[numVars] = cookie + i + 2;
  140. i += 2;
  141. foundVal = 0;
  142. }
  143. }
  144. } else { /* no cookie, no vars */
  145. numVars = 0;
  146. }
  147. }
  148. for ( i = 0 ; i < numVars ; ++i ) {
  149. if ( strcmp( vars[i], var ) == 0 ) {
  150. if ( val ) {
  151. vals[i] = STRDUP( val );
  152. } else {
  153. return vals[i];
  154. }
  155. }
  156. }
  157. return NULL;
  158. }
  159. static int adm_initialized=0;
  160. /* Initialize NSPR for all the base functions we use */
  161. NSAPI_PUBLIC int ADM_Init(void)
  162. {
  163. if(!adm_initialized) {
  164. NSPR_INIT("AdminPrograms");
  165. adm_initialized=1;
  166. }
  167. return 0;
  168. }