免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 6192 | 回复: 15
打印 上一主题 下一主题

[C] 怎么获取程序自身所在的全路径? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-12-05 16:11 |只看该作者 |正序浏览
RT。

[ 本帖最后由 wibxy 于 2007-12-5 16:42 编辑 ]

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
16 [报告]
发表于 2007-12-06 14:31 |只看该作者
有 /proc 可用时,/proc 最简单。

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
15 [报告]
发表于 2007-12-06 14:29 |只看该作者
给大家一段伪代码:
  1. sub init
  2. {
  3. *Dir = \$Bin;
  4. *RealDir = \$RealBin;

  5. if($0 eq '-e' || $0 eq '-')
  6.   {
  7.    # perl invoked with -e or script is on C<STDIN>

  8.    $Script = $RealScript = $0;
  9.    $Bin    = $RealBin    = getcwd();
  10.   }
  11. else
  12.   {
  13.    my $script = $0;

  14.    if ($^O eq 'VMS')
  15.     {
  16.      ($Bin,$Script) = VMS::Filespec::rmsexpand($0) =~ /(.*\])(.*)/s;
  17.      ($RealBin,$RealScript) = ($Bin,$Script);
  18.     }
  19.    else
  20.     {
  21.      my $dosish = ($^O eq 'MSWin32' or $^O eq 'os2');
  22.      unless(($script =~ m#/# || ($dosish && $script =~ m#\\#))
  23.             && -f $script)
  24.       {
  25.        my $dir;
  26.        foreach $dir (File::Spec->path)
  27.         {
  28.         my $scr = File::Spec->catfile($dir, $script);
  29.         if(-r $scr && (!$dosish || -x _))
  30.          {
  31.           $script = $scr;

  32.           if (-f $0)
  33.            {
  34.             # $script has been found via PATH but perl could have
  35.             # been invoked as 'perl file'. Do a dumb check to see
  36.             # if $script is a perl program, if not then $script = $0
  37.             #
  38.             # well we actually only check that it is an ASCII file
  39.             # we know its executable so it is probably a script
  40.             # of some sort.

  41.             $script = $0 unless(-T $script);
  42.            }
  43.           last;
  44.          }
  45.        }
  46.      }

  47.      croak("Cannot find current script '$0'") unless(-f $script);

  48.      # Ensure $script contains the complete path incase we C<chdir>

  49.      $script = File::Spec->catfile(getcwd(), $script)
  50.        unless File::Spec->file_name_is_absolute($script);

  51.      ($Script,$Bin) = fileparse($script);

  52.      # Resolve $script if it is a link
  53.      while(1)
  54.       {
  55.        my $linktext = readlink($script);

  56.        ($RealScript,$RealBin) = fileparse($script);
  57.        last unless defined $linktext;

  58.        $script = (File::Spec->file_name_is_absolute($linktext))
  59.                   ? $linktext
  60.                   : File::Spec->catfile($RealBin, $linktext);
  61.       }

  62.      # Get absolute paths to directories
  63.      $Bin     = abs_path($Bin)    unless (!$Bin || File::Spec->file_name_is_absolute($Bin));
  64.      $RealBin = abs_path($RealBin) unless (!$RealBin || File::Spec->file_name_is_absolute($RealBin));
  65.     }
  66.   }
  67. }
复制代码

论坛徽章:
0
14 [报告]
发表于 2007-12-06 14:23 |只看该作者
以前讨论过,确切地将没办法

论坛徽章:
95
程序设计版块每日发帖之星
日期:2015-09-05 06:20:00程序设计版块每日发帖之星
日期:2015-09-17 06:20:00程序设计版块每日发帖之星
日期:2015-09-18 06:20:002015亚冠之阿尔艾因
日期:2015-09-18 10:35:08月度论坛发贴之星
日期:2015-09-30 22:25:002015亚冠之阿尔沙巴布
日期:2015-10-03 08:57:39程序设计版块每日发帖之星
日期:2015-10-05 06:20:00每日论坛发贴之星
日期:2015-10-05 06:20:002015年亚冠纪念徽章
日期:2015-10-06 10:06:482015亚冠之塔什干棉农
日期:2015-10-19 19:43:35程序设计版块每日发帖之星
日期:2015-10-21 06:20:00每日论坛发贴之星
日期:2015-09-14 06:20:00
13 [报告]
发表于 2007-12-05 19:16 |只看该作者

回复 #1 wibxy 的帖子

没有什么可移植的方式,只能是在程序的开始从 argv[0] 反推,或者如果可以,直接读 /proc 下的相关文件。

论坛徽章:
0
12 [报告]
发表于 2007-12-05 18:01 |只看该作者
原帖由 pandorally 于 2007-12-5 17:03 发表

加上argv[0],需区分判断绝对路径。


1, 如果argv[0]里面,第一个是字符是/,则argv[0]就是。否则,如果含有/,但不在起始位置。
则getcwd/argv[0]  。这里即便有了../xx/../xx/../xx  这样无用的重复,结论还是对的。当然。要是你有兴趣
,可以简单的处理一下。遇到./分量,就消去该分量,遇到../就回退,消去前一个分量...
2. 其他情况,还是要从PATH环境变量入手,依次查找,找到第一个匹配argv[0]的文件。

如果是windows系统,就不知道是不是适用了。

[ 本帖最后由 ivhb 于 2007-12-5 18:04 编辑 ]

论坛徽章:
0
11 [报告]
发表于 2007-12-05 17:46 |只看该作者
比较笨的方法,在main开始的时候用argv[0]找'..'目录,一直找到'/'

论坛徽章:
0
10 [报告]
发表于 2007-12-05 17:03 |只看该作者
原帖由 一路征程一路笑 于 2007-12-5 16:18 发表
char *getcwd(char *buf,size_t size);  程序目前的工作目录

加上argv[0],需区分判断绝对路径。

论坛徽章:
0
9 [报告]
发表于 2007-12-05 17:03 |只看该作者
比较传统的做法是指定一个环境变量为程序路径
一路征程一路笑 该用户已被删除
8 [报告]
发表于 2007-12-05 16:56 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽
  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP