Newer
Older
/*
** getln.c for 42sh
**
** Made by Seblu
** Login <seblu@epita.fr>
**
** Started on Wed Aug 2 01:25:01 2006 Seblu
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "readline.h"
/*!
** Secure layer over strlen
**
** @param s the string
**
** @return lenght of the string
*/
{
if (s == NULL)
return 0;
return strlen(s);
}
/*
** Append a string to the buffer string
*/
static void buf_str(char **str, char *append, unsigned n)
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
if ((*str = realloc(*str, (ln + n + 1) * sizeof (char))) == NULL)
exit(1);
for (i = ln, j = 0; i < ln + n; i++, j++)
(*str)[i] = append[j];
(*str)[ln + n] = 0;
}
/*
** Memory allocation of getln buffer
*/
struct s_getln *getln_open(const int fd)
{
struct s_getln *new_buf;
if ((new_buf = malloc(sizeof (struct s_getln))) == NULL)
exit(1);
new_buf->fd = fd;
new_buf->size = 0;
new_buf->offset = 0;
return new_buf;
}
/*
** Free a getln struct
*/
void getln_close(struct s_getln *buf, const int closefd)
{
if (closefd)
close(buf->fd);
free(buf);
}
/*
** Get next line in a file
** Return NULL when nothing to read
*/
char *getln(struct s_getln *buf)
{
char *string = NULL;
int i;
do
{
for (i = buf->offset; i < buf->size; i++)
{
if (buf->data[i] == '\n')
{
buf_str(&string, buf->data + buf->offset, i - buf->offset + 1);
buf->offset = i + 1;
return string;
}
}
if (buf->size - buf->offset > 0)
buf_str(&string, buf->data + buf->offset, buf->size - buf->offset);