📄 program-get-current-dir.html
字号:
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="Author" content="Edward Fu">
<meta name="GENERATOR" content="Mozilla/4.05 [zh-CN] (X11; I; Linux 2.1.127 i686) [Netscape]">
<title>Freesoft Linux FAQ -- 如何在编程时得到程序本身所在目录</title>
</head>
<body>
Edward Fu wrote:
<p>> 请问用什么函数可以得到程序本身所在的目录?
<br>> 比如我在/root下执行/usr/local/bin/myprogram,用什么函数得到
<br>> '/usr/local/bin'目录呢?
<br>> 总不能让我取调用type命令去查吧。 :-(
<br>>
<br>> 先谢了。
<br>> 伏建军
<p>伏先生:
<p>我给你写了个函数,不知合不合你的意?源代码和例子在附件中。
<p>宫敏
<p>--
<br>----
<br>NT=No Thanks, WWW=World Wide Wait
<br>Does PnP mean "Plug and Pray"?
<p>/* getprgdir.c
<br> *
<br> * char *GetPrgDir(char *Cmd, char *DirBuf, LenLimit)
<br> * Get the directory of a program.
<br> *
<br> * Author: Min Gong
<br> *
<br> * History:
<br> *
<br> * Jun/3/1998 1, Added the Pointer checking for the DirBuf
parameter.
<br> * 2, Fixed memory leak. (forgot to free up the allocated
<br> * memory) Clean up.
<br> * 3, Added one more condition checking. For the current
<br> * directory it now returns the
absolute path instead
<br> * of "." is this more correct?
<br> * 4, The current directory is the one when the program
<br> * started. This should not be affected
by chdir() calls.
<br> *
<br> * Please tell me, if some one called putenv() or
setenv()
<br> * before calling me, how should I handle?
<br> *
<br> * Jun/2/1998 Created.
<br> *
<br> * Copyright (C) Min Gong 1998.
<br> *
<br> * Licensing: GPL V2
<br> *
<br> * A Copy of GPL V2 can be find at:
<br> * http://freesoft.cei.gov.cn/gpl-asc.txt
<br> *
<br> * A Chinese translation of GPL V2 can be find at:
<br> * http://freesoft.cei.gov.cn/gpl-gb.txt
<br> *
<br> * Note 1:
<br> * If you include this code in your program then LGPL is
applied.
<br> *
<br> * Note 2:
<br> * This code takes anti buffer overflow attack into consideration.
<br> *
<br> *
<br> * Bug report: min@freesoft.cei.gov.cn
<br> * site:
<br> * ftp://freesoft.cei.gov.cn/pub/freesoft.sic/freesoft/Linux/getprgdir.c
<br> *
<br> */
<p>#include <string.h>
<br>#include <stdlib.h>
<br>#include <unistd.h>
<br>#include <stdio.h>
<p>#define MAX_PATHLEN 1024
<p>char *GetPrgDir(char *Cmd, char *DirBuf, int LenLimit){
<br> int PathLen, OK;
<br> char *Ptmp, tmp, *PrgPtr, *Buf, *Path, *EnvPath, *PWD;
<p> if(Cmd==NULL || DirBuf==NULL) return NULL;
<br> PWD = getenv("PWD");
<br> Buf=malloc(MAX_PATHLEN);
<br> if(! (PrgPtr=strrchr(Cmd, '/'))){/* The Cmd is aprogram name,
not a path name
<br> Check $PATH is needed */
<br> EnvPath=getenv("PATH");
<br> if(EnvPath==NULL){ /* PATH is not set
*/
<br> free(Buf);
<br> return NULL; /* Hmm, this is ridiculous!
*/
<br> }
<br> PathLen=strlen(EnvPath);
<br> Path=malloc(PathLen);
<br> bcopy(EnvPath, Path, PathLen);
<br> Ptmp=Path;
<br> OK = 0;
<br> while(*Ptmp != (char)0 && !OK){
<br> PathLen=0;
<br> while(*Ptmp != ':' && *Ptmp
!= (char)0){
<br> PathLen++; Ptmp++;
<br> }
<br> if(PathLen+strlen(Cmd) > (MAX_PATHLEN-2)){
<br> free(Buf); /* Although heap is a bit safer, but I check it */
<br> free(Path);
<br> return NULL; /* anyway to protect the heap. No buffer overflow
<br> attack! */
<br> }
<br> tmp=*Ptmp;
<br> *Ptmp=(char)0;
<br> if(*(Ptmp-PathLen)=='.' && PathLen==1){
<br> PathLen=strlen(PWD);
<br> bcopy(PWD, Buf, PathLen);
<br> Ptmp=PWD+PathLen;
<br> *(Buf+PathLen) = '/';
<br> }
<br> else
<br> bcopy(Ptmp-PathLen, Buf, PathLen);
<br> *(Buf+PathLen)='/';
<br> bcopy(Cmd, Buf+PathLen+1, strlen(Cmd));
<br> *(Buf+PathLen+1+strlen(Cmd))=(char)0;
<br> OK=(0==access(Buf, X_OK)); /* Exec permision?
*/
<br> if(OK){
<br> bcopy(Ptmp-PathLen, DirBuf, PathLen);
<br> *(DirBuf+PathLen)=(char)0;
<br> free(Path);
<br> free(Buf);
<br> return DirBuf;
<br> }
<br> *Ptmp = tmp;
<br> if(*Ptmp) Ptmp++;
<br> }
<br> }
<br> else{ /* The Path is Givien */
<br> int PWDlen, Cmdlen;
<br> char *p;
<p> PWDlen = strlen(PWD);
<br> Cmdlen = strlen(Cmd);
<br> if(Cmdlen+PWDlen > MAX_PATHLEN-2){
<br> free(Buf);
<br> return NULL;
<br> }
<br> bcopy(PWD, Buf, PWDlen);
<br> Buf[PWDlen]='/';
<br> Buf[PWDlen+1]=(char)0;
<br> strcat(Buf, Cmd);
<br> if(0 != access(Buf, X_OK)){ /* This is again a ridiculous
condition. But
<br> this might happen if the passed
Cmd is not
<br> the programs arg[0].
*/
<br> free(Buf);
<br> return NULL;
<br> }
<br> p = strrchr(Buf, '/');
<br> *p = (char)0;
<br> if(p - Buf > LenLimit -1){
<br> free(Buf);
<br> return NULL; /* No way to attack me!
*/
<br> }
<br> bcopy(Buf, DirBuf, p-Buf+1);
<br> free(Buf);
<br> return DirBuf;
<br> }
<br> return NULL;
<br>}
<br>
<p>/*=================CUT HERE! This is just an Usage Example ===============*/
<br>#include <stdio.h>
<p>void main(int argc, char** argv){
<br> char Buffer[1024];
<p> chdir("/"); /* I can handle this now */
<br> printf("%s\n",GetPrgDir(argv[1], Buffer, 1024));
<br>}
<br>
<br>
<br>
<br>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -