From e8e26c0bc10213ab99d437992e8037093d6b6c0f Mon Sep 17 00:00:00 2001 From: Mike Kelly Date: Tue, 20 Dec 2011 10:27:53 -0500 Subject: increase portability getline() isn't widely available yet, use fgets() instead --- sshfs.c | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/sshfs.c b/sshfs.c index 8111f9c..2ba4659 100644 --- a/sshfs.c +++ b/sshfs.c @@ -37,6 +37,7 @@ #include #include #include +#include #include "cache.h" @@ -3483,23 +3484,23 @@ static int ssh_connect(void) return 0; } -/* remove trailing '\n', like the perl func of the same name */ -static inline void chomp(char *line) -{ - char *p = line; - if ((p = strrchr(line, '\n'))) - *p = '\0'; -} - /* number of ':' separated fields in a passwd/group file that we care * about */ #define IDMAP_FIELDS 3 /* given a line from a uidmap or gidmap, parse out the name and id */ static void parse_idmap_line(char *line, const char* filename, - const unsigned int lineno, uint32_t *ret_id, char **ret_name) + const unsigned int lineno, uint32_t *ret_id, char **ret_name, + const int eof) { - chomp(line); + /* chomp off the trailing newline */ + char *p = line; + if ((p = strrchr(line, '\n'))) + *p = '\0'; + else if (!eof) { + fprintf(stderr, "%s:%u: line too long\n", filename, lineno); + exit(1); + } char *tokens[IDMAP_FIELDS]; char *tok; int i; @@ -3517,8 +3518,7 @@ static void parse_idmap_line(char *line, const char* filename, name_tok = tokens[0]; id_tok = tokens[2]; } else { - fprintf(stderr, "Unknown format at line %u of '%s'\n", - lineno, filename); + fprintf(stderr, "%s:%u: unknown format\n", filename, lineno); exit(1); } @@ -3541,8 +3541,7 @@ static void read_id_map(char *file, uint32_t *(*map_fn)(char *), *idmap = g_hash_table_new(NULL, NULL); *r_idmap = g_hash_table_new(NULL, NULL); FILE *fp; - char *line = NULL; - size_t len = 0; + char line[LINE_MAX]; unsigned int lineno = 0; fp = fopen(file, "r"); @@ -3552,12 +3551,12 @@ static void read_id_map(char *file, uint32_t *(*map_fn)(char *), exit(1); } - while (getline(&line, &len, fp) != EOF) { + while (fgets(line, LINE_MAX, fp) != NULL) { lineno++; uint32_t remote_id; char *name; - parse_idmap_line(line, file, lineno, &remote_id, &name); + parse_idmap_line(line, file, lineno, &remote_id, &name, feof(fp)); uint32_t *local_id = map_fn(name); if (local_id == NULL) { @@ -3580,9 +3579,6 @@ static void read_id_map(char *file, uint32_t *(*map_fn)(char *), file, strerror(errno)); exit(1); } - - if (line) - free(line); } /* given a username, return a pointer to its uid, or NULL if it doesn't -- cgit v1.2.3