3.tigran.18_test.unsearcher 3.mariya.18_test.unsearcher

--- 3.tigran.18_test.filler.fmt.c	2020-06-08 11:39:11.711115418 +0300
+++ 3.mariya.18_test.filler.fmt.c	2020-06-08 11:39:11.678115061 +0300
@@ -4,22 +4,28 @@
 #include <fcntl.h>
 #include <sys/stat.h>
 #include <sys/mman.h>
+#include <sys/types.h>
 #include <string.h>
+#include <errno.h>
 int main(int argc, char *argv[]) {
-    int fd, file;
-    char *addr, *f;
-    struct stat s;
-    off_t size;
-    file = open(argv[2], O_RDONLY);
-    fd = shm_open(argv[1], O_RDWR | O_CREAT, 0);
-    size = lseek(file, 0, SEEK_END);
-    fstat(fd, &s);
-    ftruncate(fd, size);
-    addr = mmap(NULL, s.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
-    f = mmap(NULL, size, PROT_READ, MAP_SHARED, file, 0);
-    memcpy(addr, f, s.st_size);
+    int fd, f;
+    char *addr, *file;
+    struct stat st;
+    fd = shm_open(argv[1], O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
+    f = open(argv[2], O_RDONLY);
+    fstat(f, &st);
+    size_t len = st.st_size;
+    if (ftruncate(fd, len) == -1) {
+	int errsv = errno;
+	if (errno == EFBIG) {
+	    len = 10000;
+	    ftruncate(fd, len);
+	}
+    }
+    file = mmap(NULL, len, PROT_READ, MAP_SHARED, f, 0);
+    addr = mmap(NULL, len, PROT_WRITE, MAP_SHARED, fd, 0);
     close(fd);
-    close(file);
-    shm_unlink(argv[1]);
+    memcpy(addr, file, len);
+    close(f);
     return 0;
 }
--- 3.tigran.18_test.unsearcher.fmt.c	2020-06-08 11:39:11.715115461 +0300
+++ 3.mariya.18_test.unsearcher.fmt.c	2020-06-08 11:39:11.682115104 +0300
@@ -1,33 +1,33 @@
 #1 "<stdin>"
+#include <unistd.h>
 #include <stdio.h>
 #include <fcntl.h>
-#include <sys/mman.h>
 #include <sys/stat.h>
-#include <unistd.h>
-#include <stdlib.h>
-#include <string.h>
+#include <sys/mman.h>
+#include <sys/types.h>
 #include <regex.h>
+#include <stdlib.h>
 int main(int argc, char *argv[]) {
-    char *addr, *line = NULL;
+    int fd;
+    char *addr;
     struct stat sb;
-    FILE *fp;
-    regex_t regex;
-    int fd, return_value = regcomp(&regex, argv[2], 0), i = 0;
+    char *line;
+    int len = 0;
+    regex_t r;
     fd = shm_open(argv[1], O_RDONLY, 0);
-    fp = fopen("uns.txt", "w+");
     fstat(fd, &sb);
     addr = mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, fd, 0);
-    fwrite(addr, sb.st_size, 1, fp);
-    rewind(fp);
-    while (getline(&line, &i, fp) != -1) {
-	if (regexec(&regex, line, 0, NULL, 0) != 0)
+    FILE *file = fdopen(fd, "r");
+    regcomp(&r, argv[2], 0);
+    while (getline(&line, &len, file) != -1) {
+	if (regexec(&r, line, 0, NULL, 0) != 0)
 	    printf("%s", line);
 	free(line);
-	i = 0;
+	len = 0;
 	line = NULL;
     }
-    regfree(&regex);
+    regfree(&r);
     close(fd);
-    fclose(fp);
+    munmap(addr, sb.st_size);
     return 0;
 }