#include <stdio.h>
#include <stdlib.h>
#define maxsize 20
static ssize_t mygetline(char **lineptr, size_t *n, FILE *stream)
{
ssize_t count=0;
int buf;
if(*lineptr == NULL)
// free(*lineptr);
{
*n=maxsize;
*lineptr = malloc(*n);
}
if((buf=fgetc(stream))==EOF)
{
return -1;
}
while((buf=fgetc(stream))!=EOF)
{
if(buf=='\n')
{
count+=1;
break;
}
count++;
*(*lineptr+count-1)=buf;
*(*lineptr+count)='\0';
if(*n<=count)
*lineptr=realloc(*lineptr,count*2);
}
return count;
}
int main(int argc,char *argv[])
{
FILE *fp;
char *linebuf;
size_t linesize;
int n;
fp = fopen(argv[1],"r");
if(fp == NULL)
{
perror("fopen()");
exit(1);
}
linebuf = NULL;
linesize = 0;
while(1)
{
if((n= mygetline(&linebuf,&linesize,fp)) < 0)
break;
printf("%d\n",n);
printf("%s\n",linebuf);
}
if(linebuf)
free(linebuf);
fclose(fp);
return 0;
}