2019-02-20 15:39:13 +03:00
|
|
|
/*
|
|
|
|
Copyright (C) 1995 Ian Jackson <iwj10@cus.cam.ac.uk>
|
|
|
|
Copyright (C) 2001 Anthony Towns <aj@azure.humbug.org.au>
|
2022-02-20 17:06:32 +03:00
|
|
|
Copyright (C) 2008-2022 Free Software Foundation, Inc.
|
2019-02-20 15:39:13 +03:00
|
|
|
|
2022-02-20 17:06:32 +03: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
|
|
|
|
published by the Free Software Foundation, either version 3 of the
|
|
|
|
License, or (at your option) any later version.
|
2019-02-20 15:39:13 +03:00
|
|
|
|
2022-02-20 17:06:32 +03:00
|
|
|
This file is distributed in the hope that it will be useful,
|
2019-02-20 15:39:13 +03:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2022-02-20 17:06:32 +03:00
|
|
|
GNU Lesser General Public License for more details.
|
2019-02-20 15:39:13 +03:00
|
|
|
|
2022-02-20 17:06:32 +03:00
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
2019-02-20 15:39:13 +03:00
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <limits.h>
|
|
|
|
|
|
|
|
#include "lib/strutil.h"
|
|
|
|
|
|
|
|
/*** global variables ****************************************************************************/
|
|
|
|
|
|
|
|
/*** file scope macro definitions ****************************************************************/
|
|
|
|
|
|
|
|
/*** file scope type declarations ****************************************************************/
|
|
|
|
|
Update template for .c files.
Add section for forward declarations of local functions. This section is
located before file scope variables because functions can be used in
strucutres (see find.c for example):
/*** forward declarations (file scope functions) *************************************************/
/* button callbacks */
static int start_stop (WButton * button, int action);
static int find_do_view_file (WButton * button, int action);
static int find_do_edit_file (WButton * button, int action);
/*** file scope variables ************************************************************************/
static struct
{
...
bcback_fn callback;
} fbuts[] =
{
...
{ B_STOP, NORMAL_BUTTON, N_("S&uspend"), 0, 0, NULL, start_stop },
...
{ B_VIEW, NORMAL_BUTTON, N_("&View - F3"), 0, 0, NULL, find_do_view_file },
{ B_VIEW, NORMAL_BUTTON, N_("&Edit - F4"), 0, 0, NULL, find_do_edit_file }
};
Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
2023-02-24 09:27:11 +03:00
|
|
|
/*** forward declarations (file scope functions) *************************************************/
|
|
|
|
|
2019-02-20 15:39:13 +03:00
|
|
|
/*** file scope variables ************************************************************************/
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/*** file scope functions ************************************************************************/
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
2022-02-20 17:06:32 +03:00
|
|
|
/* 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~]*)*$
|
2019-02-20 15:39:13 +03:00
|
|
|
*
|
2022-06-04 08:51:04 +03: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-20 17:06:32 +03:00
|
|
|
* If *len is -1, s is a string; set *lem to s's length.
|
|
|
|
* Otherwise, *len should be nonnegative, s is a char array, and *len does not change.
|
2019-02-20 15:39:13 +03:00
|
|
|
*/
|
2022-02-20 17:06:32 +03:00
|
|
|
static ssize_t
|
|
|
|
file_prefixlen (const char *s, ssize_t * len)
|
2019-02-20 15:39:13 +03:00
|
|
|
{
|
2022-02-20 17:06:32 +03:00
|
|
|
size_t n = (size_t) (*len); /* SIZE_MAX if N == -1 */
|
2022-06-04 08:51:04 +03:00
|
|
|
size_t i = 0;
|
|
|
|
size_t prefixlen = 0;
|
2019-02-20 15:39:13 +03:00
|
|
|
|
2022-06-04 08:51:04 +03:00
|
|
|
while (TRUE)
|
2019-02-20 15:39:13 +03:00
|
|
|
{
|
2022-02-20 17:06:32 +03:00
|
|
|
gboolean done;
|
|
|
|
|
|
|
|
if (*len < 0)
|
|
|
|
done = s[i] == '\0';
|
|
|
|
else
|
|
|
|
done = i == n;
|
|
|
|
|
|
|
|
if (done)
|
2019-02-20 15:39:13 +03:00
|
|
|
{
|
2022-02-20 17:06:32 +03:00
|
|
|
*len = (ssize_t) i;
|
|
|
|
return (ssize_t) prefixlen;
|
2019-02-20 15:39:13 +03:00
|
|
|
}
|
2022-06-04 08:51:04 +03:00
|
|
|
|
|
|
|
i++;
|
|
|
|
prefixlen = i;
|
|
|
|
|
|
|
|
while (i + 1 < n && s[i] == '.' && (g_ascii_isalpha (s[i + 1]) || s[i + 1] == '~'))
|
|
|
|
for (i += 2; i < n && (g_ascii_isalnum (s[i]) || s[i] == '~'); i++)
|
|
|
|
;
|
2019-02-20 15:39:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
2022-02-20 17:06:32 +03:00
|
|
|
/* Return a version sort comparison value for @s's byte at position @pos.
|
|
|
|
*
|
|
|
|
* @param s a string
|
|
|
|
* @param pos a position in @s
|
|
|
|
* @param len a length of @s. If @pos == @len, sort before all non-'~' bytes.
|
|
|
|
*/
|
|
|
|
|
2019-02-20 15:39:13 +03:00
|
|
|
static int
|
2022-02-20 17:06:32 +03:00
|
|
|
order (const char *s, size_t pos, size_t len)
|
2019-02-20 15:39:13 +03:00
|
|
|
{
|
2022-02-20 17:06:32 +03:00
|
|
|
unsigned char c;
|
|
|
|
|
|
|
|
if (pos == len)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
c = s[pos];
|
|
|
|
|
2019-02-20 15:39:13 +03:00
|
|
|
if (g_ascii_isdigit (c))
|
|
|
|
return 0;
|
|
|
|
if (g_ascii_isalpha (c))
|
|
|
|
return c;
|
|
|
|
if (c == '~')
|
2022-02-20 17:06:32 +03:00
|
|
|
return (-2);
|
|
|
|
|
|
|
|
g_assert (UCHAR_MAX <= (INT_MAX - 1 - 2) / 2);
|
|
|
|
|
2019-02-20 15:39:13 +03:00
|
|
|
return (int) c + UCHAR_MAX + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/* Slightly modified verrevcmp function from dpkg
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* https://www.debian.org/doc/debian-policy/ch-controlfields.html#s-f-Version
|
|
|
|
*
|
2022-02-20 17:06:32 +03:00
|
|
|
* @param s1 first char array to compare
|
|
|
|
* @param s1_len length of @s1
|
|
|
|
* @param s2 second char array to compare
|
|
|
|
* @param s2_len length of @s2
|
2019-02-20 15:39:13 +03:00
|
|
|
*
|
|
|
|
* @return an integer less than, equal to, or greater than zero, if @s1 is <, == or > than @s2.
|
|
|
|
*/
|
|
|
|
static int
|
2022-02-20 17:06:32 +03:00
|
|
|
verrevcmp (const char *s1, ssize_t s1_len, const char *s2, ssize_t s2_len)
|
2019-02-20 15:39:13 +03:00
|
|
|
{
|
2022-02-20 17:06:32 +03:00
|
|
|
ssize_t s1_pos = 0;
|
|
|
|
ssize_t s2_pos = 0;
|
2019-02-20 15:39:13 +03:00
|
|
|
|
|
|
|
while (s1_pos < s1_len || s2_pos < s2_len)
|
|
|
|
{
|
|
|
|
int first_diff = 0;
|
|
|
|
|
|
|
|
while ((s1_pos < s1_len && !g_ascii_isdigit (s1[s1_pos]))
|
|
|
|
|| (s2_pos < s2_len && !g_ascii_isdigit (s2[s2_pos])))
|
|
|
|
{
|
2022-02-20 17:06:32 +03:00
|
|
|
int s1_c, s2_c;
|
2019-02-20 15:39:13 +03:00
|
|
|
|
2022-02-20 17:06:32 +03:00
|
|
|
s1_c = order (s1, s1_pos, s1_len);
|
|
|
|
s2_c = order (s2, s2_pos, s2_len);
|
2019-02-20 15:39:13 +03:00
|
|
|
|
|
|
|
if (s1_c != s2_c)
|
|
|
|
return (s1_c - s2_c);
|
|
|
|
|
|
|
|
s1_pos++;
|
|
|
|
s2_pos++;
|
|
|
|
}
|
|
|
|
|
2022-02-20 17:06:32 +03:00
|
|
|
while (s1_pos < s1_len && s1[s1_pos] == '0')
|
2019-02-20 15:39:13 +03:00
|
|
|
s1_pos++;
|
2022-02-20 17:06:32 +03:00
|
|
|
while (s2_pos < s2_len && s2[s2_pos] == '0')
|
2019-02-20 15:39:13 +03:00
|
|
|
s2_pos++;
|
|
|
|
|
2022-02-20 17:06:32 +03:00
|
|
|
while (s1_pos < s1_len && s2_pos < s2_len
|
|
|
|
&& g_ascii_isdigit (s1[s1_pos]) && g_ascii_isdigit (s2[s2_pos]))
|
2019-02-20 15:39:13 +03:00
|
|
|
{
|
|
|
|
if (first_diff == 0)
|
|
|
|
first_diff = s1[s1_pos] - s2[s2_pos];
|
|
|
|
|
|
|
|
s1_pos++;
|
|
|
|
s2_pos++;
|
|
|
|
}
|
|
|
|
|
2022-02-20 17:06:32 +03:00
|
|
|
if (s1_pos < s1_len && g_ascii_isdigit (s1[s1_pos]))
|
2019-02-20 15:39:13 +03:00
|
|
|
return 1;
|
2022-02-20 17:06:32 +03:00
|
|
|
if (s2_pos < s2_len && g_ascii_isdigit (s2[s2_pos]))
|
|
|
|
return (-1);
|
2019-02-20 15:39:13 +03:00
|
|
|
if (first_diff != 0)
|
|
|
|
return first_diff;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/*** public functions ****************************************************************************/
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/* Compare version strings.
|
|
|
|
*
|
2022-02-20 17:06:32 +03:00
|
|
|
* @param s1 first string to compare
|
|
|
|
* @param s2 second string to compare
|
2019-02-20 15:39:13 +03:00
|
|
|
*
|
|
|
|
* @return an integer less than, equal to, or greater than zero, if @s1 is <, == or > than @s2.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
filevercmp (const char *s1, const char *s2)
|
|
|
|
{
|
2022-02-20 17:06:32 +03:00
|
|
|
return filenvercmp (s1, -1, s2, -1);
|
|
|
|
}
|
2019-02-20 15:39:13 +03:00
|
|
|
|
2022-02-20 17:06:32 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/* Compare version strings.
|
|
|
|
*
|
|
|
|
* @param a first string to compare
|
|
|
|
* @param alen length of @a or (-1)
|
|
|
|
* @param b second string to compare
|
|
|
|
* @param blen length of @b or (-1)
|
|
|
|
*
|
|
|
|
* @return an integer less than, equal to, or greater than zero, if @s1 is <, == or > than @s2.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
filenvercmp (const char *a, ssize_t alen, const char *b, ssize_t blen)
|
|
|
|
{
|
|
|
|
gboolean aempty, bempty;
|
|
|
|
ssize_t aprefixlen, bprefixlen;
|
|
|
|
gboolean one_pass_only;
|
|
|
|
int result;
|
|
|
|
|
|
|
|
/* Special case for empty versions. */
|
|
|
|
aempty = alen < 0 ? a[0] == '\0' : alen == 0;
|
|
|
|
bempty = blen < 0 ? b[0] == '\0' : blen == 0;
|
|
|
|
|
|
|
|
if (aempty)
|
|
|
|
return (bempty ? 0 : -1);
|
|
|
|
if (bempty)
|
2019-02-20 15:39:13 +03:00
|
|
|
return 1;
|
|
|
|
|
2022-02-20 17:06:32 +03:00
|
|
|
/* Special cases for leading ".": "." sorts first, then "..", then other names with leading ".",
|
|
|
|
then other names. */
|
|
|
|
if (a[0] == '.')
|
2019-02-20 15:39:13 +03:00
|
|
|
{
|
2022-02-20 17:06:32 +03:00
|
|
|
gboolean adot, bdot;
|
|
|
|
gboolean adotdot, bdotdot;
|
2019-02-20 15:39:13 +03:00
|
|
|
|
2022-02-20 17:06:32 +03:00
|
|
|
if (b[0] != '.')
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
adot = alen < 0 ? a[1] == '\0' : alen == 1;
|
|
|
|
bdot = blen < 0 ? b[1] == '\0' : blen == 1;
|
|
|
|
|
|
|
|
if (adot)
|
|
|
|
return (bdot ? 0 : -1);
|
|
|
|
if (bdot)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
adotdot = a[1] == '.' && (alen < 0 ? a[2] == '\0' : alen == 2);
|
|
|
|
bdotdot = b[1] == '.' && (blen < 0 ? b[2] == '\0' : blen == 2);
|
|
|
|
if (adotdot)
|
|
|
|
return (bdotdot ? 0 : -1);
|
|
|
|
if (bdotdot)
|
|
|
|
return 1;
|
2019-02-20 15:39:13 +03:00
|
|
|
}
|
2022-02-20 17:06:32 +03:00
|
|
|
else if (b[0] == '.')
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
/* Cut file suffixes. */
|
|
|
|
aprefixlen = file_prefixlen (a, &alen);
|
|
|
|
bprefixlen = file_prefixlen (b, &blen);
|
|
|
|
|
|
|
|
/* If both suffixes are empty, a second pass would return the same thing. */
|
|
|
|
one_pass_only = aprefixlen == alen && bprefixlen == blen;
|
2019-02-20 15:39:13 +03:00
|
|
|
|
2022-02-20 17:06:32 +03:00
|
|
|
result = verrevcmp (a, aprefixlen, b, bprefixlen);
|
2019-02-20 15:39:13 +03:00
|
|
|
|
2022-02-20 17:06:32 +03:00
|
|
|
/* Return the initial result if nonzero, or if no second pass is needed.
|
|
|
|
Otherwise, restore the suffixes and try again. */
|
|
|
|
return (result != 0 || one_pass_only ? result : verrevcmp (a, alen, b, blen));
|
2019-02-20 15:39:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|