File: getlimits.c

package info (click to toggle)
findutils 4.10.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 26,108 kB
  • sloc: ansic: 126,635; sh: 11,486; yacc: 1,852; makefile: 844; exp: 839; python: 66; sed: 16
file content (76 lines) | stat: -rw-r--r-- 2,228 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/* getlimits - print various platform dependent limits.
   Copyright (C) 2023-2024 Free Software Foundation, Inc.

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */

/* Based on 'getlimits' of GNU coreutils, written by Pádraig Brady.
 * Stripped down to a minimal version by Bernhard Voelker.  */

#include <config.h>             /* sets _FILE_OFFSET_BITS=64 etc. */
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <stdint.h>

#include "system.h"
#include "intprops.h"

#ifndef UID_T_MAX
# define UID_T_MAX TYPE_MAXIMUM (uid_t)
#endif

#ifndef GID_T_MAX
# define GID_T_MAX TYPE_MAXIMUM (gid_t)
#endif

#ifndef MIN
# define MIN(a,b) (a<b?a:b)
#endif

/* Add one to the absolute value of the number whose textual
   representation is BUF + 1.  Do this in-place, in the buffer.
   Return a pointer to the result, which is normally BUF + 1, but is
   BUF if the representation grew in size.  */
static char const *
decimal_absval_add_one (char *buf)
{
  bool negative = (buf[1] == '-');
  char *absnum = buf + 1 + negative;
  char *p = absnum + strlen (absnum);
  absnum[-1] = '0';
  while (*--p == '9')
    *p = '0';
  ++*p;
  char *result = MIN (absnum, p);
  if (negative)
    *--result = '-';
  return result;
}

int
main (int argc, char **argv)
{
  char limit[100];

#define print_int(TYPE)                                      \
  sprintf (limit + 1, "%" "ju", (uintmax_t) TYPE##_MAX);     \
  printf (#TYPE"_MAX=%s\n", limit + 1);                      \
  printf (#TYPE"_OFLOW=%s\n", decimal_absval_add_one (limit))

  print_int (INT);
  print_int (UID_T);
  print_int (GID_T);

  return EXIT_SUCCESS;
}