]> ncurses.scripts.mit.edu Git - ncurses.git/blob - form/fty_ipv4.c
ncurses 5.4
[ncurses.git] / form / fty_ipv4.c
1
2 /*
3  * THIS CODE IS SPECIFICALLY EXEMPTED FROM THE NCURSES PACKAGE COPYRIGHT.
4  * You may freely copy it for use as a template for your own field types.
5  * If you develop a field type that might be of general use, please send
6  * it back to the ncurses maintainers for inclusion in the next version.
7  */
8 /***************************************************************************
9 *                                                                          *
10 *  Author : Per Foreby, perf@efd.lth.se                                    *
11 *                                                                          *
12 ***************************************************************************/
13
14 #include "form.priv.h"
15
16 MODULE_ID("$Id: fty_ipv4.c,v 1.4 2000/12/09 23:46:12 tom Exp $")
17
18 /*---------------------------------------------------------------------------
19 |   Facility      :  libnform  
20 |   Function      :  static bool Check_IPV4_Field(
21 |                                      FIELD * field,
22 |                                      const void * argp)
23 |   
24 |   Description   :  Validate buffer content to be a valid IP number (Ver. 4)
25 |
26 |   Return Values :  TRUE  - field is valid
27 |                    FALSE - field is invalid
28 +--------------------------------------------------------------------------*/
29 static bool Check_IPV4_Field(FIELD * field, const void * argp GCC_UNUSED)
30 {
31   char *bp = field_buffer(field,0);
32   int num = 0, len;
33   unsigned int d1, d2, d3, d4;
34
35   if(isdigit((unsigned char)*bp)) /* Must start with digit */
36     {
37       num = sscanf(bp, "%u.%u.%u.%u%n", &d1, &d2, &d3, &d4, &len);
38       if (num == 4)
39         {
40           bp += len;            /* Make bp point to what sscanf() left */
41           while (*bp && isspace((unsigned char)*bp))
42             bp++;               /* Allow trailing whitespace */
43         }
44     }
45   return ((num != 4 || *bp || d1 > 255 || d2 > 255
46                            || d3 > 255 || d4 > 255) ? FALSE : TRUE);
47 }
48
49 /*---------------------------------------------------------------------------
50 |   Facility      :  libnform  
51 |   Function      :  static bool Check_IPV4_Character(
52 |                                      int c, 
53 |                                      const void *argp )
54 |   
55 |   Description   :  Check a character for unsigned type or period.
56 |
57 |   Return Values :  TRUE  - character is valid
58 |                    FALSE - character is invalid
59 +--------------------------------------------------------------------------*/
60 static bool Check_IPV4_Character(int c, const void * argp GCC_UNUSED)
61 {
62   return ((isdigit(c) || (c=='.')) ? TRUE : FALSE);
63 }
64
65 static FIELDTYPE typeIPV4 = {
66   _RESIDENT,
67   1,                           /* this is mutable, so we can't be const */
68   (FIELDTYPE *)0,
69   (FIELDTYPE *)0,
70   NULL,
71   NULL,
72   NULL,
73   Check_IPV4_Field,
74   Check_IPV4_Character,
75   NULL,
76   NULL
77 };
78
79 NCURSES_EXPORT_VAR(FIELDTYPE*) TYPE_IPV4 = &typeIPV4;
80
81 /* fty_ipv4.c ends here */