How to implement readlink to find the path

后端 未结 5 1940
悲&欢浪女
悲&欢浪女 2020-12-09 03:33

Using the readlink function used as a solution to How do I find the location of the executable in C?, how would I get the path into a char array? Also, what do the variables

5条回答
  •  没有蜡笔的小新
    2020-12-09 03:47

    char *
    readlink_malloc (const char *filename)
    {
      int size = 100;
      char *buffer = NULL;
    
      while (1)
        {
          buffer = (char *) xrealloc (buffer, size);
          int nchars = readlink (filename, buffer, size);
          if (nchars < 0)
            {
              free (buffer);
              return NULL;
            }
          if (nchars < size)
            return buffer;
          size *= 2;
        }
    }
    

    Taken from: http://www.delorie.com/gnu/docs/glibc/libc_279.html

提交回复
热议问题