diff options
author | Irina Tirdea <irina.tirdea@intel.com> | 2013-09-06 19:19:44 +0300 |
---|---|---|
committer | Irina Tirdea <irina.tirdea@intel.com> | 2013-09-18 22:55:51 +0300 |
commit | e16d747b5105f52895c5e1a687568ea4cab804d5 (patch) | |
tree | db7f92ef75fc6e7c5a8d13fa985c7ace8ece4a65 | |
parent | 4a280e3dbe002bb5419ec010f89cdc158244e435 (diff) | |
download | system_core-e16d747b5105f52895c5e1a687568ea4cab804d5.zip system_core-e16d747b5105f52895c5e1a687568ea4cab804d5.tar.gz system_core-e16d747b5105f52895c5e1a687568ea4cab804d5.tar.bz2 |
Cleanup fs_getline from fs_mgr
Since getline is included in bionic, fs_mgr can use this version instead of
the one defined internally by fs_getline.
Replace fs_getline with getline.
Change-Id: I49b53d639bd995f051256fb7573ff6ab45d9c36d
Signed-off-by: Irina Tirdea <irina.tirdea@intel.com>
-rw-r--r-- | fs_mgr/fs_mgr.c | 88 |
1 files changed, 16 insertions, 72 deletions
diff --git a/fs_mgr/fs_mgr.c b/fs_mgr/fs_mgr.c index fecc556..85ce397 100644 --- a/fs_mgr/fs_mgr.c +++ b/fs_mgr/fs_mgr.c @@ -213,71 +213,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; @@ -296,9 +238,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'; } @@ -315,7 +256,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 */ @@ -327,9 +268,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'; } @@ -354,25 +294,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, @@ -388,7 +328,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, &key_loc, &part_length, @@ -401,8 +341,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) |