git: 13f6bf4f841c - stable/14 - tftp: Replace fgets with getline
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Wed, 27 May 2026 09:03:44 UTC
The branch stable/14 has been updated by des:
URL: https://cgit.FreeBSD.org/src/commit/?id=13f6bf4f841cc799d97c69e5cea502b764183846
commit 13f6bf4f841cc799d97c69e5cea502b764183846
Author: Dag-Erling Smørgrav <des@FreeBSD.org>
AuthorDate: 2026-05-22 17:57:20 +0000
Commit: Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2026-05-27 09:03:29 +0000
tftp: Replace fgets with getline
MFC after: 1 week
Reviewed by: markj
Differential Revision: https://reviews.freebsd.org/D57072
(cherry picked from commit 5fd928cf1cef21e0fc20ab7c2be156a0eeecdf40)
---
usr.bin/tftp/main.c | 90 ++++++++++++++++++++++++++---------------------------
1 file changed, 45 insertions(+), 45 deletions(-)
diff --git a/usr.bin/tftp/main.c b/usr.bin/tftp/main.c
index 10343bff412e..3a26903a2076 100644
--- a/usr.bin/tftp/main.c
+++ b/usr.bin/tftp/main.c
@@ -75,7 +75,6 @@ static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 6/6/93";
#include "tftp-options.h"
#include "tftp.h"
-#define MAXLINE (2 * MAXPATHLEN)
#define TIMEOUT 5 /* secs between rexmt's */
typedef struct sockaddr_storage peeraddr;
@@ -85,7 +84,7 @@ static jmp_buf toplevel;
static int txrx_error;
static int peer;
-#define MAX_MARGV 20
+#define MAX_MARGV 32
static int margc;
static char *margv[MAX_MARGV];
@@ -119,7 +118,7 @@ static const char *command_prompt(void);
static void urihandling(char *URI);
static void getusage(char *);
-static void makeargv(char *line);
+static void makeargv(char *argv0, char *line);
static void putusage(char *);
static void settftpmode(const char *);
@@ -351,13 +350,13 @@ setpeer0(char *host, const char *lport)
static void
setpeer(int argc, char *argv[])
{
- char line[MAXLINE];
+ static char *line;
+ static size_t sz;
if (argc < 2) {
- strcpy(line, "Connect ");
printf("(to) ");
- fgets(&line[strlen(line)], sizeof line - strlen(line), stdin);
- makeargv(line);
+ getline(&line, &sz, stdin);
+ makeargv(argv[0], line);
argc = margc;
argv = margv;
}
@@ -434,17 +433,16 @@ settftpmode(const char *newmode)
static void
put(int argc, char *argv[])
{
- int fd;
- int n;
- char *cp, *targ, *path;
- char line[MAXLINE];
+ static char *line;
+ static size_t sz;
+ int fd, n;
+ char *cp, *targ, *path;
struct stat sb;
if (argc < 2) {
- strcpy(line, "send ");
printf("(file) ");
- fgets(&line[strlen(line)], sizeof line - strlen(line), stdin);
- makeargv(line);
+ getline(&line, &sz, stdin);
+ makeargv(argv[0], line);
argc = margc;
argv = margv;
}
@@ -544,17 +542,15 @@ putusage(char *s)
static void
get(int argc, char *argv[])
{
- int fd;
- int n;
- char *cp;
- char *src;
- char line[MAXLINE];
+ static char *line;
+ static size_t sz;
+ int fd, n;
+ char *cp, *src;
if (argc < 2) {
- strcpy(line, "get ");
printf("(files) ");
- fgets(&line[strlen(line)], sizeof line - strlen(line), stdin);
- makeargv(line);
+ getline(&line, &sz, stdin);
+ makeargv(argv[0], line);
argc = margc;
argv = margv;
}
@@ -634,14 +630,14 @@ getusage(char *s)
static void
settimeoutpacket(int argc, char *argv[])
{
+ static char *line;
+ static size_t sz;
int t;
- char line[MAXLINE];
if (argc < 2) {
- strcpy(line, "Packet timeout ");
printf("(value) ");
- fgets(&line[strlen(line)], sizeof line - strlen(line), stdin);
- makeargv(line);
+ getline(&line, &sz, stdin);
+ makeargv(argv[0], line);
argc = margc;
argv = margv;
}
@@ -661,14 +657,14 @@ settimeoutpacket(int argc, char *argv[])
static void
settimeoutnetwork(int argc, char *argv[])
{
+ static char *line;
+ static size_t sz;
int t;
- char line[MAXLINE];
if (argc < 2) {
- strcpy(line, "Network timeout ");
printf("(value) ");
- fgets(&line[strlen(line)], sizeof line - strlen(line), stdin);
- makeargv(line);
+ getline(&line, &sz, stdin);
+ makeargv(argv[0], line);
argc = margc;
argv = margv;
}
@@ -744,23 +740,22 @@ command_prompt(void)
static void
command(bool interactive, EditLine *el, History *hist, HistEvent *hep)
{
+ static char *line;
+ static size_t sz;
const struct cmd *c;
const char *bp;
- char *cp;
- int len, num;
- char line[MAXLINE];
+ int len;
for (;;) {
if (interactive) {
- if ((bp = el_gets(el, &num)) == NULL || num == 0)
+ if ((bp = el_gets(el, &len)) == NULL || len == 0)
exit(0);
- len = MIN(MAXLINE, num);
- memcpy(line, bp, len);
- line[len - 1] = '\0';
- history(hist, hep, H_ENTER, bp);
+ if ((size_t)len >= sz)
+ line = realloc(line, sz = len + 1);
+ strlcpy(line, bp, sz);
+ history(hist, hep, H_ENTER, line);
} else {
- line[0] = 0;
- if (fgets(line, sizeof line , stdin) == NULL) {
+ if ((len = getline(&line, &sz, stdin)) <= 0) {
if (feof(stdin)) {
exit(txrx_error);
} else {
@@ -768,11 +763,11 @@ command(bool interactive, EditLine *el, History *hist, HistEvent *hep)
}
}
}
- if ((cp = strchr(line, '\n')))
- *cp = '\0';
+ if (line[len - 1] == '\n')
+ line[--len] = '\0';
if (line[0] == 0)
continue;
- makeargv(line);
+ makeargv(NULL, line);
if (margc == 0)
continue;
c = getcmd(margv[0]);
@@ -821,12 +816,16 @@ getcmd(const char *name)
* Slice a string up into argc/argv.
*/
static void
-makeargv(char *line)
+makeargv(char *argv0, char *line)
{
char *cp;
char **argp = margv;
margc = 0;
+ if (argv0 != NULL) {
+ *argp++ = argv0;
+ margc++;
+ }
if ((cp = strchr(line, '\n')) != NULL)
*cp = '\0';
for (cp = line; margc < MAX_MARGV - 1 && *cp != '\0';) {
@@ -835,13 +834,14 @@ makeargv(char *line)
if (*cp == '\0')
break;
*argp++ = cp;
- margc += 1;
+ margc++;
while (*cp != '\0' && !isspace(*cp))
cp++;
if (*cp == '\0')
break;
*cp++ = '\0';
}
+ /* XXX warn about truncation if *cp != '\0'? */
*argp++ = 0;
}