void ft_calc_hexa(const char *format, t_env1 *env1, t_env2 *env2) { env2->test = 1; env2->str = ft_memalloc(2); while (env2->argument1 > 0 || env2->test) { env2->test = 0; env2->result = 0; env2->reste = env2->argument1 % 16; env2->argument1 /= 16; if (env2->reste >= 10) { while (env2->reste != 10 + env2->result) env2->result++; if (format[env1->taille_f] == 'x') env2->c = 'a' + env2->result; else env2->c = 'A' + env2->result; env2->str[0] = env2->c; ft_list_push_front(&env2->tmp1, env2->str); } else ft_calc_hexa_else(env2); } ft_memdel((void **)&env2->str); }
static int elf_sections_to_list(const char* mmptr, size_t mmlen, ft_list_t** sections) { Elf32_Ehdr* ehdr; Elf32_Shdr* shdr; elf_section_t* section; unsigned int nsection; unsigned int n; ehdr = (Elf32_Ehdr*)mmptr; CHECK_MMAP(mmptr, mmlen, ehdr); nsection = ehdr->e_shnum; if (nsection == 0 || nsection > NR_MAX_SHNUM) return -1; shdr = (Elf32_Shdr*)(mmptr + ehdr->e_shoff); for (n = 0; n < nsection; ++n) { CHECK_MMAP(mmptr, mmlen, &shdr[n]); CHECK_MMAP(mmptr, mmlen, &shdr[n].sh_offset); section = elf_section_new((const char*)shdr[n].sh_name, (const char*)(mmptr + shdr[n].sh_offset), shdr[n].sh_size, n, shdr[n].sh_type, shdr[n].sh_link, shdr[n].sh_info, shdr[n].sh_flags); ft_list_push_front(sections, (void*)section); } return 0; }
t_list *ft_list_push_params(int ac, char **av) { int i; t_list *list; i = -1; list = NULL; while (++i < ac) ft_list_push_front(&list, av[i]); return (list); }
t_list *rush03(int x, int y) { int length; t_list *list; list = 0; if (x <= 0 || y <= 0) return (0); length = y; while (length >= 1) { if (length == y) ft_list_push_front(&list, ft_putline('A', 'C', 'B', x)); else if (length == 1) ft_list_push_front(&list, ft_putline('A', 'C', 'B', x)); else ft_list_push_front(&list, ft_putline('B', 'B', ' ', x)); length -= 1; } return (list); }
static t_list *read_directory(DIR *opened) { t_list *names; struct dirent *just_read; if (!opened) return (NULL); names = NULL; while ((just_read = readdir(opened))) ft_list_push_front(&names, ft_strdup(just_read->d_name)); return (names); }
void ft_rotate_rrb(t_list **l_b) { int temp; t_list *tmp; tmp = (*l_b); while (tmp->next->next) tmp = tmp->next; temp = tmp->next->content; tmp->next = NULL; (*l_b) = ft_list_push_front(l_b, temp); }
t_list *ft_putline(char start, char end, char fill, int length) { t_list *list; char *a; char *b; char *c; a = (char*)malloc(sizeof(char)); b = (char*)malloc(sizeof(char)); c = (char*)malloc(sizeof(char)); *a = start; *b = end; *c = fill; list = 0; ft_list_push_front(&list, a); while (length > 2) { ft_list_push_front(&list, c); length -= 1; } if (length == 2) ft_list_push_front(&list, b); return (list); }
t_list *ft_list_push_params(int ac, char **av) { int i; t_list *first_elem; if (ac == 0) return (NULL); i = 0; while (i < ac) { ft_list_push_front(&first_elem, av[i]); i++; } return (first_elem); }
static int elf_get_dependencies(const char* mmptr, size_t mmlen, ft_list_t* sections, ft_list_t** symbols, ft_list_t** symfiles) { /* Dependencies can be found in the DT_NEEDED entries of the dynamic section. */ Elf32_Ehdr* ehdr; unsigned int ndyn; unsigned int maxdyn; Elf32_Dyn* pdyn; ft_list_t* node; ft_list_t* libpaths; ft_list_t* nodepath; elf_section_t* dynamic; elf_section_t* strtab; char sopath[256]; /* Get the string section, this is subject to change */ ehdr = (Elf32_Ehdr*)mmptr; CHECK_MMAP(mmptr, mmlen, ehdr); /* node = ft_list_lkp(sections, match_section_bytype_fn, (void*)§ype); */ node = ft_list_lkp(sections, match_section_byname_fn, (void*)".dynstr"); if (node == 0) return -1; strtab = (elf_section_t*)node->data; /* Bootstrap the path list */ libpaths = ft_list_init(); ft_list_push_front(&libpaths, (void*)"/usr/lib/"); ft_list_push_front(&libpaths, (void*)"/lib/"); ft_list_push_front(&libpaths, (void*)"/usr/local/lib/"); /* Walk and find dynamic */ for (node = sections; node; node = node->next) { dynamic = (elf_section_t*)node->data; if (dynamic->type == SHT_DYNAMIC) { maxdyn = dynamic->size / sizeof(Elf32_Dyn); pdyn = (Elf32_Dyn*)dynamic->data; /* First pass to get the -rpath related data */ for (ndyn = 0; ndyn < maxdyn; ++ndyn) { if (pdyn[ndyn].d_tag == DT_RPATH) ft_list_push_front(&libpaths, (void*)(strtab->data + pdyn[ndyn].d_un.d_val)); } /* Second pass to get library names */ for (ndyn = 0; ndyn < maxdyn; ++ndyn) { if (pdyn[ndyn].d_tag == DT_NEEDED) { nodepath = libpaths; while (nodepath) { snprintf(sopath, sizeof(sopath), "%s/%s", (char*)nodepath->data, strtab->data + pdyn[ndyn].d_un.d_val); if (ft_symbol_list_from_file(symfiles, sopath) == FT_ERR_SUCCESS) nodepath = 0; else nodepath = nodepath->next; } } } } } /* Release the path list */ ft_list_release(&libpaths, 0); return 0; }
static int elf_get_exported_symbols(ft_symfile_t* symfile, ft_list_t* sections, ft_list_t** symbols) { ft_bool_t sharedobject; Elf32_Sym* sym; elf_section_t* strsection; elf_section_t* symsection; elf_section_t* s; ft_list_t* node; ft_list_t* lnk; ft_symbol_t* cursym; unsigned int nsyms; unsigned int n; unsigned int symtype; char* mmptr; size_t mmlen; mmptr = symfile->mmptr; mmlen = symfile->mmlen; sharedobject = false; /* Is the file a shared library */ { Elf32_Ehdr* ehdr; ehdr = (Elf32_Ehdr*)mmptr; if (ehdr->e_type == ET_DYN) sharedobject = true; } /* Get symbol table sections */ for (node = sections; node; node = node->next) { s = (elf_section_t*)node->data; if (s->type == SHT_SYMTAB) { lnk = ft_list_lkp(sections, match_byshndx_fn, (void*)&s->link); strsection = (elf_section_t*)lnk->data; /* find the section */ nsyms = s->size / sizeof(Elf32_Sym); sym = (Elf32_Sym*)s->data; CHECK_MMAP(mmptr, mmlen, sym); for (n = 0; n < nsyms; ++n) { /* Get symbol type */ symtype = ELF32_ST_TYPE(sym[n].st_info); switch (symtype) { case STT_FUNC: case STT_OBJECT: lnk = ft_list_lkp(sections, match_byshndx_fn, (void*)&(sym[n].st_shndx)); if (lnk) { symsection = (elf_section_t*)lnk->data; if (sym[n].st_name && (symsection->flags & SHF_EXECINSTR) && (ELF32_ST_BIND(sym[n].st_info) == STB_GLOBAL)) { ft_symbol_create(&cursym, symfile, (char*)(strsection->data + sym[n].st_name), (ft_addr_t)sym[n].st_value, (ft_size_t)sym[n].st_size); if (ft_list_lkp(*symbols, (int (*)(void*, void*))ft_symbol_match, (void*)cursym) == 0) { ft_list_push_front(symbols, (void*)cursym); if (sharedobject) { /* cursym->resolv_fn = resolve_from_so_offset; */ cursym->resolv_fn = dont_resolve; cursym->resolved = true; } else { cursym->resolv_fn = dont_resolve; cursym->resolved = true; } } else { ft_symbol_release(cursym); } } break; default: break; } } } } } return 0; }
int main(void) { char *exname; int exnb = 0; t_list *link; t_list *base; (void)link; base = ft_create_elem("ABC"); base->next = ft_create_elem("012"); base->next->next = ft_create_elem("xyz"); start_day(11); #ifdef ex00 exname = "ft_create_elem"; start_exo(exnb); link = ft_create_elem("A"); link->next = ft_create_elem("B"); ft_print_list(link); printf("expected:\n"); printf("[ A ] -> [ B ] -> NULL\n"); #endif exnb++; #ifdef ex01 exname = "ft_list_push_back"; start_exo(exnb); ft_warning1(); link = NULL; ft_list_push_back(&link, "A"); ft_list_push_back(&link, "B"); ft_list_push_back(&link, "C"); ft_print_list(link); printf("expected:\n"); printf("[ A ] -> [ B ] -> [ C ] -> NULL\n"); #endif exnb++; #ifdef ex02 exname = "ft_list_push_front"; start_exo(exnb); ft_warning1(); link = NULL; ft_list_push_front(&link, "C"); ft_list_push_front(&link, "B"); ft_list_push_front(&link, "A"); ft_print_list(link); printf("expected:\n"); printf("[ A ] -> [ B ] -> [ C ] -> NULL\n"); #endif exnb++; #ifdef ex03 start_exo(exnb); exname = "ft_list_size"; ft_warning1(); printf("list : "); ft_print_list(base); printf("%s = %i\n", exname, ft_list_size(base)); printf("\nlist : "); ft_print_list(NULL); printf("%s = %i\n", exname, ft_list_size(NULL)); #endif exnb++; #ifdef ex04 start_exo(exnb); exname = "ft_list_last"; ft_warning1(); printf("list : "); ft_print_list(base); printf("%s = ", exname); ft_print_list(ft_list_last(base)); printf("\nlist : "); ft_print_list(NULL); printf("%s = ", exname); ft_print_list(ft_list_last(NULL)); #endif exnb++; #ifdef ex05 start_exo(exnb); exname = "ft_list_push_params"; printf("pas encore de suite de tests pour cet exercice :(\n"); #endif exnb++; #ifdef ex06 start_exo(exnb); exname = "ft_list_clear"; printf("%s\n", exname); printf("avant : "); ft_print_list(base); ft_list_clear(&base); printf("apres : "); ft_print_list(base); base = ft_create_elem("ABC"); base->next = ft_create_elem("012"); base->next->next = ft_create_elem("xyz"); #endif exnb++; #ifdef ex07 start_exo(exnb); exname = "ft_list_at"; ft_warning1(); printf("list : "); ft_print_list(base); printf("%s(list, 0) = ", exname); ft_print_list(ft_list_at(base, 0)); printf("\nlist : "); ft_print_list(base); printf("%s(list, 1) = ", exname); ft_print_list(ft_list_at(base, 1)); printf("\n%s(NULL, 0) = ", exname); ft_print_list(ft_list_at(NULL, 0)); printf("\n%s(NULL, 1) = ", exname); ft_print_list(ft_list_at(NULL, 1)); #endif exnb++; #ifdef ex08 start_exo(exnb); exname = "ft_list_reverse"; printf("%s\n", exname); printf("avant : "); ft_print_list(base); ft_list_reverse(&base); printf("apres : "); ft_print_list(base); #endif exnb++; #ifdef ex09 start_exo(exnb); exname = "ft_list_foreach"; base = ft_create_elem("012"); base->next = ft_create_elem("ABC"); base->next->next = ft_create_elem("xyz"); printf("%s, avec la fonction ft_putstr()\n", exname); printf("list : "); ft_print_list(base); printf("%s : ", exname); fflush(stdout); ft_list_foreach(base, &corr_ft_putstr); #endif exnb++; #ifdef ex10 start_exo(exnb); exname = "ft_list_foreach_if"; printf("list : "); ft_print_list(base); printf("%s(list, &ft_putstr, '012', &strcmp) = ", exname); fflush(stdout); ft_list_foreach_if(base, &corr_ft_putstr, "012", &strcmp); printf(" (012 attendu)"); #endif exnb++; #ifdef ex11 start_exo(exnb); exname = "ft_list_find"; base = ft_create_elem("012"); base->next = ft_create_elem("ABC"); base->next->next = ft_create_elem("xyz"); printf("list : "); ft_print_list(base); printf("%s(list, 'ABC', &strcmp) = ", exname); fflush(stdout); ft_print_list(ft_list_find(base, "ABC", &strcmp)); #endif exnb++; #ifdef ex12 start_exo(exnb); exname = "ft_list_remove_if"; base = ft_create_elem("012"); base->next = ft_create_elem("ABC"); base->next->next = ft_create_elem("xyz"); printf("list : "); ft_print_list(base); printf("%s(list, 'ABC', &strcmp) = ", exname); fflush(stdout); ft_list_remove_if(&base, "ABC", &strcmp); ft_print_list(base); #endif t_list *list1; t_list *list2; (void)list1; (void)list2; exnb++; #ifdef ex13 start_exo(exnb); exname = "ft_list_merge"; ft_warning1(); list1 = NULL; list2 = NULL; printf("list1 : "); fflush(stdout); ft_print_list(list1); printf("list2 : "); fflush(stdout); ft_print_list(list2); ft_list_merge(&list1, list2); printf("merge : "); fflush(stdout); ft_print_list(list1); printf("\n"); list1 = NULL; list2 = ft_create_elem("012"); printf("list1 : "); fflush(stdout); ft_print_list(list1); printf("list2 : "); fflush(stdout); ft_print_list(list2); ft_list_merge(&list1, list2); printf("merge : "); fflush(stdout); ft_print_list(list1); printf("\n"); list1 = ft_create_elem("012"); list2 = NULL; printf("list1 : "); fflush(stdout); ft_print_list(list1); printf("list2 : "); fflush(stdout); ft_print_list(list2); ft_list_merge(&list1, list2); printf("merge : "); fflush(stdout); ft_print_list(list1); printf("\n"); list1 = ft_create_elem("012"); list1->next = ft_create_elem("345"); list2 = ft_create_elem("abc"); list2->next = ft_create_elem("def"); printf("list1 : "); fflush(stdout); ft_print_list(list1); printf("list2 : "); fflush(stdout); ft_print_list(list2); ft_list_merge(&list1, list2); printf("merge : "); fflush(stdout); ft_print_list(list1); #endif exnb++; #ifdef ex14 start_exo(exnb); exname = "ft_list_sort"; printf("%s\n", exname); ft_warning1(); base = NULL; printf("avant : "); fflush(stdout); ft_print_list(base); ft_list_sort(&base, &strcmp); printf("apres : "); fflush(stdout); ft_print_list(base); printf("\n"); base = ft_create_elem("3"); base->next = ft_create_elem("1"); base->next->next = ft_create_elem("2"); printf("avant : "); fflush(stdout); ft_print_list(base); ft_list_sort(&base, &strcmp); printf("apres : "); fflush(stdout); ft_print_list(base); #endif exnb++; #ifdef ex15 start_exo(exnb); exname = "ft_list_reverse_fun"; base = NULL; printf("%s\n", exname); printf("avant : "); ft_print_list(base); ft_list_reverse_fun(base); printf("apres : "); ft_print_list(base); printf("\n"); base = ft_create_elem("1"); base->next = ft_create_elem("2"); base->next->next = ft_create_elem("3"); printf("avant : "); ft_print_list(base); ft_list_reverse_fun(base); printf("apres : "); ft_print_list(base); printf("\n"); base = ft_create_elem("1"); base->next = ft_create_elem("2"); base->next->next = ft_create_elem("3"); base->next->next->next = ft_create_elem("4"); printf("avant : "); ft_print_list(base); ft_list_reverse_fun(base); printf("apres : "); ft_print_list(base); #endif exnb++; #ifdef ex16 start_exo(exnb); exname = "ft_sorted_list_insert"; printf("%s\n", exname); ft_warning1(); base = ft_create_elem("2"); base->next = ft_create_elem("3"); base->next->next = ft_create_elem("5"); printf("avant : "); fflush(stdout); ft_print_list(base); ft_sorted_list_insert(&base, "7", &strcmp); printf("apres : "); fflush(stdout); ft_print_list(base); base = NULL; printf("\navant : "); fflush(stdout); ft_print_list(base); ft_sorted_list_insert(&base, "0", &strcmp); printf("apres : "); fflush(stdout); ft_print_list(base); #endif exnb++; #ifdef ex17 start_exo(exnb); exname = "ft_sorted_list_merge"; printf("%s\n", exname); ft_warning1(); list1 = NULL; list2 = NULL; printf("list1 : "); fflush(stdout); ft_print_list(list1); printf("list2 : "); fflush(stdout); ft_print_list(list2); ft_sorted_list_merge(&list1, list2, &strcmp); printf("merge : "); fflush(stdout); ft_print_list(list1); printf("\n"); list1 = NULL; list2 = ft_create_elem("0"); printf("list1 : "); fflush(stdout); ft_print_list(list1); printf("list2 : "); fflush(stdout); ft_print_list(list2); ft_sorted_list_merge(&list1, list2, &strcmp); printf("merge : "); fflush(stdout); ft_print_list(list1); printf("\n"); list1 = ft_create_elem("0"); list2 = NULL; printf("list1 : "); fflush(stdout); ft_print_list(list1); printf("list2 : "); fflush(stdout); ft_print_list(list2); ft_sorted_list_merge(&list1, list2, &strcmp); printf("merge : "); fflush(stdout); ft_print_list(list1); printf("\n"); list1 = ft_create_elem("0"); list1->next = ft_create_elem("2"); list2 = ft_create_elem("1"); list2->next = ft_create_elem("3"); printf("list1 : "); fflush(stdout); ft_print_list(list1); printf("list2 : "); fflush(stdout); ft_print_list(list2); ft_sorted_list_merge(&list1, list2, &strcmp); printf("merge : "); fflush(stdout); ft_print_list(list1); #endif }
int main(int ac, char **av) { t_str str1, str2; int k, i, j; t_list *tmp; k = 0; while (k < (ac == 1 ? K_MAX : ac - 1)) switch (ac == 1 ? k++ : atoi(av[++k])) { case 0 : if (k + 1 < ac) str1 = av[++k]; else str1 = strdup("Foo Bar Baz"); printf("Ex 00 : expected [| %s |] :\t%s", str1, GREEN); ft_print_list_str(ft_create_elem(str1)); printf("%s\n", END); break ; case 1 : if (k + 1 < ac) str1 = av[++k]; else str1 = strdup("Hello"); if (k + 1 < ac) str2 = av[++k]; else str2 = strdup("World"); tmp = ft_create_elem(str1); printf("Ex 01 :\n\t- expected [| %s ; %s ; %s |] (normal push back)\n\t\t%s", str1, str2, str2, GREEN); ft_list_push_back(&tmp, str2); ft_list_push_back(&tmp, str2); ft_print_list_str(tmp); printf("%s\t- expected [| %s ; %s |] (push back on an empty list)\n\t\t%s", END, str1, str2, GREEN); tmp = NULL; ft_list_push_back(&tmp, str1); ft_list_push_back(&tmp, str2); ft_print_list_str(tmp); printf("%s\n", END); break ; case 2 : if (k + 1 < ac) str1 = av[++k]; else str1 = strdup("Caesar"); if (k + 1 < ac) str2 = av[++k]; else str2 = strdup("Ave"); tmp = ft_create_elem(str1); printf("Ex 02 :\n\t- expected [| %s ; %s ; %s |] (normal front back)\n\t\t%s", str1, str2, str2, GREEN); ft_list_push_front(&tmp, str2); ft_list_push_front(&tmp, str2); ft_print_list_str(tmp); printf("%s\t- expected [| %s ; %s |] (push front on an empty list)\n\t\t%s", END, str1, str2, GREEN); tmp = NULL; ft_list_push_front(&tmp, str1); ft_list_push_front(&tmp, str2); ft_print_list_str(tmp); printf("%s\n", END); break ; case 3 : if (k + 1 < ac) { i = atoi(av[++k]); i *= (i < 0 ? -1 : 1); } else i = 13; j = -1; tmp = NULL; str1 = strdup("Tralala"); while (++j < i) ft_list_push_front(&tmp, str1); j = ft_list_size(tmp); if (j == i) printf("%s[Ex03 OK]%s\t(tested on a list of %i elements)\n", GREEN, END, i); else printf("%s>>>[Ex03 FAIL]<<<%s\t(a list of %i elements is not %i element long)\n", RED, END, i, j); break ; case 4 : str1 = strdup("\e[1;31m>>>[Ex04 FAIL]<<<\e[0;0m"); str2 = strdup("\e[1;32m[Ex04 OK]\e[0;0m"); tmp = NULL; i = -1; ft_list_push_front(&tmp, str2); while (++i < 50) ft_list_push_front(&tmp, str1); printf("%s\t(tested on a list of 50 elements)\n", (char*)ft_list_last(tmp)->data); break ; case 5 : printf("Ex05 : You should the the arguments you passed ina reversed order :\n\t%s", GREEN); ft_print_list_str(ft_list_push_params(ac, av)); printf("%s\n", END); break ; } }
void ft_calc_hexa_else(t_env2 *env2) { env2->tmp = ft_unsigned_long_itoa(env2->reste); ft_list_push_front(&env2->tmp1, env2->tmp); ft_memdel((void **)&env2->tmp); }