Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <errno.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdint.h>
- int main(int argc, char **argv)
- {
- int fd;
- ssize_t bytesRead;
- uint8_t buf[1024];
- size_t count[sizeof(buf)];
- int closed;
- ssize_t written;
- if(argc == 1 ) //no arguments ->read from stdin and write to stdout
- {
- while(read(STDIN_FILENO, buf, 1) != 0)
- {
- write(STDOUT_FILENO, buf, 1);
- }
- exit(0);
- }
- else
- {
- for (int i = 1; i < argc; ++i) //for each argument:
- {
- fd = open(argv[i], O_RDONLY); //open file
- if (fd == -1) //check error
- {
- perror(argv[i]);
- exit(errno);
- }
- else
- {
- while((bytesRead = read(fd, buf, *buf)) != 0) //read file to the end "0"
- {
- if(bytesRead == -1) //error checking
- {
- perror(argv[i]);
- exit(errno);
- }
- else
- {
- count[i] = bytesRead; //count number of bytes in file
- written = write(STDOUT_FILENO, buf, count[i]); //print out file contents
- if(written == -1) //error checking
- {
- perror(argv[i]);
- exit(errno);
- }
- }
- }
- }
- closed = close(fd); //close file descriptor
- if(closed == -1)
- {
- perror(argv[i]);
- exit(errno);
- }
- }
- }
- return 0;
- }
- 69,1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement