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
  • 相关阅读:
    BZOJ 3531[Sdoi2014]旅行
    BZOJ4998 星球联盟
    BZOJ2959 长跑
    【北京集训D2T3】tvt
    [Bzoj]5343: [Ctsc2018]混合果汁
    HGOI20190810 省常中互测3
    HGOI20190809 省常中互测2
    HGOI20190808 省常中互测1
    组合排列和组合数 学习笔记
    2-SAT (two-statisfiability) 算法 学习笔记
  • 原文地址:https://www.cnblogs.com/unixshell/p/3519005.html
Copyright © 2011-2022 走看看