]> git.sur5r.net Git - openldap/blob - contrib/web_ldap/util.c
Update lutil_lockf (aka: ldap_lockf) to hide implementation in
[openldap] / contrib / web_ldap / util.c
1 /* util.c 
2  *
3  * This code module originates from NCSA 
4  * See: http://hoohoo.ncsa.uiuc.edu/cgi/
5  * and: ftp://ftp.ncsa.uiuc.edu/Web/httpd/Unix/ncsa_httpd/cgi/cgi-src/
6  * 
7  * Most of the above listed programs were combined into a single
8  * file (this one) - everything here is public domain and free for
9  * use in any form..
10  *
11  * Corrections made for SGI systems (Irix) and
12  * time/date functions added - R. Scott Guthrie 
13  */ 
14
15 #include "portable.h"
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <fcntl.h>
22
23 #include <ac/string.h>
24 #include <ac/ctype.h>
25 #include <ac/unistd.h>
26
27 #include "process_form.h"
28
29 /*--------------*/
30 /*  strcompare  */
31 /*--------------*/
32 /* This fixes the SGI strcmp function which aborts when passed
33  * a NULL in place of a NULL TERMINATED STRING.
34  * (The code that depended on it was ported from SUN which considered
35  * a NULL to be a NULL terminated string. The standard says strcmp
36  * action is undefined when passed a NULL. SGI abends.)
37  */
38 int strcompare(char* a, char* b)
39 {
40   if(a && b)
41     return(strcmp(a, b));  /* neither char* is NULL */
42   return(1);    /* different if either (or both) char* are NULL */
43 }
44
45
46 /*------------*/
47 /*  getvalue  */
48 /*------------*/
49 /* put a pointer to the value in 'value' for the key specified.
50  */
51 int getvalue(entry* list, char* key, char** value)
52 {
53   int index = 0;
54
55   *value = NULL;    /* initialize value to NULL */
56
57   while(list[index].name)
58   {
59     if(strcmp(list[index].name, key) == 0)
60     {
61       *value = list[index].val;
62       return(1);  /* success */
63     }
64     index++;
65   }
66   return(0);  /* no key value found in list */
67 }
68
69
70 /*------------------*/
71 /*  append_to_list  */
72 /*------------------*/
73 /* Append name/value pair to end of list */
74 int append_to_list(entry* list, char* key, char* value)
75 {
76   int index = 0;
77   char* newname;
78   char* newvalue;
79
80   /* go to end of list */
81   while(list[index].name)
82     index++;
83
84   if(index > MAX_ENTRIES)
85     return(0); /* out of room */
86
87   newname = (char *) malloc(sizeof(char) * (strlen(key) + 1));
88   strcpy(newname, key);
89   list[index].name = newname;
90
91   newvalue = (char *) malloc(sizeof(char) * (strlen(value) + 1));
92   strcpy(newvalue, value);
93   list[index].val = newvalue;
94
95   /* put new nulls at end. */
96   index++;
97   list[index].name = NULL;
98   list[index].val = NULL;
99   return(1);  /* success */
100 }
101
102 /*----------------------*/
103 /*  remove_table_entry  */
104 /*----------------------*/
105 /* replaces table entry 'name' name field with '~' */
106 int remove_table_entry(entry* list, char* name)
107 {
108   int index = 0;
109
110   /* search table for matching name entry */
111   while(1)  /* FOREVER  - breaks out with return */
112   {
113     if(list[index].name == NULL)
114       return(0);   /* not in list */
115
116     if(strcmp(list[index].name, name) == 0)
117     {
118       /* found match.  remove name */
119       free(list[index].name);
120
121       /* allocate space for deleted name */
122       if((list[index].name = (char*)malloc(2 * sizeof(char))) == NULL)
123         return(0);  /* malloc error */
124       else
125         strcpy(list[index].name, "~");    /* DELETE INDICATOR */
126       return(1);  /* replacement successful */
127     }
128     index++;  /* try next name */
129   }
130   return(0);  /* cannot get here */
131 }  /* remove_table_entry */
132
133
134 /*------------*/
135 /*  makeword  */
136 /*------------*/
137 char* makeword(char *line, char stop) 
138 {
139   int x = 0,y;
140   char *word = (char *) malloc(sizeof(char) * (strlen(line) + 1));
141
142   for(x=0;((line[x]) && (line[x] != stop));x++)
143     word[x] = line[x];
144
145   word[x] = '\0';
146   if(line[x]) ++x;
147   y=0;
148
149   while(line[y++] = line[x++]);
150   return word;
151 }
152
153
154 /*-------------*/
155 /*  fmakeword  */
156 /*-------------*/
157 char* fmakeword(FILE *f, char stop, int *cl)
158 {
159   int wsize;
160   char *word;
161   int ll;
162
163   wsize = 102400;
164   ll=0;
165   word = (char *) malloc(sizeof(char) * (wsize + 1));
166
167   while(1)
168   {
169     word[ll] = (char)fgetc(f);
170     if(ll==wsize)
171     {
172       word[ll+1] = '\0';
173       wsize+=102400;
174       word = (char *)realloc(word,sizeof(char)*(wsize+1));
175     }
176     --(*cl);
177
178     if((word[ll] == stop) || (feof(f)) || (!(*cl)))
179     {
180       if(word[ll] != stop) ll++;
181       word[ll] = '\0';
182       return word;
183     }
184     ++ll;
185   }
186   return(NULL);
187 }
188
189
190 /*-------*/
191 /*  x2c  */
192 /*-------*/
193 char x2c(char *what) 
194 {
195   register char digit;
196
197   digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 : (what[0] - '0'));
198   digit *= 16;
199   digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 : (what[1] - '0'));
200   return(digit);
201 }
202
203 /*----------------*/
204 /*  unescape_url  */
205 /*----------------*/
206 void unescape_url(char *url)
207 {
208   register int x,y;
209
210   for(x=0,y=0;url[y];++x,++y)
211   {
212     if((url[x] = url[y]) == '%')
213     {
214       url[x] = x2c(&url[y+1]);
215       y+=2;
216     }
217   }
218   url[x] = '\0';
219 }
220
221
222 /*---------------*/
223 /*  plustospace  */
224 /*---------------*/
225 void plustospace(char *str) 
226 {
227   register int x;
228
229   for(x=0;str[x];x++) if(str[x] == '+') str[x] = ' ';
230 }
231
232
233 /*-------------------------*/
234 /*  remove_leading_blanks  */
235 /*-------------------------*/
236 void remove_leading_blanks(char* str)
237 {
238   int i;
239   while(str[0] == ' ')
240   {
241     i = 1;
242     do
243     {
244       str[i-1] = str[i];
245     } while(str[i++]);
246   }
247 } /* end 'remove_leading_blanks()' */
248
249
250 /*-------------------------*/
251 /* remove_trailing_blanks  */
252 /*-------------------------*/
253 void remove_trailing_blanks(char* str)
254 {
255   while(str[strlen(str) - 1] == ' ')
256     str[strlen(str) - 1] = '\0';
257 } /* end 'remove_trailing_blanks()' */
258
259
260 /*-----------------*/
261 /*  pad_with_char  */
262 /*-----------------*/
263 void pad_with_char(char* buffer, int length, char padchar)
264 {
265   /* if the 'buffer' is >= then 'length', return.
266    * Pad the 'buffer' with 'padchar' until = 'length'
267    */
268   int pos;
269   while((pos = strlen(buffer)) < length)
270   {
271     buffer[pos] = padchar;
272     buffer[pos+1] = '\0';
273   }
274 } /* end pad_with_char */
275
276
277 /*---------------------*/
278 /*  lower_case_string  */
279 /*---------------------*/
280 char* lower_case_string(char* inputbuf)
281 {
282   int pos = 0;
283
284   while(inputbuf[pos])
285   {
286     inputbuf[pos] = (char)tolower((unsigned char) inputbuf[pos]);
287     pos++;
288   }
289   return(inputbuf);
290 }  /* lower_case_string */
291
292
293 /*---------------------*/
294 /*  upper_case_string  */
295 /*---------------------*/
296 char* upper_case_string(char* inputbuf)
297 {
298   int pos = 0;
299
300   while(inputbuf[pos])
301   {
302     inputbuf[pos] = (char)toupper((unsigned char) inputbuf[pos]);
303     pos++;
304   }
305   return(inputbuf);
306 }  /* upper_case_string */
307
308
309 /*------------*/
310 /*  strip_CR  */
311 /*------------*/
312 void strip_CR(char* buffer)
313 {
314   char* bufferptr;
315   char* newptr;
316
317   bufferptr = buffer;
318   newptr = buffer;
319
320   while(*bufferptr)
321   {
322     if(*bufferptr != '\r')
323     {
324       *newptr = *bufferptr;
325       newptr++;
326     }
327     bufferptr++; 
328   }
329   *newptr = '\0';
330
331   return;
332 }
333
334 /*------------------*/
335 /*  show_form_data  */
336 /*------------------*/
337 /* THIS ROUTINE IS USED FOR DEBUGGING and will not be called in production */
338 void show_form_data(entry* entries)
339 {
340   int x = 0;
341
342   printf("<HR><H1>Form Data</H1>");
343   printf("The following Name Value pairs currently exist:<p>%c",10);
344   printf("<ul><pre>%c",10);
345
346   while(entries[x].name)
347   {
348     printf("<li> <code>%s = [%s]</code>%c",entries[x].name,
349             entries[x].val,10);
350     x++;
351   }
352   printf("</pre></ul>%c",10);
353 }
354
355 /*------------------------*/
356 /*  maint_show_form_data  */
357 /*------------------------*/
358 /* THIS ROUTINE IS USED FOR DEBUGGING and will not be called in production */
359 void maint_show_form_data(entry* entries)
360 {
361   int x = 0;
362
363   printf("Content-type: text/html\n\n");
364   printf("<HR><H1>Form Data</H1>");
365   printf("The following Name Value pairs currently exist:<p>%c",10);
366   printf("<ul><pre>%c",10);
367
368   while(entries[x].name)
369   {
370     printf("<li> <code>%s = [%s]</code>%c",entries[x].name,
371             entries[x].val,10);
372     x++;
373   }
374   printf("</pre></ul>%c",10);
375 }
376
377 /*---------------------*/
378 /*  display_html_text  */
379 /*---------------------*/
380 /* display the text found in the indicated file */
381 void display_html_text(char* filename)
382 {
383   FILE *fp;
384   char buffer[256];
385
386   if((fp = fopen(filename, "r")) == NULL)
387   {
388     printf("%s%s%s",
389       "<p><b>(OOPS... We are unable to open file ",
390       filename,
391       " for reading)</b><p>\n");
392   }
393   while(fgets(buffer, 256, fp) != NULL)
394   {
395     if(buffer[strlen(buffer) - 1] == '\n')
396       buffer[strlen(buffer) - 1] = '\0';
397     printf("%s\n", buffer);
398   }
399   fclose(fp);
400   return ;
401 }  /* display_html_text */
402
403
404 /*-----------------*/
405 /*  unformat_cost  */
406 /*-----------------*/
407 /* this routine converts a string value and
408  * converts it to an integer.
409  */
410 long unformat_cost(char* cost)
411 {
412   char buf[100];
413   int buf_siz = 0;
414
415   char* spos = cost;
416   char* dpos = buf;
417
418   /* Make sure a string was passed */
419   if(!spos)
420     return(0L);
421
422   /* while in the string */
423   while(*spos)
424   {
425     if(*spos == '.')
426       break;
427     if(isdigit((unsigned char) *spos))
428       *dpos++ = *spos;
429     spos++;
430     if(buf_siz++ == 98) /* make sure we don't overrun buf */
431       break;
432   }
433   *spos = '\n';
434   return(atol(buf));
435 }
436
437 /*---------------*/
438 /*  digits_only  */
439 /*---------------*/
440 int digits_only(char* str)
441 {
442   char* pos;
443
444   pos = str;
445   while(*pos)
446   {
447     if(!isdigit((unsigned char) *pos))
448       return(0);    /* non-digit found */
449     pos++;
450   } 
451   return(1);
452 }
453
454 /*-------------*/
455 /*  util_year  */
456 /*-------------*/
457 /* return current year -> 0 thru 99 */
458 int util_year()
459 {
460    time_t  t;
461    struct  tm *tptr;
462    int     ret_val;
463    time(&t);
464    tptr    = localtime(&t);
465    ret_val = tptr->tm_year;
466    return(ret_val);
467 }
468
469 /*--------------*/
470 /*  util_month  */
471 /*--------------*/
472 /* return Month of current year -> 1 thru 12 */
473 int util_month()
474 {
475    time_t  t;
476    struct  tm *tptr;
477    int     ret_val;
478    time(&t);
479    tptr    = localtime(&t);
480    ret_val = tptr->tm_mon;
481    return(ret_val + 1);
482 }
483
484 /*------------*/
485 /*  util_day  */
486 /*------------*/
487 /* return day of Month -> 1 thru 31 */
488 int util_day()
489 {
490    time_t  t;
491    struct  tm *tptr;
492    int     ret_val;
493    time(&t);
494    tptr    = localtime(&t);
495    ret_val = tptr->tm_mday;
496    return(ret_val);
497 }
498
499 /*-------------*/
500 /*  util_hour  */
501 /*-------------*/
502 /* return hour of day -> 0 thru 23 */
503 int util_hour()
504 {
505    time_t  t;
506    struct  tm *tptr;
507    int     ret_val;
508    time(&t);
509    tptr    = localtime(&t);
510    ret_val = tptr->tm_hour;
511    return(ret_val);
512 }
513
514 /*---------------*/
515 /*  util_minute  */
516 /*---------------*/
517 /* return minute of day -> 0 thru 59 */
518 int util_minute()
519 {
520    time_t  t;
521    struct  tm *tptr;
522    int     ret_val;
523    time(&t);
524    tptr    = localtime(&t);
525    ret_val = tptr->tm_min;
526    return(ret_val);
527 }
528
529 /*---------------*/
530 /*  util_second  */
531 /*---------------*/
532 /* return second of day -> 0 thru 59 */
533 int util_second()
534 {
535    time_t  t;
536    struct  tm *tptr;
537    int     ret_val;
538    time(&t);
539    tptr    = localtime(&t);
540    ret_val = tptr->tm_sec;
541    return(ret_val);
542 }
543
544 /* end file 'util.c' */