replace fparseln(3) with getline(3), for better portability ok stsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
diff --git a/lib/reference.c b/lib/reference.c
index 2cd2c5f..61d4adb 100644
--- a/lib/reference.c
+++ b/lib/reference.c
@@ -359,9 +359,9 @@ open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
{
const struct got_error *err = NULL;
char *abs_refname;
- char *line;
- size_t len;
- const char delim[3] = {'\0', '\0', '\0'};
+ char *line = NULL;
+ size_t linesize = 0;
+ ssize_t linelen;
int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
*ref = NULL;
@@ -369,13 +369,15 @@ open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
if (ref_is_absolute)
abs_refname = (char *)refname;
do {
- line = fparseln(f, &len, NULL, delim, 0);
- if (line == NULL) {
+ linelen = getline(&line, &linesize, f);
+ if (linelen == -1) {
if (feof(f))
break;
err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
break;
}
+ if (linelen > 0 && line[linelen - 1] == '\n')
+ line[linelen - 1] = '\0';
for (i = 0; i < nsubdirs; i++) {
if (!ref_is_absolute &&
asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
@@ -387,10 +389,10 @@ open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
if (err || *ref != NULL)
break;
}
- free(line);
if (err)
break;
} while (*ref == NULL);
+ free(line);
return err;
}
@@ -871,6 +873,7 @@ got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
char *packed_refs_path, *path_refs = NULL;
char *abs_namespace = NULL;
char *buf = NULL, *ondisk_ref_namespace = NULL;
+ char *line = NULL;
FILE *f = NULL;
struct got_reference *ref;
struct got_reflist_entry *new;
@@ -963,19 +966,19 @@ got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
f = fopen(packed_refs_path, "r");
free(packed_refs_path);
if (f) {
- char *line;
- size_t len;
- const char delim[3] = {'\0', '\0', '\0'};
+ size_t linesize;
+ ssize_t linelen;
for (;;) {
- line = fparseln(f, &len, NULL, delim, 0);
- if (line == NULL) {
+ linelen = getline(&line, &linesize, f);
+ if (linelen == -1) {
if (feof(f))
break;
err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
goto done;
}
+ if (linelen > 0 && line[linelen - 1] == '\n')
+ line[linelen - 1] = '\0';
err = parse_packed_ref_line(&ref, NULL, line);
- free(line);
if (err)
goto done;
if (ref) {
@@ -1001,6 +1004,7 @@ got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
done:
free(abs_namespace);
free(buf);
+ free(line);
free(path_refs);
if (f && fclose(f) != 0 && err == NULL)
err = got_error_from_errno("fclose");
@@ -1174,7 +1178,7 @@ delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
const struct got_error *err = NULL, *unlock_err = NULL;
struct got_lockfile *lf = NULL;
FILE *f = NULL, *tmpf = NULL;
- char *packed_refs_path, *tmppath = NULL;
+ char *line = NULL, *packed_refs_path, *tmppath = NULL;
struct got_reflist_head refs;
int found_delref = 0;
@@ -1204,21 +1208,21 @@ delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
goto done;
}
for (;;) {
- char *line;
- size_t len;
- const char delim[3] = {'\0', '\0', '\0'};
+ size_t linesize;
+ ssize_t linelen;
struct got_reference *ref;
struct got_reflist_entry *new;
- line = fparseln(f, &len, NULL, delim, 0);
- if (line == NULL) {
+ linelen = getline(&line, &linesize, f);
+ if (linelen == -1) {
if (feof(f))
break;
err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
goto done;
}
+ if (linelen > 0 && line[linelen - 1] == '\n')
+ line[linelen - 1] = '\0';
err = parse_packed_ref_line(&ref, NULL, line);
- free(line);
if (err)
goto done;
if (ref == NULL)
@@ -1310,6 +1314,7 @@ done:
}
free(tmppath);
free(packed_refs_path);
+ free(line);
got_ref_list_free(&refs);
return err ? err : unlock_err;
}
diff --git a/lib/worktree.c b/lib/worktree.c
index 38c6dc6..5dfe5fb 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -1599,13 +1599,13 @@ get_modified_file_content_status(unsigned char *status, FILE *f)
GOT_DIFF_CONFLICT_MARKER_END
};
int i = 0;
- char *line;
- size_t len;
- const char delim[3] = {'\0', '\0', '\0'};
+ char *line = NULL;
+ size_t linesize = 0;
+ ssize_t linelen;
while (*status == GOT_STATUS_MODIFY) {
- line = fparseln(f, &len, NULL, delim, 0);
- if (line == NULL) {
+ linelen = getline(&line, &linesize, f);
+ if (linelen == -1) {
if (feof(f))
break;
err = got_ferror(f, GOT_ERR_IO);
@@ -1620,6 +1620,7 @@ get_modified_file_content_status(unsigned char *status, FILE *f)
i++;
}
}
+ free(line);
return err;
}