Author: oxygene Date: 2009-11-20 23:16:48 +0100 (Fri, 20 Nov 2009) New Revision: 60
Modified: trunk/SerialICE/util/romcc.c Log: Replace open/read/close with fopen/fread/fclose in romcc. The filesize is now determined using fseek/ftell, like in cbfstool.
Thank you Stefan, for the hint.
Tested on mingw
Modified: trunk/SerialICE/util/romcc.c =================================================================== --- trunk/SerialICE/util/romcc.c 2009-11-20 20:57:27 UTC (rev 59) +++ trunk/SerialICE/util/romcc.c 2009-11-20 22:16:48 UTC (rev 60) @@ -219,11 +219,11 @@ static char *slurp_file(const char *dirname, const char *filename, off_t *r_size) { char cwd[MAX_CWD_SIZE]; - int fd; char *buf; off_t size, progress; ssize_t result; struct stat stats; + FILE* file; if (!filename) { *r_size = 0; @@ -233,25 +233,22 @@ die("cwd buffer to small"); } xchdir(dirname); - fd = open(filename, O_RDONLY); + file = fopen(filename, "rb"); xchdir(cwd); - if (fd < 0) { + if (file == NULL) { die("Cannot open '%s' : %s\n", filename, strerror(errno)); } - result = fstat(fd, &stats); - if (result < 0) { - die("Cannot stat: %s: %s\n", - filename, strerror(errno)); - } - size = stats.st_size; + fseek(file, 0, SEEK_END); + size = ftell(file); + fseek(file, 0, SEEK_SET); *r_size = size +1; buf = xmalloc(size +2, filename); buf[size] = '\n'; /* Make certain the file is newline terminated */ buf[size+1] = '\0'; /* Null terminate the file for good measure */ progress = 0; while(progress < size) { - result = read(fd, buf + progress, size - progress); + result = fread(buf + progress, 1, size - progress, file); if (result < 0) { if ((errno == EINTR) || (errno == EAGAIN)) continue; @@ -260,11 +257,7 @@ } progress += result; } - result = close(fd); - if (result < 0) { - die("Close of %s failed: %s\n", - filename, strerror(errno)); - } + fclose(file); return buf; }