vorking vlad's
This commit is contained in:
8
lab_1/vlad`s/in.txt
Normal file
8
lab_1/vlad`s/in.txt
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
aaaaaa
|
||||||
|
aaaaaaaaaaaa
|
||||||
|
aaaaaaaaaaaa
|
||||||
|
aaaaaaaaaaaa
|
||||||
|
2
|
||||||
|
aaaaaaaaaaaa
|
||||||
|
aaaaaaaaaaaa
|
||||||
|
aaaaaa
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
aaaaaa
|
|
||||||
aaaaaa
|
|
||||||
@@ -12,12 +12,13 @@
|
|||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -32,6 +33,18 @@ static long long parse_ll(const char *s) {
|
|||||||
return v;
|
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[]) {
|
int main(int argc, char *argv[]) {
|
||||||
if (argc != 4) {
|
if (argc != 4) {
|
||||||
const char *usage =
|
const char *usage =
|
||||||
@@ -54,7 +67,7 @@ int main(int argc, char *argv[]) {
|
|||||||
die_perror("open input failed", in_path, -1);
|
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);
|
int out_fd = open(out_path, O_CREAT | O_WRONLY | O_TRUNC, filePerms);
|
||||||
if (out_fd < 0) {
|
if (out_fd < 0) {
|
||||||
die_perror("open output failed", out_path, -1);
|
die_perror("open output failed", out_path, -1);
|
||||||
@@ -64,7 +77,6 @@ int main(int argc, char *argv[]) {
|
|||||||
char wbuf[WBUFSZ];
|
char wbuf[WBUFSZ];
|
||||||
size_t wlen = 0;
|
size_t wlen = 0;
|
||||||
|
|
||||||
/* state */
|
|
||||||
long long total_replacements = 0;
|
long long total_replacements = 0;
|
||||||
int replacing_enabled = 1;
|
int replacing_enabled = 1;
|
||||||
|
|
||||||
@@ -78,14 +90,12 @@ int main(int argc, char *argv[]) {
|
|||||||
unsigned char c = (unsigned char) rbuf[i];
|
unsigned char c = (unsigned char) rbuf[i];
|
||||||
|
|
||||||
if (!have_prev) {
|
if (!have_prev) {
|
||||||
/* buffer first byte of a potential pair */
|
|
||||||
prev = c;
|
prev = c;
|
||||||
have_prev = 1;
|
have_prev = 1;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (c == prev) {
|
if (c == prev) {
|
||||||
/* non-overlapping pair found: output prev, then second element possibly replaced */
|
|
||||||
unsigned char out1 = prev;
|
unsigned char out1 = prev;
|
||||||
unsigned char out2 = c;
|
unsigned char out2 = c;
|
||||||
|
|
||||||
@@ -99,29 +109,22 @@ int main(int argc, char *argv[]) {
|
|||||||
replacing_enabled = 0;
|
replacing_enabled = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* write out1 */
|
|
||||||
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) out1;
|
wbuf[wlen++] = (char) out1;
|
||||||
|
|
||||||
/* write out2 */
|
|
||||||
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) out2;
|
wbuf[wlen++] = (char) out2;
|
||||||
|
|
||||||
/* consume pair: reset have_prev so next byte starts a new potential pair */
|
|
||||||
have_prev = 0;
|
have_prev = 0;
|
||||||
} else {
|
} else {
|
||||||
/* no pair: output prev, shift window to c */
|
|
||||||
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) prev;
|
wbuf[wlen++] = (char) prev;
|
||||||
@@ -131,23 +134,21 @@ int main(int argc, char *argv[]) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (n == 0) {
|
} else if (n == 0) {
|
||||||
/* flush any pending single prev */
|
|
||||||
if (have_prev) {
|
if (have_prev) {
|
||||||
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) prev;
|
wbuf[wlen++] = (char) prev;
|
||||||
have_prev = 0;
|
have_prev = 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -165,6 +166,5 @@ int main(int argc, char *argv[]) {
|
|||||||
(void) write(STDOUT_FILENO, res, (size_t) m);
|
(void) write(STDOUT_FILENO, res, (size_t) m);
|
||||||
}
|
}
|
||||||
|
|
||||||
int exit_code = (int) (total_replacements & 0xFF);
|
return 0;
|
||||||
return exit_code;
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user