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