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