/* * Read a soft-character definition string from a file. Strip off garbage * at the beginning (to accommodate the "font2xx" output format). */ void setup_softchars(const char *filename) { FILE *fp; int c; size_t len = 1024; size_t use = 0; char *buffer = (char *) malloc(len); char *s; char *first = 0; char *last = 0; int save_8bits = input_8bits; input_8bits = FALSE; /* use the 7-bit input-parsing */ /* read the file into memory */ if ((fp = fopen(filename, "r")) == 0) { perror(filename); exit(EXIT_FAILURE); } while ((c = fgetc(fp)) != EOF) { if (use + 1 >= len) { buffer = (char *) realloc(buffer, len *= 2); } buffer[use++] = (char) c; } buffer[use] = '\0'; fclose(fp); /* find the DCS that begins the control string */ /* and the ST that ends the control string */ for (s = buffer; *s; s++) { if (first == 0) { if (skip_dcs(s) != 0) first = s; } else { if (!strncmp(s, st_input(), (size_t) 2)) { last = s + 2; *last = '\0'; break; } } } input_8bits = save_8bits; if (first == 0 || last == 0) { fprintf(stderr, "Not a vtXXX font description: %s\n", filename); exit(EXIT_FAILURE); } for (s = buffer; (*s++ = *first++) != '\0';) ; if (LOG_ENABLED && first != 0) fprintf(log_fp, "Font String:\n%s\n", buffer); font_string = buffer; decode_header(); }
/* * Strip the string terminator (ST) from the given string, returning true if * we did this. */ int strip_terminator(char *src) { int ok = strip_suffix(src, st_input()); if (!ok) { int have = (int) strlen(src); if (have > 0 && (unsigned char) src[have - 1] == ST) { ok = TRUE; src[--have] = '\0'; } } if (!ok && LOG_ENABLED) fprintf(log_fp, "Missing ST\n"); return ok; }
int main(int argc, char *argv[]) { int fd; int argc_count; char buff[1]; int status_read; int line_count = 0; int word_count = 0; int char_count = 0; int total_word_count = 0; int total_line_count = 0; int total_char_count = 0; int close_status; char src[100]; //variable for the print errors for(argc_count = 1; argc_count < argc; argc_count++) { //if the program had arguments this loop checks each of them status_read = 1; line_count = 0; word_count = 0; char_count = 0; if((*argv[argc_count] == '-') && (strlen(argv[argc_count]) == 1)) { st_input(&line_count,&word_count,&char_count); //function st_input is called which reads from the standart input } else { fd = open(argv[argc_count], O_RDONLY); //opening the file(argument name) if (fd == -1) { //if the file cant be opened, has bad name, etc. then will be printed an error in the terminal strcpy(src, "wc: "); perror(strcat(src, argv[argc_count])); return 1; } while(status_read > 0) { // reading from file status_read = read(fd, buff, 1); if (status_read < 0) { perror(strcpy(src, "wc: read()")); //an error will be printed in the terminal if there is some issue with the reading return 1; } if(status_read != 0) { //checks if the file is empty char_count++; if(buff[0] == '\n') { // checking for lines line_count++; } if(buff[0] == ' ' || buff[0] == '\n' || buff[0] == '\t') { //checking for words if(temp != ' ' && temp != '\n' && temp != '\t' && temp != '\0' ) { word_count++; } } temp = buff[0]; } } temp = '\0'; close_status = close(fd); //closing the file if (close_status == -1) { perror(strcpy(src, "wc: close()")); //an error will be printed in the terminal if there is some issue with the closing return 1; } } printf("%d %d %d %s\n",line_count,word_count,char_count,argv[argc_count]); //printing the counters and the argument name total_word_count += word_count; total_line_count += line_count; total_char_count += char_count; } if(argc == 1) { // If there are no arguments then the program reads from the standart input st_input(&line_count,&word_count,&char_count); //function st_input is called which reads from the standart input printf("%d %d %d\n",line_count,word_count,char_count); //printing the counters } if(argc > 2) { //If there is more than one argument the program prints the total counters printf("%d %d %d total\n",total_line_count,total_word_count,total_char_count); //printing the total counters } return 0; }