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
分类:
GNU, web server
· 复杂业务系统线上问题排查过程
· 通过抓包,深入揭秘MCP协议底层通信
· 记一次.NET MAUI项目中绑定Android库实现硬件控制的开发经历
· 糊涂啊!这个需求居然没想到用时间轮来解决
· 浅谈为什么我讨厌分布式事务
· 那些年我们一起追过的Java技术,现在真的别再追了!
· 还在手写JSON调教大模型?.NET 9有新玩法
· 为大模型 MCP Code Interpreter 而生:C# Runner 开源发布
· 面试时该如何做好自我介绍呢?附带介绍样板示例!!!
· JavaScript 编年史:探索前端界巨变的幕后推手