]> git.pld-linux.org Git - packages/glibc.git/blobdiff - postshell.c
- added dirs for dz,km,mg messages
[packages/glibc.git] / postshell.c
index 1d68fb062cb32dca5b2981d9cb90d1271e113122..1cf156082b508bdd760f7e3f5ff3954bc79458d6 100644 (file)
@@ -73,6 +73,9 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <fcntl.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
 
 #define MAX_LINE 1024
 #define MAX_ARGS 32
@@ -181,36 +184,64 @@ 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) {
-               fprintf(stderr, "USAGE: %s filename\n", argv[0]);
+               error("USAGE: ");
+               error(argv[0]);
+               error(" filename\n");
                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.032798 seconds and 4 git commands to generate.