summaryrefslogtreecommitdiff
path: root/lib/libform/fty_ipv4.c
blob: 0cd429fdefaef3b368b1e2a62ef446b1ab70f3c4 (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
72
73
74
75
76
77
78
79
80
81
82
83
/*	$OpenBSD: fty_ipv4.c,v 1.5 2007/03/20 03:40:05 tedu Exp $	*/


/*
 * THIS CODE IS SPECIFICALLY EXEMPTED FROM THE NCURSES PACKAGE COPYRIGHT.
 * You may freely copy it for use as a template for your own field types.
 * If you develop a field type that might be of general use, please send
 * it back to the ncurses maintainers for inclusion in the next version.
 */
/***************************************************************************
*                                                                          *
*  Author : Per Foreby, perf@efd.lth.se                                    *
*                                                                          *
***************************************************************************/

#include "form.priv.h"

MODULE_ID("$From: fty_ipv4.c,v 1.4 2000/12/09 23:46:12 tom Exp $")

/*---------------------------------------------------------------------------
|   Facility      :  libnform  
|   Function      :  static bool Check_IPV4_Field(
|                                      FIELD * field,
|                                      const void * argp)
|   
|   Description   :  Validate buffer content to be a valid IP number (Ver. 4)
|
|   Return Values :  TRUE  - field is valid
|                    FALSE - field is invalid
+--------------------------------------------------------------------------*/
static bool Check_IPV4_Field(FIELD * field, const void * argp GCC_UNUSED)
{
  char *bp = field_buffer(field,0);
  int num = 0, len;
  unsigned int d1, d2, d3, d4;

  if(isdigit((unsigned char)*bp)) /* Must start with digit */
    {
      num = sscanf(bp, "%u.%u.%u.%u%n", &d1, &d2, &d3, &d4, &len);
      if (num == 4)
        {
          bp += len;            /* Make bp point to what sscanf() left */
          while (isspace((unsigned char)*bp))
            bp++;               /* Allow trailing whitespace */
        }
    }
  return ((num != 4 || *bp || d1 > 255 || d2 > 255
                           || d3 > 255 || d4 > 255) ? FALSE : TRUE);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform  
|   Function      :  static bool Check_IPV4_Character(
|                                      int c, 
|                                      const void *argp )
|   
|   Description   :  Check a character for unsigned type or period.
|
|   Return Values :  TRUE  - character is valid
|                    FALSE - character is invalid
+--------------------------------------------------------------------------*/
static bool Check_IPV4_Character(int c, const void * argp GCC_UNUSED)
{
  return ((isdigit(c) || (c=='.')) ? TRUE : FALSE);
}

static FIELDTYPE typeIPV4 = {
  _RESIDENT,
  1,                           /* this is mutable, so we can't be const */
  (FIELDTYPE *)0,
  (FIELDTYPE *)0,
  NULL,
  NULL,
  NULL,
  Check_IPV4_Field,
  Check_IPV4_Character,
  NULL,
  NULL
};

NCURSES_EXPORT_VAR(FIELDTYPE*) TYPE_IPV4 = &typeIPV4;

/* fty_ipv4.c ends here */