int get_line(int const fd, char **a) { int i; int stop; int n; char buf[BUFF_SIZE]; if (!a || fd < 0) return (0); *a = ft_strnew(1); n = 0; while ((i = read(fd, buf, BUFF_SIZE)) > 0) { buf[i] = '\0'; *a = (char *)ft_realloc((void *)*a, ft_strlen(*a), ft_strlen(*a) + i); ft_strcat(*a, buf); if ((stop = ft_strindex(*a, '\n')) != -1) { n = 1; break ; } } *a = ft_realloc((void *)*a, ft_strlen(*a), stop); if (!*a || i < 0 || !n) return (0); return (1); }
/** * ft_get_line - read an ASCII line from a file * @fp: file pointer * * Get a line of ASCII text from a file. The file is read * up to the first \0 or \n. * * Returns the dynamically-allocated string containing * that line. At the end of a file a string "" will * be returned. * * In *both* cases, the string must be free()d at some * point. * * %NULL may be returned on allocation failure. */ char * ft_get_line(FILE * fp) { char * buf; char * cp; int c; size_t max = 128; buf = ft_malloc(max); if (!buf) return NULL; cp = buf; do { switch (c = fgetc(fp)) { case EOF: case '\0': *cp = '\0'; return buf; break; case '\n': *cp = (char)c; cp++; if (((size_t)(cp - buf)) == max) { buf = ft_realloc(buf, max + 64); if (!buf) return NULL; cp = buf+max; max += 64; } *cp = '\0'; return buf; break; default: *cp = (char)c; cp++; if (((size_t)(cp - buf)) == max) { buf = ft_realloc(buf, max + 64); if (!buf) return NULL; cp = buf+max; max += 64; } break; } } while (1); }
char *ft_read(char *buff, int fd, int *len_tab, int *len_str) { int i; int len_tmp; i = 0; len_tmp = 0; buff = (char *)malloc(sizeof(char) * 1); while ((read(fd, &buff[i], 1)) > 0) { if (i != 0) buff = ft_realloc(buff, 1); if (buff[i] != '.' && buff[i] != 'X' && buff[i] != '\n' && buff[i] != '\0') return (NULL); if (buff[i] == '\n') { if (*len_tab == 1) *len_str = i; *len_tab = *len_tab + 1; if (*len_str != len_tmp) return (NULL); len_tmp = -1; } len_tmp++; i++; } if (buff[i - 1] == '\n') return (NULL); return (buff); }
char *add_char_to_str(t_readline *r, long key) { int j; char *tmp; j = 0; tmp = ft_strdup(r->line); r->line = ft_realloc(r->line, 1); while ((size_t)j < (size_t)r->i) { (r->line)[j] = tmp[j]; j++; } (r->line)[j] = key; j++; while (j <= (int)ft_strlen(tmp)) { (r->line)[j] = tmp[j - 1]; j++; } tputs(tgetstr("im", NULL), 0, ft_outc); ft_putchar(key); tputs(tgetstr("ei", NULL), 0, ft_outc); ft_strdel(&tmp); (r->i)++; return (r->line); }
int readnl(int fd, char **save) { char *psave; int ret; int count; count = 0; psave = *save; *save = ft_strnew(BUFF_SIZE); while ((ret = read(fd, *save + BUFF_SIZE * count, BUFF_SIZE)) > 0) { if (ret == BUFF_SIZE) *save = ft_realloc(*save, BUFF_SIZE * (count + 2), &count); if (ft_strchr(*save, '\n')) { *save = joinsave(psave, *save); return (1); } } if (ret == -1) return (-1); *save = joinsave(psave, *save); if (ret == 0 && ft_strlen(*save) == 0) return (0); return (1); }
int get_next_line(int const fd, char **line) { int ret; char bf[BUFF_SIZE + 1]; static char *tmp; static int p = 1; p = ft_realloc(line, BUFF_SIZE + 1, p); if (tmp != NULL && ft_isnewline(tmp) == 1) return (blocfaible(line, &tmp, &p)); if ((ret = read(fd, bf, BUFF_SIZE)) == -1) return (-1); bf[ret] = '\0'; if (ft_isnewline(bf) == 1 && ret != 0) return (blocdingue(&tmp, line, bf, &p)); else if (ret == 0) { *line = ft_strjoin(*line, bf); return (0); } else { *line = ft_strcat(*line, bf); return (get_next_line(fd, line)); } }
char *readline(int fd, long int *i, int *ret, int j) { char *buf; char *str; str = (char *)malloc(sizeof(char) * 1); buf = (char *)malloc(sizeof(char) * BUFF_SIZE); if (*i == 0) *ret = read(fd, buf, BUFF_SIZE); while (buf[*i] != '\n' && *ret) { str = ft_realloc(str, j); str[j] = buf[*i]; (*i)++; j++; if (buf[*i] == '\0') { *ret = read(fd, buf, BUFF_SIZE); *i = 0; } } str[j] = '\0'; (*i)++; if (buf[*i] == 0) *i = 0; free(buf); return (str); }
int get_next_line(int const fd, char **line) { static char *buf[256]; char tmp[BUFF_SIZE + 1]; int is_n; int ret; size_t size; ret = 1; if (buf[fd] == NULL && line != NULL) buf[fd] = (char *)ft_memalloc(sizeof(char) * BUFF_SIZE + 1); if (buf[fd] == NULL || fd < 0 || *line == NULL) return (-1); size = 1 + ft_strlen(buf[fd]); while ((is_n = ft_strcchr(buf[fd], '\n')) == -1 && ret > 0) { if ((ret = read(fd, tmp, BUFF_SIZE))) { tmp[ret] = '\0'; size += BUFF_SIZE; if ((buf[fd] = (char *)ft_realloc(buf[fd], size)) == NULL) return (-1); ft_strcat(buf[fd], tmp); } } return (ft_fill_line(line, &buf[fd], ret)); }
int get_get(int fd, char **buf) { char *tmp; int conteur; int file; conteur = 0; tmp = *buf; *buf = ft_strnew(BUFF_SIZE); while ((file = read(fd, *buf + BUFF_SIZE * conteur, BUFF_SIZE)) > 0) { if (file == BUFF_SIZE) *buf = ft_realloc(*buf, BUFF_SIZE * (conteur + 2)); if (ft_strchr(*buf, '\n')) { *buf = ft_strdup_2_0(*buf, tmp); return (1); } conteur++; } if (file == -1) return (-1); *buf = ft_strdup_2_0(*buf, tmp); if (file == 0 && ft_strlen(*buf) == 0) return (0); return (1); }
char *ft_get_str(char **argv, int comp) { char *buffer; char *str; int nread; int fd; if (open(argv[comp], O_RDONLY) == -1) fd = 0; else fd = open(argv[comp], O_RDONLY); buffer = (char *)malloc(sizeof(char) * (BUF_SIZE2 + 1)); str = (char *)malloc(sizeof(char)); str[0] = 0; while ((nread = read(fd, buffer, BUF_SIZE2)) && (nread != -1)) { buffer[nread] = 0; str = ft_realloc(str, buffer, 0); if (!str) return (0); } if (buffer) free(buffer); if (nread == -1) return (0); str = ft_verifstr(str, argv); return (str); }
int main(void) { char *str; int i; str = (char *)ft_malloc(sizeof(char) * 40); if (!str) { printf("str don't exist\n"); return (-1); } i = 0; while (i < 40) { str[i]= 'a'; i++; } ft_realloc(str, 3); i = -17; while (i < 10) { printf("%p => %d\n", str, (int)str[i]); i++; } ft_free(str); }
int ft_get_next_line(const int fd, char **line) { int i; static char buff[BUFF_SIZE + 1]; *line = (char *)malloc(sizeof(char *) * (BUFF_SIZE + 1)); if (fd < 0 || line == NULL) return (-1); **line = '\0'; ft_strcat(*line, buff); *buff = '\0'; i = BUFF_SIZE; while (ft_get_the_end(*line, buff) != -1 && i == BUFF_SIZE) { i = read(fd, buff, BUFF_SIZE); if (i == -1) return (-1); buff[i] = '\0'; *line = ft_realloc(*line, i); ft_strcat(*line, buff); } if (**line == '\0' && ft_strlen(buff) == 0 && i != BUFF_SIZE) return (0); return (1); }
char *ft_escape(const char *s) { int i; size_t len; char *esc; len = ft_strlen(s) + 2; if (!(esc = ft_strnew(len))) return (NULL); i = 1; *esc = '\''; while (*s) { if (*s == '\'') { esc = ft_realloc(&esc, len + 1, len + 4); ft_strcat(&esc[i], "'\\''"); len += 3; i += 4; s++; } else esc[i++] = *s++; } esc[i] = '\''; return (esc); }
char *lessthanline(int fd, int *i) { char buf[BUFF_SIZE]; static int j; static int ret; int k; char *str; char *hello; j = 0; k = 0; str = (char *)malloc((char) + 1); //if (!j) ret = read(fd, buf, BUFF_SIZE); while (buf[j] != '\n' && ret) { printf("hello"); str = ft_realloc(str, k); str[k] = buf[j]; j++; k++; if (j == BUFF_SIZE) { ret = read(fd, buf, BUFF_SIZE); j = 0; } } str[k] = '\0'; j++; *i = j; hello = str; printf("end\n"); return (hello); }
void *realloc(void *ptr, size_t size) { void *addr; pthread_mutex_lock(ft_malloc_mutex()); addr = ft_realloc(ptr, size); pthread_mutex_unlock(ft_malloc_mutex()); return (addr); }
void *ft_drealloc(void *original, size_t target_size, size_t original_size) { void *copy; copy = ft_realloc(original, target_size, original_size); if (copy == NULL) free(original); return (copy); }
static char *ft_realfree(char *buf, int i) { char *tmp; buf[i] = '\0'; tmp = ft_realloc(buf); free(buf); buf = tmp; return (buf); }
void ft_normal_keys(char *buf, t_env *e) { int len; len = e->pos + ft_strlen(buf); if ((len >= CMD_SIZE) || (len % CMD_SIZE == 0)) e->history->command = ft_realloc(e->history->command, CMD_SIZE); ft_stricpy(e->history->command, buf, e->pos); ft_putstr(buf); e->pos += len - e->pos; }
int ft_read_buf(char **line, t_info *cur, int *len) { char *n; if ((n = ft_memchr(cur->start, (int)'\n', cur->offset)) == NULL) { if (ft_realloc(line, cur->start, cur->offset, len)) return (-1); cur->offset = 0; cur->start = NULL; } else { if (ft_realloc(line, cur->start, n - cur->start, len)) return (-1); cur->offset = cur->offset - (n + 1 - cur->start); cur->start = n + 1; return (1); } return (0); }
void array_append(t_array *array, void *value) { ft_assert(array != NULL); if (array->size >= array->cap) { array->cap *= 2; array->data = ft_realloc(array->data, sizeof(void*) * array->size, \ sizeof(void*) * array->cap); ft_assert(array->data != NULL); } array->data[array->size] = value; array->size++; }
void ft_graph_tna(t_game *game, char *line) { static int first = 1; char **tab; if (first == 1) { game->team = (char**)malloc(sizeof(char*)); game->team[0] = NULL; first = 0; } if ((tab = ft_strsplit(line, ' ')) == NULL) ft_graphic_error("parse error -> tab in ft_graph_tna"); game->team = ft_realloc(game, game->team, tab[1]); ft_strdel2(&tab); }
int reader_write_char(t_rl_reader *r, char c) { if (r->end + 1 >= r->size) { r->line = ft_realloc(r->line, r->size, r->size + RL_BUFF_SIZE); if (!r->line) return (-1); r->size += RL_BUFF_SIZE; } if (r->cursor < r->end) ft_memmove(r->line + r->cursor + 1, r->line + r->cursor, r->end - r->cursor); r->line[r->cursor] = c; r->cursor++; r->end++; return (0); }
static void ft_add(char **buff, char *str, size_t *size, int type) { static size_t max; size_t len; if (!(*size)) max = 0; len = type == 1 ? 1 : ft_strlen(str); if (*buff == NULL || *size + len > max) { *buff = ft_realloc(*buff, *size); max += BUFFSIZE; } ft_strncpy(*buff + *size, str, len); if (type != 1) ft_memdel((void **)&str); *size += len; }
int reader_write(t_rl_reader *r, char *str) { if (!str) return (0); while ((int)(r->end + ft_strlen(str)) >= r->size) { r->line = ft_realloc(r->line, r->size, r->size + RL_BUFF_SIZE); if (!r->line) return (-1); r->size += RL_BUFF_SIZE; } if (r->cursor < r->end) ft_memmove(r->line + r->cursor + ft_strlen(str), r->line + r->cursor, r->end - r->cursor); ft_memcpy(r->line + r->cursor, str, ft_strlen(str)); r->end += ft_strlen(str); r->cursor += ft_strlen(str); return (ft_strlen(str)); }
int get_all(int const fd, char **a) { ssize_t i; char buf[BUFF_SIZE + 1]; if (!a || fd < 0) return (FALSE); *a = ft_strnew(1); while ((i = read(fd, buf, BUFF_SIZE)) > 0) { buf[i] = '\0'; *a = (char *)ft_realloc(\ (void *)*a, ft_strlen(*a), ft_strlen(*a) + (size_t)i); ft_strcat(*a, buf); } if (!*a || i < 0) return (FALSE); return (TRUE); }
static int ft_getline_handle_buffer(char **buf, char *sbuf, int *total, int bytes_read) { char *pos; *buf = ft_realloc(*buf, *total + bytes_read + 1); if (!*buf) return (-1); ft_strncpy(*buf + *total, sbuf, bytes_read); if ((pos = ft_strchr(*buf, '\n'))) { *pos++ = '\0'; ft_strncpy(sbuf, pos, FT_BUF_MAX); while (*pos) *pos++ = '\0'; return (1); } return (0); }
static int ft_copybuf(char **result, char *buf, size_t *i, size_t *j) { size_t s; int char_size; *i = 0; char_size = sizeof(char); while (buf[*i] != '\n' && buf[*i] != '\0' && *i < BUFF_SIZE) { s = ft_strlen(*result) + 1; *result = ft_realloc(*result, s * char_size, (s + *j) * char_size); if (!(*result)) return (-1); (*result)[*j] = buf[*i]; (*i)++; (*j)++; } (*result)[*j] = '\0'; return (0); }
int ft_setenv(const char *name, const char *value, int overwrite) { int i; char *tmp; if (!name || !*name || ft_strchr(name, '=')) return (-1); if (ft_getenv(name) && !overwrite) return (0); i = 0; ft_unsetenv(name); while (g_environ[i]) i++; g_environ = ft_realloc(&g_environ, i * sizeof(*g_environ), (i + 2) * sizeof(*g_environ)); tmp = ft_strjoin(name, "="); g_environ[i] = ft_strjoin(tmp, value); ft_strdel(&tmp); return (0); }
void *realloc(void *ptr, size_t size) { static t_zone *ptr_realloc = NULL; t_zone *current; int ret; if (size <= 0) return (NULL); if (!ptr_realloc) ptr_realloc = get_malloc(); current = ptr_realloc; while (42) { if ((ret = found_inter(ptr, current)) != -1) return (ft_realloc(ptr_realloc, ptr, current, size)); if (!current->next) break ; current = current->next; } return (NULL); }
static int create_line(char **temp, char *buf, char *rmning) { int i; int j; i = 0; j = ft_strlen(*temp); temp[0] = ft_realloc(*temp, (ft_strlen(*temp) + ft_strlen(buf) + 1)); while (buf[i] != '\0') { if (buf[i] == '\n') { *((*temp) + j) = '\0'; rmning = ft_strcpy(rmning, &buf[++i]); return (1); } temp[0][j++] = buf[i++]; } ft_bzero(rmning, ft_strlen(rmning)); *((*temp) + j) = '\0'; return (0); }