Skip to content

Commit 74a0792

Browse files
committed
Added versioning to library
1 parent e24b3ad commit 74a0792

4 files changed

Lines changed: 43 additions & 5 deletions

File tree

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ all: $(LIB_FILE) $(CMD_FILE)
4545
%.$(LIB_SUFFIX): %.c
4646
$(CC) $(CFLAGS) $(CLIBFLAGS) $(CDEFS) $< -o $@ $(LDLIBS)
4747

48+
$(CMD_FILE): $(CMD_FILE).c
49+
$(CC) $(CFLAGS) $(CDEFS) $< -o $@ $(LDLIBS)
50+
4851
%: %.c
4952
$(CC) $(CFLAGS) $(CDEFS) $< -o $@
5053

extras/test.sh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,12 @@ if [ "$IS_LINUX" ]; then
8989
else
9090
ACTUAL="$(DYLD_INSERT_LIBRARIES="./time_preload.dylib" DYLD_FORCE_FLAT_NAMESPACE=1 ./fakehostname hi ./example all | strip_newlines)"
9191
fi
92-
run_test "Preserve pre-existing preload (time)" "$EXPECTED" "$ACTUAL"
92+
run_test "preserve pre-existing preload (time)" "$EXPECTED" "$ACTUAL"
93+
94+
GIT_VER="$(git describe --tags --always --dirty 2>/dev/null || echo unknown)"
95+
EXPECTED="fakehostname version: $GIT_VER|libfakehostname version: $GIT_VER (./libfakehostname.$LIB_SUFFIX)"
96+
ACTUAL="$(./fakehostname -V | strip_newlines)"
97+
run_test "version check" "$EXPECTED" "$ACTUAL"
9398

9499
echo
95100
if [ "$FAILED" -gt 0 ]; then

src/fakehostname.c

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <dlfcn.h>
12
#include <errno.h>
23
#include <getopt.h>
34
#include <libgen.h>
@@ -62,11 +63,30 @@ char *get_lib_path() {
6263
exit(EXIT_FAILURE);
6364
}
6465

66+
void print_version() {
67+
printf("fakehostname version: " FAKE_HOSTNAME_VERSION "\n");
68+
69+
char *lib_path = get_lib_path();
70+
void *lib_handle = dlopen(lib_path, RTLD_LAZY);
71+
if (!lib_handle) {
72+
printf("Error opening library %s: \"%s\"\n", lib_path, dlerror());
73+
return;
74+
}
75+
76+
char **lib_version = dlsym(lib_handle, "version");
77+
78+
char *error;
79+
if ((error = dlerror()) != NULL) {
80+
printf("Error loading \"version\" from %s: \"%s\"\n", lib_path, error);
81+
return;
82+
}
83+
84+
printf("libfakehostname version: %s (%s)\n", *lib_version, lib_path);
85+
}
6586

6687
void usage(char *cmd_name, int exit_code) {
6788
printf(
68-
"fakehostname version " FAKE_HOSTNAME_VERSION "\n\n"
69-
"Usage: %s [-h"
89+
"\nUsage: %s [-h"
7090
#ifdef ENABLE_VERBOSE
7191
"v"
7292
#endif
@@ -86,6 +106,10 @@ void usage(char *cmd_name, int exit_code) {
86106
" -V, --version Print version information and exit\n\n"
87107
"...and remember kids, have fun!\n\n",
88108
basename(cmd_name));
109+
#ifdef ENABLE_VERBOSE
110+
verbose = 0; //shutup possible verbose output in print_version() here
111+
#endif
112+
print_version();
89113
exit(exit_code);
90114
}
91115

@@ -122,7 +146,7 @@ int parse_options(int argc, char **argv) {
122146
break;
123147
#endif
124148
case 'V':
125-
printf("fakehostname version " FAKE_HOSTNAME_VERSION "\n");
149+
print_version();
126150
exit(EXIT_SUCCESS);
127151
case '?':
128152
default:
@@ -134,6 +158,8 @@ int parse_options(int argc, char **argv) {
134158
usage(argv[0], EXIT_FAILURE);
135159
}
136160

161+
VERBOSE("Version: " FAKE_HOSTNAME_VERSION "\n");
162+
137163
new_hostname = argv[optind];
138164
VERBOSE("Faking hostname: %s\n", new_hostname)
139165
return optind + 1;

src/libfakehostname.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,18 @@
1313
static int (*__orig_gethostname)(char *name, size_t len) = NULL;
1414
static int (*__orig_uname)(struct utsname *buf) = NULL;
1515

16+
char *version = FAKE_HOSTNAME_VERSION;
17+
1618
#ifdef ENABLE_VERBOSE
1719
static int verbose = -1;
1820
#define VERBOSE(...) \
1921
if (verbose == -1) { \
2022
verbose = ((getenv(ENV_VARNAME_ENABLE_VERBOSE) == NULL) ? 0 : 1); \
21-
if (verbose) \
23+
if (verbose) { \
24+
fprintf(stderr, "libfakehostname: Version %s\n", version); \
2225
fprintf(stderr, "libfakehostname: \"" ENV_VARNAME_ENABLE_VERBOSE \
2326
"\" set. Enabling verbose mode.\n"); \
27+
} \
2428
} \
2529
if (verbose) \
2630
fprintf(stderr, "libfakehostname: " __VA_ARGS__);

0 commit comments

Comments
 (0)