home *** CD-ROM | disk | FTP | other *** search
/ The HTML Web Publisher's Construction Kit / HTMLWPCK.ISO / unix / cgi / c_src / util.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-27  |  2.8 KB  |  259 lines

  1. /* util.c standard utility functions for parsing encoded       
  2.  
  3.   strings, and other related CGI tasks. */
  4.  
  5. #include <stdio.h>
  6.  
  7. #define LF 10
  8.  
  9. #define CR 13
  10.  
  11. /* getword separates two words separated by the "stop" 
  12.  
  13.   character */
  14.  
  15. void getword(char *word, char *line, char stop) {
  16.  
  17.    int x = 0,y;
  18.  
  19.  
  20.  
  21.    for(x=0;((line[x]) && (line[x] != stop));x++)
  22.  
  23.        word[x] = line[x];
  24.  
  25.  
  26.  
  27.    word[x] = '\0';
  28.  
  29.    if(line[x]) ++x;
  30.  
  31.    y=0;
  32.  
  33.  
  34.  
  35.    while(line[y++] = line[x++]);
  36.  
  37. }
  38.  
  39. char *makeword(char *line, char stop) {
  40.  
  41.    int x = 0,y;
  42.  
  43.    char *word = (char *) malloc(sizeof(char) * (strlen(line) + 1));
  44.  
  45.  
  46.  
  47.    for(x=0;((line[x]) && (line[x] != stop));x++)
  48.  
  49.        word[x] = line[x];
  50.  
  51.  
  52.  
  53.    word[x] = '\0';
  54.  
  55.    if(line[x]) ++x;
  56.  
  57.    y=0;
  58.  
  59.  
  60.  
  61.    while(line[y++] = line[x++]);
  62.  
  63.    return word;
  64.  
  65. }
  66.  
  67. char *fmakeword(FILE *f, char stop, int *cl) {
  68.  
  69.    int wsize;
  70.  
  71.    char *word;
  72.  
  73.    int ll;
  74.  
  75.  
  76.  
  77.    wsize = 102400;
  78.  
  79.    ll=0;
  80.  
  81.    word = (char *) malloc(sizeof(char) * (wsize + 1));
  82.  
  83.  
  84.  
  85.    while(1) {
  86.  
  87.        word[ll] = (char)fgetc(f);
  88.  
  89.        if(ll==wsize) {
  90.  
  91.            word[ll+1] = '\0';
  92.  
  93.            wsize+=102400;
  94.  
  95.            word = (char *)realloc(word,sizeof(char)*(wsize+1));
  96.  
  97.        }
  98.  
  99.        --(*cl);
  100.  
  101.        if((word[ll] == stop) || (feof(f)) || (!(*cl))) {
  102.  
  103.            if(word[ll] != stop) ll++;
  104.  
  105.            word[ll] = '\0';
  106.  
  107.            return word;
  108.  
  109.        }
  110.  
  111.        ++ll;
  112.  
  113.    }
  114.  
  115. }
  116.  
  117. char x2c(char *what) {
  118.  
  119.    register char digit;
  120.  
  121.  
  122.  
  123.    digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 : (what[0] - 
  124.  
  125. '0'));
  126.  
  127.    digit *= 16;
  128.  
  129.    digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 : (what[1] 
  130.  
  131. - '0'));
  132.  
  133.    return(digit);
  134.  
  135. }
  136.  
  137. void unescape_url(char *url) {
  138.  
  139.    register int x,y;
  140.  
  141.  
  142.  
  143.    for(x=0,y=0;url[y];++x,++y) {
  144.  
  145.        if((url[x] = url[y]) == '%') {
  146.  
  147.            url[x] = x2c(&url[y+1]);
  148.  
  149.            y+=2;
  150.  
  151.        }
  152.  
  153.    }
  154.  
  155.    url[x] = '\0';
  156.  
  157. }
  158.  
  159. /* plustospace turns the ╘+╒ characters into spaces
  160.  
  161.   in an encoded string */
  162.  
  163. void plustospace(char *str) {
  164.  
  165.    register int x;
  166.  
  167.  
  168.  
  169.    for(x=0;str[x];x++) if(str[x] == '+') str[x] = ' ';
  170.  
  171. }
  172.  
  173. /* plusto_ is similar to plustospace, but replaces the ╘+╒
  174.  
  175.   with an underscore character */
  176.  
  177. void plusto_(char *str) {
  178.  
  179.    register int x;
  180.  
  181.  
  182.  
  183.    for(x=0;str[x];x++) if(str[x] == '+') str[x] = '_';
  184.  
  185. }
  186.  
  187.  
  188.  
  189. int rind(char *s, char c) {
  190.  
  191.    register int x;
  192.  
  193.    for(x=strlen(s) - 1;x != -1; x--)
  194.  
  195.        if(s[x] == c) return x;
  196.  
  197.    return -1;
  198.  
  199. }
  200.  
  201. int getline(char *s, int n, FILE *f) {
  202.  
  203.    register int i=0;
  204.  
  205.  
  206.  
  207.    while(1) {
  208.  
  209.        s[i] = (char)fgetc(f);
  210.  
  211.  
  212.  
  213.        if(s[i] == CR)
  214.  
  215.            s[i] = fgetc(f);
  216.  
  217.  
  218.  
  219.        if((s[i] == 0x4) || (s[i] == LF) || (i == (n-1))) {
  220.  
  221.            s[i] = '\0';
  222.  
  223.            return (feof(f) ? 1 : 0);
  224.  
  225.        }
  226.  
  227.        ++i;
  228.  
  229.    }
  230.  
  231. }
  232.  
  233. void send_fd(FILE *f, FILE *fd)
  234.  
  235. {
  236.  
  237.    int num_chars=0;
  238.  
  239.    char c;
  240.  
  241.  
  242.  
  243.    while (1) {
  244.  
  245.        c = fgetc(f);
  246.  
  247.        if(feof(f))
  248.  
  249.            return;
  250.  
  251.        fputc(c,fd);
  252.  
  253.    }
  254.  
  255. }
  256.  
  257.  
  258.  
  259.