/* Compares STRING obtained from the user against the full name of a COMMAND, using this algorithm: 1. Divide COMMAND into words C[0] through C[n - 1]. 2. Divide STRING into words S[0] through S[m - 1]. 3. Compare word C[i] against S[i] for 0 <= i < min(n, m), using the keyword matching algorithm implemented by lex_id_match(). If any of them fail to match, then STRING does not match COMMAND and the function returns false. 4. Otherwise, STRING and COMMAND match. Set *MISSING_WORDS to n - m. Set *EXACT to false if any of the S[i] were found to be abbreviated in the comparisons done in step 3, or to true if they were all exactly equal (modulo case). Return true. */ bool command_match (struct substring command, struct substring string, bool *exact, int *missing_words) { *exact = true; for (;;) { struct substring cw, sw; int match; if (!find_word (&command, &cw)) { *missing_words = -count_words (string); return true; } else if (!find_word (&string, &sw)) { *missing_words = 1 + count_words (command); return true; } match = lex_id_match (cw, sw); if (sw.length < cw.length) *exact = false; if (match == 0) return false; } }
static void parse(char *s) { char **tab; int words; int i; int j; words = count_words(s); if (!(tab = malloc((sizeof(char *) * (1 + words))))) return ; tab[words] = NULL; i = -1; j = -1; while (s[++i]) { if (!i || ((s[i - 1] == 0 || strchr(" \t", s[i - 1])) && !strchr(" \t", s[i]))) tab[++j] = s + i; else if (strchr(" \t", s[i])) s[i] = 0; } sort(tab); display(tab); free(tab); }
main(int argc, char *argv[]) { /* Open a file and read it */ FILE *file; char *filename = argv[1]; /* open the file for reading */ file = fopen(filename, "r"); if (file == NULL) { fprintf(stderr, "File %s could not be opened\n", filename); exit(1); } /* loop while reading a line at a time from the file and printing */ char buffer[200]; while (1) { fgets(buffer, 200, file); /* gets a line up to 200 characters */ if (!feof(file)) { lines++; } else { break; /* if EOF, break out of loop */ } total_words = count_words(buffer); } /* print totals to the console */ printf("Number of lines: %d\n", lines); printf("Number of words: %d\n", total_words); /* close the file */ fclose(file); }
/** * Procedure to parse shebang line and transform it to standard command * * In general, the shebang line has n parts at the beginnning: * argv[0] => the name of the utility stated in shebang * argv[1] => all the arguments as one string * argv[2] => the script which was executed * argv[n] => argument for executed script * * Considering the following shebang line in script ./test.py: * #!/usr/bin/scl enable collectionX * argv[0] = "/usr/bin/scl" * argv[1] = "enable collectionX" * argv[2] = "./test.py" * argv[n] = "argn" */ static int parse_shebang(int argc, char *argv[], int *_shebang_argc, char ***_shebang_argv) { int i = 0; char *p; int shebang_argc; char **shebang_argv; shebang_argc = count_words(argv[1], ' ') + argc - 1; shebang_argv = (char **)xmalloc(sizeof(char *) * shebang_argc); shebang_argv[i++] = argv[0]; p = strtok(argv[1], " "); while (p != NULL) { shebang_argv[i++] = p; p = strtok(NULL, " "); } while (i < shebang_argc) { shebang_argv[i] = argv[i - (shebang_argc - argc)]; i++; } *_shebang_argc = shebang_argc; *_shebang_argv = shebang_argv; return EOK; }
int main() { word_list_t wl; word_map_t counted; wl = word_list_create(10); word_list_insert(wl, "one"); word_list_insert(wl, "two"); word_list_insert(wl, "two"); word_list_insert(wl, "three"); word_list_insert(wl, "three"); word_list_insert(wl, "three"); word_list_insert(wl, "four"); word_list_insert(wl, "four"); word_list_insert(wl, "four"); word_list_insert(wl, "four"); counted = count_words(wl); word_map_foreach(counted, print_word_count); word_map_free(counted); word_list_free(wl); return 0; }
void wc(FILE *ofile, FILE *infile, char *inname) { char *code; int c; size_t n = 0; code = malloc(1000); int char_count = 0; int line_count = 0; int word_count = 0; char path[1024]; char result[1024]; int fd = fileno(ofile); while ((c = fgetc(ofile)) != EOF) { code[n++] = (char) c; } code[n] = '\0'; char_count = count_characters(code); line_count = count_lines(code); word_count = count_words(code); sprintf(path, "/proc/self/fd/%d",fd); memset(result, 0, sizeof(result)); readlink(path, result, sizeof(result)-1); printf(" %d\t%d\t%d\t%s\t%s\n", line_count, word_count, char_count, result, path); }
t_p *stock(t_p *f, int fd, char **argv) { char *line; int ret; f = malloc(sizeof(t_p)); f->len = 0; f->i = 0; fd = open(argv[1], O_RDONLY); if (fd == -1) { ft_putstr_fd("Fichier non valide\n", 2); exit(1); } if (!(f->map = (char **)malloc(sizeof(char *) * 10000))) exit(EXIT_FAILURE); while((ret = get_next_line(fd, &line) && ret != -1)) { if (line[0] != ' ' && !ft_isdigit(line[0])) exit(EXIT_FAILURE); f->map[f->i] = ft_strnew(1); f->map[f->i] = ft_strjoin(f->map[f->i],line); free(line); f->i++; } f->len = count_words(f->map[0], ' '); f->map[f->i] = NULL; return (f); }
int read_and_exec(int socket) { struct buf_t* buf = buf_new(8049); int pos = buf_readuntil(socket, buf, '\n'); if (pos == -2) return 0; char* buffer = buf->data; buffer[pos] = '\0'; struct execargs_t* arguments[1024]; int k = 0; while (1) { char delim; int argc = count_words(buffer, '|'); if (argc == 0) break; char* argv[argc]; int shift; int i = 0; for (i = 0; i < argc; i++) { delim = '|'; argv[i] = get_word(buffer, &delim, &shift); buffer += shift; } arguments[k] = (struct execargs_t*) malloc(sizeof(struct execargs_t)); *arguments[k] = new_args(argc, argv); shift = get_delim(buffer, '|'); buffer+=shift; k++; } buf->size -= (buffer - (char*) buf->data + 1); runpiped(arguments, k, socket); buf_free(buf); return 0; }
int main(void) { char str[] = "I am fi94ve words lon1g s9IXNOW!"; printf("%d\n", count_words(str)); return (0); }
/* * Command constructor * Used to create a new instance of a command by * malloc'ing and creating values based off the * given char * input. */ command *new_command(char *str) { char *c = str; int words = count_words(c); command *cmd = (command *)malloc(sizeof(command) + 1); cmd->name = NULL; cmd->argv = (char **)malloc(sizeof(char *) * words); cmd->output_file = NULL; cmd->input_file = NULL; cmd->argc = words; int i; char *p; for (i = 0, p = strtok(str, " "); p != NULL; i++, p = strtok(NULL, " ")) { if (strcmp(p, ">") == 0) { p = strtok(NULL, " "); cmd->output_file = (char *)malloc(sizeof(char) * strlen(p)); strcpy(cmd->output_file, p); continue; } if (strcmp(p, "<") == 0) { p = strtok(NULL, " "); cmd->input_file = (char *)malloc(sizeof(char) * strlen(p)); strcpy(cmd->input_file, p); continue; } cmd->argv[i] = (char *)malloc(sizeof(char) * strlen(p)); strcpy(cmd->argv[i], p); } cmd->name = (char *)malloc(sizeof(char) * strlen(cmd->argv[0])); strcpy(cmd->name, cmd->argv[0]); return cmd; }
//Main functions int main(int argc, char* argv[]) { //I didn't error check because this is just an example. Normally you'd want to error check to see if the user provided arguments //Creating some variables so that we don't have to write out the function names while outputting std::string text = read_file(argv[1]); std::string word = "What"; std::string filename = argv[1]; //Setting up some variables that require an integer return value int word_occurrence = count_word_occurrence(text, word); int word_count = count_words(text); int size_of_file = get_file_size(argv[1]); //Seeing what some of the text file manipulation functions! std::cout << text << "\nContains the word: " << word << " " << word_occurrence << " times! The word count of this text is: " << word_count << "\n\n"; //Getting the size of a file std::cout << "Current size of file: " << print_size_in_format(size_of_file) << "\n\n"; std::cout << "Size of the file in kb: " << bytes_to_format(size_of_file, "kb") << "KB\n"; std::cout << "Size of the file in mb: " << bytes_to_format(size_of_file, "mb") << "MB\n"; std::cout << "Size of the file in gb: " << bytes_to_format(size_of_file, "gb") << "GB\n"; return 0; }
int trie::count_words(node *n, std::string str) { if (str[0] == '\0') { return n->words; } else { int index = str[0] - 'a'; if (n->edges[index] == 0) { return 0; } str.erase(0,1); return count_words(n->edges[index],str); } }
void test_zero_words(void){ char buff[MAX_OUT_BUFF] = ""; char *words[] = {NULL}; struct yaml_stringaab * ys = create_yamlaab(); print_test_infoaab(ys, "test_zero_words", "", "", "20"); clear_hashtab(); count_words(words, 0); qsort(hashtab, HASH_TAB, sizeof(struct hashrec), cmp); print_hashtab(buff, MAX_OUT_BUFF); print_test_outputaab(ys, buff); }
void test_repeat_word(void){ char buff[MAX_OUT_BUFF] = ""; char *words[] = {"a", "a", "a", "a", "a"}; struct yaml_stringaab * ys = create_yamlaab(); print_test_infoaab(ys, "test_repeat_word", "a a a a a", "a 5", "20"); clear_hashtab(); count_words(words, sizeof(words)/sizeof(char*)); qsort(hashtab, HASH_TAB, sizeof(struct hashrec), cmp); print_hashtab(buff, MAX_OUT_BUFF); print_test_outputaab(ys, buff); }
int main() { int T = 0, x = 0; scanf("%d", &T); // bounds checker if(T < 1 || T > 100) return -1; // string initialize char str[T][255]; memset(str, 0, sizeof(str)); while(x < T) scanf(" %255[^\n]s", str[x++]); for(x = 0; x < T; x++) { int i = 0; // iteration int words = count_words(str[x]); // counts words char *iter = strtok(str[x], " "), *main_ptr[words]; // declares main_ptr based on number of words while(iter != NULL) { main_ptr[i++] = iter; // sets main_ptr to point the specific word given by iter iter = strtok(NULL, " "); // point another word } i = words - 1; // offset by one (array) // output the results printf("Case #%d: ", x + 1); while(i >= 0) printf("%s ", main_ptr[i--]); printf("\n"); } getch(); return 0; }
void test_unique_words(void){ char buff[MAX_OUT_BUFF] = ""; char *words[] = {"cat", "sat", "on", "the", "mat"}; struct yaml_stringaab * ys = create_yamlaab(); print_test_infoaab(ys, "test_unique_words", "cat sat on the mat", "cat 1\nmat 1\non 1\nsat 1\nthe 1", "20"); clear_hashtab(); count_words(words, sizeof(words)/sizeof(char*)); qsort(hashtab, HASH_TAB, sizeof(struct hashrec), cmp); print_hashtab(buff, MAX_OUT_BUFF); print_test_outputaab(ys, buff); }
static void normal_setup(const char *text, bool debug, cowmode_t mode) { char *text_copy = strdup(text); // Trim any trailing newline size_t len = strlen(text_copy); if ('\n' == text_copy[len-1]) text_copy[len-1] = '\0'; // Count the words and work out the display time, if neccessary xcowsay.display_time = get_int_option("display_time"); if (xcowsay.display_time < 0) { int words = count_words(text_copy); xcowsay.display_time = words * get_int_option("reading_speed"); debug_msg("Calculated display time as %dms from %d words\n", xcowsay.display_time, words); } else { debug_msg("Using default display time %dms\n", xcowsay.display_time); } int min_display = get_int_option("min_display_time"); int max_display = get_int_option("max_display_time"); if (xcowsay.display_time == 0) { xcowsay.display_time = INT_MAX; debug_msg("Set display time to permanent\n"); } else if (xcowsay.display_time < min_display) { xcowsay.display_time = min_display; debug_msg("Display time too short: clamped to %d\n", min_display); } else if (xcowsay.display_time > max_display) { xcowsay.display_time = max_display; debug_msg("Display time too long: clamped to %d\n", max_display); } const int cow_width = shape_width(xcowsay.cow); const int max_width = xcowsay.screen_width - cow_width; xcowsay.bubble_pixbuf = make_text_bubble( text_copy, &xcowsay.bubble_width, &xcowsay.bubble_height, max_width, mode); free(text_copy); }
static void* stats_cmd (int argc, char **argv, void *data) { int words = 0, leaves = 0, nodes = 0; Node *pos = (Node *) data; Node *node = node_root (pos); while (node) { nodes++; words += count_words ((unsigned char *)fixnullstring (node_get (node, TEXT))); if (!node_right (node)) leaves++; node = node_recurse (node); } cli_outfunf ("nodes:%i, leaves:%i words:%i", nodes, leaves, words); return pos; }
int main() { printf("Running client 2\n"); int shmid; key_t key; char *shm, *s; key = 3333; // same key as server to access the same shared memory // not creating shared memory, assume already created if ((shmid = shmget(key, MAXSIZE, 0666)) < 0) die("shmget"); if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) die("shmat"); //Now read what the server put in the memory. char store[MAXSIZE]; int counter = 0; for (s = shm, counter = 0 ; *s != '\0'; s++, counter++) { // store whatever it is shared memory into store store[counter] = *s; // print out what its reading putchar(*s); } putchar('\n'); int count = count_words(store); *s++ = count; //printf("Word count: %i\n", count); /* * Change the first character of the * segment to '*', indicating we have read * the segment. * This should make the server terminate */ *shm = '*'; exit(0); }
int main () { count_words(); auto cube = cubes(-5, 10); // move construct returned value std::cout << cube.at(-2) << '\n'; // -8 std::cout << cube.at(5) << '\n'; // 125 const int n = 30; try { std::cout << cube.at(n) << '\n'; // 30 is not in the Map } catch (std::out_of_range) { std::cout << n << " not in cubes range\n"; } // constructors and assignment examples: // initializer_list example cs540::Map<int, double> int_double_map {{1, 1.0}, {3, 5.67}, {13, 6.9}}; // must support copy construction cs540::Map<int, double> copy_example{int_double_map}; // must support move construction cs540::Map<int, double> move_example {std::move(int_double_map)}; cs540::Map<int, double> assign_example; // must support copy assignment assign_example = copy_example; // must support move assignment assign_example = std::move(move_example); // moved-from object must still support assignment move_example = copy_example; move_insert(); access_by_key(); stress(10000); return 0; }
int main (int argc, char *argv[]) { int char_count = 0; int line_count = 0; int word_count = 0; if(argv[2]) { if(access(argv[2],F_OK) != -1) { wc(fopen(argv[2], "r"),"",""); } else { char_count = count_characters(argv[2]); line_count = count_lines(argv[2]); word_count = count_words(argv[2]); printf(" %d\t%d\t%d\t%s\n", line_count, char_count, word_count, argv[2]); } } else { printf("No argument passed"); } return 0; }
lcll_t *pre_process(SFILE *sfp, size_t *wordc, size_t *linec) { char *data = &sfp->data[0], *line = NULL; *wordc = 0, *linec = 1; lc_t *new_lc; lcll_t *head = NULL, *iter, *next; line = strsep(&data, "\n"); while (line && (cgc_strlen(line) || (data && cgc_strlen(data)))) { next = malloc(sizeof(lcll_t)); if (!next) return NULL; new_lc = new_linecmp(line); if (new_lc == NULL) { free(next); next = NULL; break; } next->lc = new_lc; next->next = NULL; if (!head) head = next; else iter->next = next; iter = next; line = strsep(&data, "\n"); wordc += count_words(line); (*linec)++; } return head; }
int combinator_ctx_init (hashcat_ctx_t *hashcat_ctx) { combinator_ctx_t *combinator_ctx = hashcat_ctx->combinator_ctx; user_options_extra_t *user_options_extra = hashcat_ctx->user_options_extra; user_options_t *user_options = hashcat_ctx->user_options; combinator_ctx->enabled = false; if (user_options->left == true) return 0; if (user_options->opencl_info == true) return 0; if (user_options->show == true) return 0; if (user_options->usage == true) return 0; if (user_options->version == true) return 0; if ((user_options->attack_mode != ATTACK_MODE_COMBI) && (user_options->attack_mode != ATTACK_MODE_HYBRID1) && (user_options->attack_mode != ATTACK_MODE_HYBRID2)) return 0; combinator_ctx->enabled = true; combinator_ctx->scratch_buf = (char *) hcmalloc (HCBUFSIZ_LARGE); if (user_options->attack_mode == ATTACK_MODE_STRAIGHT) { // nothing to do } else if (user_options->attack_mode == ATTACK_MODE_COMBI) { // display char *dictfile1 = user_options_extra->hc_workv[0]; char *dictfile2 = user_options_extra->hc_workv[1]; // find the bigger dictionary and use as base FILE *fp1 = NULL; FILE *fp2 = NULL; hc_stat_t tmp_stat; if ((fp1 = fopen (dictfile1, "rb")) == NULL) { event_log_error (hashcat_ctx, "%s: %m", dictfile1); return -1; } if (hc_stat (dictfile1, &tmp_stat) == -1) { event_log_error (hashcat_ctx, "%s: %m", dictfile1); fclose (fp1); return -1; } if (S_ISDIR (tmp_stat.st_mode)) { event_log_error (hashcat_ctx, "%s must be a regular file", dictfile1); fclose (fp1); return -1; } if ((fp2 = fopen (dictfile2, "rb")) == NULL) { event_log_error (hashcat_ctx, "%s: %m", dictfile2); fclose (fp1); return -1; } if (hc_stat (dictfile2, &tmp_stat) == -1) { event_log_error (hashcat_ctx, "%s: %m", dictfile2); fclose (fp1); fclose (fp2); return -1; } if (S_ISDIR (tmp_stat.st_mode)) { event_log_error (hashcat_ctx, "%s must be a regular file", dictfile2); fclose (fp1); fclose (fp2); return -1; } combinator_ctx->combs_cnt = 1; const u64 words1_cnt = count_words (hashcat_ctx, fp1, dictfile1); if (words1_cnt == 0) { event_log_error (hashcat_ctx, "%s: empty file", dictfile1); fclose (fp1); fclose (fp2); return -1; } combinator_ctx->combs_cnt = 1; const u64 words2_cnt = count_words (hashcat_ctx, fp2, dictfile2); if (words2_cnt == 0) { event_log_error (hashcat_ctx, "%s: empty file", dictfile2); fclose (fp1); fclose (fp2); return -1; } fclose (fp1); fclose (fp2); combinator_ctx->dict1 = dictfile1; combinator_ctx->dict2 = dictfile2; if (words1_cnt >= words2_cnt) { combinator_ctx->combs_mode = COMBINATOR_MODE_BASE_LEFT; combinator_ctx->combs_cnt = words2_cnt; } else { combinator_ctx->combs_mode = COMBINATOR_MODE_BASE_RIGHT; combinator_ctx->combs_cnt = words1_cnt; // we also have to switch wordlist related rules! char *tmpc = user_options->rule_buf_l; user_options->rule_buf_l = user_options->rule_buf_r; user_options->rule_buf_r = tmpc; u32 tmpi = user_options_extra->rule_len_l; user_options_extra->rule_len_l = user_options_extra->rule_len_r; user_options_extra->rule_len_r = tmpi; } } else if (user_options->attack_mode == ATTACK_MODE_BF) { // nothing to do } else if (user_options->attack_mode == ATTACK_MODE_HYBRID1) { combinator_ctx->combs_mode = COMBINATOR_MODE_BASE_LEFT; } else if (user_options->attack_mode == ATTACK_MODE_HYBRID2) { combinator_ctx->combs_mode = COMBINATOR_MODE_BASE_RIGHT; } return 0; }
int main(int argc, char **argv) { if (argc != 2) { ft_putchar('\n'); return (0); } int words; int i; int x; int h; int trie; words = count_words(argv[1]); t_elem *tab = malloc(sizeof(t_elem) * words); i = 0; x = -1; h = 0; while (argv[1][i]) { while (argv[1][i] && (argv[1][i] == ' ' || argv[1][i] == '\t')) i++; if (argv[1][i]) { x++; h = i; } while (argv[1][h] && argv[1][h] != ' ' && argv[1][h] != '\t') h++; tab[x].str =ft_strsub(argv[1], i, h - i); tab[x].strlow = str_tolower(ft_strsub(argv[1], i, h - i)); tab[x].pos = x; tab[x].len = ft_strlen(tab[x].str); i = h; } x = 0; trie = 0; t_elem temp; while (!trie) { x = 0; trie = 1; while (x < words - 1) { if (tab[x].len > tab[x + 1].len) { temp = tab[x]; tab[x] = tab[x + 1]; tab[x + 1] = temp; trie = 0; } x++; } } x = 0; trie = 0; while (!trie) { x = 0; trie = 1; while (x < words -1) { if (tab[x].len == tab[x + 1].len && ft_strcmp(tab[x].strlow, tab[x + 1].strlow) > 0) { temp = tab[x]; tab[x] = tab[x + 1]; tab[x + 1] = temp; trie = 0; } x++; } } trie = 0; while(!trie) { x = 0; trie = 1; while (x < words - 1) { if (tab[x].len == tab[x + 1].len && ft_strcmp(tab[x].strlow, tab[x + 1].strlow) == 0) { if (tab[x].pos > tab[x + 1].pos) { temp = tab[x]; tab[x] = tab[x + 1]; tab[x + 1] = temp; trie = 0; ft_putchar('c'); } } x++; } } Dprinttab(tab, words); return (0); }
void extract_quotes (char *filename) { FILE *fp, *fp_out; char *ptr; char buf[QUOTE_BUF_SZ], *buf_ptr, *buf_end; char quote[QUOTE_BUF_SZ], *quote_ptr; int quote_begin, quote_end, quote_index, quote_word_count; int i, buf_index, buf_len; if (!filename) return; if (strstr (filename, ".eq")) { printf ("extract_quotes: Already extracted %s\n", filename); return; } printf ("extract_quotes: Extracting quotes from %s\n", filename); fp = fopen (filename, "r"); if (!fp) { perror ("fopen"); return; } ptr = str_unite ("%s.eq", filename); fp_out = fopen (ptr, "w"); if (!fp_out) { perror ("fopen(2) "); goto cleanup; } quote_begin = quote_end = quote_index = 0; while (1) { begin: if (fgets (buf, sizeof (buf) - 1, fp) == NULL) break; buf_ptr = buf; // buf_end = buf + sizeof (buf)-1; buf_len = strlen (buf_ptr); buf_end = buf_ptr + buf_len; if (buf_ptr > buf_end) { continue; } while (*buf_ptr && buf_ptr < buf_end) { if (quote_begin == 1) { // LEFTOFF if (*buf_ptr == '\n') { quote[quote_index] = ' '; quote_index++; buf_ptr++; break; } if (*buf_ptr != '\n') { quote[quote_index] = *buf_ptr; quote_index++; } /* if(*buf_ptr==' ' && *(buf_ptr+1)==' ') { quote[quote_index] = ' '; buf_ptr++; while(*buf_ptr!=' ') buf_ptr++; } */ if (*buf_ptr == '\"' && !isalnum (*(buf_ptr + 1))) { quote_ptr = clean_quote (quote); quote_word_count = count_words (quote); if (quote_word_count) fprintf (fp_out, "%i %s %s\n", quote_word_count, filename, quote_ptr); //clean_ _memset (quote, 0, sizeof (quote)); quote_begin = 0; quote_index = 0; buf_ptr++; continue; } if (quote_index > sizeof (quote)) { _memset (quote, 0, sizeof (quote)); quote_begin = 0; buf_ptr++; continue; } } else if (quote_begin == 0) { if (*buf_ptr == '\"' && !isalnum (*(buf_ptr - 1))) { puts ("FOUND QUOTE"); quote_begin = 1; _memset (quote, 0, sizeof (quote)); quote_index = 0; quote[quote_index] = *buf_ptr; quote_index++; } } buf_ptr++; } /* if (quote_begin == 1 && quote_end == 0) { while (*buf_ptr) { printf ("%c\n", *buf_ptr); if (*buf_ptr == '\n') { buf_ptr++; continue; } quote[quote_index] = *buf_ptr; quote_index++; if (quote_index > sizeof (quote)) { quote_end = 1; break; } if (*buf_ptr == '\"') { quote_end = 1; fprintf (fp_out, "%s\n", quote); break; } } buf_ptr++; } if (quote_begin == 0) { while (*buf_ptr) { if (*buf_ptr == '\"') { quote_begin = 1; _memset (quote, 0, sizeof (quote)); buf_ptr++; break; } buf_ptr++; } } if (quote_begin) goto loop; */ } cleanup: if (fp) fclose (fp); if (fp_out) fclose (fp_out); if (ptr) free (ptr); return; }
int main(int argc, char **argv){ printf("starting\n"); int num_maps = atoi(argv[1]); int BUFSZ = 250; int buffer[BUFSZ]; int child[num_maps]; int pid = 1; int cid = 0; int ptc[num_maps][2]; int ctp[num_maps][2]; int total = 0; printf("opening file\n"); //open the file FILE *fp; fp = fopen(argv[3], "r"); //find number of words each child needs to read int num_words = count_words(fp); int to_read = num_words/num_maps; printf("opening pipes\n"); //open pipes int i; for(i=0; i<num_maps; i++){ pipe(ptc[i]); pipe(ctp[i]); } printf("forking children\n"); //fork all new children for(i=0; i<num_maps; i++){ if(pid != 0){ pid = fork(); child[i] = pid; cid = i; } } //parent process if(pid>0){ printf("parent process\n"); for(int i=0;i<num_maps;i++){ //close unnecessary pipes write(ptc[i][0], buffer, sizeof(buffer)); close(ctp[i][1]); //wait for teach child to finish before reading int returnstatus; waitpid(child[i], &returnstatus, 0); //get data from child then close the child read(ctp[i][0], &buffer[i], sizeof(int)); close(ctp[i][0]); } //add up the total for(int i=0; i<num_maps; i++){ total += buffer[i]; } //print the total printf("total: %d\n", total); } //child process if(pid == 0){ printf("child process\n"); //close unnecessary pipes close(ptc[cid][1]); close(ctp[cid][0]); //find section to read int start = cid * to_read; int end = (cid+1) * to_read; //correct for integer division if (end >= (num_words-to_read)){ end = num_words; } //calc frequency fp = fopen(argv[3], "r"); int frequency = read_words(fp, start, end, argv[2]); //print out findings printf("child %d started at %d and ended at %d\n", cid, start, end); printf("child %d found: %d\n", cid, frequency); //send the frequency to parent then close pipe write(ctp[cid][1], &frequency, sizeof(frequency)); close(ctp[cid][1]); } }
int main(void) { // Create Floppy element floppy newfloppy; floppy *myfloppy = &newfloppy; unsigned short mounted = 0; while(1) { char current_directory[150] = "/"; // Create a buffer and take input from the command line char *buff = NULL; buff = takeinfo("prompt: "); // Create a String array and parse the buff into it char **buff_parsed; buff_parsed = string_array(buff); int n_words = count_words(buff); printf("n_words %i\n", n_words);//debug // If an empty command line is entered if(n_words < 1) { continue; } //----------------------------------------------------------------------------------------- // Run the comands. // Check to see if the comand is quit if(!strcmp(buff_parsed[0], "quit")) { exit(1); } else if(!strcmp(buff_parsed[0], "help")) { help(); } // Execute comands else if(!strcmp(buff_parsed[0], "clear")) { if(fork() == 0) { char *args[2] = { "clear", NULL}; execv("/usr/bin/clear", args); exit(1); } else { wait(NULL); } } else if(!strcmp(buff_parsed[0], "fmount")) { char temp[strlen(buff_parsed[1]) + 2]; memcpy(temp,"./", 2); memcpy(temp, buff_parsed[1], strlen(buff_parsed[1])); if( access( buff_parsed[1], F_OK ) != -1 ) { fprintf(stderr,"Floppy succesfully mounted\n"); mount(myfloppy, buff_parsed[1]); mounted = 1; } else { //file doesn't exist fprintf(stderr,"File/Floppy can't be found\n"); } } else if(!strcmp(buff_parsed[0], "fumount")) { if(mounted == 0) printf("There's nothing to unmount\n"); else { mounted = 0; myfloppy = NULL; fprintf(stderr,"Unmount succesfull\n"); } } else if(!strcmp(buff_parsed[0], "traverse")) { if(n_words > 1) { // if the -l flag is used print with additional info if(!strcmp(buff_parsed[1], "-l")) { traverse(myfloppy, 19, 1, current_directory); } else fprintf(stderr, "flag doesn't exist"); }else { traverse(myfloppy, 19, 0, current_directory); } } else if(!strcmp(buff_parsed[0], "structure")) { structure(myfloppy); } else if(!strcmp(buff_parsed[0], "showfat")) { showfat(myfloppy); } else { fprintf(stderr,"This command doesn't exist\n"); } } }
int main() { unsigned int key_space = 1024; HashMap * hm = create_hashmap(key_space); char * string_1 = "TI2725-C"; char * string_2 = "Embedded"; char * string_3 = "Software"; const char * key_1 = "ab"; const char * key_2 = "cd"; const char * key_3 = "ad"; const char * key_4 = "xy"; // Insert ("ab" -> "TI2725-C"). insert_data(hm, key_1, string_1, resolve_collision); assert(memcmp(get_data(hm, key_1), string_1, mystrlen(string_1)) == 0); // Insert ("cd" -> "Embedded"). insert_data(hm, key_2, string_2, resolve_collision); assert(memcmp(get_data(hm, key_2), string_2, mystrlen(string_2)) == 0); // Insert ("ad" -> "Software"). insert_data(hm, key_3, string_3, resolve_collision); assert(memcmp(get_data(hm, key_3), string_3, mystrlen(string_3)) == 0); // Insert ("ab" -> "Embedded"). insert_data(hm, key_1, string_2, resolve_collision); assert(memcmp(get_data(hm, key_1), string_2, mystrlen(string_2)) == 0); // Get data for a not inserted key. assert(get_data(hm, key_4) == NULL); // Iterate the hash map iterate(hm, print_element); #ifdef NEW_HASH set_hash_function(hm, your_own_hash); printf("\nHERE WE GO AGAIN!\n\n"); // Iterate the hash map iterate(hm, print_element); #endif // Delete key "cd". remove_data(hm, key_2, NULL); assert(get_data(hm, key_2) == NULL); // Delete key "ab". remove_data(hm, key_1, NULL); assert(get_data(hm, key_1) == NULL); // Delete key "ad". remove_data(hm, key_3, NULL); assert(get_data(hm, key_3) == NULL); // Delete the hash map. delete_hashmap(hm, NULL); #ifdef COUNTING_WORDS // Create a temporary file FILE *stream = tmpfile(); // Write to the stream fprintf(stream, "foo bar_, baz!\n"); fprintf(stream, "foo\t\"bar\".\n"); fprintf(stream, "foo?\n"); // Set the position to the start of the stream fseek(stream, 0, SEEK_SET); // Count the words count_words(stream); // Close the file fclose(stream); #endif return 0; }
int straight_ctx_update_loop (hashcat_ctx_t *hashcat_ctx) { combinator_ctx_t *combinator_ctx = hashcat_ctx->combinator_ctx; induct_ctx_t *induct_ctx = hashcat_ctx->induct_ctx; logfile_ctx_t *logfile_ctx = hashcat_ctx->logfile_ctx; mask_ctx_t *mask_ctx = hashcat_ctx->mask_ctx; status_ctx_t *status_ctx = hashcat_ctx->status_ctx; straight_ctx_t *straight_ctx = hashcat_ctx->straight_ctx; user_options_extra_t *user_options_extra = hashcat_ctx->user_options_extra; user_options_t *user_options = hashcat_ctx->user_options; if (user_options->attack_mode == ATTACK_MODE_STRAIGHT) { if (user_options_extra->wordlist_mode == WL_MODE_FILE) { if (induct_ctx->induction_dictionaries_cnt) { straight_ctx->dict = induct_ctx->induction_dictionaries[induct_ctx->induction_dictionaries_pos]; } else { straight_ctx->dict = straight_ctx->dicts[straight_ctx->dicts_pos]; } logfile_sub_string (straight_ctx->dict); for (u32 i = 0; i < user_options->rp_files_cnt; i++) { logfile_sub_var_string ("rulefile", user_options->rp_files[i]); } FILE *fd = fopen (straight_ctx->dict, "rb"); if (fd == NULL) { event_log_error (hashcat_ctx, "%s: %s", straight_ctx->dict, strerror (errno)); return -1; } const int rc = count_words (hashcat_ctx, fd, straight_ctx->dict, &status_ctx->words_cnt); fclose (fd); if (rc == -1) { event_log_error (hashcat_ctx, "Integer overflow detected in keyspace of wordlist: %s", straight_ctx->dict); return -1; } if (status_ctx->words_cnt == 0) { logfile_sub_msg ("STOP"); return 0; } } } else if (user_options->attack_mode == ATTACK_MODE_COMBI) { logfile_sub_string (combinator_ctx->dict1); logfile_sub_string (combinator_ctx->dict2); if (combinator_ctx->combs_mode == COMBINATOR_MODE_BASE_LEFT) { FILE *fd = fopen (combinator_ctx->dict1, "rb"); if (fd == NULL) { event_log_error (hashcat_ctx, "%s: %s", combinator_ctx->dict1, strerror (errno)); return -1; } const int rc = count_words (hashcat_ctx, fd, combinator_ctx->dict1, &status_ctx->words_cnt); fclose (fd); if (rc == -1) { event_log_error (hashcat_ctx, "Integer overflow detected in keyspace of wordlist: %s", combinator_ctx->dict1); return -1; } } else if (combinator_ctx->combs_mode == COMBINATOR_MODE_BASE_RIGHT) { FILE *fd = fopen (combinator_ctx->dict2, "rb"); if (fd == NULL) { event_log_error (hashcat_ctx, "%s: %s", combinator_ctx->dict2, strerror (errno)); return -1; } const int rc = count_words (hashcat_ctx, fd, combinator_ctx->dict2, &status_ctx->words_cnt); fclose (fd); if (rc == -1) { event_log_error (hashcat_ctx, "Integer overflow detected in keyspace of wordlist: %s", combinator_ctx->dict2); return -1; } } if (status_ctx->words_cnt == 0) { logfile_sub_msg ("STOP"); return 0; } } else if (user_options->attack_mode == ATTACK_MODE_BF) { logfile_sub_string (mask_ctx->mask); } else if ((user_options->attack_mode == ATTACK_MODE_HYBRID1) || (user_options->attack_mode == ATTACK_MODE_HYBRID2)) { if (induct_ctx->induction_dictionaries_cnt) { straight_ctx->dict = induct_ctx->induction_dictionaries[induct_ctx->induction_dictionaries_pos]; } else { straight_ctx->dict = straight_ctx->dicts[straight_ctx->dicts_pos]; } logfile_sub_string (straight_ctx->dict); logfile_sub_string (mask_ctx->mask); FILE *fd = fopen (straight_ctx->dict, "rb"); if (fd == NULL) { event_log_error (hashcat_ctx, "%s: %s", straight_ctx->dict, strerror (errno)); return -1; } const int rc = count_words (hashcat_ctx, fd, straight_ctx->dict, &status_ctx->words_cnt); fclose (fd); if (rc == -1) { event_log_error (hashcat_ctx, "Integer overflow detected in keyspace of wordlist: %s", straight_ctx->dict); return -1; } if (status_ctx->words_cnt == 0) { logfile_sub_msg ("STOP"); return 0; } } return 0; }
IT( "should read the mnemonic from eeprom if there is one" ); char * mnemonic; static char test_mnemonic[APP_PREFIXED_MNEMONIC_MAX_SIZE] = APP_MNEMONIC_PREFIX "This is a test"; eeprom_busy_wait(); eeprom_write_block( (void *) test_mnemonic, 0x0, APP_PREFIXED_MNEMONIC_MAX_SIZE ); mnemonic = app_init(); SHOULD_MATCH(mnemonic, "This is a test"); END_IT; IT( "should return a new mnemonic with strength 256" ); int word_count = 0; char * mnemonic = app_init(); word_count = count_words(mnemonic); SHOULD_EQUAL(word_count, ((256 / 8) * 3) / 4); END_IT; END_DESCRIBE; int app_test_run(void) { return CSpec_Run( DESCRIPTION( app_init ), CSpec_NewOutputVerbose() ); }