1 2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 2021 22 23 24 2526 27 28 29 3031 32 33 34 3536 37 38 39 4041 42 43 44 4546 47 48 49 5051 52 53 54 5556 57 58 59 6061 62 63 64 65 | #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <unistd.h> #define BUFSIZE 256 int main(int argc, char *argv[]) { FILE *fp; char buf[BUFSIZE]; char *filename; int lines, chars, cpl; setregid(1011,1011); // set real and effective GID to level10 if (argc != 2) { fprintf(stderr, "Usage: %s filename\n", argv[0]); exit(EXIT_FAILURE); } filename= argv[1]; printf("Counting %s ... \n", filename); if (snprintf(buf, BUFSIZE, "/usr/bin/wc -l %s", filename)>=BUFSIZE) { fprintf(stderr, "Filename %s is too long!\n", filename); exit(EXIT_FAILURE); } fp= popen(buf, "r"); if (!fp) { fprintf(stderr, "Command execution failed! Sorry!\n"); exit(EXIT_FAILURE); } if (1!=fscanf(fp, "%d", &lines)) { fprintf(stderr, "Cannot scan! Sorry!\n"); } pclose(fp); snprintf(buf, BUFSIZE, "/usr/bin/wc -c %s", filename); fp= popen(buf, "r"); if (!fp) { fprintf(stderr, "Command execution failed! Bummer!\n"); exit(EXIT_FAILURE); } if (1!=fscanf(fp, "%d", &chars)) { fprintf(stderr, "Cannot scan! Sorry!\n"); } pclose(fp); cpl= chars/lines; printf("Chars per line is %d.\n", cpl); return EXIT_SUCCESS; } |
Say *NO* to advertisements!