]> git.ipfire.org Git - thirdparty/util-linux.git/blob - libmount/src/version.c
libmount; fix and improve read+poll mountinfo
[thirdparty/util-linux.git] / libmount / src / version.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 /*
3 * This file is part of libmount from util-linux project.
4 *
5 * Copyright (C) 2008-2018 Karel Zak <kzak@redhat.com>
6 *
7 * libmount is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation; either version 2.1 of the License, or
10 * (at your option) any later version.
11 */
12
13 /**
14 * SECTION: version-utils
15 * @title: Version functions
16 * @short_description: functions to get the library version.
17 */
18
19 #include <ctype.h>
20
21 #include "mountP.h"
22
23 static const char *lib_version = LIBMOUNT_VERSION;
24 static const char *lib_features[] = {
25 #ifdef HAVE_LIBSELINUX
26 "selinux",
27 #endif
28 #ifdef HAVE_SMACK
29 "smack",
30 #endif
31 #ifdef HAVE_BTRFS_SUPPORT
32 "btrfs",
33 #endif
34 #ifdef USE_LIBMOUNT_SUPPORT_MTAB
35 "mtab",
36 #endif
37 #ifdef USE_LIBMOUNT_SUPPORT_NAMESPACES
38 "namespaces",
39 #endif
40 #if !defined(NDEBUG)
41 "assert", /* libc assert.h stuff */
42 #endif
43 "debug", /* always enabled */
44 NULL
45 };
46
47 /**
48 * mnt_parse_version_string:
49 * @ver_string: version string (e.g "2.18.0")
50 *
51 * Returns: release version code.
52 */
53 int mnt_parse_version_string(const char *ver_string)
54 {
55 const char *cp;
56 int version = 0;
57
58 assert(ver_string);
59
60 for (cp = ver_string; *cp; cp++) {
61 if (*cp == '.')
62 continue;
63 if (!isdigit(*cp))
64 break;
65 version = (version * 10) + (*cp - '0');
66 }
67 return version;
68 }
69
70 /**
71 * mnt_get_library_version:
72 * @ver_string: return pointer to the static library version string if not NULL
73 *
74 * Returns: release version number.
75 */
76 int mnt_get_library_version(const char **ver_string)
77 {
78 if (ver_string)
79 *ver_string = lib_version;
80
81 return mnt_parse_version_string(lib_version);
82 }
83
84 /**
85 * mnt_get_library_features:
86 * @features: returns a pointer to the static array of strings, the array is
87 * terminated by NULL.
88 *
89 * Returns: number of items in the features array not including the last NULL,
90 * or less than zero in case of error
91 *
92 * Example:
93 * <informalexample>
94 * <programlisting>
95 * const char **features;
96 *
97 * mnt_get_library_features(&features);
98 * while (features && *features)
99 * printf("%s\n", *features++);
100 * </programlisting>
101 * </informalexample>
102 *
103 */
104 int mnt_get_library_features(const char ***features)
105 {
106 if (!features)
107 return -EINVAL;
108
109 *features = lib_features;
110 return ARRAY_SIZE(lib_features) - 1;
111 }
112
113 #ifdef TEST_PROGRAM
114 static int test_version(struct libmnt_test *ts, int argc, char *argv[])
115 {
116 const char *ver;
117 const char **features;
118
119 if (argc == 2)
120 printf("Your version: %d\n",
121 mnt_parse_version_string(argv[1]));
122
123 mnt_get_library_version(&ver);
124
125 printf("Library version: %s\n", ver);
126 printf("Library API version: " LIBMOUNT_VERSION "\n");
127 printf("Library features:");
128
129 mnt_get_library_features(&features);
130 while (features && *features)
131 printf(" %s", *features++);
132
133 printf("\n");
134
135 if (mnt_get_library_version(NULL) ==
136 mnt_parse_version_string(LIBMOUNT_VERSION))
137 return 0;
138
139 return -1;
140 }
141
142 int main(int argc, char *argv[])
143 {
144 struct libmnt_test ts[] = {
145 { "--print", test_version, "print versions" },
146 { NULL }
147 };
148
149 return mnt_run_test(ts, argc, argv);
150 }
151 #endif