Merge "Cleanup fs_getline from fs_mgr"

This commit is contained in:
Elliott Hughes 2013-09-18 23:26:50 +00:00 committed by Gerrit Code Review
commit 810ff69609

View file

@ -213,71 +213,13 @@ out:
return f; return f;
} }
/* Read a line of text till the next newline character.
* If no newline is found before the buffer is full, continue reading till a new line is seen,
* then return an empty buffer. This effectively ignores lines that are too long.
* On EOF, return null.
*/
static char *fs_getline(char *buf, int size, FILE *file)
{
int cnt = 0;
int eof = 0;
int eol = 0;
int c;
if (size < 1) {
return NULL;
}
while (cnt < (size - 1)) {
c = getc(file);
if (c == EOF) {
eof = 1;
break;
}
*(buf + cnt) = c;
cnt++;
if (c == '\n') {
eol = 1;
break;
}
}
/* Null terminate what we've read */
*(buf + cnt) = '\0';
if (eof) {
if (cnt) {
return buf;
} else {
return NULL;
}
} else if (eol) {
return buf;
} else {
/* The line is too long. Read till a newline or EOF.
* If EOF, return null, if newline, return an empty buffer.
*/
while(1) {
c = getc(file);
if (c == EOF) {
return NULL;
} else if (c == '\n') {
*buf = '\0';
return buf;
}
}
}
}
struct fstab *fs_mgr_read_fstab(const char *fstab_path) struct fstab *fs_mgr_read_fstab(const char *fstab_path)
{ {
FILE *fstab_file; FILE *fstab_file;
int cnt, entries; int cnt, entries;
int len; ssize_t len;
char line[256]; size_t alloc_len = 0;
char *line = NULL;
const char *delim = " \t"; const char *delim = " \t";
char *save_ptr, *p; char *save_ptr, *p;
struct fstab *fstab; struct fstab *fstab;
@ -296,9 +238,8 @@ struct fstab *fs_mgr_read_fstab(const char *fstab_path)
} }
entries = 0; entries = 0;
while (fs_getline(line, sizeof(line), fstab_file)) { while ((len = getline(&line, &alloc_len, fstab_file)) != -1) {
/* if the last character is a newline, shorten the string by 1 byte */ /* if the last character is a newline, shorten the string by 1 byte */
len = strlen(line);
if (line[len - 1] == '\n') { if (line[len - 1] == '\n') {
line[len - 1] = '\0'; line[len - 1] = '\0';
} }
@ -315,7 +256,7 @@ struct fstab *fs_mgr_read_fstab(const char *fstab_path)
if (!entries) { if (!entries) {
ERROR("No entries found in fstab\n"); ERROR("No entries found in fstab\n");
return 0; goto err;
} }
/* Allocate and init the fstab structure */ /* Allocate and init the fstab structure */
@ -327,9 +268,8 @@ struct fstab *fs_mgr_read_fstab(const char *fstab_path)
fseek(fstab_file, 0, SEEK_SET); fseek(fstab_file, 0, SEEK_SET);
cnt = 0; cnt = 0;
while (fs_getline(line, sizeof(line), fstab_file)) { while ((len = getline(&line, &alloc_len, fstab_file)) != -1) {
/* if the last character is a newline, shorten the string by 1 byte */ /* if the last character is a newline, shorten the string by 1 byte */
len = strlen(line);
if (line[len - 1] == '\n') { if (line[len - 1] == '\n') {
line[len - 1] = '\0'; line[len - 1] = '\0';
} }
@ -354,25 +294,25 @@ struct fstab *fs_mgr_read_fstab(const char *fstab_path)
if (!(p = strtok_r(line, delim, &save_ptr))) { if (!(p = strtok_r(line, delim, &save_ptr))) {
ERROR("Error parsing mount source\n"); ERROR("Error parsing mount source\n");
return 0; goto err;
} }
fstab->recs[cnt].blk_device = strdup(p); fstab->recs[cnt].blk_device = strdup(p);
if (!(p = strtok_r(NULL, delim, &save_ptr))) { if (!(p = strtok_r(NULL, delim, &save_ptr))) {
ERROR("Error parsing mount_point\n"); ERROR("Error parsing mount_point\n");
return 0; goto err;
} }
fstab->recs[cnt].mount_point = strdup(p); fstab->recs[cnt].mount_point = strdup(p);
if (!(p = strtok_r(NULL, delim, &save_ptr))) { if (!(p = strtok_r(NULL, delim, &save_ptr))) {
ERROR("Error parsing fs_type\n"); ERROR("Error parsing fs_type\n");
return 0; goto err;
} }
fstab->recs[cnt].fs_type = strdup(p); fstab->recs[cnt].fs_type = strdup(p);
if (!(p = strtok_r(NULL, delim, &save_ptr))) { if (!(p = strtok_r(NULL, delim, &save_ptr))) {
ERROR("Error parsing mount_flags\n"); ERROR("Error parsing mount_flags\n");
return 0; goto err;
} }
tmp_fs_options[0] = '\0'; tmp_fs_options[0] = '\0';
fstab->recs[cnt].flags = parse_flags(p, mount_flags, fstab->recs[cnt].flags = parse_flags(p, mount_flags,
@ -388,7 +328,7 @@ struct fstab *fs_mgr_read_fstab(const char *fstab_path)
if (!(p = strtok_r(NULL, delim, &save_ptr))) { if (!(p = strtok_r(NULL, delim, &save_ptr))) {
ERROR("Error parsing fs_mgr_options\n"); ERROR("Error parsing fs_mgr_options\n");
return 0; goto err;
} }
fstab->recs[cnt].fs_mgr_flags = parse_flags(p, fs_mgr_flags, fstab->recs[cnt].fs_mgr_flags = parse_flags(p, fs_mgr_flags,
&key_loc, &part_length, &key_loc, &part_length,
@ -401,8 +341,12 @@ struct fstab *fs_mgr_read_fstab(const char *fstab_path)
cnt++; cnt++;
} }
fclose(fstab_file); fclose(fstab_file);
free(line);
return fstab; return fstab;
err:
free(line);
return NULL;
} }
void fs_mgr_free_fstab(struct fstab *fstab) void fs_mgr_free_fstab(struct fstab *fstab)