| 1 |
/* |
| 2 |
* version.c - Return the version of the library |
| 3 |
* |
| 4 |
* Copyright (C) 2015 Karel Zak <[email protected]> |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* SECTION: version-utils |
| 10 |
* @title: Version functions |
| 11 |
* @short_description: functions to get the library version. |
| 12 |
*/ |
| 13 |
|
| 14 |
#include <ctype.h> |
| 15 |
|
| 16 |
#include "fdiskP.h" |
| 17 |
|
| 18 |
static const char *lib_version = LIBFDISK_VERSION; |
| 19 |
static const char *lib_features[] = { |
| 20 |
#if !defined(NDEBUG) /* libc assert.h stuff */ |
| 21 |
"assert", |
| 22 |
#endif |
| 23 |
"debug", /* always enabled */ |
| 24 |
NULL |
| 25 |
}; |
| 26 |
|
| 27 |
/** |
| 28 |
* fdisk_parse_version_string: |
| 29 |
* @ver_string: version string (e.g "2.18.0") |
| 30 |
* |
| 31 |
* Returns: release version code. |
| 32 |
*/ |
| 33 |
int fdisk_parse_version_string(const char *ver_string) |
| 34 |
{ |
| 35 |
const char *cp; |
| 36 |
int version = 0; |
| 37 |
|
| 38 |
assert(ver_string); |
| 39 |
|
| 40 |
for (cp = ver_string; *cp; cp++) { |
| 41 |
if (*cp == '.') |
| 42 |
continue; |
| 43 |
if (!isdigit(*cp)) |
| 44 |
break; |
| 45 |
version = (version * 10) + (*cp - '0'); |
| 46 |
} |
| 47 |
return version; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* fdisk_get_library_version: |
| 52 |
* @ver_string: return pointer to the static library version string if not NULL |
| 53 |
* |
| 54 |
* Returns: release version number. |
| 55 |
*/ |
| 56 |
int fdisk_get_library_version(const char **ver_string) |
| 57 |
{ |
| 58 |
if (ver_string) |
| 59 |
*ver_string = lib_version; |
| 60 |
|
| 61 |
return fdisk_parse_version_string(lib_version); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* fdisk_get_library_features: |
| 66 |
* @features: returns a pointer to the static array of strings, the array is |
| 67 |
* terminated by NULL. |
| 68 |
* |
| 69 |
* Returns: number of items in the features array not including the last NULL, |
| 70 |
* or less than zero in case of error |
| 71 |
* |
| 72 |
* Example: |
| 73 |
* <informalexample> |
| 74 |
* <programlisting> |
| 75 |
* const char *features; |
| 76 |
* |
| 77 |
* fdisk_get_library_features(&features); |
| 78 |
* while (features && *features) |
| 79 |
* printf("%s\n", *features++); |
| 80 |
* </programlisting> |
| 81 |
* </informalexample> |
| 82 |
* |
| 83 |
*/ |
| 84 |
int fdisk_get_library_features(const char ***features) |
| 85 |
{ |
| 86 |
if (!features) |
| 87 |
return -EINVAL; |
| 88 |
|
| 89 |
*features = lib_features; |
| 90 |
return ARRAY_SIZE(lib_features) - 1; |
| 91 |
} |
| 92 |
|
| 93 |
#ifdef TEST_PROGRAM |
| 94 |
static int test_version(struct fdisk_test *ts __attribute__((unused)), |
| 95 |
int argc __attribute__((unused)), |
| 96 |
char *argv[] __attribute__((unused))) |
| 97 |
{ |
| 98 |
const char *ver; |
| 99 |
const char **features; |
| 100 |
|
| 101 |
fdisk_get_library_version(&ver); |
| 102 |
|
| 103 |
printf("Library version: %s\n", ver); |
| 104 |
printf("Library API version: " LIBFDISK_VERSION "\n"); |
| 105 |
printf("Library features:"); |
| 106 |
|
| 107 |
fdisk_get_library_features(&features); |
| 108 |
while (features && *features) |
| 109 |
printf(" %s", *features++); |
| 110 |
|
| 111 |
if (fdisk_get_library_version(NULL) == |
| 112 |
fdisk_parse_version_string(LIBFDISK_VERSION)) |
| 113 |
return 0; |
| 114 |
|
| 115 |
return -1; |
| 116 |
} |
| 117 |
|
| 118 |
int main(int argc, char *argv[]) |
| 119 |
{ |
| 120 |
struct fdisk_test ts[] = { |
| 121 |
{ "--print", test_version, "print versions" }, |
| 122 |
{ NULL } |
| 123 |
}; |
| 124 |
|
| 125 |
return fdisk_run_test(ts, argc, argv); |
| 126 |
} |
| 127 |
#endif |