summaryrefslogtreecommitdiff
path: root/procfs_file.c
blob: 62419ee53d592eda2e25484e9d28c2b039bedb1b (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
49
50
51
52
53
54
55
56
57
58
59
#include <hurd/hurd_types.h>
#include <stdlib.h>
#include <string.h>
#include "procfs.h"
#include "procfs_file.h"

struct procfs_file
{
  void *contents;
  size_t len;
  void (*cleanup)(void *contents);
};

error_t
procfs_file_getcontents (void *hook, void **contents, size_t *contents_len)
{
  struct procfs_file *f = hook;

  *contents = f->contents;
  *contents_len = f->len;
  return 0;
}

void
procfs_file_cleanup (void *hook)
{
  struct procfs_file *f = hook;

  if (f->cleanup)
    f->cleanup (f->contents);

  free (f);
}

struct node *
procfs_file_make_node (void *contents, ssize_t len, void (*cleanup)(void *))
{
  static const struct procfs_node_ops ops = {
    .get_contents = procfs_file_getcontents,
    .cleanup = procfs_file_cleanup,
  };
  struct procfs_file *f;
  struct node *np;

  f = malloc (sizeof *f);
  if (! f)
    return NULL;

  f->contents = contents;
  f->len = (len >= 0) ? len : strlen (f->contents);
  f->cleanup = cleanup;

  np = procfs_make_node (&ops, f);
  if (! np)
    free (f);

  return np;
}