(root)/
util-linux-2.39/
libfdisk/
src/
version.c
       1  /*
       2   * version.c - Return the version of the library
       3   *
       4   * Copyright (C) 2015 Karel Zak <kzak@redhat.com>
       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, int argc, char *argv[])
      95  {
      96  	const char *ver;
      97  	const char **features;
      98  
      99  	fdisk_get_library_version(&ver);
     100  
     101  	printf("Library version: %s\n", ver);
     102  	printf("Library API version: " LIBFDISK_VERSION "\n");
     103  	printf("Library features:");
     104  
     105  	fdisk_get_library_features(&features);
     106  	while (features && *features)
     107  		printf(" %s", *features++);
     108  
     109  	if (fdisk_get_library_version(NULL) ==
     110  			fdisk_parse_version_string(LIBFDISK_VERSION))
     111  		return 0;
     112  
     113  	return -1;
     114  }
     115  
     116  int main(int argc, char *argv[])
     117  {
     118  	struct fdisk_test ts[] = {
     119  		{ "--print", test_version, "print versions" },
     120  		{ NULL }
     121  	};
     122  
     123  	return fdisk_run_test(ts, argc, argv);
     124  }
     125  #endif