char* FAST_FUNC xmalloc_realpath_coreutils(const char *path) { char *buf; errno = 0; buf = xmalloc_realpath(path); /* * There is one case when "readlink -f" and * "realpath" from coreutils succeed, * even though file does not exist, such as: * /tmp/file_does_not_exist * (the directory must exist). */ if (!buf && errno == ENOENT) { char *last_slash = strrchr(path, '/'); if (last_slash) { *last_slash++ = '\0'; buf = xmalloc_realpath(path); if (buf) { unsigned len = strlen(buf); buf = xrealloc(buf, len + strlen(last_slash) + 2); buf[len++] = '/'; strcpy(buf + len, last_slash); } } } return buf; }
int realpath_main(int argc UNUSED_PARAM, char **argv) { int retval = EXIT_SUCCESS; if (!*++argv) { bb_show_usage(); } do { char *resolved_path = xmalloc_realpath(*argv); if (resolved_path != NULL) { puts(resolved_path); free(resolved_path); } else { retval = EXIT_FAILURE; bb_simple_perror_msg(*argv); } } while (*++argv); fflush_stdout_and_exit(retval); }