1 /****************************************************************************
2 * Copyright (c) 1998-2006,2009 Free Software Foundation, Inc. *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
27 ****************************************************************************/
29 /***************************************************************************
31 * Author : Per Foreby, perf@efd.lth.se *
33 ***************************************************************************/
35 #include "form.priv.h"
37 MODULE_ID("$Id: fty_ipv4.c,v 1.10 2009/11/07 20:17:58 tom Exp $")
39 /*---------------------------------------------------------------------------
41 | Function : static bool Check_IPV4_Field(
45 | Description : Validate buffer content to be a valid IP number (Ver. 4)
47 | Return Values : TRUE - field is valid
48 | FALSE - field is invalid
49 +--------------------------------------------------------------------------*/
51 Check_IPV4_Field(FIELD
*field
, const void *argp GCC_UNUSED
)
53 char *bp
= field_buffer(field
, 0);
55 unsigned int d1
, d2
, d3
, d4
;
57 if (isdigit(UChar(*bp
))) /* Must start with digit */
59 num
= sscanf(bp
, "%u.%u.%u.%u%n", &d1
, &d2
, &d3
, &d4
, &len
);
62 bp
+= len
; /* Make bp point to what sscanf() left */
63 while (isspace(UChar(*bp
)))
64 bp
++; /* Allow trailing whitespace */
67 return ((num
!= 4 || *bp
|| d1
> 255 || d2
> 255
68 || d3
> 255 || d4
> 255) ? FALSE
: TRUE
);
71 /*---------------------------------------------------------------------------
73 | Function : static bool Check_IPV4_Character(
77 | Description : Check a character for unsigned type or period.
79 | Return Values : TRUE - character is valid
80 | FALSE - character is invalid
81 +--------------------------------------------------------------------------*/
83 Check_IPV4_Character(int c
, const void *argp GCC_UNUSED
)
85 return ((isdigit(UChar(c
)) || (c
== '.')) ? TRUE
: FALSE
);
88 static FIELDTYPE typeIPV4
=
91 1, /* this is mutable, so we can't be const */
97 INIT_FT_FUNC(Check_IPV4_Field
),
98 INIT_FT_FUNC(Check_IPV4_Character
),
101 #if NCURSES_INTEROP_FUNCS
106 NCURSES_EXPORT_VAR(FIELDTYPE
*) TYPE_IPV4
= &typeIPV4
;
108 #if NCURSES_INTEROP_FUNCS
109 /* The next routines are to simplify the use of ncurses from
110 programming languages with restictions on interop with C level
111 constructs (e.g. variable access or va_list + ellipsis constructs)
113 NCURSES_EXPORT(FIELDTYPE
*)
120 /* fty_ipv4.c ends here */