2022-02-23 19:11:52 +00:00
|
|
|
/* Compare file names containing version numbers.
|
|
|
|
|
2016-02-21 21:25:24 +00:00
|
|
|
Copyright (C) 1995 Ian Jackson <iwj10@cus.cam.ac.uk>
|
|
|
|
Copyright (C) 2001 Anthony Towns <aj@azure.humbug.org.au>
|
2024-01-02 01:47:10 +00:00
|
|
|
Copyright (C) 2008-2024 Free Software Foundation, Inc.
|
2016-02-21 21:25:24 +00:00
|
|
|
|
Update from Gnulib
Make the following changes by hand, and run 'admin/merge-gnulib'.
* .gitignore: Add lib/malloc/*.gl.h.
* admin/merge-gnulib: Copy lib/af_alg.h and lib/save-cwd.h
directly from Gnulib, without worrying about Gnulib modules,
as these files are special cases.
(AVOIDED_MODULES): Remove malloc-posix.
* lib/malloc.c, lib/realloc.c, m4/malloc.m4, m4/realloc.m4:
* m4/year2038.m4: New files, copied from Gnulib.
* lib/malloca.c, lib/malloca.h:
* m4/close-stream.m4, m4/glibc21.m4, m4/malloca.m4:
Remove. These are either no longer present in Gnulib, or are no
longer needed by modules that Emacs uses.
* oldXMenu/AddPane.c, oldXmenu/Addsel.c: Include XmenuInt.h first;
needed for new Gnulib.
* src/xmenu.c: Call emacs_abort, not abort.
2021-10-04 19:11:39 +00:00
|
|
|
This file is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as
|
2022-02-23 19:11:52 +00:00
|
|
|
published by the Free Software Foundation, either version 3 of the
|
Update from Gnulib
Make the following changes by hand, and run 'admin/merge-gnulib'.
* .gitignore: Add lib/malloc/*.gl.h.
* admin/merge-gnulib: Copy lib/af_alg.h and lib/save-cwd.h
directly from Gnulib, without worrying about Gnulib modules,
as these files are special cases.
(AVOIDED_MODULES): Remove malloc-posix.
* lib/malloc.c, lib/realloc.c, m4/malloc.m4, m4/realloc.m4:
* m4/year2038.m4: New files, copied from Gnulib.
* lib/malloca.c, lib/malloca.h:
* m4/close-stream.m4, m4/glibc21.m4, m4/malloca.m4:
Remove. These are either no longer present in Gnulib, or are no
longer needed by modules that Emacs uses.
* oldXMenu/AddPane.c, oldXmenu/Addsel.c: Include XmenuInt.h first;
needed for new Gnulib.
* src/xmenu.c: Call emacs_abort, not abort.
2021-10-04 19:11:39 +00:00
|
|
|
License, or (at your option) any later version.
|
2016-02-21 21:25:24 +00:00
|
|
|
|
Update from Gnulib
Make the following changes by hand, and run 'admin/merge-gnulib'.
* .gitignore: Add lib/malloc/*.gl.h.
* admin/merge-gnulib: Copy lib/af_alg.h and lib/save-cwd.h
directly from Gnulib, without worrying about Gnulib modules,
as these files are special cases.
(AVOIDED_MODULES): Remove malloc-posix.
* lib/malloc.c, lib/realloc.c, m4/malloc.m4, m4/realloc.m4:
* m4/year2038.m4: New files, copied from Gnulib.
* lib/malloca.c, lib/malloca.h:
* m4/close-stream.m4, m4/glibc21.m4, m4/malloca.m4:
Remove. These are either no longer present in Gnulib, or are no
longer needed by modules that Emacs uses.
* oldXMenu/AddPane.c, oldXmenu/Addsel.c: Include XmenuInt.h first;
needed for new Gnulib.
* src/xmenu.c: Call emacs_abort, not abort.
2021-10-04 19:11:39 +00:00
|
|
|
This file is distributed in the hope that it will be useful,
|
2016-02-21 21:25:24 +00:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Update from Gnulib
Make the following changes by hand, and run 'admin/merge-gnulib'.
* .gitignore: Add lib/malloc/*.gl.h.
* admin/merge-gnulib: Copy lib/af_alg.h and lib/save-cwd.h
directly from Gnulib, without worrying about Gnulib modules,
as these files are special cases.
(AVOIDED_MODULES): Remove malloc-posix.
* lib/malloc.c, lib/realloc.c, m4/malloc.m4, m4/realloc.m4:
* m4/year2038.m4: New files, copied from Gnulib.
* lib/malloca.c, lib/malloca.h:
* m4/close-stream.m4, m4/glibc21.m4, m4/malloca.m4:
Remove. These are either no longer present in Gnulib, or are no
longer needed by modules that Emacs uses.
* oldXMenu/AddPane.c, oldXmenu/Addsel.c: Include XmenuInt.h first;
needed for new Gnulib.
* src/xmenu.c: Call emacs_abort, not abort.
2021-10-04 19:11:39 +00:00
|
|
|
GNU Lesser General Public License for more details.
|
2016-02-21 21:25:24 +00:00
|
|
|
|
Update from Gnulib
Make the following changes by hand, and run 'admin/merge-gnulib'.
* .gitignore: Add lib/malloc/*.gl.h.
* admin/merge-gnulib: Copy lib/af_alg.h and lib/save-cwd.h
directly from Gnulib, without worrying about Gnulib modules,
as these files are special cases.
(AVOIDED_MODULES): Remove malloc-posix.
* lib/malloc.c, lib/realloc.c, m4/malloc.m4, m4/realloc.m4:
* m4/year2038.m4: New files, copied from Gnulib.
* lib/malloca.c, lib/malloca.h:
* m4/close-stream.m4, m4/glibc21.m4, m4/malloca.m4:
Remove. These are either no longer present in Gnulib, or are no
longer needed by modules that Emacs uses.
* oldXMenu/AddPane.c, oldXmenu/Addsel.c: Include XmenuInt.h first;
needed for new Gnulib.
* src/xmenu.c: Call emacs_abort, not abort.
2021-10-04 19:11:39 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>. */
|
2016-02-21 21:25:24 +00:00
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include "filevercmp.h"
|
|
|
|
|
|
|
|
#include <c-ctype.h>
|
|
|
|
#include <limits.h>
|
2022-02-23 19:11:52 +00:00
|
|
|
#include <idx.h>
|
|
|
|
|
|
|
|
/* Return the length of a prefix of S that corresponds to the suffix
|
|
|
|
defined by this extended regular expression in the C locale:
|
|
|
|
(\.[A-Za-z~][A-Za-z0-9~]*)*$
|
2022-07-06 04:57:32 +00:00
|
|
|
Use the longest suffix matching this regular expression,
|
|
|
|
except do not use all of S as a suffix if S is nonempty.
|
2022-02-23 19:11:52 +00:00
|
|
|
If *LEN is -1, S is a string; set *LEN to S's length.
|
|
|
|
Otherwise, *LEN should be nonnegative, S is a char array,
|
|
|
|
and *LEN does not change. */
|
|
|
|
static idx_t
|
|
|
|
file_prefixlen (char const *s, ptrdiff_t *len)
|
2016-02-21 21:25:24 +00:00
|
|
|
{
|
2022-02-23 19:11:52 +00:00
|
|
|
size_t n = *len; /* SIZE_MAX if N == -1. */
|
2022-07-06 04:57:32 +00:00
|
|
|
idx_t prefixlen = 0;
|
2022-02-23 19:11:52 +00:00
|
|
|
|
2022-07-06 04:57:32 +00:00
|
|
|
for (idx_t i = 0; ; )
|
2016-02-21 21:25:24 +00:00
|
|
|
{
|
2022-02-23 19:11:52 +00:00
|
|
|
if (*len < 0 ? !s[i] : i == n)
|
2016-02-21 21:25:24 +00:00
|
|
|
{
|
2022-02-23 19:11:52 +00:00
|
|
|
*len = i;
|
|
|
|
return prefixlen;
|
2016-02-21 21:25:24 +00:00
|
|
|
}
|
2022-07-06 04:57:32 +00:00
|
|
|
|
|
|
|
i++;
|
|
|
|
prefixlen = i;
|
|
|
|
while (i + 1 < n && s[i] == '.' && (c_isalpha (s[i + 1])
|
|
|
|
|| s[i + 1] == '~'))
|
|
|
|
for (i += 2; i < n && (c_isalnum (s[i]) || s[i] == '~'); i++)
|
|
|
|
continue;
|
2016-02-21 21:25:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-23 19:11:52 +00:00
|
|
|
/* Return a version sort comparison value for S's byte at position POS.
|
|
|
|
S has length LEN. If POS == LEN, sort before all non-'~' bytes. */
|
|
|
|
|
2016-02-21 21:25:24 +00:00
|
|
|
static int
|
2022-02-23 19:11:52 +00:00
|
|
|
order (char const *s, idx_t pos, idx_t len)
|
2016-02-21 21:25:24 +00:00
|
|
|
{
|
2022-02-23 19:11:52 +00:00
|
|
|
if (pos == len)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
unsigned char c = s[pos];
|
2016-02-21 21:25:24 +00:00
|
|
|
if (c_isdigit (c))
|
|
|
|
return 0;
|
|
|
|
else if (c_isalpha (c))
|
|
|
|
return c;
|
|
|
|
else if (c == '~')
|
2022-02-23 19:11:52 +00:00
|
|
|
return -2;
|
2016-02-21 21:25:24 +00:00
|
|
|
else
|
2022-02-23 19:11:52 +00:00
|
|
|
{
|
2022-09-28 07:29:09 +00:00
|
|
|
static_assert (UCHAR_MAX <= (INT_MAX - 1 - 2) / 2);
|
2022-02-23 19:11:52 +00:00
|
|
|
return c + UCHAR_MAX + 1;
|
|
|
|
}
|
2016-02-21 21:25:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* slightly modified verrevcmp function from dpkg
|
2022-02-23 19:11:52 +00:00
|
|
|
S1, S2 - compared char array
|
|
|
|
S1_LEN, S2_LEN - length of arrays to be scanned
|
2016-02-21 21:25:24 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
implements that from s5.6.12 of Debian Policy v3.8.0.1
|
Merge from Gnulib
This incorporates:
2017-09-16 manywarnings: port to GCC on 64-bit MS-Windows
2017-09-13 all: Replace many more http URLs by https URLs
* build-aux/config.guess, build-aux/config.sub:
* build-aux/gitlog-to-changelog, doc/misc/texinfo.tex:
* lib/allocator.h, lib/count-leading-zeros.h:
* lib/count-trailing-zeros.h, lib/dup2.c, lib/filevercmp.c:
* lib/fstatat.c, lib/fsync.c, lib/ftoastr.c, lib/ftoastr.h:
* lib/intprops.h, lib/signal.in.h, lib/stdio-impl.h, lib/stdio.in.h:
* lib/unistd.in.h, lib/utimens.c, m4/alloca.m4, m4/extern-inline.m4:
* m4/fstatat.m4, m4/gnulib-common.m4, m4/manywarnings.m4:
* m4/std-gnu11.m4, m4/sys_types_h.m4, m4/vararrays.m4:
Copy from Gnulib.
* lib/gnulib.mk.in: Regenerate.
2017-09-17 19:56:00 +00:00
|
|
|
https://www.debian.org/doc/debian-policy/ch-controlfields.html#s-f-Version */
|
2016-02-21 21:25:24 +00:00
|
|
|
static int _GL_ATTRIBUTE_PURE
|
2022-02-23 19:11:52 +00:00
|
|
|
verrevcmp (const char *s1, idx_t s1_len, const char *s2, idx_t s2_len)
|
2016-02-21 21:25:24 +00:00
|
|
|
{
|
2022-02-23 19:11:52 +00:00
|
|
|
idx_t s1_pos = 0;
|
|
|
|
idx_t s2_pos = 0;
|
2016-02-21 21:25:24 +00:00
|
|
|
while (s1_pos < s1_len || s2_pos < 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])))
|
|
|
|
{
|
2022-02-23 19:11:52 +00:00
|
|
|
int s1_c = order (s1, s1_pos, s1_len);
|
|
|
|
int s2_c = order (s2, s2_pos, s2_len);
|
2016-02-21 21:25:24 +00:00
|
|
|
if (s1_c != s2_c)
|
|
|
|
return s1_c - s2_c;
|
|
|
|
s1_pos++;
|
|
|
|
s2_pos++;
|
|
|
|
}
|
2022-02-23 19:11:52 +00:00
|
|
|
while (s1_pos < s1_len && s1[s1_pos] == '0')
|
2016-02-21 21:25:24 +00:00
|
|
|
s1_pos++;
|
2022-02-23 19:11:52 +00:00
|
|
|
while (s2_pos < s2_len && s2[s2_pos] == '0')
|
2016-02-21 21:25:24 +00:00
|
|
|
s2_pos++;
|
2022-02-23 19:11:52 +00:00
|
|
|
while (s1_pos < s1_len && s2_pos < s2_len
|
|
|
|
&& c_isdigit (s1[s1_pos]) && c_isdigit (s2[s2_pos]))
|
2016-02-21 21:25:24 +00:00
|
|
|
{
|
|
|
|
if (!first_diff)
|
|
|
|
first_diff = s1[s1_pos] - s2[s2_pos];
|
|
|
|
s1_pos++;
|
|
|
|
s2_pos++;
|
|
|
|
}
|
2022-02-23 19:11:52 +00:00
|
|
|
if (s1_pos < s1_len && c_isdigit (s1[s1_pos]))
|
2016-02-21 21:25:24 +00:00
|
|
|
return 1;
|
2022-02-23 19:11:52 +00:00
|
|
|
if (s2_pos < s2_len && c_isdigit (s2[s2_pos]))
|
2016-02-21 21:25:24 +00:00
|
|
|
return -1;
|
|
|
|
if (first_diff)
|
|
|
|
return first_diff;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compare version strings S1 and S2.
|
|
|
|
See filevercmp.h for function description. */
|
|
|
|
int
|
|
|
|
filevercmp (const char *s1, const char *s2)
|
|
|
|
{
|
2022-02-23 19:11:52 +00:00
|
|
|
return filenvercmp (s1, -1, s2, -1);
|
|
|
|
}
|
2016-02-21 21:25:24 +00:00
|
|
|
|
2022-02-23 19:11:52 +00:00
|
|
|
/* Compare versions A (of length ALEN) and B (of length BLEN).
|
|
|
|
See filevercmp.h for function description. */
|
|
|
|
int
|
|
|
|
filenvercmp (char const *a, ptrdiff_t alen, char const *b, ptrdiff_t blen)
|
|
|
|
{
|
|
|
|
/* Special case for empty versions. */
|
|
|
|
bool aempty = alen < 0 ? !a[0] : !alen;
|
|
|
|
bool bempty = blen < 0 ? !b[0] : !blen;
|
|
|
|
if (aempty)
|
|
|
|
return -!bempty;
|
|
|
|
if (bempty)
|
2016-02-21 21:25:24 +00:00
|
|
|
return 1;
|
|
|
|
|
2022-02-23 19:11:52 +00:00
|
|
|
/* Special cases for leading ".": "." sorts first, then "..", then
|
|
|
|
other names with leading ".", then other names. */
|
|
|
|
if (a[0] == '.')
|
2016-02-21 21:25:24 +00:00
|
|
|
{
|
2022-02-23 19:11:52 +00:00
|
|
|
if (b[0] != '.')
|
|
|
|
return -1;
|
2016-02-21 21:25:24 +00:00
|
|
|
|
2022-02-23 19:11:52 +00:00
|
|
|
bool adot = alen < 0 ? !a[1] : alen == 1;
|
|
|
|
bool bdot = blen < 0 ? !b[1] : blen == 1;
|
|
|
|
if (adot)
|
|
|
|
return -!bdot;
|
|
|
|
if (bdot)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
bool adotdot = a[1] == '.' && (alen < 0 ? !a[2] : alen == 2);
|
|
|
|
bool bdotdot = b[1] == '.' && (blen < 0 ? !b[2] : blen == 2);
|
|
|
|
if (adotdot)
|
|
|
|
return -!bdotdot;
|
|
|
|
if (bdotdot)
|
|
|
|
return 1;
|
2016-02-21 21:25:24 +00:00
|
|
|
}
|
2022-02-23 19:11:52 +00:00
|
|
|
else if (b[0] == '.')
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
/* Cut file suffixes. */
|
|
|
|
idx_t aprefixlen = file_prefixlen (a, &alen);
|
|
|
|
idx_t bprefixlen = file_prefixlen (b, &blen);
|
|
|
|
|
|
|
|
/* If both suffixes are empty, a second pass would return the same thing. */
|
|
|
|
bool one_pass_only = aprefixlen == alen && bprefixlen == blen;
|
|
|
|
|
|
|
|
int result = verrevcmp (a, aprefixlen, b, bprefixlen);
|
2016-02-21 21:25:24 +00:00
|
|
|
|
2022-02-23 19:11:52 +00:00
|
|
|
/* Return the initial result if nonzero, or if no second pass is needed.
|
|
|
|
Otherwise, restore the suffixes and try again. */
|
|
|
|
return result || one_pass_only ? result : verrevcmp (a, alen, b, blen);
|
2016-02-21 21:25:24 +00:00
|
|
|
}
|