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 #include <ac/stdlib.h>
22 #include <ac/string.h>
24 #include <ac/unistd.h>
26 #include "process_form.h"
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.)
37 int strcompare(char* a, char* b)
40 return(strcmp(a, b)); /* neither char* is NULL */
41 return(1); /* different if either (or both) char* are NULL */
48 /* put a pointer to the value in 'value' for the key specified.
50 int getvalue(entry* list, char* key, char** value)
54 *value = NULL; /* initialize value to NULL */
56 while(list[index].name)
58 if(strcmp(list[index].name, key) == 0)
60 *value = list[index].val;
61 return(1); /* success */
65 return(0); /* no key value found in list */
69 /*------------------*/
71 /*------------------*/
72 /* Append name/value pair to end of list */
73 int append_to_list(entry* list, char* key, char* value)
79 /* go to end of list */
80 while(list[index].name)
83 if(index > MAX_ENTRIES)
84 return(0); /* out of room */
86 newname = (char *) malloc(sizeof(char) * (strlen(key) + 1));
88 list[index].name = newname;
90 newvalue = (char *) malloc(sizeof(char) * (strlen(value) + 1));
91 strcpy(newvalue, value);
92 list[index].val = newvalue;
94 /* put new nulls at end. */
96 list[index].name = NULL;
97 list[index].val = NULL;
98 return(1); /* success */
101 /*----------------------*/
102 /* remove_table_entry */
103 /*----------------------*/
104 /* replaces table entry 'name' name field with '~' */
105 int remove_table_entry(entry* list, char* name)
109 /* search table for matching name entry */
110 while(1) /* FOREVER - breaks out with return */
112 if(list[index].name == NULL)
113 return(0); /* not in list */
115 if(strcmp(list[index].name, name) == 0)
117 /* found match. remove name */
118 free(list[index].name);
120 /* allocate space for deleted name */
121 if((list[index].name = (char*)malloc(2 * sizeof(char))) == NULL)
122 return(0); /* malloc error */
124 strcpy(list[index].name, "~"); /* DELETE INDICATOR */
125 return(1); /* replacement successful */
127 index++; /* try next name */
129 return(0); /* cannot get here */
130 } /* remove_table_entry */
136 char* makeword(char *line, char stop)
139 char *word = (char *) malloc(sizeof(char) * (strlen(line) + 1));
141 for(x=0;((line[x]) && (line[x] != stop));x++)
148 while(line[y++] = line[x++]);
156 char* fmakeword(FILE *f, char stop, int *cl)
164 word = (char *) malloc(sizeof(char) * (wsize + 1));
168 word[ll] = (char)fgetc(f);
173 word = (char *)realloc(word,sizeof(char)*(wsize+1));
177 if((word[ll] == stop) || (feof(f)) || (!(*cl)))
179 if(word[ll] != stop) ll++;
196 digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 : (what[0] - '0'));
198 digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 : (what[1] - '0'));
205 void unescape_url(char *url)
209 for(x=0,y=0;url[y];++x,++y)
211 if((url[x] = url[y]) == '%')
213 url[x] = x2c(&url[y+1]);
224 void plustospace(char *str)
228 for(x=0;str[x];x++) if(str[x] == '+') str[x] = ' ';
232 /*-------------------------*/
233 /* remove_leading_blanks */
234 /*-------------------------*/
235 void remove_leading_blanks(char* str)
246 } /* end 'remove_leading_blanks()' */
249 /*-------------------------*/
250 /* remove_trailing_blanks */
251 /*-------------------------*/
252 void remove_trailing_blanks(char* str)
254 while(str[strlen(str) - 1] == ' ')
255 str[strlen(str) - 1] = '\0';
256 } /* end 'remove_trailing_blanks()' */
259 /*-----------------*/
261 /*-----------------*/
262 void pad_with_char(char* buffer, int length, char padchar)
264 /* if the 'buffer' is >= then 'length', return.
265 * Pad the 'buffer' with 'padchar' until = 'length'
268 while((pos = strlen(buffer)) < length)
270 buffer[pos] = padchar;
271 buffer[pos+1] = '\0';
273 } /* end pad_with_char */
276 /*---------------------*/
277 /* lower_case_string */
278 /*---------------------*/
279 char* lower_case_string(char* inputbuf)
285 inputbuf[pos] = (char)tolower((unsigned char) inputbuf[pos]);
289 } /* lower_case_string */
292 /*---------------------*/
293 /* upper_case_string */
294 /*---------------------*/
295 char* upper_case_string(char* inputbuf)
301 inputbuf[pos] = (char)toupper((unsigned char) inputbuf[pos]);
305 } /* upper_case_string */
311 void strip_CR(char* buffer)
321 if(*bufferptr != '\r')
323 *newptr = *bufferptr;
333 /*------------------*/
335 /*------------------*/
336 /* THIS ROUTINE IS USED FOR DEBUGGING and will not be called in production */
337 void show_form_data(entry* entries)
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);
345 while(entries[x].name)
347 printf("<li> <code>%s = [%s]</code>%c",entries[x].name,
351 printf("</pre></ul>%c",10);
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)
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);
367 while(entries[x].name)
369 printf("<li> <code>%s = [%s]</code>%c",entries[x].name,
373 printf("</pre></ul>%c",10);
376 /*---------------------*/
377 /* display_html_text */
378 /*---------------------*/
379 /* display the text found in the indicated file */
380 void display_html_text(char* filename)
385 if((fp = fopen(filename, "r")) == NULL)
388 "<p><b>(OOPS... We are unable to open file ",
390 " for reading)</b><p>\n");
392 while(fgets(buffer, 256, fp) != NULL)
394 if(buffer[strlen(buffer) - 1] == '\n')
395 buffer[strlen(buffer) - 1] = '\0';
396 printf("%s\n", buffer);
400 } /* display_html_text */
403 /*-----------------*/
405 /*-----------------*/
406 /* this routine converts a string value and
407 * converts it to an integer.
409 long unformat_cost(char* cost)
417 /* Make sure a string was passed */
421 /* while in the string */
426 if(isdigit((unsigned char) *spos))
429 if(buf_siz++ == 98) /* make sure we don't overrun buf */
439 int digits_only(char* str)
446 if(!isdigit((unsigned char) *pos))
447 return(0); /* non-digit found */
456 /* return current year -> 0 thru 99 */
463 tptr = localtime(&t);
464 ret_val = tptr->tm_year;
471 /* return Month of current year -> 1 thru 12 */
478 tptr = localtime(&t);
479 ret_val = tptr->tm_mon;
486 /* return day of Month -> 1 thru 31 */
493 tptr = localtime(&t);
494 ret_val = tptr->tm_mday;
501 /* return hour of day -> 0 thru 23 */
508 tptr = localtime(&t);
509 ret_val = tptr->tm_hour;
516 /* return minute of day -> 0 thru 59 */
523 tptr = localtime(&t);
524 ret_val = tptr->tm_min;
531 /* return second of day -> 0 thru 59 */
538 tptr = localtime(&t);
539 ret_val = tptr->tm_sec;
543 /* end file 'util.c' */