一些c函数

来源:互联网 发布:git 小乌龟 mac 编辑:程序博客网 时间:2024/05/29 10:20
1、strtok_r是linux平台下的strtok函数的线程安全版


char *strtok_r(char *s, const char *delim, char **save_ptr) {  
    char *token;  
  
    if (s == NULL) s = *save_ptr;  
  
    /* Scan leading delimiters.  */  
    s += strspn(s, delim);  
    if (*s == '/0')   
        return NULL;  
  
    /* Find the end of the token.  */  
    token = s;  
    s = strpbrk(token, delim);  
    if (s == NULL)  
        /* This token finishes the string.  */  
        *save_ptr = strchr(token, '/0');  
    else {  
        /* Terminate the token and make *SAVE_PTR point past it.  */  
        *s = '/0';  
        *save_ptr = s + 1;  
    }  
  
    return token;  



2、size_t strspn (const char *s,const char * accept);
函数说明 strspn()从参数s 字符串的开头计算连续的字符,而这些字符都完全是accept 所指字符串中的字符。简单的说,若strspn()返回的数值为n,则代表字符串s 开头连续有n 个字符都是属于字符串accept内的字符。


3、strpbrk
在源字符串(s1)中找出最先含有搜索字符串(s2)中任一字符的位置并返回,若找不到则返回空指针。


4、网路编程 设置SO_REUSEADDR、SO_KEEPALIVE、SO_LINGER、TCP_NODELAY在创建socket之后,bind之前


5、setbuf:是linux中的C函数,主要用于打开和关闭缓冲机制。
6、strtoul:将参数nptr字符串根据参数base来转换成无符号的长整型数。
7、isspace:主要用于检查参数c是否为空格字符。


8、后台执行函数:
int daemonize(int nochdir, int noclose)
{
    int fd;


    switch (fork()) 
 {
    case -1:
        return (-1);
    case 0:
        break;
    default:
        _exit(EXIT_SUCCESS);
    }


    if (setsid() == -1)
        return (-1);


    if (nochdir == 0) 
   {
        if(chdir("/") != 0) 
      {
            perror("chdir");
            return (-1);
        }
    }


    if (noclose == 0 && (fd = open("/dev/null", O_RDWR, 0)) != -1) 
   {
        if(dup2(fd, STDIN_FILENO) < 0) 
       {
            perror("dup2 stdin");
            return (-1);
        }
        if(dup2(fd, STDOUT_FILENO) < 0) 
       {
            perror("dup2 stdout");
            return (-1);
        }
        if(dup2(fd, STDERR_FILENO) < 0) 
       {
            perror("dup2 stderr");
            return (-1);
        }


        if (fd > STDERR_FILENO) 
       {
            if(close(fd) < 0) 
           {
                perror("close");
                return (-1);
            }
        }
    }
    return (0);
}

0 0
原创粉丝点击