commit 9fe1ca723978ede2fb6785443ffa3a5a08dab310
parent 843bda1dd605a7dfbeab4349669945d6ae71d5ac
Author: Naveen Narayanan <nan@sysgo.com>
Date: Sun, 6 Mar 2022 16:59:08 +0100
Read rom to buf
Diffstat:
M | 8.c | | | 28 | ++++++++++++++++++++++++++++ |
1 file changed, 28 insertions(+), 0 deletions(-)
diff --git a/8.c b/8.c
@@ -1,6 +1,13 @@
+#include <err.h>
+#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <sys/types.h>
+#include <sys/stat.h>
#define BUFSZ 256
@@ -302,8 +309,29 @@ usage()
int
main(int argc, char **argv)
{
+ struct stat st;
+ int fd, n, r;
+
if (argc < 2)
usage();
+ if (stat(argv[1], &st) != 0)
+ err(1, "stat failed");
+ if (st.st_size > BUFSZ) {
+ fprintf(stderr, "file too big\n");
+ return 1;
+ }
+
+ if ((fd = open(argv[1], O_RDONLY)) == -1)
+ err(1, "open failed");
+
+ r = 0;
+ while (r < st.st_size) {
+ if ((n = read(fd, buf, st.st_size-r)) == -1)
+ err(1, "read failed");
+ r += n;
+ }
+
+ init();
return 0;
}