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/
7 * Most of the above listed programs were combined into a single
8 * file (this one) - everything here is public domain and free for
11 * Corrections made for SGI systems (Irix) and
12 * time/date functions added - R. Scott Guthrie
20 #include <ac/string.h>
22 #include <ac/unistd.h>
24 #include "process_form.h"
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.)
35 int strcompare(char* a, char* b)
38 return(strcmp(a, b)); /* neither char* is NULL */
39 return(1); /* different if either (or both) char* are NULL */
46 /* put a pointer to the value in 'value' for the key specified.
48 int getvalue(entry* list, char* key, char** value)
52 *value = NULL; /* initialize value to NULL */
54 while(list[index].name)
56 if(strcmp(list[index].name, key) == 0)
58 *value = list[index].val;
59 return(1); /* success */
63 return(0); /* no key value found in list */
67 /*------------------*/
69 /*------------------*/
70 /* Append name/value pair to end of list */
71 int append_to_list(entry* list, char* key, char* value)
77 /* go to end of list */
78 while(list[index].name)
81 if(index > MAX_ENTRIES)
82 return(0); /* out of room */
84 newname = (char *) malloc(sizeof(char) * (strlen(key) + 1));
86 list[index].name = newname;
88 newvalue = (char *) malloc(sizeof(char) * (strlen(value) + 1));
89 strcpy(newvalue, value);
90 list[index].val = newvalue;
92 /* put new nulls at end. */
94 list[index].name = NULL;
95 list[index].val = NULL;
96 return(1); /* success */
99 /*----------------------*/
100 /* remove_table_entry */
101 /*----------------------*/
102 /* replaces table entry 'name' name field with '~' */
103 int remove_table_entry(entry* list, char* name)
107 /* search table for matching name entry */
108 while(1) /* FOREVER - breaks out with return */
110 if(list[index].name == NULL)
111 return(0); /* not in list */
113 if(strcmp(list[index].name, name) == 0)
115 /* found match. remove name */
116 free(list[index].name);
118 /* allocate space for deleted name */
119 if((list[index].name = (char*)malloc(2 * sizeof(char))) == NULL)
120 return(0); /* malloc error */
122 strcpy(list[index].name, "~"); /* DELETE INDICATOR */
123 return(1); /* replacement successful */
125 index++; /* try next name */
127 return(0); /* cannot get here */
128 } /* remove_table_entry */
134 char* makeword(char *line, char stop)
137 char *word = (char *) malloc(sizeof(char) * (strlen(line) + 1));
139 for(x=0;((line[x]) && (line[x] != stop));x++)
146 while(line[y++] = line[x++]);
154 char* fmakeword(FILE *f, char stop, int *cl)
162 word = (char *) malloc(sizeof(char) * (wsize + 1));
166 word[ll] = (char)fgetc(f);
171 word = (char *)realloc(word,sizeof(char)*(wsize+1));
175 if((word[ll] == stop) || (feof(f)) || (!(*cl)))
177 if(word[ll] != stop) ll++;
194 digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 : (what[0] - '0'));
196 digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 : (what[1] - '0'));
203 void unescape_url(char *url)
207 for(x=0,y=0;url[y];++x,++y)
209 if((url[x] = url[y]) == '%')
211 url[x] = x2c(&url[y+1]);
222 void plustospace(char *str)
226 for(x=0;str[x];x++) if(str[x] == '+') str[x] = ' ';
230 /*-------------------------*/
231 /* remove_leading_blanks */
232 /*-------------------------*/
233 void remove_leading_blanks(char* str)
244 } /* end 'remove_leading_blanks()' */
247 /*-------------------------*/
248 /* remove_trailing_blanks */
249 /*-------------------------*/
250 void remove_trailing_blanks(char* str)
252 while(str[strlen(str) - 1] == ' ')
253 str[strlen(str) - 1] = '\0';
254 } /* end 'remove_trailing_blanks()' */
257 /*-----------------*/
259 /*-----------------*/
260 void pad_with_char(char* buffer, int length, char padchar)
262 /* if the 'buffer' is >= then 'length', return.
263 * Pad the 'buffer' with 'padchar' until = 'length'
266 while((pos = strlen(buffer)) < length)
268 buffer[pos] = padchar;
269 buffer[pos+1] = '\0';
271 } /* end pad_with_char */
274 /*---------------------*/
275 /* lower_case_string */
276 /*---------------------*/
277 char* lower_case_string(char* inputbuf)
283 inputbuf[pos] = (char)tolower((unsigned char) inputbuf[pos]);
287 } /* lower_case_string */
290 /*---------------------*/
291 /* upper_case_string */
292 /*---------------------*/
293 char* upper_case_string(char* inputbuf)
299 inputbuf[pos] = (char)toupper((unsigned char) inputbuf[pos]);
303 } /* upper_case_string */
309 void strip_CR(char* buffer)
319 if(*bufferptr != '\r')
321 *newptr = *bufferptr;
331 /*------------------*/
333 /*------------------*/
334 /* THIS ROUTINE IS USED FOR DEBUGGING and will not be called in production */
335 void show_form_data(entry* entries)
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);
343 while(entries[x].name)
345 printf("<li> <code>%s = [%s]</code>%c",entries[x].name,
349 printf("</pre></ul>%c",10);
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)
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);
365 while(entries[x].name)
367 printf("<li> <code>%s = [%s]</code>%c",entries[x].name,
371 printf("</pre></ul>%c",10);
374 /*---------------------*/
375 /* display_html_text */
376 /*---------------------*/
377 /* display the text found in the indicated file */
378 void display_html_text(char* filename)
383 if((fp = fopen(filename, "r")) == NULL)
386 "<p><b>(OOPS... We are unable to open file ",
388 " for reading)</b><p>\n");
390 while(fgets(buffer, 256, fp) != NULL)
392 if(buffer[strlen(buffer) - 1] == '\n')
393 buffer[strlen(buffer) - 1] = '\0';
394 printf("%s\n", buffer);
398 } /* display_html_text */
401 /*-----------------*/
403 /*-----------------*/
404 /* this routine converts a string value and
405 * converts it to an integer.
407 long unformat_cost(char* cost)
415 /* Make sure a string was passed */
419 /* while in the string */
424 if(isdigit((unsigned char) *spos))
427 if(buf_siz++ == 98) /* make sure we don't overrun buf */
437 int digits_only(char* str)
444 if(!isdigit((unsigned char) *pos))
445 return(0); /* non-digit found */
454 /* return current year -> 0 thru 99 */
461 tptr = localtime(&t);
462 ret_val = tptr->tm_year;
469 /* return Month of current year -> 1 thru 12 */
476 tptr = localtime(&t);
477 ret_val = tptr->tm_mon;
484 /* return day of Month -> 1 thru 31 */
491 tptr = localtime(&t);
492 ret_val = tptr->tm_mday;
499 /* return hour of day -> 0 thru 23 */
506 tptr = localtime(&t);
507 ret_val = tptr->tm_hour;
514 /* return minute of day -> 0 thru 59 */
521 tptr = localtime(&t);
522 ret_val = tptr->tm_min;
529 /* return second of day -> 0 thru 59 */
536 tptr = localtime(&t);
537 ret_val = tptr->tm_sec;
541 /* end file 'util.c' */