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.
8 /***************************************************************************
10 * Author : Per Foreby, perf@efd.lth.se *
12 ***************************************************************************/
14 #include "form.priv.h"
16 MODULE_ID("$Id: fty_ipv4.c,v 1.6 2004/05/29 19:13:48 tom Exp $")
18 /*---------------------------------------------------------------------------
20 | Function : static bool Check_IPV4_Field(
24 | Description : Validate buffer content to be a valid IP number (Ver. 4)
26 | Return Values : TRUE - field is valid
27 | FALSE - field is invalid
28 +--------------------------------------------------------------------------*/
30 Check_IPV4_Field(FIELD *field, const void *argp GCC_UNUSED)
32 char *bp = field_buffer(field, 0);
34 unsigned int d1, d2, d3, d4;
36 if (isdigit(UChar(*bp))) /* Must start with digit */
38 num = sscanf(bp, "%u.%u.%u.%u%n", &d1, &d2, &d3, &d4, &len);
41 bp += len; /* Make bp point to what sscanf() left */
42 while (*bp && isspace(UChar(*bp)))
43 bp++; /* Allow trailing whitespace */
46 return ((num != 4 || *bp || d1 > 255 || d2 > 255
47 || d3 > 255 || d4 > 255) ? FALSE : TRUE);
50 /*---------------------------------------------------------------------------
52 | Function : static bool Check_IPV4_Character(
56 | Description : Check a character for unsigned type or period.
58 | Return Values : TRUE - character is valid
59 | FALSE - character is invalid
60 +--------------------------------------------------------------------------*/
62 Check_IPV4_Character(int c, const void *argp GCC_UNUSED)
64 return ((isdigit(UChar(c)) || (c == '.')) ? TRUE : FALSE);
67 static FIELDTYPE typeIPV4 =
70 1, /* this is mutable, so we can't be const */
82 NCURSES_EXPORT_VAR(FIELDTYPE*) TYPE_IPV4 = &typeIPV4;
84 /* fty_ipv4.c ends here */