busybox源码剖析(2)---pwd.c

     pwd命令是列出当前的路径。
 1 extern int pwd_main(int argc, char **argv)
 2 {
 3     static char *buf; 
 4     
 5     buf = xgetcwd(buf);
 6     
 7     if (buf != NULL) {
 8         puts(buf);
 9         return EXIT_SUCCESS;
10     }
11     return EXIT_FAILURE;
12 }
     通过xgetcwd函数将路径保存到buf中。
char *
xgetcwd (char *cwd)
{
  char *ret;
  unsigned path_max;
  errno = 0;
  path_max = (unsigned) PATH_MAX;
  path_max += 2;                /* The getcwd docs say to do this. */
  if(cwd==0)
    cwd = xmalloc (path_max);
  errno = 0;
  while ((ret = getcwd (cwd, path_max)) == NULL && errno == ERANGE) {
      path_max += PATH_INCR;
      cwd = xrealloc (cwd, path_max);
      errno = 0;
  }
  if (ret == NULL) {
      int save_errno = errno;
      free (cwd);
      errno = save_errno;
      perror_msg("getcwd()");
      return NULL;
  }
  return cwd;
}
       通过getcwd()得到当前路径。
       这个命令的实现很简单。记录一下。

本文转自NeilHappy 51CTO博客,原文链接:http://blog.51cto.com/neilhappy/1133847,如需转载请自行联系原作者
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值