X-Git-Url: https://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2Ffilevercmp.c;h=bc1cc07d86be56613842af9c5f84b7fc319b18d8;hb=afecf4a0bde14016fb4ab2a18395d82cfbc0fe28;hp=856f30ff0ccc6fa5349079fe083079d8f472eed9;hpb=9121662f1a3ac82621aac4e7f8e8f64722b0903b;p=gnulib.git diff --git a/lib/filevercmp.c b/lib/filevercmp.c index 856f30ff0..bc1cc07d8 100644 --- a/lib/filevercmp.c +++ b/lib/filevercmp.c @@ -1,7 +1,7 @@ /* Copyright (C) 1995 Ian Jackson Copyright (C) 2001 Anthony Towns - Copyright (C) 2008 Free Software Foundation, Inc. + Copyright (C) 2008-2012 Free Software Foundation, Inc. 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 @@ -27,7 +27,7 @@ #include /* Match a file suffix defined by this regular expression: - /(\.[A-Za-z][A-Za-z0-9]*)*$/ + /(\.[A-Za-z~][A-Za-z0-9~]*)*$/ Scan the string *STR and return a pointer to the matching suffix, or NULL if not found. Upon return, *STR points to terminating NUL. */ static const char * @@ -40,7 +40,7 @@ match_suffix (const char **str) if (read_alpha) { read_alpha = false; - if (!c_isalpha (**str)) + if (!c_isalpha (**str) && '~' != **str) match = NULL; } else if ('.' == **str) @@ -49,7 +49,7 @@ match_suffix (const char **str) if (!match) match = *str; } - else if (!c_isalnum (**str)) + else if (!c_isalnum (**str) && '~' != **str) match = NULL; (*str)++; } @@ -77,10 +77,10 @@ order (unsigned char c) This implements the algorithm for comparison of version strings specified by Debian and now widely adopted. The detailed specification can be found in the Debian Policy Manual in the - section on the `Version' control field. This version of the code + section on the 'Version' control field. This version of the code implements that from s5.6.12 of Debian Policy v3.8.0.1 http://www.debian.org/doc/debian-policy/ch-controlfields.html#s-f-Version */ -static int +static int _GL_ATTRIBUTE_PURE verrevcmp (const char *s1, size_t s1_len, const char *s2, size_t s2_len) { size_t s1_pos = 0; @@ -89,32 +89,32 @@ verrevcmp (const char *s1, size_t s1_len, const char *s2, size_t s2_len) { int first_diff = 0; while ((s1_pos < s1_len && !c_isdigit (s1[s1_pos])) - || (s2_pos < s2_len && !c_isdigit (s2[s2_pos]))) - { - int s1_c = (s1_pos == s1_len) ? 0 : order (s1[s1_pos]); - int s2_c = (s2_pos == s2_len) ? 0 : order (s2[s2_pos]); - if (s1_c != s2_c) - return s1_c - s2_c; - s1_pos++; - s2_pos++; - } + || (s2_pos < s2_len && !c_isdigit (s2[s2_pos]))) + { + int s1_c = (s1_pos == s1_len) ? 0 : order (s1[s1_pos]); + int s2_c = (s2_pos == s2_len) ? 0 : order (s2[s2_pos]); + if (s1_c != s2_c) + return s1_c - s2_c; + s1_pos++; + s2_pos++; + } while (s1[s1_pos] == '0') - s1_pos++; + s1_pos++; while (s2[s2_pos] == '0') - s2_pos++; + s2_pos++; while (c_isdigit (s1[s1_pos]) && c_isdigit (s2[s2_pos])) - { - if (!first_diff) - first_diff = s1[s1_pos] - s2[s2_pos]; - s1_pos++; - s2_pos++; - } + { + if (!first_diff) + first_diff = s1[s1_pos] - s2[s2_pos]; + s1_pos++; + s2_pos++; + } if (c_isdigit (s1[s1_pos])) - return 1; + return 1; if (c_isdigit (s2[s2_pos])) - return -1; + return -1; if (first_diff) - return first_diff; + return first_diff; } return 0; } @@ -124,8 +124,8 @@ verrevcmp (const char *s1, size_t s1_len, const char *s2, size_t s2_len) int filevercmp (const char *s1, const char *s2) { - const char *s1_pos = s1; - const char *s2_pos = s2; + const char *s1_pos; + const char *s2_pos; const char *s1_suffix, *s2_suffix; size_t s1_len, s2_len; int result; @@ -135,7 +135,34 @@ filevercmp (const char *s1, const char *s2) if (simple_cmp == 0) return 0; + /* special handle for "", "." and ".." */ + if (!*s1) + return -1; + if (!*s2) + return 1; + if (0 == strcmp (".", s1)) + return -1; + if (0 == strcmp (".", s2)) + return 1; + if (0 == strcmp ("..", s1)) + return -1; + if (0 == strcmp ("..", s2)) + return 1; + + /* special handle for other hidden files */ + if (*s1 == '.' && *s2 != '.') + return -1; + if (*s1 != '.' && *s2 == '.') + return 1; + if (*s1 == '.' && *s2 == '.') + { + s1++; + s2++; + } + /* "cut" file suffixes */ + s1_pos = s1; + s2_pos = s2; s1_suffix = match_suffix (&s1_pos); s2_suffix = match_suffix (&s2_pos); s1_len = (s1_suffix ? s1_suffix : s1_pos) - s1;