working kirill's
This commit is contained in:
@@ -1,2 +1,4 @@
|
|||||||
123456
|
123456123456123456123456123456123456123456123456
|
||||||
123456
|
123456123456
|
||||||
|
123456123456
|
||||||
|
123456123456
|
||||||
|
|||||||
@@ -12,15 +12,28 @@
|
|||||||
static void die_perror(const char *what, const char *path, int exit_code) {
|
static void die_perror(const char *what, const char *path, int exit_code) {
|
||||||
int saved = errno;
|
int saved = errno;
|
||||||
char msg[512];
|
char msg[512];
|
||||||
|
int n;
|
||||||
if (path) {
|
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 {
|
} 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);
|
_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) {
|
static long long parse_ll(const char *s) {
|
||||||
char *end = NULL;
|
char *end = NULL;
|
||||||
errno = 0;
|
errno = 0;
|
||||||
@@ -36,7 +49,7 @@ int main(int argc, char *argv[]) {
|
|||||||
if (argc != 4) {
|
if (argc != 4) {
|
||||||
const char *usage =
|
const char *usage =
|
||||||
"Usage: lab1_var_thirds_line <input.txt> <output.txt> <max_replacements>\n"
|
"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));
|
(void) write(STDERR_FILENO, usage, strlen(usage));
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -49,7 +62,7 @@ int main(int argc, char *argv[]) {
|
|||||||
int in_fd = open(in_path, O_RDONLY);
|
int in_fd = open(in_path, O_RDONLY);
|
||||||
if (in_fd < 0) die_perror("open input failed", in_path, -1);
|
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);
|
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);
|
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;
|
size_t wlen = 0;
|
||||||
|
|
||||||
long long total = 0;
|
long long total = 0;
|
||||||
long long col = 0; /* position in current line starting from 1 */
|
long long col = 0;
|
||||||
int replacing_enabled = 1;
|
int replacing_enabled = 1;
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
@@ -68,10 +81,8 @@ int main(int argc, char *argv[]) {
|
|||||||
unsigned char c = (unsigned char) rbuf[i];
|
unsigned char c = (unsigned char) rbuf[i];
|
||||||
|
|
||||||
if (c == '\n') {
|
if (c == '\n') {
|
||||||
/* newline is written as-is and resets counter */
|
|
||||||
if (wlen == sizeof(wbuf)) {
|
if (wlen == sizeof(wbuf)) {
|
||||||
ssize_t wrote = write(out_fd, wbuf, wlen);
|
xwrite_all(out_fd, wbuf, wlen, out_path);
|
||||||
if (wrote < 0 || (size_t) wrote != wlen) die_perror("write failed", out_path, -1);
|
|
||||||
wlen = 0;
|
wlen = 0;
|
||||||
}
|
}
|
||||||
wbuf[wlen++] = '\n';
|
wbuf[wlen++] = '\n';
|
||||||
@@ -79,34 +90,30 @@ int main(int argc, char *argv[]) {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
col++; /* advance within the current line */
|
|
||||||
|
|
||||||
unsigned char outc = c;
|
unsigned char outc = c;
|
||||||
if (replacing_enabled && (col % 3 == 0)) {
|
|
||||||
if (total < cap) {
|
col++;
|
||||||
outc = ' ';
|
|
||||||
total++;
|
if (replacing_enabled && (col % 3 == 0) && total < cap) {
|
||||||
if (total == cap) replacing_enabled = 0;
|
outc = ' ';
|
||||||
} else {
|
total++;
|
||||||
replacing_enabled = 0;
|
if (total == cap) replacing_enabled = 0;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (wlen == sizeof(wbuf)) {
|
if (wlen == sizeof(wbuf)) {
|
||||||
ssize_t wrote = write(out_fd, wbuf, wlen);
|
xwrite_all(out_fd, wbuf, wlen, out_path);
|
||||||
if (wrote < 0 || (size_t) wrote != wlen) die_perror("write failed", out_path, -1);
|
|
||||||
wlen = 0;
|
wlen = 0;
|
||||||
}
|
}
|
||||||
wbuf[wlen++] = (char) outc;
|
wbuf[wlen++] = (char) outc;
|
||||||
}
|
}
|
||||||
} else if (n == 0) {
|
} else if (n == 0) {
|
||||||
if (wlen > 0) {
|
if (wlen > 0) {
|
||||||
ssize_t wrote = write(out_fd, wbuf, wlen);
|
xwrite_all(out_fd, wbuf, wlen, out_path);
|
||||||
if (wrote < 0 || (size_t) wrote != wlen) die_perror("write failed", out_path, -1);
|
|
||||||
wlen = 0;
|
wlen = 0;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
|
if (errno == EINTR) continue;
|
||||||
die_perror("read failed", in_path, -1);
|
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);
|
int m = snprintf(res, sizeof(res), "%lld\n", total);
|
||||||
if (m > 0) (void) write(STDOUT_FILENO, res, (size_t) m);
|
if (m > 0) (void) write(STDOUT_FILENO, res, (size_t) m);
|
||||||
|
|
||||||
int exit_code = (int) (total & 0xFF);
|
return 0;
|
||||||
return exit_code;
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user