http server v0.1_http_webapp.c
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <errno.h> /*----------------------------------------------------------------- functionname: file_exist param: NA return: NA author: xxxx check if file is exist in webapp history: create by xxxx, 2014.1.08, add simple abilities -------------------------------------------------------------*/ int file_exist(char* url) { int ret; int len = 0; char* path= NULL; if(url == NULL) return -1; len = strlen(url); if(len <= 0) return -1; path = (char*)malloc(len); strncpy(path, url, len); printf("[%s]\n", path); // remove blanks on head and tailof url ret = access(path, R_OK); if(ret == -1) perror("uri not exist"); free(path); return ret; } long get_file_size(FILE* fs) { if(fs == NULL) return -1; fseek(fs, 0, SEEK_END); return ftell(fs); } char *itoa(int num, char *str, int radix) { //0的情况 if (num==0) { str[0]=‘0‘; str[1]=‘\0‘; return str; } char string[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; char* ptr = str; int i; int j; while (num) { *ptr++ = string[num % radix]; num /= radix; if (num < radix) { *ptr++ = string[num]; *ptr = ‘\0‘; break; } } //两边对调 j = ptr - str - 1; for (i = 0; i < (ptr - str) / 2; i++) { int temp = str[i]; str[i] = str[j]; str[j--] = temp; } return str; } int io_write(int fd, void *usrbuf, int n) { int nleft = n; int nwritten; char *bufp = usrbuf; while (nleft > 0) { if ((nwritten = write(fd, bufp, nleft)) <= 0) { if (errno == EINTR) /* interrupted by sig handler return */ nwritten = 0; /* and call write() again */ else return -1; /* errorno set by write() */ } nleft -= nwritten; bufp += nwritten; } return n; }
#ifndef __HTTP_WEBAPP_H #define __HTTP_WEBAPP_H int file_exist(char* url); long get_file_size(FILE* fs); int io_write(int fd, void *usrbuf, int n); char *itoa(int num, char *str, int radix); #endif
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。