From 7e4d75bf51bb3f34c24678622b842c54ca951618 Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Sat, 6 Nov 2010 15:23:13 +0000 Subject: [PATCH] Use realpath(3) to get canonical dream file name --- ChangeLog | 3 +++ config.h.in | 6 ++++++ configure.ac | 4 ++-- src/xcowsay.c | 19 ++++++++----------- 4 files changed, 19 insertions(+), 13 deletions(-) diff --git a/ChangeLog b/ChangeLog index c24b812..7953693 100644 --- a/ChangeLog +++ b/ChangeLog @@ -5,6 +5,9 @@ * src/bubblegen.c (bubble_content_left): Align left-handed text content correctly. + * src/xcowsay.c (main): Fix bug with absolute dream image + paths. Use realpath(3) to get canonical path. + 2010-10-23 Nick Gasson * src/bubblegen.c (bubble_init): Add a function to generate diff --git a/config.h.in b/config.h.in index d628ce0..cca2390 100644 --- a/config.h.in +++ b/config.h.in @@ -40,6 +40,9 @@ /* Define to 1 if you have the header file. */ #undef HAVE_LIBINTL_H +/* Define to 1 if you have the header file. */ +#undef HAVE_LIMITS_H + /* Define to 1 if you have the header file. */ #undef HAVE_LOCALE_H @@ -50,6 +53,9 @@ /* Define to 1 if you have the header file. */ #undef HAVE_MEMORY_H +/* Define to 1 if you have the `realpath' function. */ +#undef HAVE_REALPATH + /* Define to 1 if you have the `setlocale' function. */ #undef HAVE_SETLOCALE diff --git a/configure.ac b/configure.ac index 8ee5b08..8847805 100644 --- a/configure.ac +++ b/configure.ac @@ -17,7 +17,7 @@ AM_GNU_GETTEXT([external]) # Checks for header files. AC_HEADER_STDC -AC_CHECK_HEADERS([fcntl.h libintl.h locale.h stdlib.h string.h]) +AC_CHECK_HEADERS([fcntl.h libintl.h locale.h stdlib.h string.h limits.h]) # Checks for typedefs, structures, and compiler characteristics. AC_HEADER_STDBOOL @@ -27,7 +27,7 @@ AC_TYPE_SIZE_T # Checks for library functions. AC_FUNC_MALLOC AC_CHECK_FUNCS([strtol setlocale strcasecmp strdup strchr \ - getcwd strerror]) + getcwd strerror realpath]) # Check for pkg-config packages modules="gtk+-2.0 gdk-pixbuf-2.0" diff --git a/src/xcowsay.c b/src/xcowsay.c index 08115c1..73a488d 100644 --- a/src/xcowsay.c +++ b/src/xcowsay.c @@ -26,9 +26,9 @@ #include #include #include +#include #include -#include #include #include @@ -335,17 +335,14 @@ int main(int argc, char **argv) if (dream_file != NULL) { // Make path absolute - char *wd = getcwd(NULL, 0); - char *abs_path; - - asprintf(&abs_path, "%s/%s", wd, dream_file); - free(wd); + char *abs_path = realpath(dream_file, NULL); + if (abs_path == NULL) { + perror(dream_file); + exit(EXIT_FAILURE); + } - struct stat dummy; - if (stat(abs_path, &dummy) != 0) { - fprintf(stderr, "Error: %s: %s\n", abs_path, - strerror(errno)); - free(abs_path); + if (access(abs_path, R_OK) != 0) { + perror(abs_path); exit(EXIT_FAILURE); } -- 2.39.2