Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <ctype.h>
- #include <sys/stat.h>
- #include <string.h>
- int getFD(const char *const fname, int flags, int perm) {
- int fd = open(fname, flags, perm);
- if (fd < 0) {
- perror("");
- exit(1);
- }
- return fd;
- }
- void closeFD(int fd) {
- if (close(fd) < 0) {
- perror("");
- exit(1);
- }
- }
- int main(int argc, char *argv[]) {
- if (argc != 4) {
- fprintf(stderr, "Wrong usage!\n");
- exit(1);
- }
- int fd1 = getFD(argv[1], O_RDONLY, 0);
- int lower = 0, upper = 0, digits = 0, cnt = 0;
- char ch;
- while (read(fd1, &ch, 1) > 0) {
- if (islower(ch)) {
- lower++;
- }
- if (isupper(ch)) {
- upper++;
- }
- if (isdigit(ch)) {
- digits++;
- }
- if (ch == argv[3][0]) {
- cnt++;
- }
- }
- int dim;
- struct stat buff;
- if (fstat(fd1, &buff) == -1) {
- perror("fstat");
- exit(1);
- }
- dim = buff.st_size;
- int fd2 = getFD(argv[2], O_RDWR | O_CREAT, S_IWUSR | S_IRUSR);
- char s[30];
- sprintf(s, "nr lower: %d\n", lower);
- write(fd2, s, strlen(s));
- sprintf(s, "nr upper: %d\n", upper);
- write(fd2, s, strlen(s));
- sprintf(s, "nr digits: %d\n", digits);
- write(fd2, s, strlen(s));
- sprintf(s, "nr %c: %d\n", argv[3][0], cnt);
- write(fd2, s, strlen(s));
- sprintf(s, "size: %d\n", dim);
- write(fd2, s, strlen(s));
- closeFD(fd1);
- closeFD(fd2);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement