summaryrefslogtreecommitdiff
blob: 7e754bd3651c4f8199d00f7459d076ffce4d6a71 (plain)
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
/**** POSIX *******************************************************************/
#include <stdlib.h>

/******************************************************************************/
/**** LOCAL FUNCTIONS *********************************************************/
/******************************************************************************/

/******************************************************************************/
/**** EXPORTED FUNCTIONS ******************************************************/
/******************************************************************************/
int relabsd_util_parse_int
(
   const char string [const restrict static 1],
   const int min,
   const int max,
   int output [const restrict static 1]
)
{
   char * invalid_char; /* may become an alias of string. */
   long int buffer;

   buffer = strtol(string, &invalid_char, 10);

   if ((invalid_char[0] != '\0') || (string[0] == '\0'))
   {
      return -1;
   }

   if ((buffer < ((long int) min)) || (buffer > ((long int) max)))
   {
      return -2;
   }

   *output = ((int) buffer);
   return 0;
}

/*
 * Returns -1 on error,
 *          0 on EOF,
 *          1 on newline.
 */
int relabsd_util_reach_next_line_or_eof (FILE f [const restrict static 1])
{
   char c;

   c = (char) getc(f);

   while ((c != '\n') && c != EOF)
   {
      c = (char) getc(f);
   }

   if (ferror(f))
   {
      /*
       * The 'ferror' function's manual specifically states that it does not
       * sets errno. There is no mention of errno in the 'getc' function's
       * either, so I am assuming that errno cannot be used to indicate the
       * error.
       */
      return -1;
   }

   if (c == EOF)
   {
      return 0;
   }

   return 1;
}