Skip to content

Commit feb499d

Browse files
committed
Mark usage() and version() as __dead.
Add configure checks for __dead. Fall back to __dead2 and then __attribute__((__no_return__)) and finally undefining __dead if nothing useful is found. Add test suite.
1 parent 2808217 commit feb499d

File tree

2 files changed

+95
-6
lines changed

2 files changed

+95
-6
lines changed

configure

+92-3
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,36 @@ EOF
6565
fi
6666
}
6767

68+
deadcheck() {
69+
cat << EOF > conftest.c
70+
#include <stdlib.h>
71+
__dead usage(void){exit(1);}int main(void){usage();return 0;}
72+
EOF
73+
$cc $cflags $ldflags -o conftest conftest.c > /dev/null 2>&1
74+
if [ $? -eq 0 ] ; then
75+
rm -f conftest conftest.o conftest.c
76+
return 0
77+
else
78+
rm -f conftest conftest.o conftest.c
79+
return 1
80+
fi
81+
}
82+
83+
dead2check() {
84+
cat << EOF > conftest.c
85+
#include <stdlib.h>
86+
__dead2 usage(void){exit(1);}int main(void){usage();return 0;}
87+
EOF
88+
$cc $cflags $ldflags -o conftest conftest.c > /dev/null 2>&1
89+
if [ $? -eq 0 ] ; then
90+
rm -f conftest conftest.o conftest.c
91+
return 0
92+
else
93+
rm -f conftest conftest.o conftest.c
94+
return 1
95+
fi
96+
}
97+
6898
getprognamecheck() {
6999
cat << EOF > conftest.c
70100
#include <stdlib.h>
@@ -95,6 +125,21 @@ EOF
95125
fi
96126
}
97127

128+
noreturncheck() {
129+
cat << EOF > conftest.c
130+
#include <stdlib.h>
131+
__attribute__((__no_return__)) usage(void){exit(1);}int main(void){usage();return 0;}
132+
EOF
133+
$cc $cflags $ldflags -o conftest conftest.c > /dev/null 2>&1
134+
if [ $? -eq 0 ] ; then
135+
rm -f conftest conftest.o conftest.c
136+
return 0
137+
else
138+
rm -f conftest conftest.o conftest.c
139+
return 1
140+
fi
141+
}
142+
98143
pledgecheck() {
99144
cat << EOF > conftest.c
100145
#include <unistd.h>
@@ -178,6 +223,31 @@ if [ "x$os" = "xNetBSD" ] ; then
178223
cflags="$cflags -D_OPENBSD_SOURCE"
179224
fi
180225

226+
printf "checking for __dead... "
227+
deadcheck
228+
if [ $? -eq 0 ] ; then
229+
echo "yes"
230+
else
231+
echo "no"
232+
printf "checking for __dead2... "
233+
dead2check
234+
if [ $? -eq 0 ] ; then
235+
cflags="$cflags -D__dead=__dead2"
236+
echo "yes"
237+
else
238+
echo "no"
239+
printf "checking for __attribute__((__no_return__))... "
240+
noreturncheck
241+
if [ $? -eq 0 ] ; then
242+
cflags="$cflags -D__dead=\"__attribute__((__no_return__))\""
243+
echo "yes"
244+
else
245+
cflags="$cflags -D__dead="
246+
echo "no"
247+
fi
248+
fi
249+
fi
250+
181251
printf "checking for arc4random_uniform... "
182252
arc4randomuniformcheck
183253
if [ $? -eq 0 ] ; then
@@ -260,16 +330,35 @@ cat << EOF >> Makefile
260330
PREFIX ?= /usr/local
261331
MANDIR ?= \${PREFIX}/man
262332
263-
PROG = shuf
264-
OBJS = shuf.o
333+
PROG = shuf
334+
OBJS = shuf.o
265335
266336
all: \${OBJS}
267-
\${CC} \${CFLAGS} \${LDFLAGS} -o \${PROG} \${OBJS} $libs
337+
\${CC} \${LDFLAGS} -o \${PROG} \${OBJS} $libs
268338
269339
install:
270340
install -c -S -s -m 755 \${PROG} \${PREFIX}/bin
271341
install -c -S -m 644 shuf.1 \${MANDIR}/man1/\${PROG}.1
272342
343+
test:
344+
@echo ====================================
345+
@echo BSD shuf test suite
346+
@echo ====================================
347+
@echo Test 1: File input
348+
@echo shuf shuf.c
349+
@./shuf shuf.c || (echo Test 1 failed; exit 1)
350+
@echo ====================================
351+
@echo Test 2: -e flag, -n flag, -r flag
352+
@echo shuf -e -n 10 -r Heads Tails
353+
@./shuf -e -n 10 -r Heads Tails || (echo Test 2 failed; exit 1)
354+
@echo ====================================
355+
@echo Test 3: -i flag
356+
@echo shuf -i 1-20 -n 10
357+
@./shuf -i 1-20 -n 10 || (echo Test 3 failed; exit 1)
358+
@echo ====================================
359+
@echo All tests passed
360+
@echo ====================================
361+
273362
clean:
274363
rm -f \${PROG} \${OBJS} \${PROG}.core
275364

shuf.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ repledge(int oflag)
165165
#endif
166166
}
167167

168-
static void
168+
static void __dead
169169
usage(void)
170170
{
171171
const char *name;
@@ -180,11 +180,11 @@ usage(void)
180180
exit(1);
181181
}
182182

183-
static void
183+
static void __dead
184184
version(void)
185185
{
186186

187-
fputs("shuf 2.4\n"
187+
fputs("shuf 2.5\n"
188188
"Copyright (c) 2017-2019 Brian Callahan <[email protected]>\n"
189189
"\nPermission to use, copy, modify, and distribute this software"
190190
" for any\npurpose with or without fee is hereby granted, "

0 commit comments

Comments
 (0)