getty: code cleanup, part 1

* Avoid unnecessary use of `unsigned char *`
* Use explicit casts when assigning `unsigned char *` to `char *` or vice versa
* Drop unused global variables (and fix memory leak in `gettable()`)
* Use `snprintf()` instead of `strcpy()` + `strcat()`
* Drop spurious braces in switch

Sponsored by:	Klara, Inc.
Obtained from:	Apple OSS Distributions (in part)
Differential Revision: https://reviews.freebsd.org/D37263
This commit is contained in:
Dag-Erling Smørgrav 2022-11-04 14:23:33 +01:00
parent f9349d4274
commit 8ad7a14ab4
5 changed files with 26 additions and 26 deletions

View file

@ -59,10 +59,10 @@ static volatile int alarmed = 0;
static void chat_alrm(int);
static int chat_unalarm(void);
static int getdigit(unsigned char **, int, int);
static int getdigit(char **, int, int);
static char **read_chat(char **);
static char *cleanchr(char **, unsigned char);
static const char *cleanstr(const unsigned char *, int);
static const char *cleanstr(const char *, int);
static const char *result(int);
static int chat_expect(const char *);
static int chat_send(char const *);
@ -104,7 +104,7 @@ chat_unalarm(void)
*/
static int
getdigit(unsigned char **ptr, int base, int max)
getdigit(char **ptr, int base, int max)
{
int i, val = 0;
char * q;
@ -149,10 +149,10 @@ read_chat(char **chatstr)
p != NULL;
p = strtok(NULL, ws))
{
unsigned char *q, *r;
char *q, *r;
/* Read escapes */
for (q = r = (unsigned char *)p; *r; ++q)
for (q = r = p; *r; ++q)
{
if (*q == '\\')
{
@ -271,9 +271,9 @@ cleanchr(char **buf, unsigned char ch)
*/
static const char *
cleanstr(const unsigned char *s, int l)
cleanstr(const char *s, int l)
{
static unsigned char * tmp = NULL;
static char * tmp = NULL;
static int tmplen = 0;
if (tmplen < l * 4 + 1)

View file

@ -48,7 +48,7 @@ const char *autobaud(void);
int delaybits(void);
void edithost(const char *);
void gendefaults(void);
void gettable(const char *, char *);
void gettable(const char *);
void makeenv(char *[]);
const char *portselector(void);
void set_ttydefaults(int);

View file

@ -53,7 +53,7 @@ static char nullstr[] = "";
static char loginprg[] = _PATH_LOGIN;
static char datefmt[] = "%+";
#define M(a) (&omode.c_cc[a])
#define M(a) (char *)(&omode.c_cc[a])
struct gettystrs gettystrs[] = {
{ "nx", NULL, NULL }, /* next table */

View file

@ -58,6 +58,7 @@ __FBSDID("$FreeBSD$");
#include <libutil.h>
#include <setjmp.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
@ -100,10 +101,7 @@ static char name[MAXLOGNAME*3];
static char ttyn[32];
#define OBUFSIZ 128
#define TABBUFSIZ 512
static char defent[TABBUFSIZ];
static char tabent[TABBUFSIZ];
static const char *tname;
static char *env[128];
@ -191,7 +189,7 @@ main(int argc, char *argv[])
gethostname(hostname, sizeof(hostname) - 1);
hostname[sizeof(hostname) - 1] = '\0';
if (hostname[0] == '\0')
strcpy(hostname, "Amnesiac");
snprintf(hostname, sizeof(hostname), "Amnesiac");
/*
* Limit running time to deal with broken or dead lines.
@ -201,7 +199,7 @@ main(int argc, char *argv[])
limit.rlim_cur = GETTY_TIMEOUT;
(void)setrlimit(RLIMIT_CPU, &limit);
gettable("default", defent);
gettable("default");
gendefaults();
tname = "default";
if (argc > 1)
@ -215,10 +213,9 @@ main(int argc, char *argv[])
* J. Gettys - MIT Project Athena.
*/
if (argc <= 2 || strcmp(argv[2], "-") == 0)
strcpy(ttyn, ttyname(STDIN_FILENO));
snprintf(ttyn, sizeof(ttyn), "%s", ttyname(STDIN_FILENO));
else {
strcpy(ttyn, _PATH_DEV);
strlcat(ttyn, argv[2], sizeof(ttyn));
snprintf(ttyn, sizeof(ttyn), "%s%s", _PATH_DEV, argv[2]);
if (strcmp(argv[0], "+") != 0) {
chown(ttyn, 0, 0);
chmod(ttyn, 0600);
@ -762,7 +759,7 @@ putf(const char *cp)
puts(editedhost);
break;
case 'd': {
case 'd':
t = (time_t)0;
(void)time(&t);
if (Lo)
@ -786,7 +783,6 @@ putf(const char *cp)
case 'v':
puts(kerninfo.version);
break;
}
case '%':
putchr('%');
@ -804,7 +800,7 @@ dogettytab(void)
{
/* Read the database entry. */
gettable(tname, tabent);
gettable(tname);
/*
* Avoid inheriting the parity values from the default entry

View file

@ -60,8 +60,9 @@ static const char rcsid[] =
* Get a table entry.
*/
void
gettable(const char *name, char *buf)
gettable(const char *name)
{
char *buf = NULL;
struct gettystrs *sp;
struct gettynums *np;
struct gettyflags *fp;
@ -155,6 +156,7 @@ gettable(const char *name, char *buf)
fp->value = 1 ^ fp->invrt;
}
}
free(buf);
}
void
@ -202,13 +204,15 @@ charnames[] = {
&SU, &DS, &RP, &FL, &WE, &LN, 0
};
#define CV(a) (char *)(&tmode.c_cc[a])
static char *
charvars[] = {
&tmode.c_cc[VERASE], &tmode.c_cc[VKILL], &tmode.c_cc[VINTR],
&tmode.c_cc[VQUIT], &tmode.c_cc[VSTART], &tmode.c_cc[VSTOP],
&tmode.c_cc[VEOF], &tmode.c_cc[VEOL], &tmode.c_cc[VSUSP],
&tmode.c_cc[VDSUSP], &tmode.c_cc[VREPRINT], &tmode.c_cc[VDISCARD],
&tmode.c_cc[VWERASE], &tmode.c_cc[VLNEXT], 0
CV(VERASE), CV(VKILL), CV(VINTR),
CV(VQUIT), CV(VSTART), CV(VSTOP),
CV(VEOF), CV(VEOL), CV(VSUSP),
CV(VDSUSP), CV(VREPRINT), CV(VDISCARD),
CV(VWERASE), CV(VLNEXT), 0
};
void