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 <sys/types.h>
23 #include "process_form.h"
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.)
34 int strcompare(char* a, char* b)
37 return(strcmp(a, b)); /* neither char* is NULL */
38 return(1); /* different if either (or both) char* are NULL */
45 /* put a pointer to the value in 'value' for the key specified.
47 int getvalue(entry* list, char* key, char** value)
51 *value = NULL; /* initialize value to NULL */
53 while(list[index].name)
55 if(strcmp(list[index].name, key) == 0)
57 *value = list[index].val;
58 return(1); /* success */
62 return(0); /* no key value found in list */
66 /*------------------*/
68 /*------------------*/
69 /* Append name/value pair to end of list */
70 int append_to_list(entry* list, char* key, char* value)
76 /* go to end of list */
77 while(list[index].name)
80 if(index > MAX_ENTRIES)
81 return(0); /* out of room */
83 newname = (char *) malloc(sizeof(char) * (strlen(key) + 1));
85 list[index].name = newname;
87 newvalue = (char *) malloc(sizeof(char) * (strlen(value) + 1));
88 strcpy(newvalue, value);
89 list[index].val = newvalue;
91 /* put new nulls at end. */
93 list[index].name = NULL;
94 list[index].val = NULL;
95 return(1); /* success */
98 /*----------------------*/
99 /* remove_table_entry */
100 /*----------------------*/
101 /* replaces table entry 'name' name field with '~' */
102 int remove_table_entry(entry* list, char* name)
106 /* search table for matching name entry */
107 while(1) /* FOREVER - breaks out with return */
109 if(list[index].name == NULL)
110 return(0); /* not in list */
112 if(strcmp(list[index].name, name) == 0)
114 /* found match. remove name */
115 free(list[index].name);
117 /* allocate space for deleted name */
118 if((list[index].name = (char*)malloc(2 * sizeof(char))) == NULL)
119 return(0); /* malloc error */
121 strcpy(list[index].name, "~"); /* DELETE INDICATOR */
122 return(1); /* replacement successful */
124 index++; /* try next name */
126 return(0); /* cannot get here */
127 } /* remove_table_entry */
133 char* makeword(char *line, char stop)
136 char *word = (char *) malloc(sizeof(char) * (strlen(line) + 1));
138 for(x=0;((line[x]) && (line[x] != stop));x++)
145 while(line[y++] = line[x++]);
153 char* fmakeword(FILE *f, char stop, int *cl)
161 word = (char *) malloc(sizeof(char) * (wsize + 1));
165 word[ll] = (char)fgetc(f);
170 word = (char *)realloc(word,sizeof(char)*(wsize+1));
174 if((word[ll] == stop) || (feof(f)) || (!(*cl)))
176 if(word[ll] != stop) ll++;
193 digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 : (what[0] - '0'));
195 digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 : (what[1] - '0'));
202 void unescape_url(char *url)
206 for(x=0,y=0;url[y];++x,++y)
208 if((url[x] = url[y]) == '%')
210 url[x] = x2c(&url[y+1]);
221 void plustospace(char *str)
225 for(x=0;str[x];x++) if(str[x] == '+') str[x] = ' ';
229 /*-------------------------*/
230 /* remove_leading_blanks */
231 /*-------------------------*/
232 void remove_leading_blanks(char* str)
243 } /* end 'remove_leading_blanks()' */
246 /*-------------------------*/
247 /* remove_trailing_blanks */
248 /*-------------------------*/
249 void remove_trailing_blanks(char* str)
251 while(str[strlen(str) - 1] == ' ')
252 str[strlen(str) - 1] = '\0';
253 } /* end 'remove_trailing_blanks()' */
256 /*-----------------*/
258 /*-----------------*/
259 void pad_with_char(char* buffer, int length, char padchar)
261 /* if the 'buffer' is >= then 'length', return.
262 * Pad the 'buffer' with 'padchar' until = 'length'
265 while((pos = strlen(buffer)) < length)
267 buffer[pos] = padchar;
268 buffer[pos+1] = '\0';
270 } /* end pad_with_char */
273 /*---------------------*/
274 /* lower_case_string */
275 /*---------------------*/
276 char* lower_case_string(char* inputbuf)
282 inputbuf[pos] = (char)tolower((unsigned char) inputbuf[pos]);
286 } /* lower_case_string */
289 /*---------------------*/
290 /* upper_case_string */
291 /*---------------------*/
292 char* upper_case_string(char* inputbuf)
298 inputbuf[pos] = (char)toupper((unsigned char) inputbuf[pos]);
302 } /* upper_case_string */
308 void strip_CR(char* buffer)
318 if(*bufferptr != '\r')
320 *newptr = *bufferptr;
330 /*------------------*/
332 /*------------------*/
333 /* THIS ROUTINE IS USED FOR DEBUGGING and will not be called in production */
334 void show_form_data(entry* entries)
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);
342 while(entries[x].name)
344 printf("<li> <code>%s = [%s]</code>%c",entries[x].name,
348 printf("</pre></ul>%c",10);
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)
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);
364 while(entries[x].name)
366 printf("<li> <code>%s = [%s]</code>%c",entries[x].name,
370 printf("</pre></ul>%c",10);
373 /*---------------------*/
374 /* display_html_text */
375 /*---------------------*/
376 /* display the text found in the indicated file */
377 void display_html_text(char* filename)
382 if((fp = fopen(filename, "r")) == NULL)
385 "<p><b>(OOPS... We are unable to open file ",
387 " for reading)</b><p>\n");
389 while(fgets(buffer, 256, fp) != NULL)
391 if(buffer[strlen(buffer) - 1] == '\n')
392 buffer[strlen(buffer) - 1] = '\0';
393 printf("%s\n", buffer);
397 } /* display_html_text */
400 /*-----------------*/
402 /*-----------------*/
403 /* this routine converts a string value and
404 * converts it to an integer.
406 long unformat_cost(char* cost)
414 /* Make sure a string was passed */
418 /* while in the string */
423 if(isdigit((unsigned char) *spos))
426 if(buf_siz++ == 98) /* make sure we don't overrun buf */
436 int digits_only(char* str)
443 if(!isdigit((unsigned char) *pos))
444 return(0); /* non-digit found */
453 /* return current year -> 0 thru 99 */
460 tptr = localtime(&t);
461 ret_val = tptr->tm_year;
468 /* return Month of current year -> 1 thru 12 */
475 tptr = localtime(&t);
476 ret_val = tptr->tm_mon;
483 /* return day of Month -> 1 thru 31 */
490 tptr = localtime(&t);
491 ret_val = tptr->tm_mday;
498 /* return hour of day -> 0 thru 23 */
505 tptr = localtime(&t);
506 ret_val = tptr->tm_hour;
513 /* return minute of day -> 0 thru 59 */
520 tptr = localtime(&t);
521 ret_val = tptr->tm_min;
528 /* return second of day -> 0 thru 59 */
535 tptr = localtime(&t);
536 ret_val = tptr->tm_sec;
540 /* end file 'util.c' */