Edward Fu wrote:

> 请问用什么函数可以得到程序本身所在的目录?
> 比如我在/root下执行/usr/local/bin/myprogram,用什么函数得到
> '/usr/local/bin'目录呢?
> 总不能让我取调用type命令去查吧。 :-(
>
> 先谢了。
> 伏建军

伏先生:

我给你写了个函数,不知合不合你的意?源代码和例子在附件中。

宫敏

--
----
NT=No Thanks, WWW=World Wide Wait
Does PnP mean "Plug and Pray"?

/* getprgdir.c
 *
 * char *GetPrgDir(char *Cmd, char *DirBuf, LenLimit)
 * Get the directory of a program.
 *
 * Author: Min Gong
 *
 * History:
 *
 * Jun/3/1998  1, Added the Pointer checking for the DirBuf parameter.
 *   2, Fixed memory leak. (forgot to free up the allocated
 *      memory) Clean up.
 *   3, Added one more condition checking. For the current
 *      directory it now returns the absolute path instead
 *      of "." is this more correct?
 *   4, The current directory is the one when the program
 *      started. This should not be affected by chdir() calls.
 *
 *   Please tell me, if some one called putenv() or setenv()
 *   before calling me, how should I handle?
 *
 * Jun/2/1998      Created.
 *
 * Copyright (C) Min Gong 1998.
 *
 * Licensing: GPL V2
 *
 * A Copy of GPL V2 can be find at:
 *  http://freesoft.cei.gov.cn/gpl-asc.txt
 *
 * A Chinese translation of GPL V2 can be find at:
 *  http://freesoft.cei.gov.cn/gpl-gb.txt
 *
 * Note 1:
 *  If you include this code in your program then LGPL is applied.
 *
 * Note 2:
 * This code takes anti buffer overflow attack into consideration.
 *
 *
 * Bug report: min@freesoft.cei.gov.cn
 * site:
 * ftp://freesoft.cei.gov.cn/pub/freesoft.sic/freesoft/Linux/getprgdir.c
 *
 */

#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>

#define  MAX_PATHLEN 1024

char *GetPrgDir(char *Cmd, char *DirBuf, int LenLimit){
  int PathLen, OK;
  char *Ptmp, tmp, *PrgPtr, *Buf, *Path, *EnvPath, *PWD;

  if(Cmd==NULL || DirBuf==NULL) return NULL;
  PWD = getenv("PWD");
  Buf=malloc(MAX_PATHLEN);
  if(! (PrgPtr=strrchr(Cmd, '/'))){/* The Cmd is aprogram name, not a path name
       Check $PATH is needed  */
    EnvPath=getenv("PATH");
    if(EnvPath==NULL){  /* PATH is not set  */
      free(Buf);
      return NULL;  /* Hmm, this is ridiculous! */
    }
    PathLen=strlen(EnvPath);
    Path=malloc(PathLen);
    bcopy(EnvPath, Path, PathLen);
    Ptmp=Path;
    OK = 0;
    while(*Ptmp != (char)0 && !OK){
      PathLen=0;
      while(*Ptmp != ':' && *Ptmp != (char)0){
 PathLen++; Ptmp++;
      }
      if(PathLen+strlen(Cmd) > (MAX_PATHLEN-2)){
 free(Buf); /* Although heap is a bit safer, but I check it */
 free(Path);
 return NULL; /* anyway to protect the heap. No buffer overflow
      attack! */
      }
      tmp=*Ptmp;
      *Ptmp=(char)0;
      if(*(Ptmp-PathLen)=='.' && PathLen==1){
 PathLen=strlen(PWD);
 bcopy(PWD, Buf, PathLen);
 Ptmp=PWD+PathLen;
 *(Buf+PathLen) = '/';
      }
      else
 bcopy(Ptmp-PathLen, Buf, PathLen);
      *(Buf+PathLen)='/';
      bcopy(Cmd, Buf+PathLen+1, strlen(Cmd));
      *(Buf+PathLen+1+strlen(Cmd))=(char)0;
      OK=(0==access(Buf, X_OK)); /* Exec permision? */
      if(OK){
 bcopy(Ptmp-PathLen, DirBuf, PathLen);
 *(DirBuf+PathLen)=(char)0;
 free(Path);
 free(Buf);
 return DirBuf;
      }
      *Ptmp = tmp;
      if(*Ptmp) Ptmp++;
    }
  }
  else{  /* The Path is Givien */
    int PWDlen, Cmdlen;
    char *p;

    PWDlen = strlen(PWD);
    Cmdlen = strlen(Cmd);
    if(Cmdlen+PWDlen > MAX_PATHLEN-2){
      free(Buf);
      return NULL;
    }
    bcopy(PWD, Buf, PWDlen);
    Buf[PWDlen]='/';
    Buf[PWDlen+1]=(char)0;
    strcat(Buf, Cmd);
    if(0 != access(Buf, X_OK)){ /* This is again a ridiculous condition. But
       this might happen if the passed Cmd is not
       the programs arg[0].   */
      free(Buf);
      return NULL;
    }
    p = strrchr(Buf, '/');
    *p = (char)0;
    if(p - Buf > LenLimit -1){
      free(Buf);
      return NULL; /* No way to attack me! */
    }
    bcopy(Buf, DirBuf, p-Buf+1);
    free(Buf);
    return DirBuf;
  }
  return NULL;
}
 

/*=================CUT HERE! This is just an Usage Example ===============*/
#include <stdio.h>

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

  chdir("/"); /* I can handle this now */
  printf("%s\n",GetPrgDir(argv[1], Buffer, 1024));
}
 
 
 
 

本文转自中文Linux论坛