Reputation: 4658
I need a function who reproduce a basic of the getline function for another program, this function should be able to read a FP properly and without a huge buffer (256 should be enough here) I have 2 problem on the code below - the function start to return wrong info when the buffer is exeeded, for this file for example http://pastebin.com/BXj3SH92 ( ./a.out < file ) - when i call my function without giving it a file it should work just like a cat but it stop after i press the first enter key ( ./a.out )
here is the code:
#define MOAR_BUF 256
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
char *my_getline(const int fd)
{
static char save[MOAR_BUF];
static int i = MOAR_BUF;
int g;
int f;
char *save2;
save2 = NULL;
g = 0;
f = 0;
if (g == 0 && i >= (MOAR_BUF -1))
{
i = 0;
g = read(fd, save, MOAR_BUF + 1);
save[g] = '\0';
}
if (i <= MOAR_BUF && save[i] != '\0')
{
save2 = malloc((sizeof(*save2) * MOAR_BUF));
while(save[i] != '\n' && save[i] != '\0')
save2[f++] = save[i++];
save2[f] = '\0';
if (save[i] != '\0')
i = i + 1;
}
return(save2);
}
int main()
{
int i;
char **converted_map;
char *map;
i = 0;
converted_map = malloc(sizeof(*converted_map) * 30);
while(map = my_getline(0))
{
converted_map[i] = strdup(map);
printf("%s\n", converted_map[i]);
i++;
}
}
Upvotes: 0
Views: 692
Reputation: 183878
static char save[MOAR_BUF];
g = read(fd, save, MOAR_BUF);
save[g] = '\0';
You tell read
to read MOAR_BUF
bytes, and when it succeeds reading that many, the 0-termination writes past the end of the array, invoking undefined behaviour.
You need to tell read
to read fewer bytes, MOAR_BUF - 1
for example, or make the buffer larger, MOAR_BUF + 1
, for example.
Then, in main
,
while(map = my_getline(0))
{
printf("%s\n", converted_map[i]);
converted_map[i] = strdup(map);
i++;
}
you print out converted_map[i]
before it is assigned, that's more undefined behaviour. You need to first strdup(map)
to converted_map[i]
, then you can print it.
Upvotes: 3