new libbb func: xmalloc_realpath (+ use it where makes sense)
[oweals/busybox.git] / coreutils / realpath.c
1 /* vi: set sw=4 ts=4: */
2
3 /* BB_AUDIT SUSv3 N/A -- Apparently a busybox extension. */
4
5 /* Mar 16, 2003      Manuel Novoa III   (mjn3@codepoet.org)
6  *
7  * Now does proper error checking on output and returns a failure exit code
8  * if one or more paths cannot be resolved.
9  *
10  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
11  */
12
13 #include "busybox.h"
14
15 int realpath_main(int argc, char **argv)
16 {
17         int retval = EXIT_SUCCESS;
18
19 #if PATH_MAX > (BUFSIZ+1)
20         RESERVE_CONFIG_BUFFER(resolved_path, PATH_MAX);
21 # define resolved_path_MUST_FREE 1
22 #else
23 #define resolved_path bb_common_bufsiz1
24 # define resolved_path_MUST_FREE 0
25 #endif
26
27         if (--argc == 0) {
28                 bb_show_usage();
29         }
30
31         do {
32                 argv++;
33                 if (realpath(*argv, resolved_path) != NULL) {
34                         puts(resolved_path);
35                 } else {
36                         retval = EXIT_FAILURE;
37                         bb_perror_msg("%s", *argv);
38                 }
39         } while (--argc);
40
41 #if ENABLE_FEATURE_CLEAN_UP && resolved_path_MUST_FREE
42         RELEASE_CONFIG_BUFFER(resolved_path);
43 #endif
44
45         fflush_stdout_and_exit(retval);
46 }