X-Git-Url: https://www.tinc-vpn.org/git/browse?a=blobdiff_plain;f=src%2Futils.c;h=5f40b8afac98ee4d891ed7b4f1961293f4eb8400;hb=3d787920d51a35e74e442c7265be3b13b69ad8e4;hp=6104fada3ab62b2007be2d5f4c835c06f963970a;hpb=b2701c7c54b11cda71461c5dbbc985476bf5b221;p=tinc diff --git a/src/utils.c b/src/utils.c index 6104fada..5f40b8af 100644 --- a/src/utils.c +++ b/src/utils.c @@ -18,6 +18,8 @@ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ +#include + #include "logger.h" #include "system.h" #include "utils.h" @@ -79,6 +81,57 @@ size_t bin2hex(const void *vsrc, char *dst, size_t length) { return length * 2; } +char *absolute_path(const char *path) { +#ifdef HAVE_WINDOWS + // Works for nonexistent paths + return _fullpath(NULL, path, 0); +#else + + if(!path || !*path) { + return NULL; + } + + // If an absolute path was passed, return its copy + if(*path == '/') { + return xstrdup(path); + } + + // Try using realpath. If it fails for any reason + // other than that the file was not found, bail out. + char *abs = realpath(path, NULL); + + if(abs || errno != ENOENT) { + return abs; + } + + // Since the file does not exist, we're forced to use a fallback. + // Get current working directory and concatenate it with the argument. + char cwd[PATH_MAX]; + + if(!getcwd(cwd, sizeof(cwd))) { + return NULL; + } + + // Remove trailing slash if present since we'll be adding our own + size_t cwdlen = strlen(cwd); + + if(cwdlen && cwd[cwdlen - 1] == '/') { + cwd[cwdlen - 1] = '\0'; + } + + // We don't do any normalization because it's complicated, and the payoff is small. + // If user passed something like '.././../foo' — that's their choice; fopen works either way. + xasprintf(&abs, "%s/%s", cwd, path); + + if(strlen(abs) >= PATH_MAX) { + free(abs); + abs = NULL; + } + + return abs; +#endif +} + size_t b64decode_tinc(const char *src, void *dst, size_t length) { size_t i; uint32_t triplet = 0; @@ -119,6 +172,24 @@ size_t b64decode_tinc(const char *src, void *dst, size_t length) { } } +bool is_decimal(const char *str) { + if(!str) { + return false; + } + + errno = 0; + char *badchar = NULL; + strtol(str, &badchar, 10); + return !errno && badchar != str && !*badchar; +} + +// itoa() conflicts with a similarly named function under MinGW. +char *int_to_str(int num) { + char *str = NULL; + xasprintf(&str, "%d", num); + return str; +} + static size_t b64encode_tinc_internal(const void *src, char *dst, size_t length, const char *alphabet) { uint32_t triplet; const unsigned char *usrc = (unsigned char *)src; @@ -176,7 +247,7 @@ size_t b64encode_tinc_urlsafe(const void *src, char *dst, size_t length) { return b64encode_tinc_internal(src, dst, length, base64_urlsafe); } -#ifdef HAVE_MINGW +#ifdef HAVE_WINDOWS const char *winerror(int err) { static char buf[1024], *ptr; @@ -195,17 +266,6 @@ const char *winerror(int err) { } #endif -unsigned int bitfield_to_int(const void *bitfield, size_t size) { - unsigned int value = 0; - - if(size > sizeof(value)) { - size = sizeof(value); - } - - memcpy(&value, bitfield, size); - return value; -} - bool check_id(const char *id) { if(!id || !*id) { return false;