1 /****************************************************************************
2 * Copyright 2020,2021 Thomas E. Dickey *
3 * Copyright 1998-2006,2009 Free Software Foundation, Inc. *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
28 ****************************************************************************/
30 /***************************************************************************
32 * Author : Per Foreby, perf@efd.lth.se *
34 ***************************************************************************/
36 #include "form.priv.h"
38 MODULE_ID("$Id: fty_ipv4.c,v 1.16 2021/06/17 21:11:08 tom Exp $")
40 /*---------------------------------------------------------------------------
42 | Function : static bool Check_IPV4_Field(
46 | Description : Validate buffer content to be a valid IP number (Ver. 4)
48 | Return Values : TRUE - field is valid
49 | FALSE - field is invalid
50 +--------------------------------------------------------------------------*/
52 Check_IPV4_Field(FIELD *field, const void *argp GCC_UNUSED)
54 char *bp = field_buffer(field, 0);
56 unsigned int d1 = 0, d2 = 0, d3 = 0, d4 = 0;
58 if (isdigit(UChar(*bp))) /* Must start with digit */
60 num = sscanf(bp, "%u.%u.%u.%u%n", &d1, &d2, &d3, &d4, &len);
63 bp += len; /* Make bp point to what sscanf() left */
64 while (isspace(UChar(*bp)))
65 bp++; /* Allow trailing whitespace */
68 return ((num != 4 || *bp || d1 > 255 || d2 > 255
69 || d3 > 255 || d4 > 255) ? FALSE : TRUE);
72 /*---------------------------------------------------------------------------
74 | Function : static bool Check_IPV4_Character(
78 | Description : Check a character for unsigned type or period.
80 | Return Values : TRUE - character is valid
81 | FALSE - character is invalid
82 +--------------------------------------------------------------------------*/
84 Check_IPV4_Character(int c, const void *argp GCC_UNUSED)
86 return ((isdigit(UChar(c)) || (c == '.')) ? TRUE : FALSE);
89 static FIELDTYPE typeIPV4 =
92 1, /* this is mutable, so we can't be const */
98 INIT_FT_FUNC(Check_IPV4_Field),
99 INIT_FT_FUNC(Check_IPV4_Character),
102 #if NCURSES_INTEROP_FUNCS
107 FORM_EXPORT_VAR(FIELDTYPE *) TYPE_IPV4 = &typeIPV4;
109 #if NCURSES_INTEROP_FUNCS
110 /* The next routines are to simplify the use of ncurses from
111 programming languages with restrictions on interop with C level
112 constructs (e.g. variable access or va_list + ellipsis constructs)
114 FORM_EXPORT(FIELDTYPE *)
121 /* fty_ipv4.c ends here */