Skip to content

Instantly share code, notes, and snippets.

@Fluf22
Last active January 1, 2016 10:39
Show Gist options
  • Save Fluf22/8132698 to your computer and use it in GitHub Desktop.
Save Fluf22/8132698 to your computer and use it in GitHub Desktop.
Minishell_1
Tu vas ouvrir ce fichier avec un nom aussi intrigant.
Tu vas lire ces lignes.
Tu vas comprendre que ce fichier n'a qu'un seul but.
Que ce fichier ne souhaite que te transmettre un simple message.
Tu vas lire et voir que ce message est le suivant:
Tu vas venir avec nous au ski !
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* fdf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thoraffr <thoraffr@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/12/17 13:22:44 by thoraffr #+# #+# */
/* Updated: 2013/12/22 12:05:21 by thoraffr ### ########.fr */
/* */
/* ************************************************************************** */
#include "fdf.h"
int ft_draw(t_tab **tab_list, t_pxl *p, size_t *i, size_t *l)
{
t_tab *tmp;
size_t lh;
lh = l[2] + l[1];
tmp = malloc(sizeof(t_tab));
tmp = *tab_list;
i[0] = -1;
while (++i[0] < l[0] - 1)
{
p[0].z = ft_atoi(tmp->tab[i[0]]);
p[1].z = ft_atoi((tmp->next)->tab[i[0]]);
p[0].x = (50 + (float)(l[1] + i[0] - i[1]) * 1.732 * (750 / (lh)));
p[0].y = (75 + (i[1] + i[0]) * (750 / (lh)) - p[0].z);
p[1].x = (50 + (float)(l[1] + i[0] - i[1] - 1) * 1.732 * (750 / (lh)));
p[1].y = (75 + (i[1] + 1 + i[0]) * (750 / (lh)) - p[1].z);
ft_draw_line(p[0], p[1], *tmp->env);
p[0].x = (50 + (float)(l[1] + i[0] - i[1]) * 1.732 * (750 / (lh)));
p[0].y = (75 + (i[1] + i[0]) * (750 / (lh)) - p[0].z);
p[1].x = (50 + (float)(l[1] + i[0] - i[1] + 1) * 1.732 * (750 / (lh)));
p[1].z = ft_atoi(tmp->tab[i[0] + 1]);
p[1].y = (75 + (i[1] + 1 + i[0]) * (750 / (lh)) - p[1].z);
ft_draw_line(p[0], p[1], *tmp->env);
}
draw_final_line_v(&tmp, p, i, l);
return (++i[1]);
}
void draw_image(t_tab **tab_list)
{
t_tab *tmp;
t_pxl p[2];
size_t i[2];
size_t l[3];
tmp = malloc(sizeof(t_tab));
tmp = *tab_list;
i[1] = 0;
while (tmp->next != NULL)
{
l[0] = 0;
while (tmp->tab[l[0]] != '\0')
l[0]++;
p[0].len = ft_get_infos(tab_list);
p[1].len = ft_get_infos(tab_list);
l[2] = p[0].len->lmax;
l[1] = p[0].len->height;
i[1] = ft_draw(&tmp, p, i, l);
tmp = tmp->next;
}
draw_final_line_h(&tmp, p, i, l);
}
int key_hook(int keycode)
{
if (keycode == 65307)
exit(0);
return (0);
}
int expose_hook(t_tab **tab_list)
{
draw_image(tab_list);
return (0);
}
int main(int ac, char **av)
{
t_env env;
t_tab *tab_list;
t_len *len;
if ((ac != 2) || ((tab_list = ft_read_file(av[1], &env)) == 0))
{
ft_putendl("Error.");
ft_putendl("Usage: ./fdf <fichier>");
return (0);
}
len = ft_get_infos(&tab_list);
env.mlx = mlx_init();
env.win = mlx_new_window(env.mlx, len->lmax * 80, len->height * 88, "FdF");
expose_hook(&tab_list);
mlx_key_hook(env.win, key_hook, &env);
mlx_loop(env.mlx);
return (0);
}
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* fdf.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thoraffr <thoraffr@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/12/17 13:23:42 by thoraffr #+# #+# */
/* Updated: 2013/12/22 11:45:59 by thoraffr ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FDF_H
# define FDF_H
#include <mlx.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include "libft/libft.h"
#define BUFF_SIZE 1000
typedef struct s_read
{
int size;
int index;
char *read;
int fd;
struct s_read *next;
} t_read;
typedef struct s_len
{
size_t lmax;
size_t height;
} t_len;
typedef struct s_pxl
{
float x;
float y;
float z;
struct s_len *len;
} t_pxl;
typedef struct s_tab
{
char **tab;
struct s_env *env;
struct s_tab *next;
} t_tab;
typedef struct s_env
{
void *mlx;
void *win;
} t_env;
t_tab *ft_read_file(char *str, t_env *env);
int gnl(int fd, char **line);
void ft_list_push_back(t_tab **begin_list, t_env *env, char **tab);
t_tab *ft_create_elem(t_env *env, char **tab);
int key_hook(int keycode);
int expose_hook(t_tab **tab_list);
void draw_image(t_tab **tab_list);
int ft_draw(t_tab **tmp, t_pxl *p, size_t *i, size_t *l);
t_len *ft_get_infos(t_tab **tab_list);
void ft_draw_line(t_pxl p1, t_pxl p2, t_env env);
void ft_define_max(t_pxl p1, t_pxl p2, t_pxl *max, t_pxl *min);
void ft_draw_equal(t_pxl min, t_pxl max, t_env env);
void ft_draw_line_xsup(t_pxl max, t_pxl p1, t_pxl p2, t_env env);
void ft_draw_line_ysup(t_pxl max, t_pxl p1, t_pxl p2, t_env env);
void draw_final_line_h(t_tab **tab_list, t_pxl *p, size_t *i, size_t *l);
void draw_final_line_v(t_tab **tab_list, t_pxl *p, size_t *i, size_t *l);
#endif /* !FDF_H */
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thoraffr <thoraffr@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/11/21 11:11:44 by thoraffr #+# #+# */
/* Updated: 2013/11/26 16:10:20 by thoraffr ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_power(int nb, unsigned int p)
{
int buffer;
if (p == 0)
return (1);
buffer = nb;
while (p > 1)
{
buffer = buffer * nb;
p--;
}
return (buffer);
}
static int ft_nbr_len(const char *str)
{
int i;
if (!str)
return (0);
i = 0;
while (str[i] >= '0' && str[i] <= '9')
i++;
return (i);
}
static int ft_additionner(const char *str, int sign)
{
int i;
int ret;
int len;
i = 0;
ret = 0;
len = ft_nbr_len(str);
while (i < len)
{
ret = ret + sign * (str[len - 1 - i] - '0') * ft_power(10, i);
i++;
}
return (ret);
}
int ft_atoi(const char *str)
{
int i;
if (!str)
return (0);
i = 0;
while (((str[i] >= 9) && (str[i] <= 13)) || (str[i] == 32))
i++;
if ((str[i] < '0' || str[i] > '9') && str[i] != '-' && str[i] != '+')
return (0);
if (str[i] == '-')
return (ft_additionner(str + i + 1, -1));
if (str[i] == '+')
return (ft_additionner(str + i + 1, 1));
return (ft_additionner(str + i, 1));
}
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_bzero.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thoraffr <thoraffr@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/11/22 11:18:25 by thoraffr #+# #+# */
/* Updated: 2013/12/03 11:20:15 by thoraffr ### ########.fr */
/* */
/* ************************************************************************** */
#include <string.h>
#include "libft.h"
void ft_bzero(void *s, size_t n)
{
unsigned char *mem;
mem = s;
if (n == 0)
return ;
else
{
while (n > 0)
{
n--;
mem[n] = 0;
}
}
}
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_draw.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thoraffr <thoraffr@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/12/21 11:50:03 by thoraffr #+# #+# */
/* Updated: 2013/12/21 12:12:29 by thoraffr ### ########.fr */
/* */
/* ************************************************************************** */
#include "fdf.h"
void ft_draw_line(t_pxl p1, t_pxl p2, t_env env)
{
t_pxl max;
t_pxl min;
ft_define_max(p1, p2, &max, &min);
if (max.x == min.x || max.y == min.y)
ft_draw_equal(min, max, env);
else if (max.x - min.x >= max.y - min.y)
{
max.y = min.x;
ft_draw_line_xsup(max, p1, p2, env);
}
else
{
max.x = min.y;
ft_draw_line_ysup(max, p1, p2, env);
}
}
void ft_define_max(t_pxl p1, t_pxl p2, t_pxl *max, t_pxl *min)
{
if (p1.x < p2.x)
{
(*min).x = p1.x;
(*max).x = p2.x;
}
else
{
(*min).x = p2.x;
(*max).x = p1.x;
}
if (p1.y < p2.y)
{
(*min).y = p1.y;
(*max).y = p2.y;
}
else
{
(*min).y = p2.y;
(*max).y = p1.y;
}
}
void ft_draw_equal(t_pxl min, t_pxl max, t_env env)
{
int loop;
if (max.x == min.x)
{
loop = min.y;
while (loop <= max.y)
{
mlx_pixel_put(env.mlx, env.win, min.x, loop, 0xFFFFFF * ((max.z * 12) + 1));
++loop;
}
}
else
{
loop = min.x;
while (loop <= max.x)
{
mlx_pixel_put(env.mlx, env.win, loop, min.y, 0xFFFFFF * ((min.z * 12) + 1));
++loop;
}
}
}
void ft_draw_line_xsup(t_pxl max, t_pxl p1, t_pxl p2, t_env env)
{
float a;
float b;
int j;
int jj;
int i;
a = (float)(p1.y - p2.y) / ((float)(p1.x - p2.x));
b = p1.y - a * p1.x - 1;
i = max.y;
while (i <= max.x)
{
jj = a * i + b;
j = jj;
++j;
mlx_pixel_put(env.mlx, env.win, i, j, 0xFFFFFF * ((p1.z * 12) + 1));
++i;
}
}
void ft_draw_line_ysup(t_pxl max, t_pxl p1, t_pxl p2, t_env env)
{
float a;
float b;
int i;
int ii;
int j;
a = (float)(p1.y - p2.y) / ((float)(p1.x - p2.x));
b = p1.y - a * p1.x - 1;
j = max.x;
while (j <= max.y)
{
ii = (j - b) / a;
i = ii;
++i;
mlx_pixel_put(env.mlx, env.win, i, j, 0xFFFFFF * ((p1.z * 12) + 1));
++j;
}
}
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_draw_final_lines.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thoraffr <thoraffr@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/12/22 11:41:31 by thoraffr #+# #+# */
/* Updated: 2013/12/22 12:05:13 by thoraffr ### ########.fr */
/* */
/* ************************************************************************** */
#include "fdf.h"
void draw_final_line_h(t_tab **tab_list, t_pxl *p, size_t *i, size_t *l)
{
t_tab *tmp;
size_t lh;
lh = l[2] + l[1];
tmp = malloc(sizeof(t_tab));
tmp = *tab_list;
while (tmp->next != NULL)
tmp = tmp->next;
i[0] = -1;
while (++i[0] < l[0] - 1)
{
p[0].x = (50 + (float)(l[1] + i[0] - i[1]) * 1.732 * (750 / (lh)));
p[0].y = (75 + (i[1] + i[0]) * (750 / (lh)) - p[0].z);
p[1].x = (50 + (float)(l[1] + i[0] - i[1] + 1) * 1.732 * (750 / (lh)));
p[1].z = ft_atoi(tmp->tab[i[0] + 1]);
p[1].y = (75 + (i[1] + 1 + i[0]) * (750 / (lh)) - p[1].z);
ft_draw_line(p[0], p[1], *tmp->env);
}
}
void draw_final_line_v(t_tab **tab_list, t_pxl *p, size_t *i, size_t *l)
{
t_tab *tmp;
size_t lh;
lh = l[2] + l[1];
tmp = malloc(sizeof(t_tab));
tmp = *tab_list;
p[0].x = (50 + (float)(l[1] + i[0] - i[1]) * 1.732 * (750 / (lh)));
p[0].y = (75 + (i[1] + i[0]) * (750 / (lh)) - p[0].z);
p[1].x = (50 + (float)(l[1] + i[0] - i[1] - 1) * 1.732 * (750 / (lh)));
p[1].y = (75 + (i[1] + 1 + i[0]) * (750 / (lh)) - p[1].z);
ft_draw_line(p[0], p[1], *tmp->env);
}
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isalnum.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thoraffr <thoraffr@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/11/19 17:11:52 by thoraffr #+# #+# */
/* Updated: 2013/11/22 16:34:08 by thoraffr ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isalnum(int c)
{
if ((ft_isalpha(c)) || (ft_isdigit(c)))
return (1);
else
return (0);
}
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isalpha.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thoraffr <thoraffr@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/11/19 17:11:52 by thoraffr #+# #+# */
/* Updated: 2013/11/22 16:10:38 by thoraffr ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isalpha(int c)
{
if (((c >= 65) && (c <= 90)) || ((c >= 97) && (c <= 122)))
return (1);
else
return (0);
}
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isascii.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thoraffr <thoraffr@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/11/21 10:30:27 by thoraffr #+# #+# */
/* Updated: 2013/11/22 16:10:47 by thoraffr ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isascii(int c)
{
if ((c >= 0) && (c <= 127))
return (1);
else
return (0);
}
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isdigit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thoraffr <thoraffr@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/11/19 15:18:29 by thoraffr #+# #+# */
/* Updated: 2013/11/22 16:11:00 by thoraffr ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isdigit(int c)
{
if ((c > 47) && (c < 58))
return (1);
else
return (0);
}
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isprint.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thoraffr <thoraffr@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/11/19 16:24:45 by thoraffr #+# #+# */
/* Updated: 2013/11/22 16:11:07 by thoraffr ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isprint(int c)
{
if ((c >= 32) && (c < 127))
return (1);
else
return (0);
}
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thoraffr <thoraffr@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/11/27 13:52:55 by thoraffr #+# #+# */
/* Updated: 2013/11/28 11:17:09 by thoraffr ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_nbrlen(int n, int sign)
{
int i;
i = 0;
while (n > 0)
{
n /= 10;
i++;
}
return (i + sign);
}
char *ft_itoa(int n)
{
int i;
int sign;
int end;
char *nbr;
sign = 0;
nbr = (char *) malloc(sizeof(char) * 12);
if (n == 0)
return (nbr = ft_strcpy(nbr, "0"));
if (n < 0)
{
nbr[0] = '-';
n *= -1;
sign = 1;
}
i = ft_nbrlen(n, sign);
end = i;
while (n > 0)
{
nbr[i - 1] = (n % 10) + '0';
n /= 10;
i--;
}
nbr[end] = '\0';
return (nbr);
}
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memalloc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thoraffr <thoraffr@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/11/27 15:28:32 by thoraffr #+# #+# */
/* Updated: 2013/12/10 12:45:55 by thoraffr ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include <string.h>
#include "libft.h"
void *ft_memalloc(size_t size)
{
void *loc;
if ((loc = malloc(size)) == NULL)
return (NULL);
ft_memset(loc, 0, size);
return (loc);
}
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memccpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thoraffr <thoraffr@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/11/22 14:04:42 by thoraffr #+# #+# */
/* Updated: 2013/11/26 16:13:53 by thoraffr ### ########.fr */
/* */
/* ************************************************************************** */
#include <string.h>
#include <stdlib.h>
#include "libft.h"
static int test_null(void *s1, const void *s2)
{
if ((s1 == NULL) || (s2 == NULL))
return (0);
else
return (1);
}
void *ft_memccpy(void *s1, const void *s2, int c, size_t n)
{
char *str1;
const char *str2;
unsigned int i;
if (test_null(s1, s2) == 0)
return (s1);
i = 0;
str1 = s1;
str2 = s2;
while (i < n)
{
*(str1 + i) = *(str2 + i);
if (*(str2 + i) == (unsigned char) c)
return (s1 + i + 1);
i++;
}
return (NULL);
}
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thoraffr <thoraffr@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/11/20 18:18:18 by thoraffr #+# #+# */
/* Updated: 2013/11/25 16:43:30 by thoraffr ### ########.fr */
/* */
/* ************************************************************************** */
#include <string.h>
#include "libft.h"
void *ft_memchr(const void *s, int c, size_t n)
{
size_t i;
unsigned char *str;
unsigned char *ret;
i = 0;
str = (unsigned char *) s;
while (i < n)
{
if (str[i] == (unsigned char) c)
{
ret = (str + i);
return (ret);
}
i++;
}
return (NULL);
}
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memcmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thoraffr <thoraffr@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/11/22 13:17:43 by thoraffr #+# #+# */
/* Updated: 2013/12/12 15:27:40 by thoraffr ### ########.fr */
/* */
/* ************************************************************************** */
#include <string.h>
#include "libft.h"
static int test_null(const void *s1, const void *s2, size_t n)
{
if ((s1 == NULL) || (s2 == NULL) || (n == 0))
return (0);
else
return (1);
}
int ft_memcmp(const void *str1, const void *str2, size_t n)
{
size_t i;
unsigned char *s1;
unsigned char *s2;
if (test_null(str1, str2, n) == 0)
return (0);
i = 0;
s1 = (unsigned char *) str1;
s2 = (unsigned char *) str2;
while (i < n)
{
if (*(s1 + i) != *(s2 + i))
return (*(s1 + i) - *(s2 + i));
i++;
}
return (0);
}
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memcpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thoraffr <thoraffr@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/11/22 14:07:40 by thoraffr #+# #+# */
/* Updated: 2013/11/23 11:57:00 by thoraffr ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "libft.h"
static int test_null(void *s1, const void *s2, size_t n)
{
if ((s1 == NULL) || (s2 == NULL) || (n == 0))
return (0);
else
return (1);
}
void *ft_memcpy(void *s1, const void *s2, size_t n)
{
char *tmp;
const char *tmp2;
unsigned int i;
if (test_null(s1, s2, n) == 0)
return (s1);
i = 0;
tmp = s1;
tmp2 = s2;
if (s1 > s2)
{
while (n-- != '\0')
*(tmp + n) = *(tmp2 + n);
}
else
{
while (i < n)
{
*(tmp + i) = *(tmp2 + i);
i++;
}
}
return (s1);
}
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memdel.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thoraffr <thoraffr@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/11/27 15:49:57 by thoraffr #+# #+# */
/* Updated: 2013/11/27 16:27:13 by thoraffr ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "libft.h"
void ft_memdel(void **ap)
{
free(*ap);
*ap = NULL;
}
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memmove.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thoraffr <thoraffr@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/11/22 14:09:26 by thoraffr #+# #+# */
/* Updated: 2013/11/25 15:09:28 by thoraffr ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "libft.h"
void *ft_memmove(void *s1, const void *s2, size_t n)
{
char *dest;
const char *src;
char *swap;
unsigned int i;
i = 0;
dest = s1;
src = s2;
swap = (char *) malloc(sizeof(char) * (n + 1));
while (i < n)
{
*(swap + i) = *(src + i);
i++;
}
swap[i] = '\0';
i = 0;
while (*(swap + i) != 0)
{
*(dest + i) = *(swap + i);
i++;
}
if (i != n)
*(dest + i) = '\0';
free(swap);
return (s1);
}
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memset.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thoraffr <thoraffr@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/11/22 11:05:04 by thoraffr #+# #+# */
/* Updated: 2013/11/22 16:12:02 by thoraffr ### ########.fr */
/* */
/* ************************************************************************** */
#include <string.h>
#include "libft.h"
void *ft_memset(void *b, int c, size_t len)
{
unsigned char *mem;
mem = b;
while (len > 0)
{
len--;
mem[len] = c;
}
return (b);
}
#include "ft_minishell1.h"
int main(int ac, char **av, char **env)
{
int i;
printf("Nombre d'arguments : %d\n", ac);
i = -1;
while (++i < ac)
printf("Argument %d : %s\n", i, av[i]);
i = -1;
while (env[++i] != NULL)
printf("Environnement %d : %s\n", i, env[i]);
return (0);
}
#ifndef FT_MINISHELL1_H
# define FT_MINISHELL1_H
#include <stdio.h>
#include <string.h>
#endif /* !FT_MINISHELL1_H */
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putchar.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thoraffr <thoraffr@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/11/22 15:12:41 by thoraffr #+# #+# */
/* Updated: 2013/11/22 16:12:08 by thoraffr ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include "libft.h"
void ft_putchar(char c)
{
write(1, &c, 1);
}
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putchar_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thoraffr <thoraffr@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/11/27 13:16:57 by thoraffr #+# #+# */
/* Updated: 2013/11/27 13:18:30 by thoraffr ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include "libft.h"
void ft_putchar_fd(char c, int fd)
{
write(fd, &c, 1);
}
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putendl.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thoraffr <thoraffr@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/11/22 15:39:43 by thoraffr #+# #+# */
/* Updated: 2013/11/22 16:12:13 by thoraffr ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include "libft.h"
void ft_putendl(const char *s)
{
int i;
i = 0;
while (s[i] != '\0')
{
write(1, &s[i], 1);
i++;
}
write(1, "\n", 1);
}
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putendl_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thoraffr <thoraffr@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/11/27 13:17:04 by thoraffr #+# #+# */
/* Updated: 2013/11/27 13:18:15 by thoraffr ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include "libft.h"
void ft_putendl_fd(const char *s, int fd)
{
int i;
i = 0;
while (s[i] != '\0')
{
write(fd, &s[i], 1);
i++;
}
write(fd, "\n", 1);
}
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thoraffr <thoraffr@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/11/27 11:22:51 by thoraffr #+# #+# */
/* Updated: 2013/11/27 13:12:27 by thoraffr ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include "libft.h"
void ft_putnbr(int n)
{
int i;
if (n < 0)
{
write(1, "-", 1);
n *= -1;
}
if (n < 10)
{
n += 48;
write(1, &n, 1);
}
else
{
i = n % 10;
n /= 10;
ft_putnbr(n);
i += 48;
write(1, &i, 1);
}
}
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thoraffr <thoraffr@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/11/27 13:17:13 by thoraffr #+# #+# */
/* Updated: 2013/11/27 14:45:01 by thoraffr ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include "libft.h"
void ft_putnbr_fd(int n, int fd)
{
int i;
if (n < 0)
{
write(fd, "-", 1);
n *= -1;
}
if (n < 10)
{
n += 48;
write(fd, &n, 1);
}
else
{
i = n % 10;
n /= 10;
ft_putnbr_fd(n, fd);
i += 48;
write(fd, &i, 1);
}
}
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thoraffr <thoraffr@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/11/22 15:14:50 by thoraffr #+# #+# */
/* Updated: 2013/11/22 16:12:20 by thoraffr ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include "libft.h"
void ft_putstr(const char *s)
{
int i;
i = 0;
while (s[i] != '\0')
{
write(1, &s[i], 1);
i++;
}
}
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putstr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thoraffr <thoraffr@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/11/27 13:17:21 by thoraffr #+# #+# */
/* Updated: 2013/11/27 13:17:50 by thoraffr ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include "libft.h"
void ft_putstr_fd(const char *s, int fd)
{
int i;
i = 0;
while (s[i] != '\0')
{
write(fd, &s[i], 1);
i++;
}
}
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_str_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thoraffr <thoraffr@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/12/17 18:08:02 by thoraffr #+# #+# */
/* Updated: 2013/12/18 13:45:11 by thoraffr ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static void goto_next(char **s, char c)
{
while (**s == c)
(*s)++;
}
static int count_strings(char *s, char c)
{
char *temp;
int count;
temp = s;
count = 1;
while (*temp)
{
if (*temp == c && *(temp + 1) != c && *(temp + 1))
count++;
temp++;
}
return (count);
}
static int string_length(char *s, char c)
{
char *temp;
int count;
temp = s;
count = 0;
while (*temp && *temp++ != c)
count++;
return (count);
}
char **ft_str_split(char const *s, char c)
{
char **ret;
char **ret_backup;
char *work;
int i[2];
work = (char *)s;
if (*work == c)
goto_next(&work, c);
i[0] = count_strings(work, c);
ret = (char **)malloc((i[0] + 1) * sizeof(char *));
if (ret == NULL)
return (NULL);
ret[i[0]] = '\0';
ret_backup = ret;
while (i[0]--)
{
i[1] = string_length(work, c);
if (i[1] == 0)
*ret_backup++ = NULL;
else
*ret_backup++ = ft_strsub(work, 0, i[1]);
work += i[1];
goto_next(&work, c);
}
return (ret);
}
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strcat.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thoraffr <thoraffr@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/11/20 10:34:08 by thoraffr #+# #+# */
/* Updated: 2013/11/22 16:12:30 by thoraffr ### ########.fr */
/* */
/* ************************************************************************** */
#include <string.h>
#include "libft.h"
char *ft_strcat(char *dest, const char *src)
{
int i;
int j;
i = 0;
j = 0;
while (dest[i] != '\0')
i++;
while (src[j] != '\0')
{
dest[i] = src[j];
i++;
j++;
}
dest[i] = '\0';
return (dest);
}
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thoraffr <thoraffr@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/11/20 18:18:18 by thoraffr #+# #+# */
/* Updated: 2013/11/26 16:19:53 by thoraffr ### ########.fr */
/* */
/* ************************************************************************** */
#include <string.h>
#include "libft.h"
char *ft_strchr(const char *str, int c)
{
int i;
char *dest;
i = 0;
while (str[i] != c)
{
if (str[i] == '\0')
return (NULL);
else
i++;
}
dest = (char *) (str + i);
return (dest);
}
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strclr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thoraffr <thoraffr@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/11/22 15:17:38 by thoraffr #+# #+# */
/* Updated: 2013/11/28 10:59:36 by thoraffr ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include "libft.h"
void ft_strclr(char *s)
{
size_t i;
i = ft_strlen(s);
while (i > 0)
{
i--;
s[i] = '\0';
}
}
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strcmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thoraffr <thoraffr@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/11/20 17:23:09 by thoraffr #+# #+# */
/* Updated: 2013/11/25 20:08:56 by thoraffr ### ########.fr */
/* */
/* ************************************************************************** */
#include <string.h>
#include "libft.h"
int ft_strcmp(const char *s1, const char *s2)
{
int i;
i = 0;
while (s1[i] == s2[i])
{
if ((s1[i] == '\0') && (s2[i] == '\0'))
break ;
else
i++;
}
if (s1[i] > s2[i])
return (s1[i] - s2[i]);
else if (s1[i] < s2[i])
return (s1[i] - s2[i]);
return (0);
}
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strcpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thoraffr <thoraffr@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/11/19 17:38:29 by thoraffr #+# #+# */
/* Updated: 2013/11/26 16:21:30 by thoraffr ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strcpy(char *dest, const char *src)
{
int i;
i = 0;
while (src[i] != '\0')
{
dest[i] = src[i];
i++;
}
dest[i] = src[i];
return (dest);
}
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strdel.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thoraffr <thoraffr@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/11/27 15:19:50 by thoraffr #+# #+# */
/* Updated: 2013/11/27 15:53:15 by thoraffr ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "libft.h"
void ft_strdel(char **as)
{
free(*as);
*as = NULL;
}
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strdup.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thoraffr <thoraffr@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/11/21 17:49:03 by thoraffr #+# #+# */
/* Updated: 2013/11/26 16:22:20 by thoraffr ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "libft.h"
char *ft_strdup(const char *s1)
{
int i;
int j;
char *dest;
i = 0;
j = 0;
while (s1[i] != '\0')
i++;
if ((dest = (char*) malloc(sizeof(*dest) * i + 1)) == NULL)
return (NULL);
while (s1[j] != '\0')
{
dest[j] = s1[j];
j++;
}
dest[j] = '\0';
return (dest);
}
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strequ.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thoraffr <thoraffr@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/11/27 13:26:44 by thoraffr #+# #+# */
/* Updated: 2013/11/27 13:28:13 by thoraffr ### ########.fr */
/* */
/* ************************************************************************** */
#include <string.h>
#include "libft.h"
int ft_strequ(const char *s1, const char *s2)
{
int i;
i = 0;
while (s1[i] == s2[i])
{
if ((s1[i] == '\0') && (s2[i] == '\0'))
break ;
else
i++;
}
if (s1[i] > s2[i])
return (0);
else if (s1[i] < s2[i])
return (0);
return (1);
}
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_striter.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thoraffr <thoraffr@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/11/29 13:14:04 by thoraffr #+# #+# */
/* Updated: 2013/11/29 13:15:57 by thoraffr ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_striter(char *s, void (*f)(char *))
{
int i;
i = 0;
while (s[i] != '\0')
{
f(&(s[i]));
i++;
}
}
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_striteri.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thoraffr <thoraffr@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/11/29 13:20:13 by thoraffr #+# #+# */
/* Updated: 2013/11/29 13:22:08 by thoraffr ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_striteri(char *s, void (*f)(unsigned int, char *))
{
unsigned int i;
i = 0;
while (s[i] != '\0')
{
f(i, &(s[i]));
i++;
}
}
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strjoin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thoraffr <thoraffr@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/11/27 16:10:12 by thoraffr #+# #+# */
/* Updated: 2013/12/10 12:47:56 by thoraffr ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "libft.h"
char *ft_strjoin(char const *s1, char const *s2)
{
char *str;
int i;
int j;
i = 0;
j = 0;
if ((str = (char *)malloc(sizeof(char) * (ft_strlen(s1)
+ ft_strlen(s2) + 1))) == NULL)
return (NULL);
while (s1[i] != '\0')
{
str[i] = s1[i];
i++;
}
while (s2[j] != '\0')
{
str[i + j] = s2[j];
j++;
}
str[i + j] = '\0';
return (str);
}
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlcat.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thoraffr <thoraffr@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/11/22 10:32:53 by thoraffr #+# #+# */
/* Updated: 2013/12/03 11:21:19 by thoraffr ### ########.fr */
/* */
/* ************************************************************************** */
#include <string.h>
#include "libft.h"
size_t ft_strlcat(char *dest, const char *src, size_t n)
{
size_t i;
size_t j;
size_t k;
i = 0;
j = 0;
k = 0;
while (dest[i] && i < n)
i++;
while (src[k])
k++;
while ((src[j]) && ((i + j + 1) < n))
{
dest[i + j] = src[j];
j++;
}
if (i != n)
dest[i + j] = '\0';
return (i + k);
}
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlen.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thoraffr <thoraffr@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/11/19 14:11:07 by thoraffr #+# #+# */
/* Updated: 2013/12/03 11:21:01 by thoraffr ### ########.fr */
/* */
/* ************************************************************************** */
#include <string.h>
#include "libft.h"
size_t ft_strlen(const char *str)
{
size_t i;
i = 0;
while (str[i] != '\0')
i++;
return (i);
}
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strmap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thoraffr <thoraffr@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/11/29 13:30:21 by thoraffr #+# #+# */
/* Updated: 2013/11/29 13:35:51 by thoraffr ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strmap(char const *s, char (*f)(char))
{
int i;
char *str;
i = 0;
while (s[i] != '\0')
i++;
str = (char *) malloc(sizeof(char) * (i + 1));
str[i] = '\0';
i = 0;
while (s[i] != '\0')
{
str[i] = f(s[i]);
i++;
}
return (str);
}
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strmapi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thoraffr <thoraffr@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/11/29 13:36:20 by thoraffr #+# #+# */
/* Updated: 2013/11/29 13:37:08 by thoraffr ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strmapi(char const *s, char (*f)(unsigned int, char))
{
unsigned int i;
char *str;
i = 0;
while (s[i] != '\0')
i++;
str = (char *) malloc(sizeof(char) * (i + 1));
str[i] = '\0';
i = 0;
while (s[i] != '\0')
{
str[i] = f(i, s[i]);
i++;
}
return (str);
}
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strncat.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thoraffr <thoraffr@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/11/20 11:07:45 by thoraffr #+# #+# */
/* Updated: 2013/11/26 13:32:10 by thoraffr ### ########.fr */
/* */
/* ************************************************************************** */
#include <string.h>
#include "libft.h"
char *ft_strncat(char *s1, const char *s2, size_t n)
{
int i;
size_t j;
i = 0;
j = 0;
while (s1[i] != '\0')
i++;
while ((j < n) && (s2[j] != '\0'))
{
s1[i] = s2[j];
i++;
j++;
}
s1[i] = '\0';
return (s1);
}
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strncmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thoraffr <thoraffr@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/11/20 17:23:09 by thoraffr #+# #+# */
/* Updated: 2013/11/25 20:16:56 by thoraffr ### ########.fr */
/* */
/* ************************************************************************** */
#include <string.h>
#include "libft.h"
int ft_strncmp(const char *s1, const char *s2, size_t n)
{
size_t i;
i = 0;
while ((i < (n - 1)) && (s1[i] == s2[i]))
{
if ((s1[i] == '\0') && (s2[i] == '\0'))
break ;
else
i++;
}
if (s1[i] > s2[i])
return (s1[i] - s2[i]);
else if (s1[i] < s2[i])
return (s1[i] - s2[i]);
return (0);
}
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strncpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thoraffr <thoraffr@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/11/19 17:38:29 by thoraffr #+# #+# */
/* Updated: 2013/11/23 13:58:38 by thoraffr ### ########.fr */
/* */
/* ************************************************************************** */
#include <string.h>
#include "libft.h"
char *ft_strncpy(char *dest, const char *src, size_t n)
{
size_t i;
i = 0;
while (i < n)
{
if (src[i] != '\0')
{
dest[i] = src[i];
i++;
}
else
{
while (i < n)
{
dest[i] = '\0';
i++;
}
}
}
return (dest);
}
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnequ.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thoraffr <thoraffr@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/11/27 13:26:51 by thoraffr #+# #+# */
/* Updated: 2013/11/27 13:27:48 by thoraffr ### ########.fr */
/* */
/* ************************************************************************** */
#include <string.h>
#include "libft.h"
int ft_strnequ(const char *s1, const char *s2, size_t n)
{
size_t i;
i = 0;
while ((i < (n - 1)) && (s1[i] == s2[i]))
{
if ((s1[i] == '\0') && (s2[i] == '\0'))
break ;
else
i++;
}
if (s1[i] > s2[i])
return (0);
else if (s1[i] < s2[i])
return (0);
return (1);
}
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnew.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thoraffr <thoraffr@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/11/27 14:50:44 by thoraffr #+# #+# */
/* Updated: 2013/11/28 11:30:01 by thoraffr ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdlib.h>
#include <string.h>
char *ft_strnew(size_t size)
{
char *str;
size_t i;
i = 0;
if ((str = (char *) malloc(sizeof(char) * (size + 1))) == NULL)
return (NULL);
while (i < size)
{
str[i] = '\0';
i++;
}
str[size + 1] = '\0';
return (str);
}
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thoraffr <thoraffr@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/11/21 14:13:53 by thoraffr #+# #+# */
/* Updated: 2013/11/26 16:24:25 by thoraffr ### ########.fr */
/* */
/* ************************************************************************** */
#include <string.h>
#include "libft.h"
char *ft_strnstr(const char *s1, const char *s2, size_t n)
{
size_t i;
size_t j;
size_t t;
i = 0;
t = n;
if (s2[i] == '\0')
return ((char *) s1);
while (n > i && s1[i])
{
j = 0;
while (s2[j] == s1[i + j] && s2[j] && i + j < n)
j++;
if (s2[j] == '\0')
return ((char *) &s1[i]);
i++;
}
return (NULL);
}
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strrchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thoraffr <thoraffr@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/11/20 18:18:18 by thoraffr #+# #+# */
/* Updated: 2013/11/26 16:25:13 by thoraffr ### ########.fr */
/* */
/* ************************************************************************** */
#include <string.h>
#include "libft.h"
char *ft_strrchr(const char *str, int c)
{
int i;
char *dest;
i = 0;
while (str[i] != '\0')
i++;
while (str[i] != c)
{
if (i == 0)
return (NULL);
else
i--;
}
dest = (char *) (str + i);
return (dest);
}
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thoraffr <thoraffr@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/11/21 14:13:53 by thoraffr #+# #+# */
/* Updated: 2013/11/26 13:42:02 by thoraffr ### ########.fr */
/* */
/* ************************************************************************** */
#include <string.h>
#include "libft.h"
char *ft_strstr(const char *s1, const char *s2)
{
int i;
i = 0;
if (s2[i] == '\0')
return ((char *) s1);
if (ft_strlen(s2) > ft_strlen(s1))
return (NULL);
while (s1[i] != '\0')
{
if (!(ft_strncmp(&s1[i], s2, ft_strlen(s2))))
return ((char *) (s1 + i));
i++;
}
return (NULL);
}
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strsub.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thoraffr <thoraffr@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/11/27 17:26:35 by thoraffr #+# #+# */
/* Updated: 2013/11/28 14:58:29 by thoraffr ### ########.fr */
/* */
/* ************************************************************************** */
#include <string.h>
#include <stdlib.h>
#include "libft.h"
char *ft_strsub(char const *s, unsigned int start, size_t len)
{
char *str;
unsigned int i;
i = start;
if ((str = (char *) malloc(sizeof(char) * (len + 1))) == NULL)
return (NULL);
while (i < len + start)
{
str[i - start] = s[i];
i++;
}
str[i - start] = '\0';
return (str);
}
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thoraffr <thoraffr@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/11/27 18:15:09 by thoraffr #+# #+# */
/* Updated: 2013/11/28 14:47:02 by thoraffr ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "libft.h"
#include <stdio.h>
char *ft_strtrim(char const *s)
{
int i;
int j;
int k;
char *str;
i = 0;
k = 0;
while ((s[i] == 9) || (s[i] == 10) || (s[i] == 32))
i++;
j = ft_strlen(s) - 1;
while ((s[j] == 9) || (s[j] == 10) || (s[j] == 32))
j--;
if (j - i < 0)
return (ft_strdup("\0"));
if ((str = (char *) malloc(sizeof(char) * (j - i + 1))) == NULL)
return (NULL);
while (j >= i)
{
str[k] = s[i];
i++;
k++;
}
str[k] = '\0';
return (str);
}
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_tolower.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thoraffr <thoraffr@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/11/19 16:53:18 by thoraffr #+# #+# */
/* Updated: 2013/11/22 16:14:12 by thoraffr ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_tolower(int c)
{
if ((c >= 65) && (c <= 90))
c += 32;
return (c);
}
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_toupper.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thoraffr <thoraffr@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/11/19 16:53:18 by thoraffr #+# #+# */
/* Updated: 2013/11/22 16:14:30 by thoraffr ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_toupper(int c)
{
if ((c >= 97) && (c <= 122))
c -= 32;
return (c);
}
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* gnl.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thoraffr <thoraffr@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/12/14 12:36:16 by thoraffr #+# #+# */
/* Updated: 2013/12/22 12:12:48 by thoraffr ### ########.fr */
/* */
/* ************************************************************************** */
#include "fdf.h"
static t_read *ft_freeread(t_read *red, t_read *prev, t_read **start)
{
if (!prev)
*start = red->next;
else
prev->next = red->next;
free(red->read);
free(red);
if (!prev)
return (*start);
else
return (prev->next);
}
static t_read *ft_newread(int fd)
{
t_read *red;
void *tmp;
int ret;
if (!(red = (t_read *)malloc(sizeof(t_read))))
return (NULL);
if (!(tmp = malloc(sizeof(char) * BUFF_SIZE)))
{
free(red);
return (NULL);
}
if ((ret = read(fd, tmp, BUFF_SIZE)) < 0)
{
free(red);
free(tmp);
return (NULL);
}
red->read = (char *)tmp;
red->fd = fd;
red->size = ret;
red->next = NULL;
red->index = 0;
return (red);
}
static int ft_print(int n, t_read **redtmp, t_read **s, char **l)
{
char *tmpstr;
int index;
if (!redtmp[0])
return (-1);
index = redtmp[0]->index;
if (n == -1 || !(tmpstr = (char *)malloc(sizeof (char) * (n + 1))))
return (-1);
*l = tmpstr;
while (n--)
{
*tmpstr++ = redtmp[0]->read[index++];
if (index == redtmp[0]->size)
{
redtmp[0] = ft_freeread(redtmp[0], redtmp[1], s);
index = 0;
}
}
*tmpstr = 0;
if (!redtmp[0] || (index == redtmp[0]->size && redtmp[0]->size < BUFF_SIZE))
return (0);
redtmp[0]->index = index + 1;
if (redtmp[0]->index == redtmp[0]->size)
redtmp[0] = ft_freeread(redtmp[0], redtmp[1], s);
return (1);
}
static int ft_findendl(int fd, t_read *red)
{
int index;
int size;
t_read *tmplst;
size = 0;
index = red->index;
while (red->read[index] != '\n' && index < red->size)
{
index++;
size++;
if (index == red->size && red->size == BUFF_SIZE)
{
if (!(tmplst = ft_newread(fd)))
return (-1);
tmplst->next = red->next;
red->next = tmplst;
red = tmplst;
index = 0;
}
}
return (size);
}
int gnl(int fd, char **line)
{
static t_read *start = NULL;
t_read **redtmp;
redtmp = malloc(sizeof(t_read *) * 2);
if (fd < 0)
return (-1);
redtmp[1] = NULL;
if (!start)
start = ft_newread(fd);
redtmp[0] = start;
while (redtmp[0]->fd != fd)
{
if (!(redtmp[0]->next))
redtmp[0]->next = ft_newread(fd);
redtmp[1] = redtmp[0];
redtmp[0] = redtmp[0]->next;
}
if (!redtmp[0] || !start)
return (-1);
return (ft_print(ft_findendl(fd, redtmp[0]), redtmp, &start, line));
}
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* libft.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thoraffr <thoraffr@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/11/22 14:14:05 by thoraffr #+# #+# */
/* Updated: 2013/12/18 13:44:51 by thoraffr ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LIBFT_H
# define LIBFT_H
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
void ft_bzero(void *s, size_t n);
int ft_isalpha(int c);
int ft_isdigit(int c);
int ft_isalnum(int c);
int ft_isascii(int c);
int ft_isprint(int c);
void *ft_memcpy(void *s1, const void *s2, size_t n);
void *ft_memccpy(void *s1, const void *s2, int c, size_t n);
void *ft_memchr(const void *s, int c, size_t n);
int ft_memcmp(const void *str1, const void *str2, size_t n);
void *ft_memmove(void *s1, const void *s2, size_t n);
void *ft_memset(void *b, int c, size_t len);
char *ft_strcat(char *dest, const char *src);
char *ft_strchr(const char *str, int c);
int ft_strcmp(const char *s1, const char *s2);
char *ft_strcpy(char *dest, const char *src);
char *ft_strdup(const char *s1);
size_t ft_strlcat (char *dest, const char *src, size_t n);
char *ft_strnstr(const char *s1, const char *s2, size_t n);
char *ft_strrchr(const char *str, int c);
char *ft_strstr(const char *s1, const char *s2);
int ft_tolower(int c);
int ft_toupper(int c);
void ft_putchar(char c);
void ft_putendl(const char *s);
void ft_putstr(const char *s);
void ft_strclr(char *s);
char *ft_strncat(char *dest, const char *src, size_t n);
char *ft_strncpy(char *dest, const char *src, size_t n);
size_t ft_strlen(const char *str);
int ft_strncmp(const char *s1, const char *s2, size_t n);
int ft_atoi(const char *str);
void ft_putnbr(int n);
void ft_putchar_fd(char c, int fd);
void ft_putendl_fd(const char *s, int fd);
void ft_putstr_fd(const char *s, int fd);
void ft_putnbr_fd(int n, int fd);
int ft_strequ(const char *s1, const char *s2);
int ft_strnequ(const char *s1, const char *s2, size_t n);
char *ft_itoa(int n);
char *ft_strnew(size_t size);
void ft_strdel(char **as);
void *ft_memalloc(size_t size);
void ft_memdel(void **ap);
char *ft_strjoin(char const *s1, char const *s2);
char *ft_strsub(char const *s, unsigned int start, size_t len);
char *ft_strtrim(char const *s);
char **ft_str_split(char const *s, char c);
void ft_striter(char *s, void (*f)(char *));
void ft_striteri(char *s, void (*f)(unsigned int, char *));
char *ft_strmap(char const *s, char (*f)(char));
char *ft_strmapi(char const *s, char (*f)(unsigned int, char));
typedef struct s_list
{
void *content;
size_t content_size;
struct s_list *next;
} t_list;
#endif
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: thoraffr <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2013/08/26 14:19:42 by thoraffr #+# #+# #
# Updated: 2013/12/18 13:44:14 by thoraffr ### ########.fr #
# #
# **************************************************************************** #
CC = gcc
CFLAGS = -Wall -Wextra -Werror
NAME = libft.a
SRC = ft_atoi.c \
ft_bzero.c \
ft_isalpha.c \
ft_isascii.c \
ft_isalnum.c \
ft_isdigit.c \
ft_isprint.c \
ft_memccpy.c \
ft_memchr.c \
ft_memcmp.c \
ft_memcpy.c \
ft_memmove.c \
ft_memset.c \
ft_strcat.c \
ft_strchr.c \
ft_strcmp.c \
ft_strcpy.c \
ft_strdup.c \
ft_strlcat.c \
ft_strlen.c \
ft_strncpy.c \
ft_strncat.c \
ft_strncmp.c \
ft_strnstr.c \
ft_strrchr.c \
ft_strstr.c \
ft_tolower.c \
ft_toupper.c \
ft_putchar.c \
ft_putstr.c \
ft_putendl.c \
ft_strclr.c \
ft_putnbr.c \
ft_putchar_fd.c \
ft_putstr_fd.c \
ft_putendl_fd.c \
ft_putnbr_fd.c \
ft_strequ.c \
ft_strnequ.c \
ft_itoa.c \
ft_strnew.c \
ft_strdel.c \
ft_memalloc.c \
ft_memdel.c \
ft_strjoin.c \
ft_strsub.c \
ft_strtrim.c \
ft_str_split.c \
ft_striter.c \
ft_striteri.c \
ft_strmap.c \
ft_strmapi.c
OBJ = $(SRC:.c=.o)
all: $(NAME)
$(NAME): $(OBJ)
ar rc $@ $(OBJ)
ranlib $(NAME)
$(OBJ): $(SRC)
$(CC) $(CFLAGS) -c $^
clean:
rm -f $(OBJ)
fclean: clean
rm -f $(NAME)
re: fclean all
.PHONY: all clean fclean re
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* read.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thoraffr <thoraffr@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/12/17 16:50:19 by thoraffr #+# #+# */
/* Updated: 2013/12/22 11:47:58 by thoraffr ### ########.fr */
/* */
/* ************************************************************************** */
#include "fdf.h"
t_len *ft_get_infos(t_tab **tab_list)
{
t_tab *tmp;
t_len *len;
size_t tab_len;
size_t tab_height;
len = malloc(sizeof(t_len));
tmp = malloc(sizeof(t_tab));
tmp = *tab_list;
tab_height = 1;
tab_len = 0 ;
while (tmp->next != NULL)
{
tab_height++;
tmp = tmp->next;
}
while (tmp->tab[tab_len] != '\0')
tab_len++;
len->lmax = tab_len;
len->height = tab_height;
return (len);
}
t_tab *ft_create_elem(t_env *env, char **tab)
{
t_tab *new_elem;
new_elem = malloc(sizeof(t_tab));
new_elem->tab = tab;
new_elem->env = env;
new_elem->next = NULL;
return (new_elem);
}
void ft_list_push_back(t_tab **begin_list, t_env *env, char **tab)
{
t_tab *tmp;
if (*begin_list == NULL)
*begin_list = ft_create_elem(env, tab);
else
{
tmp = malloc(sizeof(t_tab));
tmp = *begin_list;
while (tmp->next != NULL)
tmp = tmp->next;
tmp->next = ft_create_elem(env, tab);
}
}
t_tab *ft_read_file(char *str, t_env *env)
{
int fd;
char *line;
char **tab;
t_tab *begin_list;
tab = NULL;
begin_list = malloc(sizeof(t_tab));
begin_list = NULL;
if ((fd = open(str, O_RDONLY)) == -1)
return (NULL);
while ((gnl(fd, &line)) > 0)
{
tab = ft_str_split(line, ' ');
ft_list_push_back(&begin_list, env, tab);
}
return (begin_list);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment