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
6687void 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 ;
0 commit comments