Skip to content

bee_version_compare: Rewrite compare_version_strings() #53

Merged
merged 8 commits into from Jul 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
@@ -1,7 +1,7 @@
BEE_VERSION = 1.2.24

CC=gcc
CFLAGS=-Wall -g
CFLAGS=-Wall -Wextra -Wno-override-init -Werror -g -O3
LDFLAGS=

PREFIX = /usr
Expand Down
100 changes: 27 additions & 73 deletions src/bee_version_compare.c
Expand Up @@ -28,86 +28,40 @@

#include "bee_version.h"

int compare_version_strings(char *v1, char *v2) {
char *a, *b;
long long i,j;

assert(v1);
assert(v2);

a = v1;
b = v2;

while(*a && *b && *a == *b) {
a++;
b++;

if (*a == *b)
// '2.23.2' > '2.23.0'
//' 2.23.10' > '2.23.8'
// '2.23.001' == '2.23.1'
// '2.23.1001' > '2.23.101'

static int compare_version_strings(char *a, char *b) {
while (1) {

if (isdigit(*a) && isdigit (*b)) {
long i_a = atoll(a);
long i_b = atoll(b);
if (i_a < i_b)
return -1;
if (i_a > i_b)
return 1;
while(isdigit(*a))
a++;
while(isdigit(*b))
b++;
continue;

/* skip leading zeros of numbers != 0 */
if (isdigit(*a) && isdigit(*b)) {
char *c;

for (c=a; *c == '0'; c++)
;
if (isdigit(*c))
a = c;

for (c=b; *c == '0'; c++)
;
if (isdigit(*c))
b = c;
}
}

/* strings are equal ; *a==*b==0*/
if(*a == *b)
return(0);

if(isdigit(*a)) {
if(isdigit(*b)) {
/* rewind string to first digit */
/* e.g. to compare 12 vs 100 and not 2 vs 00 */
while(a > v1 && isdigit(*(a-1)) &&
b > v2 && isdigit(*(b-1))) {
a--;
b--;
}
i = atoll(a);
j = atoll(b);

if(i<j)
return(-1);
if(i>j)
return(1);

/* numbers are equal but strings are not? */
/* yes -> leading zeros: atoll("01") == atoll("1") */
return(0);
}
/* a > ('.',alpha, 0) */
return(1);
}

if(isalpha(*a)) {
if (*a < *b)
return -1;
if (*a > *b)
return 1;

/* alpha < digit */
if(isdigit(*b))
return(-1);
if (*a == '\0') // implies *b == '\0', too
return 0;

if(isalpha(*b)) {
if(*a < *b)
return(-1);
return(1);
}
return(1);
a++;
b++;
}

if(! *b)
return(1);

return(-1);
}

int compare_beepackage_names(struct beeversion *v1, struct beeversion *v2) {
Expand Down
1 change: 0 additions & 1 deletion src/bee_version_compare.h
Expand Up @@ -24,7 +24,6 @@

#include "bee_version.h"

int compare_version_strings(char *v1, char *v2);
int compare_beepackage_names(struct beeversion *v1, struct beeversion *v2);
int compare_beeversions(struct beeversion *v1, struct beeversion *v2);
int compare_beepackages(struct beeversion *v1, struct beeversion *v2);
1 change: 1 addition & 0 deletions src/bee_version_output.c
Expand Up @@ -156,6 +156,7 @@ void print_format(char* s, struct beeversion *v, char *filter_pkgfullname)
p++;
continue;
}
// fall through
default:
printf("%%%c", *p);
break;
Expand Down
12 changes: 6 additions & 6 deletions src/beeflock.c
Expand Up @@ -66,7 +66,7 @@ void usage(void)
#define BEEFLOCK_UNLOCK LOCK_UN
#define BEEFLOCK_NOBLOCK LOCK_NB

int bee_flock_fd(int fd, int operation, int flags)
int bee_flock_fd(int fd, int operation)
{
int res;

Expand Down Expand Up @@ -123,7 +123,7 @@ int bee_flock_close(int fd)
return res;
}

int bee_flock(char *filename, int operation, int flags)
int bee_flock(char *filename, int operation)
{
int res;
int fd, fd2;
Expand All @@ -134,7 +134,7 @@ int bee_flock(char *filename, int operation, int flags)
return -1;

while (1) {
res = bee_flock_fd(fd, operation, 0);
res = bee_flock_fd(fd, operation);
if (res < 0)
return -1;

Expand Down Expand Up @@ -169,7 +169,7 @@ int bee_flock(char *filename, int operation, int flags)
}


int bee_execute_command(char *command[], int flags)
int bee_execute_command(char *command[])
{
pid_t fpid, wpid;
int wstatus;
Expand Down Expand Up @@ -265,12 +265,12 @@ int main(int argc, char *argv[])
BEE_EXIT(OSERR);
}

fd = bee_flock(lockfilename, operation, 0);
fd = bee_flock(lockfilename, operation);
if (fd < 0) {
BEE_EXIT(SOFTWARE);
}

res = bee_execute_command(command, 0);
res = bee_execute_command(command);
bee_flock_close(fd);
if (res < 0) {
BEE_EXIT(SOFTWARE);
Expand Down
1 change: 1 addition & 0 deletions src/beegetopt.c
Expand Up @@ -114,6 +114,7 @@ int main(int argc, char *argv[])
switch(opt) {
case 'V':
printf("beegetopt Vx.x\n");
// fall through
case 'h':
usage();
exit(0);
Expand Down
2 changes: 1 addition & 1 deletion src/beesep.c
Expand Up @@ -70,7 +70,7 @@ static void print_escaped(char *s, size_t n)

bee_fprint(stdout, "'");

while ((c = strchr(s, '\'')) && c - s < n) {
while ((c = strchr(s, '\'')) && c < s + n) {
if (c-s)
bee_fnprint(stdout, c - s, s);
bee_fprint(stdout, "'\\''");
Expand Down
1 change: 1 addition & 0 deletions src/beesort.c
Expand Up @@ -61,6 +61,7 @@ int my_compare_data(void *a, void *b)

void my_print(void *key, void *data)
{
(void)key;
fputs(data, stdout);
}

Expand Down