diff options
Diffstat (limited to 'Src')
-rw-r--r-- | Src/hist.c | 75 | ||||
-rw-r--r-- | Src/string.c | 14 | ||||
-rw-r--r-- | Src/system.h | 6 |
3 files changed, 70 insertions, 25 deletions
diff --git a/Src/hist.c b/Src/hist.c index b9480d786..138cb1bf5 100644 --- a/Src/hist.c +++ b/Src/hist.c @@ -1350,28 +1350,45 @@ hcomsearch(char *str) int remtpath(char **junkptr) { - char *str = *junkptr, *remcut; - - if ((remcut = strrchr(str, '/'))) { - if (str != remcut) - *remcut = '\0'; - else - str[1] = '\0'; - return 1; + char *str = strend(*junkptr); + + /* ignore trailing slashes */ + while (str >= *junkptr && IS_DIRSEP(*str)) + --str; + /* skip filename */ + while (str >= *junkptr && !IS_DIRSEP(*str)) + --str; + if (str < *junkptr) { + *junkptr = dupstring ("."); + return 0; } - return 0; + /* repeated slashes are considered like a single slash */ + while (str > *junkptr && IS_DIRSEP(str[-1])) + --str; + /* never erase the root slash */ + if (str == *junkptr) { + ++str; + /* Leading doubled slashes (`//') have a special meaning on cygwin + and some old flavor of UNIX, so we do not assimilate them to + a single slash. However a greater number is ok to squeeze. */ + if (IS_DIRSEP(*str) && !IS_DIRSEP(str[1])) + ++str; + } + *str = '\0'; + return 1; } /**/ int remtext(char **junkptr) { - char *str = *junkptr, *remcut; + char *str; - if ((remcut = strrchr(str, '.')) && remcut != str) { - *remcut = '\0'; - return 1; - } + for (str = strend(*junkptr); str >= *junkptr && !IS_DIRSEP(*str); --str) + if (*str == '.') { + *str = '\0'; + return 1; + } return 0; } @@ -1379,12 +1396,15 @@ remtext(char **junkptr) int rembutext(char **junkptr) { - char *str = *junkptr, *remcut; + char *str; - if ((remcut = strrchr(str, '.')) && remcut != str) { - *junkptr = dupstring(remcut + 1); /* .xx or xx? */ - return 1; - } + for (str = strend(*junkptr); str >= *junkptr && !IS_DIRSEP(*str); --str) + if (*str == '.') { + *junkptr = dupstring(str + 1); /* .xx or xx? */ + return 1; + } + /* no extension */ + *junkptr = dupstring (""); return 0; } @@ -1392,13 +1412,20 @@ rembutext(char **junkptr) mod_export int remlpaths(char **junkptr) { - char *str = *junkptr, *remcut; + char *str = strend(*junkptr); - if ((remcut = strrchr(str, '/'))) { - *remcut = '\0'; - *junkptr = dupstring(remcut + 1); - return 1; + if (IS_DIRSEP(*str)) { + /* remove trailing slashes */ + while (str >= *junkptr && IS_DIRSEP(*str)) + --str; + str[1] = '\0'; } + for (; str >= *junkptr; --str) + if (IS_DIRSEP(*str)) { + *str = '\0'; + *junkptr = dupstring(str + 1); + return 1; + } return 0; } diff --git a/Src/string.c b/Src/string.c index 3dad89911..a0fc2ee8c 100644 --- a/Src/string.c +++ b/Src/string.c @@ -79,7 +79,7 @@ zhtricat(char const *s1, char const *s2, char const *s3) char *ptr; size_t l1 = strlen(s1); size_t l2 = strlen(s2); - + ptr = (char *)zhalloc(l1 + l2 + strlen(s3) + 1); strcpy(ptr, s1); strcpy(ptr + l1, s2); @@ -133,3 +133,15 @@ appstr(char *base, char const *append) { return strcat(realloc(base, strlen(base) + strlen(append) + 1), append); } + +/* Return a pointer to the last character of a string, + unless the string is empty. */ + +/**/ +mod_export char * +strend(char *str) +{ + if (*str == '\0') + return str; + return str + strlen (str) - 1; +} diff --git a/Src/system.h b/Src/system.h index 4ba9bf8ab..50661e0fd 100644 --- a/Src/system.h +++ b/Src/system.h @@ -657,3 +657,9 @@ extern short ospeed; #ifndef MAILDIR_SUPPORT #define mailstat(X,Y) stat(X,Y) #endif + +#ifdef __CYGWIN__ +# define IS_DIRSEP(c) ((c) == '/' || (c) == '\\') +#else +# define IS_DIRSEP(c) ((c) == '/') +#endif |