am c4295d9b: am 810ff696: Merge "Cleanup fs_getline from fs_mgr"

* commit 'c4295d9b72812afbaebd7f2512c7a5bba9e7d5fa':
  Cleanup fs_getline from fs_mgr
This commit is contained in:
Elliott Hughes 2013-09-18 16:33:55 -07:00 committed by Android Git Automerger
commit 9f25b565f7

View file

@ -235,71 +235,13 @@ out:
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)
{
FILE *fstab_file;
int cnt, entries;
int len;
char line[256];
ssize_t len;
size_t alloc_len = 0;
char *line = NULL;
const char *delim = " \t";
char *save_ptr, *p;
struct fstab *fstab;
@ -315,9 +257,8 @@ struct fstab *fs_mgr_read_fstab(const char *fstab_path)
}
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 */
len = strlen(line);
if (line[len - 1] == '\n') {
line[len - 1] = '\0';
}
@ -334,7 +275,7 @@ struct fstab *fs_mgr_read_fstab(const char *fstab_path)
if (!entries) {
ERROR("No entries found in fstab\n");
return 0;
goto err;
}
/* Allocate and init the fstab structure */
@ -346,9 +287,8 @@ struct fstab *fs_mgr_read_fstab(const char *fstab_path)
fseek(fstab_file, 0, SEEK_SET);
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 */
len = strlen(line);
if (line[len - 1] == '\n') {
line[len - 1] = '\0';
}
@ -373,25 +313,25 @@ struct fstab *fs_mgr_read_fstab(const char *fstab_path)
if (!(p = strtok_r(line, delim, &save_ptr))) {
ERROR("Error parsing mount source\n");
return 0;
goto err;
}
fstab->recs[cnt].blk_device = strdup(p);
if (!(p = strtok_r(NULL, delim, &save_ptr))) {
ERROR("Error parsing mount_point\n");
return 0;
goto err;
}
fstab->recs[cnt].mount_point = strdup(p);
if (!(p = strtok_r(NULL, delim, &save_ptr))) {
ERROR("Error parsing fs_type\n");
return 0;
goto err;
}
fstab->recs[cnt].fs_type = strdup(p);
if (!(p = strtok_r(NULL, delim, &save_ptr))) {
ERROR("Error parsing mount_flags\n");
return 0;
goto err;
}
tmp_fs_options[0] = '\0';
fstab->recs[cnt].flags = parse_flags(p, mount_flags, NULL,
@ -406,7 +346,7 @@ struct fstab *fs_mgr_read_fstab(const char *fstab_path)
if (!(p = strtok_r(NULL, delim, &save_ptr))) {
ERROR("Error parsing fs_mgr_options\n");
return 0;
goto err;
}
fstab->recs[cnt].fs_mgr_flags = parse_flags(p, fs_mgr_flags,
&flag_vals, NULL, 0);
@ -419,8 +359,12 @@ struct fstab *fs_mgr_read_fstab(const char *fstab_path)
cnt++;
}
fclose(fstab_file);
free(line);
return fstab;
err:
free(line);
return NULL;
}
void fs_mgr_free_fstab(struct fstab *fstab)