bool determine_repeating_words(void) { int i = 0; struct stack_words *st = { NULL }; FILE *f; char symbol; bool flag = false; if (!(f = fopen("input_file.txt", "r+t"))) { printf("File couldn't be opened. Maybe it doesn't exist\n"); return false; } if (!(st = (struct stack_words*)calloc(1, sizeof(struct stack_words))) || !(st->word = (char*)calloc((STRING_LENGTH + 1), sizeof(char)))) { printf("Memory is not allocated\n"); func_delete(st); exit(-1); } while (1) { if (feof(f)) break; fscanf(f, "%c", &symbol); flag = true; if (symbol >= 'a' && symbol <= 'z') { st->word[i++] = symbol; } else { if (i != 0) { st = insert_word(st, st->word); i = 0; } } } flag = true; flag = print_results(st); flag = func_delete(st); fclose(f); return (flag == false) ? false : true; }
struct stack_words *insert_word(struct stack_words *st, char *word) { struct queue_words *q_current; struct stack_words *top = { NULL }; bool flag = false; if (!(top = (struct stack_words*)calloc(1, sizeof(struct stack_words))) || !(top->word = (char*)calloc((STRING_LENGTH + 1), sizeof(char)))) { printf("Memory is not allocated\n"); func_delete(st); exit(-1); } if (q_first == NULL) { queue_push(st, st->word); top->next = st; return top; } else { q_current = q_first; do { if (strcmp(q_current->word, st->word) == 0) { q_current->repeating_number++; flag = true; break; } q_current = q_current->next; } while (q_current != NULL); if (flag == false) { queue_push(st, st->word); } } top->next = st; return top; }
void queue_push(struct stack_words *st, char *string) { struct queue_words *q; int i = 0; if (!(q = (struct queue_words*)calloc(1, sizeof(struct queue_words))) || !(q->word = (char*)calloc(WORD_LENGTH, sizeof(char)))) { printf("Memory is not allocated\n"); func_delete(st); exit(-1); } do { q->word[i] = string[i++]; } while (string[i] != '\0'); q->repeating_number = 1; if (q_first == NULL) q_first = q_last = q; else { q_last->next = q; q_last = q; } }
int main(int argc, char *argv[]) { void (*OldSig)(int); OldSig=signal(SIGINT,SIG_IGN); printf("nn************************************************************rn"); printf("* zFTP *rn"); printf("************************************************************rn"); printf(" Copyright (c) 2005rn"); printf(" All rights reserved.rn"); printf(" By Ji Surn"); if(argc > 0) { printf("nPlease use 'connect <system name> <user>' command to connect to a FTPserverrn"); printf("For anonymous user, please leave <user> blank.rn"); } //Main loop starts; //Checks on keyboard i/o for interaction and socket communication. int exit=0; char command[1024]; printf("nzftp>"); while(!exit) { if( ReadCommand ) { printf("nzftp>"); fflush(stdout); } if( !CheckFds(command) ) continue; switch(check_cmd(command) ) { case READ: func_read(command); break; case WRITE: func_write(command); break; case BINARY: func_binary_mode(); break; case ASCII: func_ascii_mode(); break; case CLOSE: func_close(); break; case CONNECT: func_connect(command); break; case CD: func_cdir(command); break; case DELETE: func_delete(command); break; case PWD: func_pwd(command); break; case LS: func_list(command); break; case HELP: func_help(command); break; case SHELL: func_Shell_cmd(command); break; case EXIT: exit = 1; if( flag_connected ) func_close(); break; default: if(command[0] == 0 ) ; else printf("Invalid command: %sn",command); break; } } (void)signal(SIGINT,OldSig); return 0; }