Chinaunix首页 | 论坛 | 博客
  • 博客访问: 343711
  • 博文数量: 60
  • 博客积分: 1570
  • 博客等级: 上尉
  • 技术积分: 620
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-02 23:37
文章分类

全部博文(60)

文章存档

2012年(2)

2010年(2)

2009年(56)

分类: LINUX

2009-11-10 00:16:11

一、getcwd函数说明:

getcwd函数获取的是当前的工作目录。下面以示例说明:

目录/home/liumin/program/getcwd/ 下程序main.c:(不包括任何错误处理)

 

#include
#include

int main(int argc, char *argv[])
{
        char buf[1024];

        printf("getcwd=%s\n", getcwd(buf, sizeof(buf)));
        printf("argv[0]=%s\n", argv[0]);

        return 0;
}

编译后生成执行文件:m

操作:

情况1. 在根目录 / 下执行:

[liumin@localhost /]$ /home/liumin/program/getcwd/m

输出结果:

getcwd=/
argv[0]=/home/liumin/program/getcwd/m

情况2. 在根目录 / 下执行:

[liumin@localhost /]$ ./home/liumin/program/getcwd/m

输出结果:

getcwd=/
argv[0]=./home/liumin/program/getcwd/m

情况3. 在当前目录下(/home/liumin/program/getcwd)执行

[liumin@localhost getcwd]$ ./m

输出结果:

getcwd=/home/liumin/program/getcwd
argv[0]=./m

情况4. 在目录:/home/liumin/test下执行shell脚本中调用m

[liumin@localhost test]$ ./test.sh

输出结果:
getcwd=/home/liumin/test
argv[0]=/home/liumin/program/getcwd/m

test.sh在目录 /home/liumin/test下,其内容为:

#!/bin/sh
        /home/liumin/program/getcwd/m

 

二、目的:

无论如何执行,都需要得到结果:/home/liumin/program/getcwd/m

三、思路:getcwd + argv[0] + 一些小处理。

四、示例代码:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.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

/* also return allocated size, if nonnull*/
char *path_alloc(unsigned int *sizep)
{
    char *ptr;
    unsigned int size;

    if (posix_version == 0)
        posix_version = sysconf(_SC_VERSION);
    
    if (pathmax == 0) { /* first time through */
        errno = 0;
        if ((pathmax = pathconf("/", _PC_PATH_MAX)) < 0) {
            if (errno == 0)
                pathmax = PATH_MAX_GUESS; /* it's indeterminate */
            else {
                perror("pathconf error for _PC_PATH_MAX!");
                return NULL;
            }
        } 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) {
        perror("malloc error for pathname!");
        return NULL;
    }

    if (sizep != NULL)
        *sizep = size;
    return ptr;
}

char *get_absolute_name(const char *argv0, char *buf, size_t size)
{
    unsigned int len;
    char *pathbuf;
    unsigned int buflen;
    const char *index;

    if (!buf) {
        errno = EFAULT;
        return NULL;
    }

    pathbuf = path_alloc(&buflen);
    if (pathbuf == NULL)
        return NULL;        

    if (getcwd(pathbuf, buflen) == NULL) {
        perror("getcwd error!");

        free(pathbuf);
        return NULL;
    }

    if (*argv0 == '/' && len != 1) {    
        if (strlen(argv0) > size) {
            errno = ERANGE;

            free(pathbuf);
            return NULL;
        }
        else {
            strncpy(buf, argv0, strlen(argv0));
            free(pathbuf);

            return buf;
        }
    }

    /* getcwd + argv[0] */
    len = strlen(pathbuf);
    if(pathbuf[len - 1] != '/')
        pathbuf[len] = '/';
    index = argv0;
    while ((*index == '.') || (*index == '/'))
        index++;

    if (strlen(pathbuf) + strlen(index) > size) {
        errno = ERANGE;

        free(pathbuf);
        return NULL;
    } else {
        sprintf(buf, "%s", pathbuf);
        strcat(buf, index);
    }
    free(pathbuf);
    return buf;
}


int main(int argc, char *argv[])
{
    char buf[1024];
    int ret;
    char *p;

    p = get_absolute_name(argv[0], buf, sizeof(buf));
    if(p != NULL) {
        printf("cwd=%s\n", buf);
    }
    else
        perror("getcwd");

    exit(0);
}

 

参考资料:

apue2 程序清单2-3
 17 楼思路:getcwd + argv[0] + 一些小处理。


 


阅读(1973) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~