static int read_file(int const fd, char **line, char *buf) { int n; int ret; char *tmp; while ((ret = read(fd, buf, BUF_SIZE)) > 0) { buf[ret] = '\0'; if (ret == -1) return (-1); n = ft_strichr(buf, '\n'); if (n == -1) { tmp = ft_strnjoin(*line, buf, ret); free(*line); *line = tmp; } else { *line = ft_strnjoin(*line, buf, n); ft_strcpy(buf, buf + n + 1); return (1); } } ft_bzero(buf, BUF_SIZE); return (**line != '\0'); }
int get_next_line(int const fd_const, char **line) { static t_gnl gnl[64] = {{{0}, 0, 0, 0}}; t_norme n; if (!(n.n = NULL) && (!line || fd_const < 0)) return (-1); n.f = fd_const % 64; *line = NULL; while (!n.n) { if (gnl[n.f].i == gnl[n.f].r && !(gnl[n.f].i = 0)) { if ((gnl[n.f].r = read(fd_const, gnl[n.f].buf, BUFF_SIZE)) <= 0) { n.t = gnl[n.f].r; gnl[n.f].r = 0; return (*line ? 1 : n.t); } } n.n = ft_memchr(&gnl[n.f].buf[gnl[n.f].i], 10, gnl[n.f].r - gnl[n.f].i); n.l = (n.n) ? n.n - &gnl[n.f].buf[gnl[n.f].i] : gnl[n.f].r - gnl[n.f].i; *line = (*line) ? ft_strnjoin(*line, &gnl[n.f].buf[gnl[n.f].i], n.l, &gnl[n.f].s) : ft_strndup(&gnl[n.f].buf[gnl[n.f].i], n.l, &gnl[n.f].s); gnl[n.f].i = (n.n) ? gnl[n.f].i + n.l + 1 : gnl[n.f].r; } return (1); }
static char *ft_passkip(char *next, char *buf, int nbr) { char *d; d = next; next = ft_strnjoin(next, buf, nbr); free(d); return (next); }
static char *ft_freejoin(char *tmp, char *buf, int ret) { char *copy; copy = tmp; tmp = ft_strnjoin(tmp, buf, ret); free(copy); return (tmp); }
static char *ft_freejoin(char *tmp, char *buf, int ret) { char *l; l = tmp; tmp = ft_strnjoin(tmp, buf, ret); free(l); return (tmp); }
static char *ft_joincontent(char *tmp, char *buf, int read_val) { char *ptr; ptr = tmp; tmp = ft_strnjoin(tmp, buf, read_val); free(ptr); return (tmp); }
static int join_line_buf(char **line, char *buf, char *to_join) { char *tmp; tmp = ft_strnjoin(to_join, buf, ft_strlen(buf)); if (!tmp) return (-1); ft_bzero(buf, sizeof(buf)); *line = tmp; return (0); }
static char *read_til_next_line(int fd, t_buf *buf) { char *newstr; ssize_t len; newstr = NULL; while ((len = ft_len_to_endline(fd, buf)) == -1 && buf->size > 0) { newstr = ft_strnjoin(newstr, buf->buf + buf->cursor, buf->size - buf->cursor); buf->cursor = buf->size; } if (buf->size == 0) return (newstr); else if (buf->size == -1) return (NULL); newstr = ft_strnjoin(newstr, buf->buf + buf->cursor, len); if (newstr == NULL) return (NULL); buf->cursor += len + 1; return (newstr); }
static int join_line_buf_shifted(char **line, char *buf, char *to_join) { char *tmp; char *posn; posn = ft_strchr(buf, '\n'); tmp = ft_strnjoin(to_join, buf, posn - buf); if (!tmp) return (-1); ft_strdel(line); *line = tmp; ft_strncpy(buf, posn + 1, ft_strlen(buf) - (posn - buf)); return (1); }
static void apply_padding(t_pad *pad, t_lst **lst) { t_lst *tmp; tmp = *lst; if (tmp->next) { while (tmp) { tmp->link = put_s_before(tmp->link, pad->len_lnk); tmp->user_id = ft_strnjoin(tmp->user_id, " ", (pad->len_usr - ft_strlen(tmp->user_id))); tmp->group_id = ft_strnjoin(tmp->group_id, " ", (pad->len_grp - ft_strlen(tmp->group_id))); tmp->size = put_s_before(tmp->size, pad->len_siz); tmp->maj = put_s_before(tmp->maj, pad->len_maj); tmp->min = put_s_before(tmp->min, pad->len_min); tmp->majmin = ft_strjoin(tmp->maj, " "); tmp->majmin = ft_strjoin(tmp->majmin, tmp->min); tmp = tmp->next; } } }
char *ft_strwnjoin(char const *s1, char const *s2, size_t len, size_t a) { char *ts1; char *ts2; char *join; ts1 = a == 0 || a == 2 ? (char*)s1 : NULL; ts2 = a == 1 || a == 2 ? (char*)s2 : NULL; join = ft_strnjoin(s1, s2, len); if (a == 0) free(ts1); else if (a == 1) free(ts2); else if (a == 2) { free(ts1); free(ts2); } return (join); }
int init_history(t_history *history, int size) { char *home_dir; if (size < 0 || size > DEFAULT_HISTSIZE) size = DEFAULT_HISTSIZE; if (!(history->history = (char**)malloc(sizeof(char*) * (size + 1)))) return (MALLOC_FAIL); ft_memset(history->history, 0, (size + 1) * sizeof(char*)); if (!(home_dir = get_home_dir())) return (MALLOC_FAIL); if (!(history->file = ft_strnjoin(home_dir, 2, "/", ".42sh_history"))) { ft_strdel(&home_dir); return (MALLOC_FAIL); } history->current = 0; history->len = 0; history->size = size; import_history_from_file(history); return (0); }
int l_read(char **line, char *str, size_t *cursor, char *newlinepos) { *line = ft_strnjoin(*line, str + *cursor, newlinepos - (str + *cursor)); *cursor = (newlinepos + 1) - str; return (1); }