Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //count.h
- #ifndef __count__
- #define __count__
- #include <string.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <ctype.h>
- #include "error.h"
- typedef struct
- {
- int newline;
- int word;
- int byte;
- } info;
- int len(int a);
- int max(int a, int b);
- info count(int descr);
- #endif
- ///////////////////////////////////////////////////////////
- //count.c
- #include "count.h"
- int len(int a)
- {
- int res = 0;
- while(a)
- {
- res++;
- a/=10;
- }
- return res;
- }
- int max(int a, int b)
- {
- return ((a > b) ? a : b);
- }
- info count(int descr)
- {
- info kol;
- kol.newline = 0;
- kol.word = 0;
- kol.byte = 0;
- int len = 512;
- char s[512];
- int n;
- errno = 0;
- int flag1 = 1;
- int flag2 = 1;
- while(n = read(descr, s, len))
- {
- if(n == -1)
- error("Error while reading.", errno);
- errno = 0;
- kol.byte+=n;
- for(int i=0; i<n; i++)
- {
- if(flag1)
- {
- kol.newline++;
- flag1 = 0;
- }
- if(isblank(s[i]) || s[i] == '\n')
- flag2 = 1;
- else
- {
- if(flag2)
- {
- kol.word++;
- flag2 = 0;
- }
- }
- if(s[i] == '\n')
- flag1 = 1;
- }
- }
- return kol;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement