working kirill's
This commit is contained in:
@@ -1,2 +1,4 @@
|
||||
123456
|
||||
123456
|
||||
123456123456123456123456123456123456123456123456
|
||||
123456123456
|
||||
123456123456
|
||||
123456123456
|
||||
|
||||
@@ -12,15 +12,28 @@
|
||||
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);
|
||||
}
|
||||
|
||||
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;
|
||||
@@ -36,7 +49,7 @@ 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 byte in each line with a space; counter resets after each newline.\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;
|
||||
}
|
||||
@@ -49,7 +62,7 @@ int main(int argc, char *argv[]) {
|
||||
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; /* 0666 */
|
||||
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);
|
||||
|
||||
@@ -58,7 +71,7 @@ int main(int argc, char *argv[]) {
|
||||
size_t wlen = 0;
|
||||
|
||||
long long total = 0;
|
||||
long long col = 0; /* position in current line starting from 1 */
|
||||
long long col = 0;
|
||||
int replacing_enabled = 1;
|
||||
|
||||
for (;;) {
|
||||
@@ -68,10 +81,8 @@ int main(int argc, char *argv[]) {
|
||||
unsigned char c = (unsigned char) rbuf[i];
|
||||
|
||||
if (c == '\n') {
|
||||
/* newline is written as-is and resets counter */
|
||||
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++] = '\n';
|
||||
@@ -79,34 +90,30 @@ int main(int argc, char *argv[]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
col++; /* advance within the current line */
|
||||
|
||||
unsigned char outc = c;
|
||||
if (replacing_enabled && (col % 3 == 0)) {
|
||||
if (total < cap) {
|
||||
outc = ' ';
|
||||
total++;
|
||||
if (total == cap) replacing_enabled = 0;
|
||||
} else {
|
||||
replacing_enabled = 0;
|
||||
}
|
||||
|
||||
col++;
|
||||
|
||||
if (replacing_enabled && (col % 3 == 0) && total < cap) {
|
||||
outc = ' ';
|
||||
total++;
|
||||
if (total == cap) replacing_enabled = 0;
|
||||
}
|
||||
|
||||
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) outc;
|
||||
}
|
||||
} else if (n == 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);
|
||||
}
|
||||
}
|
||||
@@ -118,6 +125,5 @@ int main(int argc, char *argv[]) {
|
||||
int m = snprintf(res, sizeof(res), "%lld\n", total);
|
||||
if (m > 0) (void) write(STDOUT_FILENO, res, (size_t) m);
|
||||
|
||||
int exit_code = (int) (total & 0xFF);
|
||||
return exit_code;
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user