C related question

Dec 15, 2016 at 9:42pm
Hello,

I'm trying to understand some code dealing with low-level I/O. I'm unfamiliar of a good forum for C related questions, but if you have any please let me know as well. The code essentially passed a pointer to the contents of a file. It reads until the buffer is full and the stores the data in a static variable. This process is completed in a while loop until all of the data is entered. You'll notice some common C library functions labeled ft_SOMECLIBRARYRECREATE just assume that those functions on. I've recreated similar functions that mimic the C library in order to better understand how they work and to gain personal knowledge along the way. My main concern is the following when looking at some reference material. With the given code below, isn't it true that the lines after the while loop will not be reached? If they are reached can you explain to me why? Thank you!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
  int get_next_line(int const fd, char ** line)
{
	static char	*stock = NULL;
	char		*endl_index;
    int         ret;

    if (!stock && (stock = (char *)ft_memalloc(sizeof(char))) == NULL)
        return (-1);
	endl_index = ft_strchr(stock, ENDL);
	while (endl_index == NULL)
	{
        ret = read_from_fd_into_stock(fd, &stock);
        if (ret == 0)
        {
            if ( (endl_index = ft_strchr(stock, '\0')) == stock )
                return (0);
        } else if (ret < 0)
            return (-1);
        else
            endl_index = ft_strchr(stock, ENDL);
	}
    *line = ft_strsub(stock, 0, endl_index - stock);
    if (!*line)
        return (-1);
    endl_index = ft_strdup(endl_index + 1);
    free(stock);
    stock = endl_index;
	return (1);
}
Last edited on Jan 19, 2017 at 1:59am
Dec 15, 2016 at 10:16pm
closed account (48T7M4Gy)
https://github.com/dasilvacontin/-42-get_next_line/blob/master/get_next_line.c

Proper indentation would go a long way to resolving your question regarding Monsieur da silva' s function.
Jan 19, 2017 at 1:59am
Really? Indentation is all you could comment on? Congrats on finding the original source code, which was completely irrelevant.
Jan 19, 2017 at 2:51am
I'm unfamiliar of a good forum for C related questions, but if you have any please let me know as well.


I started C before C++ and these folks were very helpful (and quite opinionated!):
https://cboard.cprogramming.com/c-programming/
Topic archived. No new replies allowed.