diff --git a/lab_1/ilya`s/1/file.in b/lab_1/ilya`s/file.in similarity index 100% rename from lab_1/ilya`s/1/file.in rename to lab_1/ilya`s/file.in diff --git a/lab_1/ilya`s/1/file.out b/lab_1/ilya`s/file.out similarity index 100% rename from lab_1/ilya`s/1/file.out rename to lab_1/ilya`s/file.out diff --git a/lab_1/ilya`s/1/signs b/lab_1/ilya`s/signs similarity index 100% rename from lab_1/ilya`s/1/signs rename to lab_1/ilya`s/signs diff --git a/lab_1/ilya`s/1/signs.cpp b/lab_1/ilya`s/signs.cpp similarity index 100% rename from lab_1/ilya`s/1/signs.cpp rename to lab_1/ilya`s/signs.cpp diff --git a/lab_1/kirill`s/lab1/in.txt b/lab_1/kirill`s/in.txt similarity index 100% rename from lab_1/kirill`s/lab1/in.txt rename to lab_1/kirill`s/in.txt diff --git a/lab_1/kirill`s/lab1/task18.cpp b/lab_1/kirill`s/task18.cpp similarity index 100% rename from lab_1/kirill`s/lab1/task18.cpp rename to lab_1/kirill`s/task18.cpp diff --git a/lab_1/vlad`s/in.txt b/lab_1/vlad`s/in.txt new file mode 100644 index 0000000..d31d5e0 --- /dev/null +++ b/lab_1/vlad`s/in.txt @@ -0,0 +1,8 @@ +aaaaaa +aaaaaaaaaaaa +aaaaaaaaaaaa +aaaaaaaaaaaa + 2 +aaaaaaaaaaaa +aaaaaaaaaaaa +aaaaaa \ No newline at end of file diff --git a/lab_1/vlad`s/lab1/in.txt b/lab_1/vlad`s/lab1/in.txt deleted file mode 100644 index 50fee4e..0000000 --- a/lab_1/vlad`s/lab1/in.txt +++ /dev/null @@ -1,2 +0,0 @@ -aaaaaa -aaaaaa \ No newline at end of file diff --git a/lab_1/vlad`s/lab1/task11.cpp b/lab_1/vlad`s/task11.c similarity index 71% rename from lab_1/vlad`s/lab1/task11.cpp rename to lab_1/vlad`s/task11.c index f2512be..db1bfd4 100644 --- a/lab_1/vlad`s/lab1/task11.cpp +++ b/lab_1/vlad`s/task11.c @@ -12,12 +12,13 @@ static void die_perror(const char *what, const char *path, int exit_code) { int saved = errno; char msg[512]; + int n; if (path) { - snprintf(msg, sizeof(msg), "%s: %s: %s\n", what, path, strerror(saved)); + n = snprintf(msg, sizeof(msg), "%s: %s: %s\n", what, path, strerror(saved)); } else { - snprintf(msg, sizeof(msg), "%s: %s\n", what, strerror(saved)); + n = snprintf(msg, sizeof(msg), "%s: %s\n", what, strerror(saved)); } - (void) write(STDERR_FILENO, msg, strlen(msg)); + if (n > 0) (void) write(STDERR_FILENO, msg, (size_t) n); _exit(exit_code); } @@ -32,6 +33,18 @@ static long long parse_ll(const char *s) { return v; } +static void xwrite_all(int fd, const char *buf, size_t len, const char *path) { + size_t off = 0; + while (off < len) { + ssize_t n = write(fd, buf + off, len - off); + if (n < 0) { + if (errno == EINTR) continue; + die_perror("write failed", path, -1); + } + off += (size_t) n; + } +} + int main(int argc, char *argv[]) { if (argc != 4) { const char *usage = @@ -54,7 +67,7 @@ int main(int argc, char *argv[]) { die_perror("open input failed", in_path, -1); } - mode_t filePerms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; /* rw-rw-rw- */ + mode_t filePerms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; int out_fd = open(out_path, O_CREAT | O_WRONLY | O_TRUNC, filePerms); if (out_fd < 0) { die_perror("open output failed", out_path, -1); @@ -64,7 +77,6 @@ int main(int argc, char *argv[]) { char wbuf[WBUFSZ]; size_t wlen = 0; - /* state */ long long total_replacements = 0; int replacing_enabled = 1; @@ -78,14 +90,12 @@ int main(int argc, char *argv[]) { unsigned char c = (unsigned char) rbuf[i]; if (!have_prev) { - /* buffer first byte of a potential pair */ prev = c; have_prev = 1; continue; } if (c == prev) { - /* non-overlapping pair found: output prev, then second element possibly replaced */ unsigned char out1 = prev; unsigned char out2 = c; @@ -99,29 +109,22 @@ int main(int argc, char *argv[]) { replacing_enabled = 0; } - /* write out1 */ if (wlen == sizeof(wbuf)) { - ssize_t wrote = write(out_fd, wbuf, wlen); - if (wrote < 0 || (size_t) wrote != wlen) die_perror("write failed", out_path, -1); + xwrite_all(out_fd, wbuf, wlen, out_path); wlen = 0; } wbuf[wlen++] = (char) out1; - /* write out2 */ if (wlen == sizeof(wbuf)) { - ssize_t wrote = write(out_fd, wbuf, wlen); - if (wrote < 0 || (size_t) wrote != wlen) die_perror("write failed", out_path, -1); + xwrite_all(out_fd, wbuf, wlen, out_path); wlen = 0; } wbuf[wlen++] = (char) out2; - /* consume pair: reset have_prev so next byte starts a new potential pair */ have_prev = 0; } else { - /* no pair: output prev, shift window to c */ if (wlen == sizeof(wbuf)) { - ssize_t wrote = write(out_fd, wbuf, wlen); - if (wrote < 0 || (size_t) wrote != wlen) die_perror("write failed", out_path, -1); + xwrite_all(out_fd, wbuf, wlen, out_path); wlen = 0; } wbuf[wlen++] = (char) prev; @@ -131,23 +134,21 @@ int main(int argc, char *argv[]) { } } } else if (n == 0) { - /* flush any pending single prev */ if (have_prev) { if (wlen == sizeof(wbuf)) { - ssize_t wrote = write(out_fd, wbuf, wlen); - if (wrote < 0 || (size_t) wrote != wlen) die_perror("write failed", out_path, -1); + xwrite_all(out_fd, wbuf, wlen, out_path); wlen = 0; } wbuf[wlen++] = (char) prev; have_prev = 0; } if (wlen > 0) { - ssize_t wrote = write(out_fd, wbuf, wlen); - if (wrote < 0 || (size_t) wrote != wlen) die_perror("write failed", out_path, -1); + xwrite_all(out_fd, wbuf, wlen, out_path); wlen = 0; } break; } else { + if (errno == EINTR) continue; die_perror("read failed", in_path, -1); } } @@ -165,6 +166,5 @@ int main(int argc, char *argv[]) { (void) write(STDOUT_FILENO, res, (size_t) m); } - int exit_code = (int) (total_replacements & 0xFF); - return exit_code; + return 0; }