X-Git-Url: http://cgit.sxemacs.org/?a=blobdiff_plain;f=src%2Fsysdep.h;h=f34b9446d37fa5ad69f3e1568a4aecf993ce83c8;hb=56c8b099ccef8240d76c6f88c3f31d86ae22cfba;hp=65d70c4ffa78b2ea2e04a49d97023d04deedbbbd;hpb=d28db7f744455e7e376e2b7737e0f804456a686d;p=sxemacs diff --git a/src/sysdep.h b/src/sysdep.h index 65d70c4..f34b944 100644 --- a/src/sysdep.h +++ b/src/sysdep.h @@ -3,19 +3,19 @@ * Copyright (C) 2008 Sebastian Freundt * Copyright (C) 1985, 1993, 1994 Free Software Foundation, Inc. * - * + * * This file is part of SXEmacs - * + * * SXEmacs 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 3 of the License, or * (at your option) any later version. - * + * * SXEmacs 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, see . * @@ -177,66 +177,10 @@ extern int h_errno; char *xrealpath(const char *path, char restrict resolved_path[]); #endif -extern_inline size_t xmin_size_t(size_t a, size_t b); extern_inline void x__dirname(char *restrict res, const char *file, size_t len); extern_inline size_t x__dirlen(const char *file, size_t len); extern_inline char *xdirname(const char *file); - -/* str funs */ -/* thought these were defined already :O */ -#define xstrlen strlen -#define xstrcmp strcmp -#define xstrcat strcat -#define xstrncmp strncmp -#define xstrncpy strncpy -#define xstrncat strncat -#if defined HAVE_STPCPY -# define xstpcpy stpcpy -#else -extern_inline char* -xstpcpy(char *target, const char *source) - __attribute__((always_inline)); -extern_inline char* -xstpcpy(char *target, const char *source) -{ - char *p = target; - size_t len = xstrlen(source); - - strcpy(target, source); - p += len; - return p; -} -#endif /* !HAVE_STPCPY */ -#if defined HAVE_STPNCPY -# define xstpncpy stpncpy -#else -extern_inline char* -xstpncpy(char *target, const char *source, size_t len) - __attribute__((always_inline)); -extern_inline char* -xstpncpy(char *target, const char *source, size_t len) -{ - char *p = target; - strncpy(target, source, len); - p += len; - return p; -} -#endif /* !HAVE_STPNCPY */ - -#define xmemcmp memcmp -#define xmemcpy memcpy - -extern_inline size_t -xmin_size_t(size_t a, size_t b) -{ - if (a < b) { - return a; - } else { - return b; - } -} - /* big dirname magic, some of it stolen from dirname.c from coreutils 6.9 */ /* POSIX says: @@ -257,6 +201,9 @@ xmin_size_t(size_t a, size_t b) #define ISSLASH(C) ((C) == DIRECTORY_SEPARATOR) #endif +extern_inline size_t +x__dirlen(const char *file, size_t len) + __attribute__((always_inline)); extern_inline size_t x__dirlen(const char *file, size_t len) { @@ -273,42 +220,37 @@ x__dirlen(const char *file, size_t len) return len; } -#if defined(HAVE_DIRNAME) && defined(DIRNAME_SIDE_EFFECT) extern_inline void x__dirname(char *restrict res, const char *file, size_t len) -{ - /* assumes res is malloc'd of size LEN */ - xstrncpy(res, file, len); - dirname(res); - return; -} -#elif defined(HAVE_DIRNAME) && !defined(DIRNAME_ACCEPTS_PROTMEM) -extern_inline void -x__dirname(char *restrict res, const char *file, size_t len) -{ - /* assumes res is malloc'd of size LEN */ - char *result; - xstrncpy(res, file, len); - /* if we were using side effects we woulda matched the above cond */ - result = dirname(res); - xstrncpy(res, result, xmin_size_t(len, xstrlen(result))); - return; -} -#elif defined(HAVE_DIRNAME) + __attribute__((always_inline)); +#if defined(HAVE_DIRNAME) extern_inline void x__dirname(char *restrict res, const char *file, size_t len) { - /* assumes res is malloc'd of size LEN */ - char *result = dirname(res); - xstrncpy(res, result, xmin_size_t(len, xstrlen(result))); - return; + /* assumes res is malloc'd of size LEN */ + char *result; + xstrncpy(res, file, len); + result = dirname(res); + if (res == result) { + return; + } + if (result < res || result >= (res+len)) { + xstrncpy(res, result, len); + } else { + /* Use memmove if result overlaps res */ + memmove(res, result, strlen(result)+1); + } + return; } #endif +extern_inline char* +xdirname(const char *file) + __attribute__((always_inline)); extern_inline char* xdirname(const char *file) { - size_t len = xstrlen(file); + size_t len = xstrlen(file)+1; char *res = xmalloc_atomic(len); x__dirname(res, file, len);