Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
N
nettle
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Labels
Merge Requests
5
Merge Requests
5
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
Nettle
nettle
Commits
2bd9d1b8
Commit
2bd9d1b8
authored
Sep 10, 2012
by
Niels Möller
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Reorganized read_file loop.
parent
3ead2b97
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
30 additions
and
19 deletions
+30
-19
ChangeLog
ChangeLog
+3
-0
examples/io.c
examples/io.c
+22
-17
examples/io.h
examples/io.h
+5
-2
No files found.
ChangeLog
View file @
2bd9d1b8
2012-09-10 Niels Möller <nisse@lysator.liu.se>
* examples/io.c (read_file): Explicitly treat an empty file as an
error. Rearrange loop, check for short fread return value.
* desdata.c: Don't declare printf, include <stdio.h> instead. Also
deleted casts of printf return value.
...
...
examples/io.c
View file @
2bd9d1b8
...
...
@@ -71,8 +71,7 @@ werror(const char *format, ...)
unsigned
read_file
(
const
char
*
name
,
unsigned
max_size
,
char
**
contents
)
{
unsigned
size
;
unsigned
done
;
unsigned
size
,
done
;
char
*
buffer
;
FILE
*
f
;
...
...
@@ -82,21 +81,10 @@ read_file(const char *name, unsigned max_size, char **contents)
werror
(
"Opening `%s' failed: %s
\n
"
,
name
,
strerror
(
errno
));
return
0
;
}
buffer
=
NULL
;
if
(
max_size
&&
max_size
<
100
)
size
=
max_size
;
else
size
=
100
;
/* FIXME: The use of feof and ferror in this loop is a bit confused
(but I think it is still correct). We should check the return
value of fread, and call feof and/or ferror when we get a short
item count. */
size
=
100
;
for
(
done
=
0
;
(
!
max_size
||
done
<
max_size
)
&&
!
feof
(
f
);
size
*=
2
)
for
(
buffer
=
NULL
,
done
=
0
;;
size
*=
2
)
{
char
*
p
;
...
...
@@ -118,8 +106,25 @@ read_file(const char *name, unsigned max_size, char **contents)
buffer
=
p
;
done
+=
fread
(
buffer
+
done
,
1
,
size
-
done
,
f
);
if
(
ferror
(
f
))
goto
fail
;
if
(
done
<
size
)
{
/* Short count means EOF or read error */
if
(
ferror
(
f
))
{
fprintf
(
stderr
,
"Reading `%s' failed: %s
\n
"
,
name
,
strerror
(
errno
));
goto
fail
;
}
if
(
done
==
0
)
/* Treat empty file as error */
goto
fail
;
break
;
}
if
(
size
==
max_size
)
break
;
}
fclose
(
f
);
...
...
examples/io.h
View file @
2bd9d1b8
...
...
@@ -39,8 +39,11 @@ xalloc(size_t size);
void
werror
(
const
char
*
format
,
...)
PRINTF_STYLE
(
1
,
2
);
/* If size is > 0, read at most that many bytes. If size == 0,
* read until EOF. Allocates the buffer dynamically. */
/* If size is > 0, read at most that many bytes. If size == 0, read
* until EOF. Allocates the buffer dynamically. An empty file is
* treated as an error; return value is zero, and no space is
* allocated. The returned data is NUL-terminated, for convenience. */
unsigned
read_file
(
const
char
*
name
,
unsigned
size
,
char
**
buffer
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment