zoukankan      html  css  js  c++  java
  • 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()得到当前路径。

           这个命令的实现很简单。记录一下。

  • 相关阅读:
    爱信诺面试总结
    项目进展日志6
    项目进展日志5
    项目进展日志4
    项目进展日志3
    项目进展日志2
    项目阶段总结
    项目进展日志
    事物的ACID特性
    5.27作业
  • 原文地址:https://www.cnblogs.com/NeilHappy/p/2912703.html
Copyright © 2011-2022 走看看