2007-10-07 11:44:02 +00:00
|
|
|
/*
|
2021-12-05 09:35:27 -06:00
|
|
|
* SPDX-FileCopyrightText: 1990 , Julianne Frances Haugh
|
|
|
|
* SPDX-FileCopyrightText: 1996 - 1997, Marek Michałkiewicz
|
|
|
|
* SPDX-FileCopyrightText: 2003 - 2005, Tomasz Kłoczko
|
|
|
|
* SPDX-FileCopyrightText: 2007 , Nicolas François
|
2007-10-07 11:44:02 +00:00
|
|
|
*
|
2021-12-05 09:35:27 -06:00
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
2007-10-07 11:44:02 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2007-11-10 23:46:11 +00:00
|
|
|
#ident "$Id$"
|
2007-10-07 11:47:01 +00:00
|
|
|
|
2007-10-07 11:44:02 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "prototypes.h"
|
2008-05-24 15:19:02 +00:00
|
|
|
|
2007-10-07 11:44:02 +00:00
|
|
|
/*
|
|
|
|
* valid_field - insure that a field contains all legal characters
|
|
|
|
*
|
2008-04-27 00:24:49 +00:00
|
|
|
* The supplied field is scanned for non-printable and other illegal
|
|
|
|
* characters.
|
2023-03-23 23:39:38 +00:00
|
|
|
* + -1 is returned if an illegal or control character is present.
|
|
|
|
* + 1 is returned if no illegal or control characters are present,
|
|
|
|
* but the field contains a non-printable character.
|
2008-04-27 00:24:49 +00:00
|
|
|
* + 0 is returned otherwise.
|
2007-10-07 11:44:02 +00:00
|
|
|
*/
|
2007-10-07 11:45:23 +00:00
|
|
|
int valid_field (const char *field, const char *illegal)
|
2007-10-07 11:44:02 +00:00
|
|
|
{
|
|
|
|
const char *cp;
|
2008-04-27 00:24:49 +00:00
|
|
|
int err = 0;
|
2007-10-07 11:44:02 +00:00
|
|
|
|
2011-07-08 19:56:18 +00:00
|
|
|
if (NULL == field) {
|
2011-02-16 20:32:16 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-05-24 15:19:02 +00:00
|
|
|
/* For each character of field, search if it appears in the list
|
|
|
|
* of illegal characters. */
|
2023-03-31 14:46:50 +02:00
|
|
|
if (illegal && NULL != strpbrk (field, illegal)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Search if there are non-printable or control characters */
|
2008-05-24 15:19:02 +00:00
|
|
|
for (cp = field; '\0' != *cp; cp++) {
|
2023-03-31 14:46:50 +02:00
|
|
|
unsigned char c = *cp;
|
|
|
|
if (!isprint (c)) {
|
|
|
|
err = 1;
|
|
|
|
}
|
|
|
|
if (iscntrl (c)) {
|
2008-05-24 15:19:02 +00:00
|
|
|
err = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-04-27 00:24:49 +00:00
|
|
|
|
|
|
|
return err;
|
2007-10-07 11:44:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* change_field - change a single field if a new value is given.
|
|
|
|
*
|
|
|
|
* prompt the user with the name of the field being changed and the
|
|
|
|
* current value.
|
|
|
|
*/
|
2007-10-07 11:45:23 +00:00
|
|
|
void change_field (char *buf, size_t maxsize, const char *prompt)
|
2007-10-07 11:44:02 +00:00
|
|
|
{
|
|
|
|
char newf[200];
|
|
|
|
char *cp;
|
|
|
|
|
2008-05-24 15:19:02 +00:00
|
|
|
if (maxsize > sizeof (newf)) {
|
2007-10-07 11:45:23 +00:00
|
|
|
maxsize = sizeof (newf);
|
2008-05-24 15:19:02 +00:00
|
|
|
}
|
2007-10-07 11:44:02 +00:00
|
|
|
|
|
|
|
printf ("\t%s [%s]: ", prompt, buf);
|
2008-05-24 15:19:02 +00:00
|
|
|
(void) fflush (stdout);
|
2023-02-01 13:50:48 +01:00
|
|
|
if (fgets (newf, maxsize, stdin) != newf) {
|
2007-10-07 11:44:02 +00:00
|
|
|
return;
|
2008-05-24 15:19:02 +00:00
|
|
|
}
|
2007-10-07 11:44:02 +00:00
|
|
|
|
2008-05-24 15:19:02 +00:00
|
|
|
cp = strchr (newf, '\n');
|
|
|
|
if (NULL == cp) {
|
2007-10-07 11:44:02 +00:00
|
|
|
return;
|
2008-05-24 15:19:02 +00:00
|
|
|
}
|
2007-10-07 11:44:02 +00:00
|
|
|
*cp = '\0';
|
|
|
|
|
2008-05-24 15:19:02 +00:00
|
|
|
if ('\0' != newf[0]) {
|
2007-10-07 11:44:02 +00:00
|
|
|
/*
|
|
|
|
* Remove leading and trailing whitespace. This also
|
|
|
|
* makes it possible to change the field to empty, by
|
|
|
|
* entering a space. --marekm
|
|
|
|
*/
|
|
|
|
|
2023-03-11 13:43:36 -08:00
|
|
|
while (newf < cp && isspace (cp[-1])) {
|
|
|
|
cp--;
|
|
|
|
}
|
2008-05-24 15:19:02 +00:00
|
|
|
*cp = '\0';
|
2007-10-07 11:44:02 +00:00
|
|
|
|
|
|
|
cp = newf;
|
2023-03-11 13:41:54 -08:00
|
|
|
while (isspace (*cp)) {
|
2007-10-07 11:44:02 +00:00
|
|
|
cp++;
|
2008-05-24 15:19:02 +00:00
|
|
|
}
|
2007-10-07 11:44:02 +00:00
|
|
|
|
2023-03-11 00:01:02 -08:00
|
|
|
strcpy (buf, cp);
|
2007-10-07 11:44:02 +00:00
|
|
|
}
|
|
|
|
}
|