Krolyxon
Commit ea23115

Branches

Refs →

nsxivea23115

Use getline instead of readline

Author
Bert
Date
2011-05-29 11:45 +0200
Parent
2252a01
Commit
ea23115af449e086ba05c9757ad22108944f6ec2

+19 −52

6 files changed

  1. M Makefile +1 −1
  2. M main.c +15 −10
  3. M options.c +1 −1
  4. M options.h +1 −1
  5. M util.c +1 −37
  6. M util.h +0 −2
M Makefile View
@@ -1,6 +1,6 @@
11 all: sxiv
22
3−VERSION=git-20110525
3+VERSION=git-20110529
44
55 CC?=gcc
66 DESTDIR?=
M main.c View
@@ -16,6 +16,8 @@
1616 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
1717 */
1818
19+#define _XOPEN_SOURCE 700
20+
1921 #include <stdlib.h>
2022 #include <stdio.h>
2123 #include <string.h>
@@ -59,7 +61,7 @@ img_t img;
5961 tns_t tns;
6062 win_t win;
6163
62−const char **filenames;
64+char **filenames;
6365 int filecnt, fileidx;
6466 size_t filesize;
6567
@@ -88,7 +90,7 @@ void remove_file(int n, unsigned char silent) {
8890
8991 if (n + 1 < filecnt)
9092 memmove(filenames + n, filenames + n + 1, (filecnt - n - 1) *
91− sizeof(const char*));
93+ sizeof(char*));
9294 if (n + 1 < tns.cnt) {
9395 memmove(tns.thumbs + n, tns.thumbs + n + 1, (tns.cnt - n - 1) *
9496 sizeof(thumb_t));
@@ -150,7 +152,7 @@ void update_title() {
150152 win_set_title(&win, win_title);
151153 }
152154
153−int check_append(const char *filename) {
155+int check_append(char *filename) {
154156 if (!filename)
155157 return 0;
156158
@@ -160,8 +162,7 @@ int check_append(const char *filename) {
160162 } else {
161163 if (fileidx == filecnt) {
162164 filecnt *= 2;
163− filenames = (const char**) s_realloc(filenames,
164− filecnt * sizeof(const char*));
165+ filenames = (char**) s_realloc(filenames, filecnt * sizeof(char*));
165166 }
166167 filenames[fileidx++] = filename;
167168 return 1;
@@ -173,8 +174,9 @@ int fncmp(const void *a, const void *b) {
173174 }
174175
175176 int main(int argc, char **argv) {
176− int i, start;
177− const char *filename;
177+ int i, len, start;
178+ size_t n;
179+ char *filename = NULL;
178180 struct stat fstats;
179181 r_dir_t dir;
180182
@@ -196,13 +198,16 @@ int main(int argc, char **argv) {
196198 else
197199 filecnt = options->filecnt;
198200
199− filenames = (const char**) s_malloc(filecnt * sizeof(const char*));
201+ filenames = (char**) s_malloc(filecnt * sizeof(char*));
200202 fileidx = 0;
201203
202204 if (options->from_stdin) {
203− while ((filename = readline(stdin))) {
205+ while ((len = getline(&filename, &n, stdin)) > 0) {
206+ if (filename[len-1] == '\n')
207+ filename[len-1] = '\0';
204208 if (!*filename || !check_append(filename))
205− free((void*) filename);
209+ free(filename);
210+ filename = NULL;
206211 }
207212 } else {
208213 for (i = 0; i < options->filecnt; ++i) {
M options.c View
@@ -124,7 +124,7 @@ void parse_options(int argc, char **argv) {
124124 }
125125 }
126126
127− _options.filenames = (const char**) argv + optind;
127+ _options.filenames = argv + optind;
128128 _options.filecnt = argc - optind;
129129 _options.from_stdin = _options.filecnt == 1 &&
130130 strcmp(_options.filenames[0], "-") == 0;
M options.h View
@@ -22,7 +22,7 @@
2222 #include "image.h"
2323
2424 typedef struct {
25− const char **filenames;
25+ char **filenames;
2626 unsigned char from_stdin;
2727 int filecnt;
2828 int startnum;
M util.c View
@@ -140,7 +140,7 @@ char* absolute_path(const char *filename) {
140140 path = (char*) s_malloc(len);
141141 snprintf(path, len, "%s/%s", dir, basename);
142142
143−goto end;
143+ goto end;
144144
145145 error:
146146 if (path) {
@@ -297,39 +297,3 @@ int r_mkdir(const char *path) {
297297
298298 return err;
299299 }
300−
301−char* readline(FILE *stream) {
302− size_t len;
303− char *buf, *s, *end;
304−
305− if (!stream || feof(stream) || ferror(stream))
306− return NULL;
307−
308− len = FNAME_LEN;
309− s = buf = (char*) s_malloc(len * sizeof(char));
310−
311− do {
312− *s = '\0';
313− fgets(s, len - (s - buf), stream);
314− if ((end = strchr(s, '\n'))) {
315− *end = '\0';
316− } else if (strlen(s) + 1 == len - (s - buf)) {
317− buf = (char*) s_realloc(buf, 2 * len * sizeof(char));
318− s = buf + len - 1;
319− len *= 2;
320− } else {
321− s += strlen(s);
322− }
323− } while (!end && !feof(stream) && !ferror(stream));
324−
325− if (ferror(stream)) {
326− s = NULL;
327− } else {
328− s = (char*) s_malloc((strlen(buf) + 1) * sizeof(char));
329− strcpy(s, buf);
330− }
331−
332− free(buf);
333−
334− return s;
335−}
M util.h View
@@ -61,6 +61,4 @@ int r_closedir(r_dir_t*);
6161 char* r_readdir(r_dir_t*);
6262 int r_mkdir(const char *);
6363
64−char* readline(FILE*);
65−
6664 #endif /* UTIL_H */