/* realpath - return the canonicalized absolute pathname Copyright (C) 2001 Steven Barker This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ /* Written by Steven Barker. */ #include #include #include /* this should be changed to use a generic realpath function if libc does not have one of its own */ #include #include #include "system.h" #include "long-options.h" #include "error.h" #include "closeout.h" /* The official name of this program (e.g., no `g' prefix). */ #define PROGRAM_NAME "realpath" #define AUTHORS "Steven Barker" /* The name this program was run with. */ char *program_name; void usage (int status) { if (status != 0) fprintf (stderr, _("Try `%s --help' for more information.\n"), program_name); else { printf (_("\ Usage: %s PATH\n\ or: %s OPTION\n\ "), program_name, program_name); printf (_("\ Print PATH with all references to `.', `..', and symlinks resolved.\n\ \n\ --help display this help and exit\n\ --version output version information and exit\n\ ")); puts (_("\nReport bugs to .")); } exit (status); } int main (int argc, char **argv) { char *result; long int path_max; program_name = argv[0]; setlocale (LC_ALL, ""); bindtextdomain (PACKAGE, LOCALEDIR); textdomain (PACKAGE); atexit (close_stdout); parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION, AUTHORS, usage); /* The above handles --help and --version. Since there is no other invocation of getopt, handle `--' here. */ if (argc > 1 && STREQ (argv[1], "--")) { --argc; ++argv; } if (argc != 2) { error (0, 0, argc < 2 ? _("too few arguments") : _("too many arguments")); usage (1); } #ifdef PATH_MAX path_max = PATH_MAX; #else path_max = pathconf (name, _PC_PATH_MAX); if (path_max <= 0) path_max = 4096; #endif result = malloc(path_max); if(! realpath (argv[1], result)) { free(result); error(1, errno, argv[1]); } printf ("%s\n", result); free(result); exit (0); }