Super User's BSD Cross Reference: /OpenBSD/lib/libform/fty_ipv4.c

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

AltStyle によって変換されたページ (->オリジナル) /