zoukankan      html  css  js  c++  java
  • 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]
    ", 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]='';
                    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 = '';
                            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
  • 相关阅读:
    问题:关于抛出例外的一个问题。
    向北京球迷致敬!!!
    [下载]高质量C++C编程指南
    WinCE.NET中播放声音
    WINCE.NET中程序只运行一次
    解决vs2003开发PDA(wince.net4.2)调试与部署问题
    WinCE.NET中设置系统日期时间
    网页上发送mail(PHP)
    点阵字库预览工具 V1.0.0
    WINCE.NET4.2下如何获取程序当前运行目录
  • 原文地址:https://www.cnblogs.com/unixshell/p/3519005.html
Copyright © 2011-2022 走看看