Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <unistd.h>
- ssize_t c_write(int fd, const void *buff, int bytesToWrite) {
- size_t bytesWritten;
- for (bytesWritten = 0; bytesWritten < bytesToWrite; ) {
- ssize_t currentlyWritten = write(fd, buff + bytesWritten, bytesToWrite - bytesWritten);
- if (currentlyWritten == 0) {
- return bytesWritten;
- }
- if (currentlyWritten == -1) {
- return -1;
- }
- bytesWritten += currentlyWritten;
- }
- return bytesWritten;
- }
- ssize_t c_read(int fd, void *buff, int bytesToRead) {
- size_t bytesRead;
- for (bytesRead = 0; bytesRead < bytesToRead; ) {
- ssize_t currentlyRead = read(fd, buff + bytesRead, bytesToRead - bytesRead);
- if (currentlyRead == 0) {
- return bytesRead;
- }
- if (currentlyRead == -1) {
- return -1;
- }
- bytesRead += currentlyRead;
- }
- return bytesRead;
- }
- int main(int argc, char **argv) {
- int exit_code = 0;
- int in = open(argv[1], O_RDONLY);
- if (in == -1) {
- exit_code = 1;
- goto finally;
- }
- int out_digits = open(argv[2], O_WRONLY | O_CREAT, 0640);
- int out_other = open(argv[3], O_WRONLY | O_CREAT, 0640);
- if (out_digits == -1 || out_other == -1) {
- exit_code = 2;
- goto finally;
- }
- ssize_t in_read, out_d_read, out_o_read;
- char buffer[1024], in_byte;
- int out;
- while ((in_read = c_read(in, &buffer, sizeof(buffer))) > 0) {
- for (int i = 0; i < in_read; ++i) {
- in_byte = buffer[i];
- out = '0' <= in_byte && in_byte <= '9'
- ? out_digits
- : out_other;
- if (write(out, &in_byte, sizeof(in_byte)) == -1) {
- exit_code = 3;
- goto finally;
- }
- }
- }
- finally:
- close(in);
- close(out_digits);
- close(out_other);
- return exit_code;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement