Sunteți pe pagina 1din 9

Get Next Line

Assignment name : get_next_line


Expected files : get_next_line.c get_next_line.h
Allowed functions: read, free, malloc
--------------------------------------------------------------------------------

Write a function will store, in the parameter "line", a line that has been read from the file
descriptor 0.

Your function must be prototyped as follows: int get_next_line(char **line);

Your function should be memory leak free.

What we call a "line that has been read" is a succession of 0 to n characters that end with '\n'
(ascii code 0x0a) or with End Of File (EOF).

The string stored in the parameter "line" should not contained any '\n'.

The parameter is the address of a pointer to a character that will be used to store the line
read.

The return value can be 1, 0 or -1 depending on whether a line has been read, when the
reading has been completed (meaning read has returned 0), or if an error has happened
respectively.

When you've reached the End Of File, you must store the current buffer in "line". If the buffer
is empty you must store an empty string in "line".

When you've reached the End Of File, your function should keep 0 memory allocated with
malloc except the last buffer that you should have stored in "line".

What you've stored in "line" should be free-able.

Calling your function get_next_line in a loop will therefore allow you to read the text available
on a file descriptor one line at a time until the end of the text, no matter the size of either the
text or one of its lines.

Make sure that your function behaves well when it reads from a file, from the standard
output, from a redirection etc.

No call to another function will be done on the file descriptor between 2 calls of
get_next_line.

Finally we consider that get_next_line has an undefined behavior when reading from a
binary file.

You should use the test.sh to help you test your get_next_line.
#include "get_next_line.h"

size_t ft_strlen(char *str)


{
size_t i = 0;

while (str[i] != '\0')


i++;
return (i);
}

char *ft_strchr(char *str, int c)


{
unsigned int i = 0, leng;

if (!str)
return (NULL);
leng = ft_strlen(str);
while (*str && *str != c)
{
i++;
str++;
}
if (*str != c && i == leng)
return (NULL);
return (str);
}

char *ft_strjoin(char *s1, char *s2)


{
int i = 0;
char *dest;

if (!(dest = malloc(ft_strlen(s1)+ ft_strlen(s2) + 1)))


return (NULL);
while (*s1 != '\0')
dest[i++] = *s1++;
while (*s2 != '\0')
dest[i++] = *s2++;
dest[i] = '\0';
return (dest);
}

char *ft_strdup(char *str)


{
int i = 0;
char *dest;

if (!(dest = malloc(ft_strlen(str) + 1)))


return (NULL);
while (str[i] != '\0')
{
dest[i] = str[i];
i++;
}
dest[i] = '\0';
return (dest);
}

int getchar(char **line,char **aux, int n)


{
char *temp, *temp2;

if (n < 0)
{
if (*aux != NULL)
{
free(*aux);
aux = NULL;
}
return (-1);
}
if (!n && !*aux)
{
*line = ft_strdup("");
return (0);
}
if ((temp = ft_strchr(*aux, '\n')))
{
*temp = 0;
*line = ft_strdup(*aux);
temp2 = ft_strdup(++temp);
free(*aux);
*aux = temp2;
}
else
{
*line = ft_strdup(*aux);
free(*aux);
*aux = NULL;
return (0);
}
return (1);
}

int get_next_line(char **line)


{
static char *aux;
char buffer[512];
int n;
char *temp;

if (!line)
return (-1);
while (ft_strchr(aux, '\n') == NULL)
{
n = read(0, buffer, 511);
if (n < 0)
return (-1);
if (n == 0)
break ;
buffer[n] = 0;
if (!aux)
aux = ft_strdup(buffer);
else
{
temp = ft_strjoin (aux, buffer);
free(aux);
aux = temp;
}
}
return (getchar(line, &aux, n));

GET NEXT LINE.H

#ifndef GET_NEXT_LINE_H
# define GET_NEXT_LINE_H

# include <unistd.h>
# include <stdlib.h>

int get_next_line(char **line);

#endif

INTER

Escriba un programa que reciba como parámetros dos cadenas de caracteres y que
muestre, sin duplicados, los caracteres comunes a las dos cadenas.

Se mostrarán en el orden de aparición dentro de la primera cadena.


La visualización se debe terminar con un salto de línea.

Si el número de parámetros transmitidos es distinto de 2, el programa mostrará


'\n'.
Ejemplos:
$>./inter "padinton" "paqefwtdjetyiytjneytjoeyjnejeyj" | cat -e
padinto$
$>./inter ddf6vewg64f gtwthgdwthdwfteewhrtag6h4ffdhsd | cat -e
df6ewg4$
$>./inter "algo" "no crea que esta frase esconde algo" | cat -e
algo$
$>./inter | cat -e
$

#include <unistd.h>

int main(int argc, char **argv)


{
int used[255];
int i, j;

if (argc == 3)
{
i = 0;
while (i < 255)
used[i++] = 0;
i = 2;
while (i > 0)
{
j = 0;
while (argv[i][j])
{
if (i == 2 && !used[(unsigned char)argv[i][j]])
used[(unsigned char)argv[i][j]] = 1;
else if (i == 1 && used[(unsigned char)argv[i][j]] == 1)
{
write(1, &argv[i][j], 1);
used[(unsigned char)argv[i][j]] = 2;
}
j++;
}
i--;
}
}
write(1, "\n", 1);
return (0);
}

UNION

Escriba un programa que se llame union, que reciba como parámetro dos cadenas de
de caracteres y muestre, sin duplicados, los caracteres que aparecen en
la una o en la otra.

Se mostrarán en el orden de aparición dentro de la línea de comandos.

La visualización irá seguida de un salto de línea.

Si el número de parámetros transmitidos es distinto de 2, el programa mostrará


\n.

Ejemplo:

$>./union zpadinton "paqefwtdjetyiytjneytjoeyjnejeyj" | cat -e


zpadintoqefwjy$
$>./union ddf6vewg64f gtwthgdwthdwfteewhrtag6h4ffdhsd | cat -e
df6vewg4thras$
$>./union "rien" "cette phrase ne cache rien" | cat -e
rienct phas$
$>./union | cat -e
$
$>
$>./union "rien" | cat -e
$

#include <unistd.h>

int main(int argc, char **argv)


{
int used[255];
int i, j;

if (argc == 3)
{
i = 0;
while (i < 255)
used[i++] = 0;
i = 1;
while (i < 3)
{
j = 0;
while (argv[i][j])
{
if (!used[(unsigned char)argv[i][j]])
{
used[(unsigned char)argv[i][j]] = 1;
write(1, &argv[i][j], 1);
}
j++;
}
i++;
}
}
write(1, "\n", 1);
return (0);
}

PRINTF

#include <stdio.h>
#include <stdarg.h>
#include <unistd.h>

static int ft_strlen(char *s)
{
int i = 0;
while (s[i])
i++;
return (i);
}

static int ft_count(long long n, int base_len)
{
int i = 1;
while (n >= base_len)
{
n /= base_len;
i++;
}
return (i);
}

static void ft_putnbr(long long n, int base_len, char *base)
{
if (n >= base_len)
ft_putnbr(n / base_len, base_len, base);
write(1, &base[n % base_len], 1);
}

int ft_printf(const char *format, ...)
{
va_list ap;
int res = 0, len = 0, prec = 0, flag_prec = 0, neg = 0, zeros = 0, spaces = 0, width = 0,
base_len = 0;
long num = 0;
char *base, *str, *s;
va_start(ap, format);
str = (char *)format;
while (*str)
{
if (*str == '%')
{
str++;
len = 0, prec = 0, flag_prec = 0, neg = 0, zeros = 0, spaces = 0, width
= 0, base_len = 0;
while (*str <= '9' && *str >= '0')
{
width = width * 10 + *str - '0';
str++;
}
if (*str == '.')
{
str++;
flag_prec = 1;
while (*str <= '9' && *str >= '0')
{
prec = prec * 10 + *str - '0';
str++;
}
}
if (*str == 's')
{
s = va_arg(ap, char *);
if (!s)
s = "(null)";
len = ft_strlen(s);
}
if (*str == 'd')
{
num = va_arg(ap, int);
base_len = 10;
base = "0123456789";
if (num < 0)
{
neg = 1;
num *= -1;
}
len = ft_count(num, base_len) + neg;
}
if (*str == 'x')
{
num = va_arg(ap, unsigned);
base_len = 16;
base = "0123456789abcdef";
len = ft_count(num, base_len);
}
if (flag_prec == 1 && prec > len && *str != 's')
zeros = prec - len + neg;
else if (flag_prec == 1 && prec < len && *str == 's')
len = prec;
else if (flag_prec == 1 && prec == 0 && (num == 0 || *str == 's'))
len = 0;
spaces = width - len - zeros;
while (spaces-- > 0)
res += write(1, " ", 1);
if (neg)
write(1, "-", 1);
while (zeros-- > 0)
res += write(1, "0", 1);
if (*str == 's')
write(1, s, len);
else if (len > 0)
ft_putnbr(num, base_len, base);
res += len;
}
else
res += write(1, str, 1);
str++;
}
va_end(ap);
return (res);
}

S-ar putea să vă placă și