/* * myfgets(), as seen in lab4 * * Slightly modified version from the * encoder/decoder HW I did. * * Changes: * - ignores '\n', replaces with '\0' * returns * - takes in *fp to allow to read from file * - replaces '\t' with ' ' * */ char *myfgets(FILE *fp) { char *prgc, *prgcT; int cTemp; int iLength; prgc = NULL; /* Changed to fgetc(FILE *) to allow file reading */ for(iLength=0;;iLength++){ cTemp = fgetc(fp); if(cTemp==EOF && prgc == NULL){ return prgc; } else if(cTemp==EOF && prgc != NULL){ prgcT = my_realloc(prgc,(iLength+1)*sizeof(char)); if(prgcT == NULL){ free(prgc); return NULL; } prgc = prgcT; *(prgc+iLength) = '\0'; return prgc; } else if(cTemp == '\n'){ prgcT = my_realloc(prgc,(iLength+1)*sizeof(char)); if(prgcT == NULL){ free(prgc); return NULL; } prgc = prgcT; *(prgc+iLength) = '\0'; return prgc; } else if(cTemp == '\t'){ prgcT = my_realloc(prgc,(iLength+1)*sizeof(char)); if(prgcT == NULL){ free(prgc); return NULL; } prgc = prgcT; *(prgc+iLength) = ' '; } else{ prgcT = my_realloc(prgc,(iLength+1)*sizeof(char)); if(prgcT == NULL){ free(prgc); return NULL; } prgc = prgcT; *(prgc+iLength) = (char)cTemp; } } }
BOOL mixer_getlinecontrols(const LPMIXERLINE line) { int i; int lines; int mutes; MIXERLINECONTROLS controls; controls.cbStruct = sizeof(MIXERLINECONTROLS); controls.dwLineID = line->dwLineID; controls.cControls = line->cControls; controls.cbmxctrl = sizeof(MIXERCONTROL); controls.pamxctrl = NULL; if (!my_realloc(&controls.pamxctrl, sizeof(MIXERCONTROL) * controls.cControls)) return (FALSE); CHECK_MMRETURN(mixerGetLineControls((HMIXEROBJ)s_hMixer, &controls, MIXER_GETLINECONTROLSF_ALL)); for (i = 0, lines = s_lines.count, mutes = s_mutes.count; i < (int)controls.cControls; i++) { switch (controls.pamxctrl[i].dwControlType) { case MIXERCONTROL_CONTROLTYPE_VOLUME: lines++; break; case MIXERCONTROL_CONTROLTYPE_MUTE: mutes++; break; } } if (!my_realloc((void **)&s_lines.mixers, sizeof(MIXERCONTROL) * lines)) return (FALSE); if (!my_realloc((void **)&s_mutes.mixers, sizeof(MIXERCONTROL) * mutes)) return (FALSE); for (i = 0; i < (int)controls.cControls; i++) { switch (controls.pamxctrl[i].dwControlType) { case MIXERCONTROL_CONTROLTYPE_VOLUME: s_lines.mixers[s_lines.count++] = controls.pamxctrl[i]; break; case MIXERCONTROL_CONTROLTYPE_MUTE: s_mutes.mixers[s_mutes.count++] = controls.pamxctrl[i]; break; } } free(controls.pamxctrl); return (TRUE); }
char *get_next_line(const int fd) { static int ind; int debut; int flag; char *str; int ret; static char buffer[READ_SIZE]; flag = 0; str = NULL; while (flag != 1) { if (ind == 0) { if ((ret = read((int)fd, buffer, READ_SIZE)) == 0 || ret == -1) return (str); buffer[ret] = 0; } debut = ind--; while (buffer[++ind] != '\n' && buffer[ind] != 0); if ((str = my_realloc(str, sizeof(char) * (ind - debut + 1))) == NULL) return (NULL); my_strncpy(&str[my_strlen(str)], &buffer[debut], ind - debut); my_replace_flag(&flag, &ind, buffer); } return (str); }
char *get_next_line(const int fd) { char c; char *str; int i; int j; if ((str = malloc(READ_SIZE + 1)) == NULL || fd == -1) return (NULL); my_memset(str, READ_SIZE + 1); j = 2; i = 0; c = read_char(fd); while (c != '\n' && c != 0) { str[i++] = c; c = read_char(fd); if (i == (READ_SIZE * (j - 1))) { str = my_realloc(str, READ_SIZE * j); j++; } } str[i] = 0; if (str[0] == 0 && c == 0) return (NULL); return (str); }
/** * Allocates space for a SFLAdaptor and its HVSPortInfo userData. * Copies the guid to SFLAdaptor->deviceName (so guid can be freed), sets the * ifIndex and default values for ifSpeed etc. Adds the new adaptor to * vAdaptors and returns the new adaptor. * Does not check whether there is already an adaptor with the device name first. */ SFLAdaptor *addVAdaptor(SFLAdaptorList *vAdaptors, char *guid, uint32_t ifIndex) { SFLAdaptor *vAdaptor = (SFLAdaptor *)my_calloc(sizeof(SFLAdaptor)); vAdaptor->deviceName = my_strdup(guid); vAdaptor->ifIndex = ifIndex; vAdaptor->ifDirection = 3; vAdaptor->ifSpeed = 1000000000UL; vAdaptor->promiscuous = 2; HVSVPortInfo *portInfo = (HVSVPortInfo *)my_calloc(sizeof(HVSVPortInfo)); portInfo->filterEnabled = FALSE; portInfo->portId = 0; portInfo->revision = 0; portInfo->portFriendlyName = NULL; portInfo->portCountersInstance = NULL; portInfo->switchName = NULL; portInfo->vmSystemName = NULL; vAdaptor->userData = portInfo; if(vAdaptors->num_adaptors == vAdaptors->capacity) { // grow vAdaptors->capacity *= 2; vAdaptors->adaptors = (SFLAdaptor **)my_realloc(vAdaptors->adaptors, vAdaptors->capacity * sizeof(SFLAdaptor *)); } vAdaptors->adaptors[vAdaptors->num_adaptors++] = vAdaptor; return vAdaptor; }
my_bool allocate_dynamic(DYNAMIC_ARRAY *array, uint max_elements) { if (max_elements >= array->max_element) { uint size; uchar *new_ptr; size= (max_elements + array->alloc_increment)/array->alloc_increment; size*= array->alloc_increment; if (array->buffer == (uchar *)(array + 1)) { /* In this senerio, the buffer is statically preallocated, so we have to create an all-new malloc since we overflowed */ if (!(new_ptr= (uchar *) my_malloc(size * array->size_of_element, MYF(MY_WME)))) return 0; memcpy(new_ptr, array->buffer, array->elements * array->size_of_element); } else if (!(new_ptr= (uchar*) my_realloc(array->buffer,size* array->size_of_element, MYF(MY_WME | MY_ALLOW_ZERO_PTR)))) return TRUE; array->buffer= new_ptr; array->max_element= size; } return FALSE; }
static void add_to_heap (struct s_heap *hptr) { /* Adds an item to the heap, expanding the heap if necessary. */ int ito, ifrom; struct s_heap *temp_ptr; if (heap_tail > heap_size ) { /* Heap is full */ heap_size *= 2; heap = my_realloc ((void *)(heap + 1), heap_size * sizeof (struct s_heap *)); heap--; /* heap goes from [1..heap_size] */ } heap[heap_tail] = hptr; ifrom = heap_tail; ito = ifrom/2; heap_tail++; while ((ito >= 1) && (heap[ifrom]->cost < heap[ito]->cost)) { temp_ptr = heap[ito]; heap[ito] = heap[ifrom]; heap[ifrom] = temp_ptr; ifrom = ito; ito = ifrom/2; } }
my_bool set_dynamic(DYNAMIC_ARRAY *array, gptr element, uint idx) { if (idx >= array->elements) { if (idx >= array->max_element) { uint size; char *new_ptr; size=(idx+array->alloc_increment)/array->alloc_increment; size*= array->alloc_increment; if (!(new_ptr=(char*) my_realloc(array->buffer,size* array->size_of_element, MYF(MY_WME | MY_ALLOW_ZERO_PTR)))) return TRUE; array->buffer=new_ptr; array->max_element=size; } bzero((gptr) (array->buffer+array->elements*array->size_of_element), (idx - array->elements)*array->size_of_element); array->elements=idx+1; } memcpy(array->buffer+(idx * array->size_of_element),element, (size_t) array->size_of_element); return FALSE; }
static void add_dns_server(char *ip) { servers = (dns_server_t *) my_realloc(servers, (nservers+1)*sizeof(*servers)); servers[nservers].ip = strdup(ip); nservers++; sdprintf("Added NS: %s", ip); }
static void answer_add(dns_answer_t *answer, const char *what) { answer->list = (char **) my_realloc(answer->list, sizeof(*answer->list) * (answer->len+2)); answer->list[answer->len] = strdup(what); answer->len++; answer->list[answer->len] = NULL; }
char *get_next_line(const int fd) { char *str; char *buff; int i; int readed; i = 0; if ((str = malloc((2) * sizeof(char))) == NULL || (buff = malloc((2) * sizeof(char))) == NULL) return (NULL); buff[0] = 0; while ((readed = read(fd, str, 1)) > 0) { buff[i] = str[0]; buff[i + 1] = 0; if (buff[i] == '\n' || buff[i] == 0) { buff[i] = 0; return (free(str), buff); } buff = my_realloc(buff); i++; } if (buff[0] == 0) return (free(str), NULL); return (free(str), buff); }
char *get_next_line(const int fd, t_mysh *all) { int rd; char *output; static int i = 0; static char *buffer = NULL; /* if (fd == 0) return (empty_buffer(buffer));*/ rd = 1; if (buffer == NULL || buffer[i] == '\0') if ((buffer = my_read(fd, &i, &rd, buffer)) == NULL) return (NULL); output = x_malloc(sizeof(output) * (BUF_SIZE + 1)); all->gnl_j = 0; while (buffer[i] != '\n' && rd > 0) if (buffer[i] == '\0') { if ((buffer = my_read(fd, &i, &rd, buffer)) == NULL) return (output); output = my_realloc(all->gnl_j, output); } else output[all->gnl_j++] = buffer[i++]; output[all->gnl_j] = '\0'; i++; return (output); }
bool add_spaced_seed(char const * seed_string) { int i; seed = (struct seed_type *) //xrealloc(seed, sizeof(struct seed_type) * (n_seeds + 1)); my_realloc(seed, (n_seeds + 1) * sizeof(seed[0]), n_seeds * sizeof(seed[0]), &mem_small, "seed"); seed[n_seeds].mask[0] = 0x0; seed[n_seeds].span = strlen(seed_string); seed[n_seeds].weight = strchrcnt(seed_string, '1'); if (seed[n_seeds].span < 1 || seed[n_seeds].span > MAX_SEED_SPAN || seed[n_seeds].weight < 1 || (int)strchrcnt(seed_string, '0') != seed[n_seeds].span - seed[n_seeds].weight) return false; for (i = 0; i < seed[n_seeds].span; i++) bitmap_prepend(seed[n_seeds].mask, 1, (seed_string[i] == '1' ? 1 : 0)); max_seed_span = MAX(max_seed_span, seed[n_seeds].span); min_seed_span = MIN(min_seed_span, seed[n_seeds].span); n_seeds++; avg_seed_span = 0; for (i = 0; i < n_seeds; i++) avg_seed_span += seed[i].span; avg_seed_span = avg_seed_span/n_seeds; return true; }
char *wordtab_in_str(char **tab, int mode) { char *str; int i; int j; int k; int len; i = -1; k = 0; str = NULL; len = 0; if (!tab[0]) return (NULL); while (tab[++i]) { j = 0; len += strlen(tab[i]) + 1; str = my_realloc(str, sizeof(char) * (len + 1)); while (tab[i][j]) str[k++] = tab[i][j++]; str[k++] = ' '; if (mode == 1) free(tab[i]); } str[k - 1] = 0; return (str); }
/* Return an allocated buffer which contains a copy of the string * 'str', with all 'div' characters escaped by 'mask'. 'mask' * characters are escaped too. * * Remember to free the returned memory block. */ char *str_escape(const char *str, const char divc, const char mask) { const size_t len = strlen(str); size_t buflen = (2 * len), blen = 0; char *buf = NULL, *b = NULL; const char *s = NULL; b = buf = (char *) my_calloc(1, buflen + 1); for (s = str; *s; s++) { /* Resize buffer. */ if ((buflen - blen) <= 3) { buflen <<= 1; /* * 2 */ buf = (char *) my_realloc(buf, buflen + 1); if (!buf) return NULL; b = buf + blen; } if (*s == divc || *s == mask) { simple_snprintf(b, buflen, "%c%02x", mask, *s); b += 3; blen += 3; } else { *(b++) = *s; blen++; } } *b = 0; return buf; }
static void add_host(char *host, char *ip) { hosts = (dns_host_t *) my_realloc(hosts, (nhosts+1)*sizeof(*hosts)); hosts[nhosts].host = strdup(host); hosts[nhosts].ip = strdup(ip); nhosts++; }
char *get_real_path(char *pwd_asked, char *act_pwd) { int i; bool next; size_t len; i = -1; next = true; while (pwd_asked[++i]) { if (pwd_asked[i] == '.') { if (pwd_asked[i + 1] == '.') modify_pwd(act_pwd); } else if (pwd_asked[i] != '/' && next) { len = next_file_len(&pwd_asked[i]); act_pwd = my_realloc(act_pwd, len); strcatnext_file(act_pwd, &pwd_asked[i]); next = false; } else if (pwd_asked[i] == '/') next = true; } return (act_pwd); }
/* * Function: dhcpol_get * * Purpose: * Accumulate all occurences of the given option into a * malloc'd buffer, and return its length. Used to get * all occurrences of a particular option in a single * data area. * Note: * Use _FREE(val, M_TEMP) to free the returned data area. */ void * dhcpol_get(dhcpol_t * list, int tag, int * len_p) { int i; char * data = NULL; int data_len = 0; if (tag == dhcptag_end_e || tag == dhcptag_pad_e) return (NULL); for (i = 0; i < dhcpol_count(list); i++) { unsigned char * option = dhcpol_element(list, i); if (option[DHCP_TAG_OFFSET] == tag) { int len = option[DHCP_LEN_OFFSET]; if (data_len == 0) { data = my_malloc(len); } else { data = my_realloc(data, data_len, data_len + len); } bcopy(option + DHCP_OPTION_OFFSET, data + data_len, len); data_len += len; } } *len_p = data_len; return (data); }
char *get_next_line(const int fd) { static char buff[BUFF_SIZE]; static int len; static int a; char *str; int n; n = 0; if ((read_gnl(fd, &len, &a, buff)) == 0) return (NULL); if ((str = malloc(sizeof(char) * (BUFF_SIZE + 1))) == NULL) return (NULL); my_memset(str, 0, BUFF_SIZE + 1); while (buff[a] && buff[a] != '\n') { str[n++] = buff[a++]; if (n >= BUFF_SIZE) str = my_realloc(str, 1); if ((read_gnl(fd, &len, &a, buff)) == 0) return (str); } a++; str[n] = '\0'; return (str); }
char *get_next_line_suite(t_gnl *line, char *buffer, int i, const int fd) { int j; int ret; if ((ret = read(fd, buffer, READ_SIZE)) == 0) return (NULL); buffer[ret] = 0; while (ret != 0) { if (line->line != NULL) i = my_strlen_spe(line->line, 0); line->line = my_realloc(line->line, READ_SIZE); j = -1; while (buffer[++j]) { if (buffer[j] == '\n') { line->line[++i] = 0; return (line->line); } line->line[++i] = buffer[j]; } ret = read(fd, buffer, READ_SIZE); buffer[ret] = 0; } return (buffer); }
uchar *alloc_dynamic(DYNAMIC_ARRAY *array) { if (array->elements == array->max_element) { char *new_ptr; if (array->buffer == (uchar *)(array + 1)) { /* In this senerio, the buffer is statically preallocated, so we have to create an all-new malloc since we overflowed */ if (!(new_ptr= (char *) my_malloc((array->max_element+ array->alloc_increment) * array->size_of_element, MYF(MY_WME)))) return 0; memcpy(new_ptr, array->buffer, array->elements * array->size_of_element); } else if (!(new_ptr=(char*) my_realloc(array->buffer,(array->max_element+ array->alloc_increment)* array->size_of_element, MYF(MY_WME | MY_ALLOW_ZERO_PTR)))) return 0; array->buffer= (uchar*) new_ptr; array->max_element+=array->alloc_increment; } return array->buffer+(array->elements++ * array->size_of_element); }
char *step_thru_file(FILE *fd) { if (fd == NULL) { return NULL; } char tempBuf[1024] = "", *retStr = NULL; size_t ret_siz = 0; while (!feof(fd)) { if (fgets(tempBuf, sizeof(tempBuf), fd) && !feof(fd)) { if (retStr == NULL) { ret_siz = strlen(tempBuf) + 2; retStr = (char *) my_calloc(1, ret_siz); strlcpy(retStr, tempBuf, ret_siz); } else { ret_siz = strlen(retStr) + strlen(tempBuf); retStr = (char *) my_realloc(retStr, ret_siz); strlcat(retStr, tempBuf, ret_siz); } if (retStr[strlen(retStr)-1] == '\n') { retStr[strlen(retStr)-1] = 0; break; } } } return retStr; }
void *traitement(t_get *g, const int fd) { while (g->buff[g->i] != '\n' && g->buff[g->i] != 0) if (g->j + 1 == g->len || (g->buff[g->i + 1] == '\n' || g->buff[g->i + 1] < 32)) { while (g->str[g->count++]); if (g->str) CHECK_RETURN(g->str = my_realloc(g->str, (g->count + g->i + 1)), NULL, NULL); my_strncat(g->str, g->buff, g->i + 1); tronque(g->buff, g->i + 1, g->len); ++(g->j); g->i = 0; if (g->buff[g->i] == '\n' || g->buff[g->i] < 32) if (g->buff[g->i] == '\0') { if ((g->len = read(fd, g->buff, READ)) == 0) return (g->str); g->j = 0; } } else { ++(g->j); ++(g->i); } }
char *build_accolades_contact_list (void) { char *str; char *t; int len; /* De la liste des contacts, je cree ca : {nick1,nick2,nick3,etc...}\0 */ str = my_malloc (sizeof (char) * 2); my_strcpy (str, "{"); while ((t = get_next_contact_list_name ())) { len = my_strlen (str); str = my_realloc (str, len, (len + my_strlen (t) + 3) * sizeof (char)); if (len != 1) { my_strcpy (str + len, ","); my_strcpy (str + len + 1, t); /*, my_strlen (t)); */ } else my_strcpy (str + 1, t); } my_strcat (str, "}"); return (str); }
char *get_next_line(void) { char *line; char *buffer; int i; i = 0; if ((line = malloc(2)) == NULL) return (NULL); if ((buffer = malloc(2)) == NULL) return (NULL); ini_line_buffer(line, buffer); while (buffer[0] != '\n' && (buffer[0] != '\0' || i == 0)) { if (next_read(buffer, i) == -1) return (NULL); if (buffer[0] != '\n') { line[i++] = buffer[0]; if ((line = my_realloc(line, i + 1)) == NULL) return (NULL); } } free(buffer); return (line); }
static void string_ensureLen_(mj_buffer_t* pcs, size_t len) { char* newPtr = NULL; size_t capacity = (0 == len) ? 1u : len; cstring_assert(NULL != pcs); if (0 != pcs->capacity && pcs->capacity > len) return ; capacity = (capacity + (CSTRING_ALLOC_GRANULARITY - 1)) & ~(CSTRING_ALLOC_GRANULARITY - 1); if (capacity < pcs->capacity * 2) capacity = pcs->capacity * 2; if(0 == pcs->str) newPtr = (char*)my_malloc(capacity + 1); else newPtr = (char*)my_realloc(pcs->str, capacity + 1); pcs->str = newPtr; pcs->str[pcs->len] = '\0'; pcs->capacity = capacity; return ; }
int my_setenv(char **tab, t_shell *sh) { int i; i = -1; sh->bol = 0; if (!exit_setenv(tab)) return (-1); if (!tab[1]) return (my_env(sh)); while (sh->env[++i]) { if (!strncmp(sh->env[i], tab[1], strlen(tab[1])) && sh->env[i][strlen(tab[1])] == '=') { free(sh->env[i]); sh->env[i] = concat_str(tab[1], tab[2], '='); sh->bol = 1; } } if (!sh->bol) { sh->env = my_realloc(sh->env, ((tab_len(sh->env) + 2) * sizeof(char *))); sh->env[i] = concat_str(tab[1], tab[2], '='); sh->env[i + 1] = NULL; } return (0); }
int connections(t_coord *elem, char *tmp, char *tmp2, int i) { if (strcmp(elem->room_name, tmp) == 0) /* strcmp */ { i = 0; if (elem->connection == NULL) { elem->connection = malloc(sizeof(char*) * 2); if (!elem->connection) return (-1); elem->connection[0] = NULL; } else { elem->connection = my_realloc(elem->connection); if (!elem->connection) return (-1); } while (elem->connection[i]) i = i + 1; elem->connection[i] = strdup(tmp2); if (!elem->connection[i]) return (-1); elem->connection[i + 1] = NULL; } return (0); }
/* Traverse outputs of output pin or primitive to see what input pins it reaches Record list of input pins based on depth */ static void expand_pb_graph_node_and_load_output_to_input_connections( INOUTP t_pb_graph_pin *current_pb_graph_pin, INOUTP t_pb_graph_pin *reference_pin, INP int depth) { int i; if (current_pb_graph_pin->scratch_pad == OPEN && current_pb_graph_pin->parent_node->pb_type->depth > depth) { current_pb_graph_pin->scratch_pad = 1; for (i = 0; i < current_pb_graph_pin->num_output_edges; i++) { assert(current_pb_graph_pin->output_edges[i]->num_output_pins == 1); expand_pb_graph_node_and_load_output_to_input_connections( current_pb_graph_pin->output_edges[i]->output_pins[0], reference_pin, depth); } if (current_pb_graph_pin->parent_node->pb_type->num_modes == 0 && current_pb_graph_pin->port->type == IN_PORT) { reference_pin->num_connectable_primtive_input_pins[depth]++; reference_pin->list_of_connectable_input_pin_ptrs[depth] = (t_pb_graph_pin**) my_realloc( reference_pin->list_of_connectable_input_pin_ptrs[depth], reference_pin->num_connectable_primtive_input_pins[depth] * sizeof(t_pb_graph_pin*)); reference_pin->list_of_connectable_input_pin_ptrs[depth][reference_pin->num_connectable_primtive_input_pins[depth] - 1] = current_pb_graph_pin; } } }
int test_realloc(){ int index=0; size_t new_size,old_free_space,new_free_space; uint8_t *add; print_history(); printf("Enter index to choose address: "); scanf("%d",&index); if(index>=ALLOC_HISTORY){ printf("Invalid Selection\nTEST FAILED\n"); return 1; } printf("Enter new size: "); scanf("%lu",&new_size); old_free_space = free_space_in_my_heap(); /* Reallocate memory */ add = (uint8_t *)my_realloc((void *)address[index],new_size); new_free_space = free_space_in_my_heap(); /* Add to history for tracking */ if(add!=NULL && add!=address[index]){ address[pos]=add; pos = (pos+1)%ALLOC_HISTORY; } /* NULL address with size greater than 0 should work like malloc */ if(address[index]==NULL && new_size > 0){ if(add!=NULL && (new_free_space < old_free_space)){ printf("TEST PASEED\n"); return 0; } printf("TEST FAILED\n"); return 1; } /* Not NULL address and size greater than 0, may shrink or expand memory */ if(address[index]!=NULL && new_size > 0){ if(add!=NULL){ printf("TEST PASSED\n"); return 0; } printf("TEST FAILED\n"); return 1; } /* Not NULL address and size equal to zero should work like free */ if(address[index]!=NULL && new_size==0){ if(add==NULL && (new_free_space > old_free_space)){ printf("TEST PASSED\n"); return 0; } printf("TEST FAILED\n"); return 1; } printf("TEST FAILED\n"); return 0; }