fty_ipv4.c 2.9 KB

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