Files
CS-LABS/kirill/lab_3/task18.c
2025-12-10 16:50:28 +07:00

130 lines
3.7 KiB
C

#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#define RBUFSZ 4096
#define WBUFSZ 4096
static void die_perror(const char *what, const char *path, int exit_code) {
int saved = errno;
char msg[512];
int n;
if (path) {
n = snprintf(msg, sizeof(msg), "%s: %s: %s\n", what, path, strerror(saved));
} else {
n = snprintf(msg, sizeof(msg), "%s: %s\n", what, strerror(saved));
}
if (n > 0) (void) write(STDERR_FILENO, msg, (size_t) n);
_exit(exit_code);
}
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;
}
}
static long long parse_ll(const char *s) {
char *end = NULL;
errno = 0;
long long v = strtoll(s, &end, 10);
if (errno != 0 || end == s || *end != '\0' || v < 0) {
errno = EINVAL;
return -1;
}
return v;
}
int main(int argc, char *argv[]) {
if (argc != 4) {
const char *usage =
"Usage: lab1_var_thirds_line <input.txt> <output.txt> <max_replacements>\n"
"Replace every third non-newline byte in each line with a space; counter resets after LF.\n";
(void) write(STDERR_FILENO, usage, strlen(usage));
return -1;
}
const char *in_path = argv[1];
const char *out_path = argv[2];
long long cap = parse_ll(argv[3]);
if (cap < 0) die_perror("invalid max_replacements", argv[3], -1);
int in_fd = open(in_path, O_RDONLY);
if (in_fd < 0) die_perror("open input failed", in_path, -1);
mode_t perms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
int out_fd = open(out_path, O_CREAT | O_WRONLY | O_TRUNC, perms);
if (out_fd < 0) die_perror("open output failed", out_path, -1);
char rbuf[RBUFSZ];
char wbuf[WBUFSZ];
size_t wlen = 0;
long long total = 0;
long long col = 0;
int replacing_enabled = 1;
for (;;) {
ssize_t n = read(in_fd, rbuf, sizeof(rbuf));
if (n > 0) {
for (ssize_t i = 0; i < n; i++) {
unsigned char c = (unsigned char) rbuf[i];
if (c == '\n') {
if (wlen == sizeof(wbuf)) {
xwrite_all(out_fd, wbuf, wlen, out_path);
wlen = 0;
}
wbuf[wlen++] = '\n';
col = 0;
continue;
}
unsigned char outc = c;
col++;
if (replacing_enabled && (col % 3 == 0) && total < cap) {
outc = ' ';
total++;
if (total == cap) replacing_enabled = 0;
}
if (wlen == sizeof(wbuf)) {
xwrite_all(out_fd, wbuf, wlen, out_path);
wlen = 0;
}
wbuf[wlen++] = (char) outc;
}
} else if (n == 0) {
if (wlen > 0) {
xwrite_all(out_fd, wbuf, wlen, out_path);
wlen = 0;
}
break;
} else {
if (errno == EINTR) continue;
die_perror("read failed", in_path, -1);
}
}
if (close(in_fd) < 0) die_perror("close input failed", in_path, -1);
if (close(out_fd) < 0) die_perror("close output failed", out_path, -1);
char res[64];
int m = snprintf(res, sizeof(res), "%lld\n", total);
if (m > 0) (void) write(STDOUT_FILENO, res, (size_t) m);
return 0;
}