Linux的path_alloc函数

#include "apue.h"
#include <errno.h>
#include <limits.h>
#ifdef   PATH_MAX
static int   pathmax = PATH_MAX;
#else
static int   pathmax = 0;
#endif
#define SUSV3 200112L
static long posix_version = 0;
/* If PATH_MAX is indeterminate, no guarantee this is adequate */
#define PATH_MAX_GUESS   1024
char *
path_alloc(int *sizep) /* also return allocated size, if nonnull */
{
char *ptr;
int size;
if (posix_version == 0)
       posix_version = sysconf(_SC_VERSION);
if (pathmax == 0) {     /* first time through */
       errno = 0;       /* From: http://www.bianceng.cn */
       if ((pathmax = pathconf("/", _PC_PATH_MAX)) < 0) {
         if (errno == 0)
            pathmax = PATH_MAX_GUESS; /* it's indeterminate */
         else
            err_sys("pathconf error for _PC_PATH_MAX");
       } else {
         pathmax++;    /* add one since it's relative to root */
       }
}
if (posix_version < SUSV3)
       size = pathmax + 1;
else
       size = pathmax;
if ((ptr = malloc(size)) == NULL)
       err_sys("malloc error for pathname");
if (sizep != NULL)
       *sizep = size;
return(ptr);
} 

或
char*path_alloc(int* size)
{
char *p = NULL;
if(!size) return NULL;
p = malloc(256);
if(p)
*size = 256;
else
*size = 0;
return p;
}

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索include
, return
, posix
, null
, if
, size
else
linux realpath函数、path alloc、linux fallocate、linux idr alloc、linux alloc,以便于您获取更多的相关知识。

时间: 2024-08-01 08:49:27

Linux的path_alloc函数的相关文章

Linux中system函数的实现

#include <errno.h> #include <signal.h> #include <unistd.h> int system(const char *cmdstring) /* with appropriate signal handling */ { pid_t pid; int status; struct sigaction ignore, saveintr, savequit; sigset_t chldmask, savemask; if (cm

linux c clock函数持续时间一直为0;

问题描述 linux c clock函数持续时间一直为0: #include "chat.h" int main (void) { char ch; long i = 10000000; clock_t st,ed; double duration; st=clock(); while (1) { ch = getchar(); if(ch=='y') break; } ed = clock(); duration = (double)(ed - st) / CLOCKS_PER_SE

PHP执行linux命令常用函数汇总_php实例

一般情况下,很少会用php去执行linux命令,不过特殊情况下,你也许会用到这些函数.以前我知道有二个函数可以执行linux命令,一个是exec,一个是shell_exec.其实有很多的,结合手册内容,介绍以下6个函数. 1,exec函数 <?php $test = "ls /tmp/test"; //ls是linux下的查目录,文件的命令 exec($test,$array); //执行命令 print_r($array); ?> 返回结果如下: [root@krlcgc

linux c fcntl函数返回值

问题描述 linux c fcntl函数返回值 if((val = fcntl(fd,F_GETFL,0))<0) ....... val |= O_NONBLOCK; if(fcntl(fd,F_SETFL,val)<0) ....... 设置非阻塞. 第三行为什么能这样设,val不是反回文件描述符fd么? 解决方案 fcntl函数原型为:int fcntl(int fd, int cmd, long arg); 其中cmd支持的命令有多种,其中设置文件描述词的状态,可选的类型有O_APPE

linux驱动-关于Linux驱动probe函数以及detect函数不被调用的问题

问题描述 关于Linux驱动probe函数以及detect函数不被调用的问题 我在写一个驱动,在init里面向i2c总线注册了一个结构体,结构体中包含了probe和detect以及remove系列函数,最后能打印成功注册的信息,但是probe和detect函数中printk("===%s===",-FUNCTION-)打印信息却没有打印,这几个函数里面只有一句打印,其余的还没有写上去,这是不是probe和detect都没有被调用啊,问题出在哪? 解决方案 init是肯定会执行的,然后i

Linux中mkdir函数与Windows中_mkdir函数的区别_Linux

下面先来给大家介绍windows下_mkdir函数 复制代码 代码如下: #include<direct.h> int _mkdir( const char *dirname ); 参数: dirname是目录的路径名指针 返回值: 如果新目录的创建时间,这些功能中的每一个返回值 0. 在错误,则函数返回 – 1 linux下mkdir函数mode_t参数详解 复制代码 代码如下: #include <sys/stat.h> int mkdir(const char *path,

Linux中fork()函数实例分析_Linux

一.fork 入门知识  一个进程,包括代码.数据和分配给进程的资源.fork()函数通过系统调用创建一个与原来进程几乎完全相同的进程,也就是两个进程可以做完全相同的事,但如果初始参数或者传入的变量不同,两个进程也可以做不同的事.  一个进程调用fork()函数后,系统先给新的进程分配资源,例如存储数据和代码的空间.然后把原来的进程的所有值都复制到新的新进程中,只有少数值与原来的进程的值不同.相当于克隆了一个自己.  我们来看一个例子: /* * fork_test.c * version 1

Linux C 创建目录函数mkdir相关【转】

转自:http://blog.csdn.net/fallenink/article/details/8480483 原文地址:http://sharp2wing.iteye.com/blog/1280802 ------------------------------------------------ I.Linux C 创建目录函数mkdir的mode设置问题  函数原型:  #include <sys/stat.h>  int mkdir(const char *path, mode_t

知识点查缺补漏贴02:Linux环境fork()函数详解

引言 先来看一段代码吧, #include <sys/types.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> int main(int argc, char **argv) { pid_t pid = fork(); if(pid < 0){ fprintf(stderr,"error!"); }else if(0 == pid){ fprintf(st