(root)/
util-linux-2.39/
tests/
helpers/
test_sysinfo.c
       1  /*
       2   * SPDX-License-Identifier: GPL-2.0-or-later
       3   *
       4   * Copyright (C) 2007 Karel Zak <kzak@redhat.com>
       5   *
       6   * This file is part of util-linux.
       7   *
       8   * This file is free software; you can redistribute it and/or modify
       9   * it under the terms of the GNU General Public License as published by
      10   * the Free Software Foundation; either version 2 of the License, or
      11   * (at your option) any later version.
      12   *
      13   * This file is distributed in the hope that it will be useful,
      14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
      15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      16   * GNU General Public License for more details.
      17   */
      18  
      19  #include <stdio.h>
      20  #include <stdlib.h>
      21  #include <string.h>
      22  #include <unistd.h>
      23  #include <limits.h>
      24  #include <stdint.h>
      25  #include <inttypes.h>
      26  #include <wchar.h>
      27  #include <errno.h>
      28  #include <sys/ioctl.h>
      29  #include <sys/mount.h>
      30  
      31  #include "mount-api-utils.h"
      32  
      33  typedef struct {
      34  	const char	*name;
      35  	int		(*fnc)(void);
      36  } mntHlpfnc;
      37  
      38  static int hlp_wordsize(void)
      39  {
      40  	printf("%zu\n", CHAR_BIT * sizeof(void*));
      41  	return 0;
      42  }
      43  
      44  static int hlp_endianness(void)
      45  {
      46  #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
      47  	printf("LE\n");
      48  #else
      49  	printf("BE\n");
      50  #endif
      51  	return 0;
      52  }
      53  
      54  
      55  static int hlp_pagesize(void)
      56  {
      57  	printf("%d\n", getpagesize());
      58  	return 0;
      59  }
      60  
      61  static int hlp_int_max(void)
      62  {
      63  	printf("%d\n", INT_MAX);
      64  	return 0;
      65  }
      66  
      67  static int hlp_uint_max(void)
      68  {
      69  	printf("%u\n", UINT_MAX);
      70  	return 0;
      71  }
      72  
      73  static int hlp_long_max(void)
      74  {
      75  	printf("%ld\n", LONG_MAX);
      76  	return 0;
      77  }
      78  
      79  static int hlp_ulong_max(void)
      80  {
      81  	printf("%lu\n", ULONG_MAX);
      82  	return 0;
      83  }
      84  
      85  static int hlp_u64_max(void)
      86  {
      87  	printf("%" PRIu64 "\n", UINT64_MAX);
      88  	return 0;
      89  }
      90  
      91  static int hlp_ulong_max32(void)
      92  {
      93  #if __WORDSIZE == 64
      94  	printf("%lu\n", ULONG_MAX >> 32);
      95  #else
      96  	printf("%lu\n", ULONG_MAX);
      97  #endif
      98  	return 0;
      99  }
     100  
     101  static int hlp_wcsspn_ok(void)
     102  {
     103  	printf("%d\n", wcsspn(L"FOO", L"F") == 1);
     104  	return 0;
     105  }
     106  
     107  static int hlp_enotty_ok(void)
     108  {
     109  	errno = 0;
     110  	ioctl(STDOUT_FILENO, 0);
     111  
     112  	printf("%d\n", errno != ENOSYS);
     113  	return 0;
     114  }
     115  
     116  static int hlp_fsopen_ok(void)
     117  {
     118  #ifdef FSOPEN_CLOEXEC
     119  	errno = 0;
     120  	fsopen(NULL, FSOPEN_CLOEXEC);
     121  #else
     122  	errno = ENOSYS;
     123  #endif
     124  	printf("%d\n", errno != ENOSYS);
     125  	return 0;
     126  }
     127  
     128  static mntHlpfnc hlps[] =
     129  {
     130  	{ "WORDSIZE",	hlp_wordsize	},
     131  	{ "pagesize",	hlp_pagesize	},
     132  	{ "INT_MAX",	hlp_int_max	},
     133  	{ "UINT_MAX",   hlp_uint_max	},
     134  	{ "LONG_MAX",   hlp_long_max	},
     135  	{ "ULONG_MAX",  hlp_ulong_max	},
     136  	{ "ULONG_MAX32",hlp_ulong_max32	},
     137  	{ "UINT64_MAX", hlp_u64_max     },
     138  	{ "byte-order", hlp_endianness  },
     139  	{ "wcsspn-ok",  hlp_wcsspn_ok   },
     140  	{ "enotty-ok",  hlp_enotty_ok   },
     141  	{ "fsopen-ok",  hlp_fsopen_ok   },
     142  	{ NULL, NULL }
     143  };
     144  
     145  int main(int argc, char **argv)
     146  {
     147  	int re = 0;
     148  	mntHlpfnc *fn;
     149  
     150  	if (argc == 1) {
     151  		for (fn = hlps; fn->name; fn++) {
     152  			printf("%15s: ", fn->name);
     153  			re += fn->fnc();
     154  		}
     155  	} else {
     156  		int i;
     157  
     158  		if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0) {
     159  			printf("%s <option>\n", argv[0]);
     160  			fputs("options:\n", stdout);
     161  			for (fn = hlps; fn->name; fn++)
     162  				printf("\t%s\n", fn->name);
     163  			exit(EXIT_SUCCESS);
     164  		}
     165  
     166  		for (i=1; i < argc; i++) {
     167  			for (fn = hlps; fn->name; fn++) {
     168  				if (strcmp(fn->name, argv[i]) == 0)
     169  					re += fn->fnc();
     170  			}
     171  		}
     172  	}
     173  
     174  	exit(re ? EXIT_FAILURE : EXIT_SUCCESS);
     175  }
     176