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
  • 相关阅读:
    Linux操作系统 (RHEL 7/CentOS 7)
    (OK) 运行cBPM in Fedora23
    (OK) 运行cBPM—Fedora23
    (OK) Install codeblocks_16.01 on Fedora23
    (OK) Fedora23——Docker——CORE—testing
    (OK) CORE nodes access Internet—虚拟节点访问互联网—commands
    (OK-half) Fedora23——Docker——CORE—testing
    (OK) running imunes in Fedora 23
    (OK-HALF) To Find a Rogue DHCP Server—tcpdump/dhclient—nmap
    (OK) shell script—Find a Rogue DHCP Server—tcpdump/dhclient—nmap
  • 原文地址:https://www.cnblogs.com/unixshell/p/3519005.html
Copyright © 2011-2022 走看看