- 论坛徽章:
- 0
|
回复 #6 Steiny 的帖子
确实做不来,学习学习.喜洋洋的做法 让我 想起来以前一个用c写的:
- char* get_self_executable_directory ()
- {
- int rval;
- char link_target[1024];//目标地址
- char* last_slash;
- size_t result_length;//结果的长度
- char* result;
- /* Read the target of the symbolic link /proc/self/exe. */
- 读取绝对路径
- rval = readlink ("/proc/self/exe", link_target, sizeof (link_target) - 1);
- if (rval == -1)
- /* The call to readlink failed, so bail. */
- abort ();
- else
- /* NUL-terminate the target. */
- link_target[rval] = '\0';
- /* We want to trim the name of the executable file, to obtain the
- directory that contains it. Find the rightmost slash. */
- 找到最后一个/
- last_slash = strrchr (link_target, '/');
- 如果是空或者是以/开头,则退出
- if (last_slash == NULL || last_slash == link_target)
- /* Something stange is going on. */
- abort ();
- last_slash保存的是最后的/的地址
- /* Allocate a buffer to hold the resulting path. */
- link_target开始的地址
- result_length = last_slash - link_target;
- result = (char*) xmalloc (result_length + 1);
- /* Copy the result. */
- strncpy (result, link_target, result_length);
- result[result_length] = '\0';
- return result;
- }
复制代码 |
|