Skip to content
This repository has been archived by the owner on Oct 6, 2024. It is now read-only.

Commit

Permalink
Makefile and code improvements
Browse files Browse the repository at this point in the history
Signed-off-by: Mattias Andrée <[email protected]>
  • Loading branch information
maandree committed Sep 16, 2021
1 parent fce3f10 commit 71d6f9e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
11 changes: 6 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
CONFIGFILE = config.mk
include $(CONFIGFILE)


all: median

median: median.o
$(CC) -o $@ median.o $(LDFLAGS)
.o:
$(CC) -o $@ $< $(LDFLAGS)

median.o: median.c
$(CC) -c -o $@ median.c $(CFLAGS) $(CPPFLAGS)
.c.o:
$(CC) -c -o $@ $< $(CFLAGS) $(CPPFLAGS)

check: median
test "$$(printf '%s\n' 1 5 2 | ./median)" = 2
Expand All @@ -36,7 +37,7 @@ uninstall:
-rm -f -- "$(DESTDIR)$(MANPREFIX)/man1/median.1"

clean:
-rm -f -- median *.o
-rm -f -- median *.o *.su

.SUFFIXES:
.SUFFIXES: .c .o
Expand Down
8 changes: 5 additions & 3 deletions config.mk
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
PREFIX = /usr
MANPREFIX = $(PREFIX)/share/man

CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_XOPEN_SOURCE=700
CFLAGS = -std=c99 -Wall -O2
LDFLAGS = -s
CC = cc

CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_XOPEN_SOURCE=700
CFLAGS = -std=c99 -Wall -O2
LDFLAGS = -s
22 changes: 14 additions & 8 deletions median.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
#include <stdlib.h>
#include <string.h>

#if defined(__GNUC__)
# define PURE __attribute__((__pure__))
#else
# define PURE
#endif


struct group {
char *key;
Expand All @@ -24,7 +30,7 @@ static struct group groups_head;
static struct group groups_tail;


static int
PURE static int
isnumerical(const char *s)
{
if (*s == '+' || *s == '-')
Expand All @@ -42,11 +48,11 @@ isnumerical(const char *s)
}


static int
PURE static int
cmp_num(const void *apv, const void *bpv)
{
const char *a = *(const char **)apv;
const char *b = *(const char **)bpv;
const char *a = *(const char *const *)apv;
const char *b = *(const char *const *)bpv;
int mul = 1;
size_t an = 0, bn = 0, i;

Expand Down Expand Up @@ -100,8 +106,8 @@ cmp_num(const void *apv, const void *bpv)
static int
cmp_str(const void *apv, const void *bpv)
{
const char *a = *(const char **)apv;
const char *b = *(const char **)bpv;
const char *a = *(const char *const *)apv;
const char *b = *(const char *const *)bpv;
return strcmp(a, b);
}

Expand All @@ -114,7 +120,7 @@ avg(char *a, const char *b)
for (i = 0; a[i]; i++) {
val = (a[i] & 15) + (b[i] & 15);
carry = val & 1;
a[i] = (val >> 1) | '0';
a[i] = (char)((val >> 1) | '0');
}
return carry;
}
Expand All @@ -128,7 +134,7 @@ subavg(char *a, const char *b)
for (i = 0; a[i]; i++) {
val = (a[i] & 15) - (b[i] & 15);
carry = val & 1;
a[i] = (val >> 1) | '0';
a[i] = (char)((val >> 1) | '0');
}
return carry;
}
Expand Down

0 comments on commit 71d6f9e

Please sign in to comment.