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