summaryrefslogtreecommitdiff
path: root/fmemopen-example.c
blob: 665795be7901e859bbd955a269845cb3ea2c5753 (plain)
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <err.h>

const char* data="\
[instaweb]\n\
local = true\n\
httpd = lighttpd\n\
port = 5151\n\
modulepath = /usr/lib/apache2/modules";

void fmemopen_test()
{
  size_t count=0;
  ssize_t len;
  char *line=NULL;
  size_t linecap=0;

  FILE *fp = fmemopen((void*)data,strlen(data),"r");
  if (fp==NULL)
    err(1,"fmemopen failed");

  while ((len = getline(&line, &linecap, fp)) > 0) {
    if (line[len-1]=='\n') {
      line[len-1]=0;
      --len;
    }
    ++count;

    printf("line %zu: %s\n", count, line);
  }
  if (ferror(fp))
    err(1,"memfile read error");

  if (fclose(fp)!=0)
    err(1,"fclose");

  free(line);
}


int main()
{
  fmemopen_test();
  return 0;
}