Exam 42
Exam 42
Exam 42
Write a function will store, in the parameter "line", a line that has been read from the file
descriptor 0.
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".
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"
if (!str)
return (NULL);
leng = ft_strlen(str);
while (*str && *str != c)
{
i++;
str++;
}
if (*str != c && i == leng)
return (NULL);
return (str);
}
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);
}
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));
#ifndef GET_NEXT_LINE_H
# define GET_NEXT_LINE_H
# include <unistd.h>
# include <stdlib.h>
#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.
#include <unistd.h>
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.
Ejemplo:
#include <unistd.h>
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);
}