]> git.ipfire.org Git - thirdparty/util-linux.git/blob - libmount/src/version.c
libmount: fix mount.nfs segfault, rely on assert() rather than on nonnull
[thirdparty/util-linux.git] / libmount / src / version.c
1 /*
2 * version.c - Return the version of the blkid library
3 *
4 * Copyright (C) 2008 Karel Zak <kzak@redhat.com>
5 * [Based on libblkid/version.c by Theodore Ts'o]
6 *
7 * See COPYING.libmount for the License of this software.
8 */
9
10 /**
11 * SECTION: version
12 * @title: Version functions
13 * @short_description: functions to get library version.
14 */
15
16 #include <ctype.h>
17
18 #include "mountP.h"
19
20 static const char *lib_version = LIBMOUNT_VERSION;
21 static const char *lib_features[] = {
22 #ifdef HAVE_LIBSELINUX
23 "selinux",
24 #endif
25 #ifdef CONFIG_LIBMOUNT_DEBUG
26 "debug",
27 #endif
28 #ifdef CONFIG_LIBMOUNT_ASSERT
29 "assert",
30 #endif
31 NULL
32 };
33
34 /**
35 * mnt_parse_version_string:
36 * @ver_string: version string (e.g "2.18.0")
37 *
38 * Returns: release version code.
39 */
40 int mnt_parse_version_string(const char *ver_string)
41 {
42 const char *cp;
43 int version = 0;
44
45 assert(ver_string);
46
47 for (cp = ver_string; *cp; cp++) {
48 if (*cp == '.')
49 continue;
50 if (!isdigit(*cp))
51 break;
52 version = (version * 10) + (*cp - '0');
53 }
54 return version;
55 }
56
57 /**
58 * mnt_get_library_version:
59 * @ver_string: return pointer to the static library version string if not NULL
60 *
61 * Returns: release version number.
62 */
63 int mnt_get_library_version(const char **ver_string)
64 {
65 if (ver_string)
66 *ver_string = lib_version;
67
68 return mnt_parse_version_string(lib_version);
69 }
70
71 /**
72 * mnt_get_library_features:
73 * @features: returns pointer to the static array of strings, the array is
74 * terminated by NULL.
75 *
76 * Returns: number of items in the features array not including the last NULL,
77 * or less then zero in case of error
78 *
79 * Example:
80 * <informalexample>
81 * <programlisting>
82 * const char *features;
83 *
84 * mnt_get_library_features(&features);
85 * while (features && *features)
86 * printf("%s\n", *features++);
87 * </programlisting>
88 * </informalexample>
89 *
90 */
91 int mnt_get_library_features(const char ***features)
92 {
93 if (!features)
94 return -EINVAL;
95
96 *features = lib_features;
97 return ARRAY_SIZE(lib_features) - 1;
98 }
99
100 #ifdef TEST_PROGRAM
101 int test_version(struct libmnt_test *ts, int argc, char *argv[])
102 {
103 const char *ver;
104 const char **features;
105
106 mnt_get_library_version(&ver);
107
108 printf("Library version: %s\n", ver);
109 printf("Library API version: " LIBMOUNT_VERSION "\n");
110 printf("Library features:");
111
112 mnt_get_library_features(&features);
113 while (features && *features)
114 printf(" %s", *features++);
115
116 if (mnt_get_library_version(NULL) ==
117 mnt_parse_version_string(LIBMOUNT_VERSION))
118 return 0;
119
120 return -1;
121 }
122
123 int main(int argc, char *argv[])
124 {
125 struct libmnt_test ts[] = {
126 { "--print", test_version, "print versions" },
127 { NULL }
128 };
129
130 return mnt_run_test(ts, argc, argv);
131 }
132 #endif