From: Elan Ruusamäe Date: Fri, 17 Jun 2005 17:27:07 +0000 (+0000) Subject: - skip stdio (fopen), 4k saved with diet X-Git-Tag: setup_2_4_11~39 X-Git-Url: http://git.pld-linux.org/?p=projects%2Fsetup.git;a=commitdiff_plain;h=3a827ec177d8c8cbcbccba2f2d49e1cbcdff7f33 - skip stdio (fopen), 4k saved with diet Changed files: postshell.c -> 1.5 --- diff --git a/postshell.c b/postshell.c index 852c8cb..02b2d20 100644 --- a/postshell.c +++ b/postshell.c @@ -73,6 +73,8 @@ #include #include #include +#include +#include #define MAX_LINE 1024 #define MAX_ARGS 32 @@ -181,23 +183,48 @@ void exec_line(char *s) exit_status = ret; } -void exec_file(FILE *f) +void exec_file(int fd) { char line[MAX_LINE]; + struct stat sbuf; + char *p, *s, *a; - while (fgets(line, sizeof(line), f)) { - /* chomp it */ - if (line[strlen(line) - 1] == '\n') - line[strlen(line) - 1] = 0; - /* and exec. */ + if (fstat(fd, &sbuf) < 0) { + perror("fstat()"); + exit(1); + } + + if ((p = mmap(0, sbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0)) < 0) { + perror("mmap()"); + exit(1); + } + + for (a = s = p; s < p + sbuf.st_size; s++) { + if (*s == '\n') { + memcpy(line, a, s - a); + line[s - a] = '\0'; + exec_line(line); + a = ++s; + } + } + + // last line was not terminated. + if (s == p + sbuf.st_size) { + memcpy(line, a, s - a); + line[s - a] = '\0'; exec_line(line); } + + if (munmap(p, sbuf.st_size) < 0) { + perror("munmap()"); + exit(1); + } } #define error(msg) write(2, msg, strlen(msg)) int main(int argc, char **argv) { - FILE *f; + int fd; if (argc < 2) { error("USAGE: "); @@ -206,14 +233,14 @@ int main(int argc, char **argv) exit(1); } - f = fopen(argv[1], "r"); + fd = open(argv[1], O_RDONLY); - if (f == NULL) { + if (fd == -1) { perror(argv[1]); exit(1); } - exec_file(f); - fclose(f); + exec_file(fd); + close(fd); exit(exit_status); }