cgi_functions.c
来自「这是我自己写的用于嵌入式设备的CGI程序」· C语言 代码 · 共 1,932 行 · 第 1/4 页
C
1,932 行
#include <sys/types.h>#include <stdlib.h>#include <stdio.h>#include <string.h>#include <unistd.h>#include <fcntl.h>#include <errno.h>#include <sys/time.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <sys/ioctl.h>#include <sys/timeb.h>#include <sys/msg.h>#include <dirent.h>#include <sys/stat.h>#include <linux/videodev.h>#include <linux/fb.h>#include <sys/mman.h>#include <stddef.h>#include <ctype.h>#include <sys/ioctl.h>#include <sys/ipc.h>#include <pthread.h>#include<sys/wait.h>#include <signal.h>#include <time.h>#include <sys/shm.h>#include "cgi_config.h"#include "cgic.h"#define SERVER_NAME cgiServerName/*--------------------------------------------------------------------------* * 这个函数读取一个模版文件到缓冲区中,并返回缓冲区指针 * * @param const char* template_filename 模版文件名 * * @return char* 缓冲区指针,如果读取模版文件失败,则返回 NULL *--------------------------------------------------------------------------*//*char* read_template(const char* template_filename){ FILE* fp = NULL; size_t block_size = 1024; size_t buffer_size = block_size; size_t length = 0; size_t read_size = 0; char* buffer = malloc(buffer_size); char* tmp_buffer = buffer; fp = fopen(template_filename, "r"); if (fp != NULL) { memset(buffer, 0, buffer_size); while ((read_size = fread(tmp_buffer, 1, block_size, fp)) != 0) { length += read_size; if (length >= buffer_size) { // 如果读取的文件长度已经达到了缓冲区的长度,则需要扩展缓冲区 buffer_size += block_size; tmp_buffer = (char*)malloc(buffer_size); memset(tmp_buffer, 0, buffer_size); memcpy(tmp_buffer, buffer, length); free(buffer); buffer = tmp_buffer; tmp_buffer = buffer + length; } else { break; } } fclose(fp); } return buffer;}*//*---------------------------------------------------------------------------*//Michael写的替代函数 *---------------------------------------------------------------------------*/char* read_template(const char* template_filename){ struct stat statbuf; int FileSize; FILE* f; char* buf = NULL; int i = stat(template_filename, &statbuf); if(i<0) return NULL; S_ISDIR(statbuf.st_mode); S_ISREG(statbuf.st_mode); FileSize = statbuf.st_size; if(FileSize<=0) return NULL; buf = (char*)malloc(FileSize+1); if(!buf) return NULL; f = fopen(template_filename, "r+"); if(fread(buf, 1, FileSize, f) == 0) { free(buf); fclose(f); return NULL; } else { buf[FileSize]='\0'; fclose(f); return buf; }}/*--------------------------------------------------------------------------*//*--------------------------------------------------------------------------* * 在模版内容缓冲区中使用正则表达式进行替换 * * 由于替换结果有可能超过缓冲区长度,因此 template_content 参数以指针的地址形式传递。 * * @param char** template_content 模版缓冲区指针的地址 * @param REPLACEABLE_TAG_LIST tag_list 要替换的标记列表 * @param int tag_count 标记列表包含的项目总数 * * @return int 成功替换的标记 *--------------------------------------------------------------------------*/int parse_template(char** template_content, REPLACEABLE_TAG_LIST tag_list, int tag_count){ char* buffer = *template_content; char* tmp_buffer = NULL; char* pos = buffer; size_t tag_length = 0; size_t value_length = 0; size_t length = 0; size_t buffer_size = 0; int count = 0; while (tag_count) { do { tag_length = strlen(tag_list[tag_count - 1].tag); value_length = strlen(tag_list[tag_count - 1].value); if (tag_length == 0) { break; } pos = strstr(buffer, tag_list[tag_count - 1].tag); if (pos == NULL) { break; } if (value_length <= tag_length) { // 不需要重新分配内存 length = strlen(pos) - tag_length; strcpy(pos, tag_list[tag_count - 1].value); memmove(pos + value_length, pos + tag_length, length * sizeof(char) + 1); pos += value_length; break; } // 需要重新分配内存 // 计算新缓冲的大小并分配内存 length = strlen(buffer); buffer_size = (length + (value_length - tag_length) + 1) * sizeof(char); tmp_buffer = (char*)malloc(buffer_size); memset(tmp_buffer, 0, buffer_size); // 构造新字符串 length = pos - buffer; strncpy(tmp_buffer, buffer, length); strcpy(tmp_buffer + length, tag_list[tag_count - 1].value); length += value_length; strcpy(tmp_buffer + length, pos + tag_length); // 释放原有的内存 free(buffer); buffer = tmp_buffer; } while (0); tag_count--; } *template_content = buffer; return count;}/*--------------------------------------------------------------------------*//////////////////////////////////////////////////////////////////////////////Str请申明为静态字符数组//Writed by Michaelint StringReplace(char* Str, char* OldStr, char* NewStr){ static char Dst[1024*100]; int OldLen; int NewLen; int StrLen; int PosLen; int DstLen; char* Pos; if(!Str) return 0; if(!OldStr) return 0; if(!NewStr) return 0; OldLen = strlen(OldStr); NewLen = strlen(NewStr); StrLen = strlen(Str); PosLen = 0; Pos = strstr(Str, OldStr); if(Pos == NULL) return 0; PosLen = strlen(Pos); memcpy(Dst,Pos+OldLen, PosLen-OldLen); DstLen=StrLen+NewLen-OldLen; memcpy(Str+StrLen-PosLen,NewStr,NewLen); memcpy(Str+StrLen-PosLen+NewLen, Dst, PosLen-OldLen); Str[DstLen]='\0'; return DstLen;}typedef enum{false,true}bool;//-------------------------------------------------------------------------FILE* FileCreate(char* FileName){ return fopen(FileName, "w+b");}//-------------------------------------------------------------------------FILE* FileOpen(char* FileName){ return fopen(FileName, "r+");}//-------------------------------------------------------------------------bool FileClose(FILE* FileHandle){ if (fclose(FileHandle) == 0) return false; else return true;}//-------------------------------------------------------------------------bool FileWrite(FILE* FileHandle, void* Buf, int Len){ if (fwrite(Buf, Len, 1, FileHandle) == 0) return false; else return true;}//-------------------------------------------------------------------------bool FileRead(FILE* FileHandle, void* Buf, int Len){ if (fread(Buf,Len,1,FileHandle) == 0) return false; else return true;}//-------------------------------------------------------------------------bool FileSeek(FILE* FileHandle, int Offset, int Origin){ if (fseek(FileHandle, Offset, Origin) == 0) return false; else return true;}//-------------------------------------------------------------------------bool FileEof(FILE* FileHandle){ return feof(FileHandle);}int GetFileSize(char* FileName){ struct stat statbuf; int i=stat (FileName, &statbuf); if (i<0) return -1; S_ISDIR (statbuf.st_mode); S_ISREG (statbuf.st_mode); return statbuf.st_size;}//-------------------------------------------------------------------------/*--------------------------------------------------------------------------* 将文件记录(结构)存入文件 *--------------------------------------------------------------------------*/int save_to_file(void * conf_var, int varsize, char * conf_path){ FILE * cfgfile; if ((cfgfile = fopen(conf_path, "w+")) == NULL) { fputs("Can't open cfg file,plese check it!\n",stderr); return -1; } fwrite(conf_var,varsize,1,cfgfile); fclose(cfgfile); return 0;}/*--------------------------------------------------------------------------*//*--------------------------------------------------------------------------* 将文件记录(结构)追加存入文件 *--------------------------------------------------------------------------*/int add_to_file(void * conf_var, int varsize, char * conf_path){ FILE * cfgfile; if ((cfgfile = fopen(conf_path, "a+")) == NULL) { fputs("Can't open cfg file,plese check it!\n",stderr); return -1; } fwrite(conf_var,varsize,1,cfgfile); fclose(cfgfile); return 0;}/*--------------------------------------------------------------------------*//*--------------------------------------------------------------------------* 将文件记录读出到结构变量,record_no是记录的序号,返回-1则文件打开出错,返回-2则读取出错 *--------------------------------------------------------------------------*/int get_from_file(void * conf_var, int varsize, int record_no, char * conf_path){ FILE * cfgfile; size_t read_t; if ((cfgfile = fopen(conf_path, "r")) == NULL) { fputs("Can't open cfg file,plese check it!\n",stderr); return -1; } fseek(cfgfile, (long)((record_no-1)*varsize), SEEK_CUR); read_t = fread(conf_var,varsize,1,cfgfile); fclose(cfgfile); if ( read_t < 1 ) return -2; return 0;}/*--------------------------------------------------------------------------*//*--------------------------------------------------------------------------* 在文件中更新某记录,record_no是记录的序号,返回-1则文件打开出错,返回-2则读取出错 *--------------------------------------------------------------------------*/int update_record(void * record_var, int varsize, int record_no, char * conf_path){ FILE *fp; byte *r_tmp, *w_tmp; long size; int i, count; fp = fopen( conf_path, "r" ); if (fp==NULL) return -1; //文件不存在或读取错误 fseek( fp, 0L, SEEK_END ); size=ftell(fp); count = (int) (size / varsize); //记录数 r_tmp = (byte *)malloc( size * sizeof(byte)); rewind(fp); for (i=0;i<count;i++) { fread(r_tmp, varsize, 1, fp); r_tmp = r_tmp+varsize; } r_tmp = r_tmp - size; w_tmp = r_tmp; fclose (fp); r_tmp = r_tmp + (record_no -1)*varsize; memcpy ( r_tmp, record_var, varsize ); r_tmp = w_tmp; fp = fopen( conf_path, "w" ); if (fp==NULL) return -1; fwrite(r_tmp, size, 1, fp); fclose(fp); return 0;}/*--------------------------------------------------------------------------*//*--------------------------------------------------------------------------* 在文件中删除某记录,record_no是记录的序号,返回-1则文件打开出错,返回-2则删除时重写出错,返回-3则记录编号不存在 *--------------------------------------------------------------------------*/int delete_record(int varsize, int record_no, char * conf_path){ FILE *fp; byte *r_tmp, *w_tmp; long size; int i, count; fp = fopen( conf_path, "r" ); if (fp==NULL) return -1; //文件不存在或读取错误 fseek( fp, 0L, SEEK_END ); size=ftell(fp); count = (int) (size / varsize); //记录数 if (record_no <1 || record_no>count ) return -3; r_tmp = (byte *)malloc( size * sizeof(byte)); w_tmp = r_tmp; rewind(fp); for (i=0;i<count;i++) { fread(r_tmp, varsize, 1, fp); if (i != (record_no -1)) r_tmp = r_tmp+varsize; } fclose (fp); fp = fopen( conf_path, "w" ); if (fp==NULL) return -1; r_tmp = w_tmp; fwrite(r_tmp, size-varsize, 1, fp); fclose(fp); return 0;}/*--------------------------------------------------------------------------*//*--------------------------------------------------------------------------* 将文件记录读出到结构数组,num是记录数, 函数返回一个指向数组的指针,如果记录为空,指针被置为NULL *--------------------------------------------------------------------------*/void* read_records(int varsize, int * num, char * conf_path){ FILE * fp; long size; void *var_list_p; int i, count; fp = fopen( conf_path, "r" ); if (fp==NULL) return NULL; fseek( fp, 0L, SEEK_END ); size=ftell(fp); count = (int) (size / varsize); *num = count; //printf("num:%d\n",*num); if (num==0) return NULL; else { var_list_p = (byte **)malloc( count * varsize ); rewind(fp); for (i=0;i<count;i++) { fread(var_list_p, varsize, 1, fp); var_list_p=var_list_p+varsize; } fclose(fp); } var_list_p = var_list_p - size; return var_list_p;}/*--------------------------------------------------------------------------*//*--------------------------------------------------------------------------* 添加用户帐号,返回0添加PK,返回-1用户帐号已存在,返回-2读写错误,返回-3达到用户数量最大限制 *--------------------------------------------------------------------------*/int add_user(USER_CONF user_add, char * conf_path){ USER_CONF * users_list; int i, users_num; users_list = read_records(sizeof(USER_CONF), &users_num, conf_path);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?