]> git.pld-linux.org Git - packages/setup.git/commitdiff
- skip stdio (fopen), 4k saved with diet auto/ac/glibc-2_3_5-4 auto/ac/glibc-2_3_5-5 auto/ac/glibc-2_3_5-6 auto/ac/glibc-2_3_5-8 auto/ac/glibc-2_3_5-8_1 auto/ac/glibc-2_3_5-9 auto/ac/glibc-2_3_6-0_1 auto/ac/glibc-2_3_6-1 auto/ac/glibc-2_3_6-2 auto/ac/glibc-2_3_6-2_9 auto/ac/glibc-2_3_6-3 auto/ac/glibc-2_3_6-5 auto/ac/glibc-2_3_6-6
authorElan Ruusamäe <glen@pld-linux.org>
Fri, 17 Jun 2005 17:27:07 +0000 (17:27 +0000)
committercvs2git <feedback@pld-linux.org>
Sun, 24 Jun 2012 12:13:13 +0000 (12:13 +0000)
Changed files:
    postshell.c -> 1.5

postshell.c

index 852c8cbeb67f2eeee4280e49f1372c953de43ef9..02b2d20ed0ed90e6f530108b89dabc064072a074 100644 (file)
@@ -73,6 +73,8 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <fcntl.h>
+#include <sys/mman.h>
 
 #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);
 }
This page took 0.044004 seconds and 4 git commands to generate.