Compare commits

...

10 Commits

Author SHA1 Message Date
Camille Chauvet
9b1684a85a add: README 2023-03-27 16:01:52 +02:00
Camille Chauvet
38ce8029e5 fd > 1024 fix 2022-11-23 15:28:59 +01:00
Camille Chauvet
d612a3c927 final mais vraiment 2022-11-14 17:31:11 +01:00
Camille Chauvet
e37bd854c6 kekw 2022-11-14 16:11:57 +01:00
Camille Chauvet
9102ae31ab bonus: final 2022-11-14 16:10:06 +01:00
Camille Chauvet
f0189fa7c4 bonus fix 2022-11-14 15:53:40 +01:00
Camille Chauvet
66aa7a2268 bonus, first implementation 2022-11-14 15:45:44 +01:00
Camille Chauvet
87432b195e kekw 2022-11-08 17:45:10 +01:00
Camille Chauvet
d4f57e8f7b kekw 2022-11-08 14:49:12 +01:00
Camille Chauvet
386bef0a64 only 7bytes of leaks 2022-10-28 13:41:06 +02:00
10 changed files with 477 additions and 92 deletions

31
README.md Normal file
View File

@ -0,0 +1,31 @@
# get_next_line
This is my implementation of the get_next_line project for the 42 school.
## Description
get_next_line is a function that reads a file descriptor (FD) line by line until the end of file (EOF) or an error occurs.
The function returns each line as a string, without the line break character '\n'.
The purpose of the project is to provide a function for reading lines from a file descriptor, which can be useful for various applications.
## Usage
To use the function, simply include the header file and call the function with the file descriptor as the first argument.
The function will return:
- The line: if a line has been read
- Empty string: if the end of file has been reached
- NULL: if an error occurred
It is important to note that the function uses dynamic memory allocation to return the line as a string, so it is necessary to free the memory after use.
## Resources
- ![The project subject](./subject.pdf)
- The standard C library documentation
## Authors
This project was created by Camille CHAUVET. If you have any questions or suggestions, feel free to contact me.

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/26 00:52:47 by cchauvet #+# #+# */
/* Updated: 2022/10/26 16:39:09 by cchauvet ### ########.fr */
/* Updated: 2022/11/14 15:37:48 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -17,11 +17,11 @@ char *ft_getstash(int fd)
char *str;
int readed;
str = ft_calloc(BUFFER_SIZE, sizeof(char));
str = ft_calloc(BUFFER_SIZE + 1, sizeof(char));
if (str == NULL)
return (NULL);
readed = read(fd, str, BUFFER_SIZE);
if (readed < 0)
if (readed < 1)
{
free(str);
return (NULL);
@ -29,39 +29,18 @@ char *ft_getstash(int fd)
return (str);
}
ssize_t ft_strchr(char *str, char c)
{
size_t i;
if (str == NULL)
return (-1);
i = 0;
while (str[i] != c && str[i] != '\0')
i++;
if (str[i] == '\0')
return (-1);
return (i);
}
char *ft_getline(int fd)
{
char *stash;
char *buf;
size_t i;
stash = NULL;
i = 0;
while (ft_strchr(stash + i, '\n') == -1)
buf = NULL;
while (ft_strchr(stash, '\n') == -1)
{
i = i + ft_strlen(buf);
buf = ft_getstash(fd);
if (buf == NULL)
return (NULL);
if (buf[0] == '\0')
{
free(buf);
break ;
}
return (stash);
stash = ft_strfjoin(stash, buf);
if (stash == NULL)
return (NULL);
@ -69,26 +48,53 @@ char *ft_getline(int fd)
return (stash);
}
char *ft_getreturn(char *str)
{
int i;
if (str == NULL)
return (NULL);
i = ft_strchr(str, '\n') + 1;
if (i == 0)
i = ft_strlen(str);
return (ft_strndup(str, i));
}
char *ft_getextra(char *str)
{
int i;
int j;
if (str == NULL)
return (NULL);
i = ft_strchr(str, '\n') + 1;
if (i == 0)
return (NULL);
j = ft_strlen(str + i);
return (ft_strndup(str + i, j));
}
char *get_next_line(int fd)
{
static char *stash = NULL;
char *buf1;
char *buf2;
buf2 = stash;
if (ft_strchr(stash, '\n') == -1)
{
buf1 = ft_getline(fd);
if (buf1 == NULL)
return (NULL);
stash = ft_strfjoin(stash, buf1);
if (stash == NULL)
buf2 = ft_strfjoin(stash, buf1);
if (buf2 == NULL)
{
free(buf1);
return (NULL);
}
}
buf1 = ft_strndup(stash, ft_strchr(stash, '\n') + 1);
buf2 = ft_strndup(stash + ft_strchr(stash, '\n') + 1, ft_strlen(stash));
free(stash);
stash = buf2;
if (stash == NULL || buf1 == NULL)
buf1 = ft_getreturn(buf2);
stash = ft_getextra(buf2);
free(buf2);
if (buf1 == NULL)
{
free(stash);
free(buf1);

View File

@ -1,14 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/14 15:38:06 by cchauvet #+# #+# */
/* Updated: 2022/11/14 17:28:39 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef GET_NEXT_LINE_H
# define GET_NEXT_LINE_H
# include <stdlib.h>
# include <unistd.h>
# define BUFFER_SIZE 10
# ifndef BUFFER_SIZE
# define BUFFER_SIZE 42
# endif
void *ft_calloc(size_t nmemb, size_t size);
void *ft_realloc(void *ptr, size_t size);
char *ft_strfjoin(char *dst, char *src);
char *ft_strndup(char *src, size_t n);
size_t ft_strlen(char *str);
ssize_t ft_strchr(char *str, char c);
char *get_next_line(int fd);
#endif

100
get_next_line_bonus.c Normal file
View File

@ -0,0 +1,100 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/26 00:52:47 by cchauvet #+# #+# */
/* Updated: 2022/11/23 15:19:15 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line_bonus.h"
char *ft_getstash(int fd)
{
char *str;
int readed;
str = ft_calloc(BUFFER_SIZE + 1, sizeof(char));
if (str == NULL)
return (NULL);
readed = read(fd, str, BUFFER_SIZE);
if (readed < 1)
{
free(str);
return (NULL);
}
return (str);
}
char *ft_getline(int fd)
{
char *stash;
char *buf;
stash = NULL;
buf = NULL;
while (ft_strchr(stash, '\n') == -1)
{
buf = ft_getstash(fd);
if (buf == NULL)
return (stash);
stash = ft_strfjoin(stash, buf);
if (stash == NULL)
return (NULL);
}
return (stash);
}
char *ft_getreturn(char *str)
{
int i;
if (str == NULL)
return (NULL);
i = ft_strchr(str, '\n') + 1;
if (i == 0)
i = ft_strlen(str);
return (ft_strndup(str, i));
}
char *ft_getextra(char *str)
{
int i;
int j;
if (str == NULL)
return (NULL);
i = ft_strchr(str, '\n') + 1;
if (i == 0)
return (NULL);
j = ft_strlen(str + i);
return (ft_strndup(str + i, j));
}
char *get_next_line(int fd)
{
static char *stash[1024] = {0};
char *buf1;
char *buf2;
if (fd > 1023)
return (NULL);
buf2 = stash[fd];
if (ft_strchr(stash[fd], '\n') == -1)
{
buf1 = ft_getline(fd);
buf2 = ft_strfjoin(stash[fd], buf1);
}
buf1 = ft_getreturn(buf2);
stash[fd] = ft_getextra(buf2);
if (buf1 == NULL || buf2 == NULL)
{
free(stash[fd]);
free(buf1);
}
free(buf2);
return (buf1);
}

28
get_next_line_bonus.h Normal file
View File

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_bonus.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/14 15:38:06 by cchauvet #+# #+# */
/* Updated: 2022/11/14 17:29:19 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef GET_NEXT_LINE_BONUS_H
# define GET_NEXT_LINE_BONUS_H
# include <stdlib.h>
# include <unistd.h>
# ifndef BUFFER_SIZE
# define BUFFER_SIZE 42
# endif
void *ft_calloc(size_t nmemb, size_t size);
void *ft_realloc(void *ptr, size_t size);
char *ft_strfjoin(char *dst, char *src);
char *ft_strndup(char *src, size_t n);
size_t ft_strlen(char *str);
ssize_t ft_strchr(char *str, char c);
char *get_next_line(int fd);
#endif

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/26 00:55:44 by cchauvet #+# #+# */
/* Updated: 2022/10/26 16:33:23 by cchauvet ### ########.fr */
/* Updated: 2022/11/14 15:39:50 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -31,31 +31,6 @@ void *ft_calloc(size_t nmemb, size_t size)
return ((void *) tab);
}
void *ft_realloc(void *ptr, size_t size)
{
char *tab;
char *in;
size_t i;
in = ptr;
tab = ft_calloc(sizeof(char), size);
if (tab == NULL)
return (NULL);
if (ptr == NULL)
{
free(ptr);
return (tab);
}
i = 0;
while (in[i] != 0 && i < size)
{
tab[i] = in[i];
i++;
}
free(ptr);
return (tab);
}
size_t ft_strlen(char *str)
{
size_t i;
@ -70,8 +45,8 @@ size_t ft_strlen(char *str)
char *ft_strfjoin(char *s1, char *s2)
{
size_t i;
size_t j;
ssize_t i;
ssize_t j;
char *out;
out = ft_calloc((ft_strlen(s1) + ft_strlen(s2) + 1), sizeof(char));
@ -87,11 +62,11 @@ char *ft_strfjoin(char *s1, char *s2)
}
}
free(s1);
j = 0;
while (s2[j] != '\0')
j = -1;
if (s2 != NULL)
{
out[i + j] = s2[j];
j++;
while (s2[++j] != '\0')
out[i + j] = s2[j];
}
free(s2);
return (out);
@ -102,6 +77,8 @@ char *ft_strndup(char *src, size_t n)
char *out;
size_t i;
if (src[0] == '\0')
return (NULL);
out = ft_calloc(n + 1, sizeof(char));
if (out == NULL)
return (NULL);
@ -113,3 +90,17 @@ char *ft_strndup(char *src, size_t n)
}
return (out);
}
ssize_t ft_strchr(char *str, char c)
{
size_t i;
if (str == NULL)
return (-1);
i = 0;
while (str[i] != c && str[i] != '\0')
i++;
if (str[i] == '\0')
return (-1);
return (i);
}

106
get_next_line_utils_bonus.c Normal file
View File

@ -0,0 +1,106 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/26 00:55:44 by cchauvet #+# #+# */
/* Updated: 2022/11/14 15:50:27 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line_bonus.h"
void *ft_calloc(size_t nmemb, size_t size)
{
char *tab;
size_t i;
if (nmemb == 0 || size * nmemb / nmemb != size)
return (NULL);
tab = malloc(nmemb * size);
if (tab == NULL)
return (NULL);
i = 0;
while (i < size * nmemb)
{
tab[i] = 0;
i++;
}
return ((void *) tab);
}
size_t ft_strlen(char *str)
{
size_t i;
if (str == NULL)
return (0);
i = 0;
while (str[i] != '\0')
i++;
return (i);
}
char *ft_strfjoin(char *s1, char *s2)
{
ssize_t i;
ssize_t j;
char *out;
out = ft_calloc((ft_strlen(s1) + ft_strlen(s2) + 1), sizeof(char));
if (out == NULL)
return (NULL);
i = 0;
if (s1 != NULL)
{
while (s1[i] != '\0')
{
out[i] = s1[i];
i++;
}
}
free(s1);
j = -1;
if (s2 != NULL)
{
while (s2[++j] != '\0')
out[i + j] = s2[j];
}
free(s2);
return (out);
}
char *ft_strndup(char *src, size_t n)
{
char *out;
size_t i;
if (src[0] == '\0')
return (NULL);
out = ft_calloc(n + 1, sizeof(char));
if (out == NULL)
return (NULL);
i = 0;
while (src[i] != '\0' && i < n)
{
out[i] = src[i];
i++;
}
return (out);
}
ssize_t ft_strchr(char *str, char c)
{
size_t i;
if (str == NULL)
return (-1);
i = 0;
while (str[i] != c && str[i] != '\0')
i++;
if (str[i] == '\0')
return (-1);
return (i);
}

19
main.c
View File

@ -1,19 +0,0 @@
#include "get_next_line.h"
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(void)
{
int fd = open("tester", O_RDONLY);
char *str = get_next_line(fd);
printf("%s", str);
free(str);
// printf("%s", get_next_line(fd));
// printf("%s", get_next_line(fd));
// printf("%s", get_next_line(fd));
// printf("%s", get_next_line(fd));
// printf("%s", get_next_line(fd));
}

131
subject.pdf Normal file
View File

@ -0,0 +1,131 @@
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8'>
<meta content='IE=Edge,chrome=1' http-equiv='X-UA-Compatible'>
<meta content='width=device-width,height=device-height,user-scalable=no,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0' name='viewport'>
<meta content='yes' name='mobile-web-app-capable'>
<meta content='yes' name='apple-mobile-web-app-capable'>
<meta content='translucent-black' name='apple-mobile-web-app-status-bar-style'>
<meta content='Intranet 42' name='apple-mobile-web-app-title'>
<meta content='on' http-equiv='cleartype'>
<link href='https://cdn.intra.42.fr/' rel='preconnect'>
<link href='https://signin.intra.42.fr/assets/42_logo_black-684989d43d629b3c0ff6fd7e1157ee04db9bb7a73fba8ec4e01543d650a1c607.png' rel='icon' type='image/png'>
<link href='https://signin.intra.42.fr/assets/42_logo_black-684989d43d629b3c0ff6fd7e1157ee04db9bb7a73fba8ec4e01543d650a1c607.png' rel='shortcut icon' type='image/png'>
<title>Intra Signin New</title>
<meta name="csrf-param" content="authenticity_token" />
<meta name="csrf-token" content="aQQWo5tAv+V6w2tSRFc4iq9f/kmWzgxP4ynnuIRicZeEQf8JSgoow9s4Bp9IQhfFzwPghzs3TeM8SxkQ6kWgQQ==" />
<link rel="stylesheet" media="all" href="/assets/application-0f8ff68ca8056b0b2e36230747f7e997b432658c4a215497757d1ab87ae73b0d.css" />
<style>
.deprecated {
-webkit-filter: grayscale(100%);
filter: grayscale(100%); }
</style>
<script>
(function() {
this._environment = "production";
this._git_commit = "";
this._release = this._git_commit;
}).call(this);
</script>
<body class=' '>
<div class='notifications-flash-top-bar'>
</div>
<div class='page'>
<div class='page-sidebar left-main-container page-sidebar-fixed-left under-main-navbar'></div>
<div class='login-page page-content page-content-fluid' data-turbolinks-scaffold>
<div class='row'>
<div class='main-top-menu'>
</div>
<div class='col-lg-12'>
<div class='login-container'>
<div class='login-logo'>
<img src="/assets/42_logo-7dfc9110a5319a308863b96bda33cea995046d1731cebb735e41b16255106c12.svg" alt="42 logo" />
</div>
<div class='login-between'></div>
<div class='login-main'>
<a class="btn btn-login-student" href="https://profile.intra.42.fr/users/auth/keycloak_student">Sign in as student</a>
<a class="btn btn-login-admin" href="https://profile.intra.42.fr/users/auth/keycloak_admin">Sign in as staff</a>
<a class="forgot-password-link" href="https://profile.intra.42.fr/legal">Read our terms and conditions</a><br />
</div>
</div>
</div>
</div>
</div>
</div>
<div aria-hidden='true' aria-labelledby='smartModalLabel' class='modal fade' id='smartModal' role='dialog' tabindex='-1'>
<div class='modal-dialog'>
<div class='modal-content'>
<div class='modal-header'>
<button aria-hidden='true' class='close' data-dismiss='modal' type='button'>
x
</button>
<h3 id='smartModalLabel'>
</h3>
</div>
<div class='modal-body'>
<h4 class='note-title'></h4>
</div>
<div class='modal-footer'>
<button aria-hidden='true' class='btn' data-dismiss='modal'>Cancel</button>
<button class='send btn btn-primary'>Send</button>
</div>
</div>
</div>
</div>
<div aria-hidden='true' aria-labelledby='flashModalLabel' class='modal fade' id='flashModal' role='dialog' tabindex='-1'>
<div class='modal-dialog modal-lg'>
<div class='modal-content'>
<div class='modal-header'>
<button aria-hidden='true' class='close' data-dismiss='modal' type='button'>
x
</button>
<h3 id='flashModalLabel'></h3>
</div>
<div class='modal-body'>
<h4 class='note-title'>Flash modal content (raw)</h4>
</div>
<div class='modal-footer'>
<button aria-hidden='true' class='btn btn-block btn-default' data-dismiss='modal'>Close</button>
</div>
</div>
</div>
</div>
<div data-cache="true" class="hide"></div>
</body>
<script async await src='https://www.googletagmanager.com/gtag/js?id=G-BJ34XNRJCV'></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-BJ34XNRJCV');
</script>
<script>
var jsClasses = ["new_message", "topic-reply"];
jsClasses.forEach(function(className) {
Array.prototype.forEach.call(document.getElementsByClassName(className), function (e) {
e.classList.add("js-hidden");
});
});
</script>
<script src="/assets/application-fed7f3dd771fa014c9bc9723023f686a0aef20cadbc8fdab7fce5ffd3d1e46b7.js"></script>
</head>
</html>

3
tester
View File

@ -1,3 +0,0 @@
test1
test2
test3