Cgitxtdb

CGI 程式設計

第一個程式

環境變數

get 參數

post 參數

常用函數

參數解碼

範例:檔案編輯

Apache 安裝

訊息

相關網站

參考文獻

最新修改

簡體版

English

檔案:txtdb.c

#include <stdio.h>
#include <stdlib.h>

void replace(char *str, char cFrom, char cTo) {
    int x;
    for(x=0;str[x];x++) 
      if(str[x] == cFrom) 
        str[x] = cTo;
}

char x2c(char *what) {
    register char digit;
    digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 : (what[0] - '0'));
    digit *= 16;
    digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 : (what[1] - '0'));
    return(digit);
}

void unescape_url(char *url) {
    register int x,y;
    for(x=0,y=0;url[y];++x,++y) {
        if((url[x] = url[y]) == '%') {
//            url[x] = x2c(&url[y+1]);
            int c;
            sscanf(&url[y+1], "%2X", &c);
            url[x] = c;
            y+=2;
        }
    }
    url[x] = '\0';
    replace(url, '+', ' ');
}

#define MAX_PARAM_LEN 10000

void initParam(char *params) {
   char *get = getenv("QUERY_STRING");
   unescape_url(get);
//   printf(" <get>%s</get>\n", get);
   int length=atoi(getenv("CONTENT_LENGTH"));
//   printf(" <length>%d</length>\n", length);
   char format[20], post[MAX_PARAM_LEN];
   sprintf(format, "%%%dc", length);
   scanf(format, post);
   unescape_url(post);
   sprintf(params, "&%s&%s&", get, post);
//   printf(" <post>\n%s\n </post>\n", post);
}

char *getInner(char *params, char *innerText, int maxLen, char *beginMark, char *endMark) {
   char *beginPtr = strstr(params, beginMark);
   char *endPtr = strstr(beginPtr+1, endMark);
   char *innerBegin = beginPtr+strlen(beginMark);
   int len = endPtr-innerBegin;
   if (len < maxLen) {
     strncpy(innerText, innerBegin, endPtr-innerBegin);
     innerText[endPtr-innerBegin] = '\0';
     return innerText;
   } else
     return NULL;
}

void text2file(char *text, char *path) {
  FILE *file=fopen(path, "wt");
  if (file == NULL) {
    printf(" <error>text2file()</error>\n");
    return;
  }
  fprintf(file, "%s", text);
  fclose(file);
}

void file2text(char *text, char *path) {
  FILE *file=fopen(path, "rt");
  if (file == NULL) {
    strcpy(text, "");
    printf(" <error>file2text()</error>\n");
    return;
  }
  fscanf(file, "%[^\0]", text);
  fclose(file);
}

char *root = "/txtdb";

int main(int argc, char* argv[], char *envp[])
{
   char params[MAX_PARAM_LEN], op[10], path[100], text[MAX_PARAM_LEN], absPath[500];
   printf("Content-type: text/plain\n\n");
   printf("<txtdb>\n");
   initParam(params);
   getInner(params, op, 10, "&op=", "&");
   printf(" <op>%s</op>\n", op);
   getInner(params, path, 100, "&path=", "&");
   printf(" <path>%s</path>\n", path);
   sprintf(absPath, "%s/%s", root, path);
   if (strcmp(op, "put")==0) {
     getInner(params, text, MAX_PARAM_LEN, "&text=", "&");
     text2file(text, absPath);
   }
   else if (strcmp(op, "get")==0) {
     file2text(text, absPath);
   }
   printf(" <text>\n%s\n </text>\n", text);
   printf("</txtdb>\n");
}

檔案:txtdb.htm

<html>
<body>
<form action="/cgi-bin/txtdb.cgi" method="post">
op:<input type="text" name="op" value="put"/>
path:<input type="text" name="path" value="ccc/test"/>
text:
<textarea name="text" rows="30" cols="100">
http://ccckmit.wikidot.com/
 
+ 1
++ 1.1
+++ 1.1.1
+ 2
 
|| a || b ||
|| c || d ||
 
中文字串
 
</textarea>
<BR/>
<input type="submit" value="送出">
</form>
</body>
</html>

Facebook

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License