static void test_ffs() { char str_buf[128] = {0}; uint8_t s = BIT(3) | BIT(6) | BIT(0) | BIT(7); int results[4] = {1, 4, 7, 8}; for (int i = 0; i < 4; i++) { int ret = ffs(s); test_equal(results[i], ret); printf("%02x(%s) -> %d\n", s, bin_str(&s, 1, str_buf, 128), ret); s &= ~BIT((ret - 1)); } }
int main(int argc, char **argv) { struct makefile *Makefile; FILE *rw_to = NULL; char *contents = NULL; char *conv_str = NULL; char byte[CHAR_BIT]; int error_code; int i, c, x; int run_flag = 0; int comp_flag = 0; // Attempt to find BFMakefile if ((rw_to = fopen("BFMakefile", "r")) == NULL) { // No BFMakefile found :( (void) fprintf(stderr, "[ERROR] BFMakefile was not found\nCreating...\n\n"); // Attempt creation of BFMakefle if ((rw_to = fopen("BFMakefile", "w")) == NULL) // Creation failed (void) fprintf(stderr, "[ERROR] Unable to create BFMakefile\nExiting\n"); // Creation successful, close the _file stream else { (void) fclose(rw_to); (void) fprintf(stderr, "[INFO] BFMakefile has been created\n"); } error_code = errno; return error_code; } // Get contents of makefile, NULL if fail if ((contents = read_file(rw_to)) == NULL) return MALLOC_ERR; (void) fclose(rw_to); // Check if number of bits is correct if ((i = strlen(contents) % 8)) { (void) fprintf(stderr, "[ERROR] Incorrect length\nExcess Length: %d", i); return NO_MAKE; } // Allocate 8 "bits" to a char, so divide by 8 +1 for NT if ((conv_str = (char *) malloc(sizeof(char) * (strlen(contents) / 8) + 1)) == NULL) { (void) fprintf(stderr, "[ERROR] Malloc of conv_str failed\n\n"); (void) fprintf(stderr, "File: %s\n", __FILE__); (void) fprintf(stderr, "Line: %d\n", __LINE__); return MALLOC_ERR; } for (c = 0, i = 0, x = 0; contents[c] != '\0'; ++c, ++x) { byte[x] = contents[c]; if (x + 1 == CHAR_BIT) { conv_str[i++] = bin_str(byte, NULL); x = 0; ++c; } } free(contents); (void) fprintf(rw_to, "\n\n\n"); (void) fclose(rw_to); // Terminate string conv_str[i] = '\0'; // Allocate space for Makefile, NULL if fail if ((Makefile = (struct makefile *) malloc(sizeof(struct makefile))) == NULL) { (void) fprintf(stderr, "[ERROR] Malloc of Makefile failed\n\n"); (void) fprintf(stderr, "File: %s\n", __FILE__); (void) fprintf(stderr, "Line: %d\n", __LINE__); return MALLOC_ERR; } else if (parse_make(conv_str, Makefile)) return BAD_MAKE; free(conv_str); if (src_hdr_write(Makefile->src_files, Makefile->src_count, SRC_MODE)) { (void) fprintf(stderr, "[ERROR] Failed to handle Makefile->src_files correctly\n"); return EXIT_FAILURE; } free(Makefile->src_files); (void) printf("\nMakefile->src_files successfully handled\n"); if (src_hdr_write(Makefile->hdr_files, Makefile->hdr_count, HDR_MODE)) { (void) fprintf(stderr, "[ERROR] Failed to handle Makefile->hdr_files correctly\n"); return EXIT_FAILURE; } free(Makefile->hdr_files); (void) printf("Makefile->hdr_files successfully handled\n"); if (obj_exc_write(Makefile->obj_files, Makefile->obj_count, OBJ_MODE)) { (void) fprintf(stderr, "[ERROR] Failed to handle Makefile->obj_files correctly\n"); return EXIT_FAILURE; } free(Makefile->obj_files); (void) printf("Makefile->obj_files successfully handled\n"); if (obj_exc_write(Makefile->exc_name, 1, EXC_MODE)) { (void) fprintf(stderr, "[ERROR] Failed to handle Makefile->exc_name correctly\n"); return EXIT_FAILURE; } (void) printf("Makefile->exc_name successfully handled\n\n"); for (i = 1; i < argc; ++i) { if (!strcmp(argv[i], "--make")) comp_flag = 1; if (!strcmp(argv[i], "--run")) run_flag = 1; } if (comp_flag) if (system("make")) return EXIT_FAILURE; (void) printf("\n\n"); if (run_flag) { if ((Makefile->exc_name = (char*) realloc(Makefile->exc_name, sizeof(char) * Makefile->exc_len + strlen(EXC_EXT) + 1)) == NULL) { (void) fprintf(stderr, "[ERROR] Realloc of Makefile->exc_name failed\n"); return EXIT_FAILURE; } (void) system(strcat(Makefile->exc_name, EXC_EXT)); } free(Makefile->exc_name); free(Makefile); return EXIT_SUCCESS; }