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
19 #ifdef HAVE_SYS_TIME_H
23 #include <ac/string.h>
25 #include <ac/unistd.h>
27 #include "process_form.h"
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.)
38 int strcompare(char* a, char* b)
41 return(strcmp(a, b)); /* neither char* is NULL */
42 return(1); /* different if either (or both) char* are NULL */
49 /* put a pointer to the value in 'value' for the key specified.
51 int getvalue(entry* list, char* key, char** value)
55 *value = NULL; /* initialize value to NULL */
57 while(list[index].name)
59 if(strcmp(list[index].name, key) == 0)
61 *value = list[index].val;
62 return(1); /* success */
66 return(0); /* no key value found in list */
70 /*------------------*/
72 /*------------------*/
73 /* Append name/value pair to end of list */
74 int append_to_list(entry* list, char* key, char* value)
80 /* go to end of list */
81 while(list[index].name)
84 if(index > MAX_ENTRIES)
85 return(0); /* out of room */
87 newname = (char *) malloc(sizeof(char) * (strlen(key) + 1));
89 list[index].name = newname;
91 newvalue = (char *) malloc(sizeof(char) * (strlen(value) + 1));
92 strcpy(newvalue, value);
93 list[index].val = newvalue;
95 /* put new nulls at end. */
97 list[index].name = NULL;
98 list[index].val = NULL;
99 return(1); /* success */
102 /*----------------------*/
103 /* remove_table_entry */
104 /*----------------------*/
105 /* replaces table entry 'name' name field with '~' */
106 int remove_table_entry(entry* list, char* name)
110 /* search table for matching name entry */
111 while(1) /* FOREVER - breaks out with return */
113 if(list[index].name == NULL)
114 return(0); /* not in list */
116 if(strcmp(list[index].name, name) == 0)
118 /* found match. remove name */
119 free(list[index].name);
121 /* allocate space for deleted name */
122 if((list[index].name = (char*)malloc(2 * sizeof(char))) == NULL)
123 return(0); /* malloc error */
125 strcpy(list[index].name, "~"); /* DELETE INDICATOR */
126 return(1); /* replacement successful */
128 index++; /* try next name */
130 return(0); /* cannot get here */
131 } /* remove_table_entry */
137 char* makeword(char *line, char stop)
140 char *word = (char *) malloc(sizeof(char) * (strlen(line) + 1));
142 for(x=0;((line[x]) && (line[x] != stop));x++)
149 while(line[y++] = line[x++]);
157 char* fmakeword(FILE *f, char stop, int *cl)
165 word = (char *) malloc(sizeof(char) * (wsize + 1));
169 word[ll] = (char)fgetc(f);
174 word = (char *)realloc(word,sizeof(char)*(wsize+1));
178 if((word[ll] == stop) || (feof(f)) || (!(*cl)))
180 if(word[ll] != stop) ll++;
197 digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 : (what[0] - '0'));
199 digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 : (what[1] - '0'));
206 void unescape_url(char *url)
210 for(x=0,y=0;url[y];++x,++y)
212 if((url[x] = url[y]) == '%')
214 url[x] = x2c(&url[y+1]);
225 void plustospace(char *str)
229 for(x=0;str[x];x++) if(str[x] == '+') str[x] = ' ';
233 /*-------------------------*/
234 /* remove_leading_blanks */
235 /*-------------------------*/
236 void remove_leading_blanks(char* str)
247 } /* end 'remove_leading_blanks()' */
250 /*-------------------------*/
251 /* remove_trailing_blanks */
252 /*-------------------------*/
253 void remove_trailing_blanks(char* str)
255 while(str[strlen(str) - 1] == ' ')
256 str[strlen(str) - 1] = '\0';
257 } /* end 'remove_trailing_blanks()' */
260 /*-----------------*/
262 /*-----------------*/
263 void pad_with_char(char* buffer, int length, char padchar)
265 /* if the 'buffer' is >= then 'length', return.
266 * Pad the 'buffer' with 'padchar' until = 'length'
269 while((pos = strlen(buffer)) < length)
271 buffer[pos] = padchar;
272 buffer[pos+1] = '\0';
274 } /* end pad_with_char */
277 /*---------------------*/
278 /* lower_case_string */
279 /*---------------------*/
280 char* lower_case_string(char* inputbuf)
286 inputbuf[pos] = (char)tolower((unsigned char) inputbuf[pos]);
290 } /* lower_case_string */
293 /*---------------------*/
294 /* upper_case_string */
295 /*---------------------*/
296 char* upper_case_string(char* inputbuf)
302 inputbuf[pos] = (char)toupper((unsigned char) inputbuf[pos]);
306 } /* upper_case_string */
312 void strip_CR(char* buffer)
322 if(*bufferptr != '\r')
324 *newptr = *bufferptr;
334 /*------------------*/
336 /*------------------*/
337 /* THIS ROUTINE IS USED FOR DEBUGGING and will not be called in production */
338 void show_form_data(entry* entries)
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);
346 while(entries[x].name)
348 printf("<li> <code>%s = [%s]</code>%c",entries[x].name,
352 printf("</pre></ul>%c",10);
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)
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);
368 while(entries[x].name)
370 printf("<li> <code>%s = [%s]</code>%c",entries[x].name,
374 printf("</pre></ul>%c",10);
377 /*---------------------*/
378 /* display_html_text */
379 /*---------------------*/
380 /* display the text found in the indicated file */
381 void display_html_text(char* filename)
386 if((fp = fopen(filename, "r")) == NULL)
389 "<p><b>(OOPS... We are unable to open file ",
391 " for reading)</b><p>\n");
393 while(fgets(buffer, 256, fp) != NULL)
395 if(buffer[strlen(buffer) - 1] == '\n')
396 buffer[strlen(buffer) - 1] = '\0';
397 printf("%s\n", buffer);
401 } /* display_html_text */
404 /*-----------------*/
406 /*-----------------*/
407 /* this routine converts a string value and
408 * converts it to an integer.
410 long unformat_cost(char* cost)
418 /* Make sure a string was passed */
422 /* while in the string */
427 if(isdigit((unsigned char) *spos))
430 if(buf_siz++ == 98) /* make sure we don't overrun buf */
440 int digits_only(char* str)
447 if(!isdigit((unsigned char) *pos))
448 return(0); /* non-digit found */
457 /* return current year -> 0 thru 99 */
464 tptr = localtime(&t);
465 ret_val = tptr->tm_year;
472 /* return Month of current year -> 1 thru 12 */
479 tptr = localtime(&t);
480 ret_val = tptr->tm_mon;
487 /* return day of Month -> 1 thru 31 */
494 tptr = localtime(&t);
495 ret_val = tptr->tm_mday;
502 /* return hour of day -> 0 thru 23 */
509 tptr = localtime(&t);
510 ret_val = tptr->tm_hour;
517 /* return minute of day -> 0 thru 59 */
524 tptr = localtime(&t);
525 ret_val = tptr->tm_min;
532 /* return second of day -> 0 thru 59 */
539 tptr = localtime(&t);
540 ret_val = tptr->tm_sec;
544 /* end file 'util.c' */