summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeremie Koenig <jk@jk.fr.eu.org>2010-08-17 10:26:12 +0000
committerJeremie Koenig <jk@jk.fr.eu.org>2010-08-30 14:14:48 +0200
commit56ba0ac1e3a3f32763060ab862a6f0054c8a50bf (patch)
tree5d1c05531572f8eb9e3b6508714d66f4be7ae639
parent0a4c7a1ca8bff458eb11322d2c94ec9ffd832524 (diff)
Implement simple directories
* procfs_dir.h: New file; declare the procfs_dir_make_node function, based on the procfs_dir_entry structure. * procfs_dir.c: New file; implement simple directories. * Makefile: Add the procfs_dir module. * main.c: Use it.
-rw-r--r--Makefile2
-rw-r--r--main.c29
-rw-r--r--procfs_dir.c79
-rw-r--r--procfs_dir.h15
4 files changed, 103 insertions, 22 deletions
diff --git a/Makefile b/Makefile
index e7282ce..ab6e406 100644
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,5 @@
TARGET = procfs
-OBJS = procfs.o netfs.o procfs_file.o main.o
+OBJS = procfs.o netfs.o procfs_file.o procfs_dir.o main.o
LIBS = -lnetfs
CC = gcc
diff --git a/main.c b/main.c
index 457cf6a..62e440d 100644
--- a/main.c
+++ b/main.c
@@ -4,32 +4,19 @@
#include <hurd/netfs.h>
#include "procfs.h"
#include "procfs_file.h"
+#include "procfs_dir.h"
-static error_t get_entries (void *hook, void **contents, size_t *contents_len)
+static struct node *make_file (void *dir_hook, void *ent_hook)
{
- static const char entries[] = "hello";
- *contents = (void *) entries;
- *contents_len = sizeof entries;
- return 0;
-}
-
-static error_t lookup (void *hook, const char *name, struct node **np)
-{
- if (strcmp (name, "hello"))
- return ENOENT;
-
- *np = procfs_file_make_node ("Hello, World!\n", -1, NULL);
- if (! *np)
- return ENOMEM;
-
- return 0;
+ return procfs_file_make_node (ent_hook, -1, NULL);
}
int main (int argc, char **argv)
{
- static const struct procfs_node_ops ops = {
- .get_contents = get_entries,
- .lookup = lookup,
+ static const struct procfs_dir_entry entries[] = {
+ { "hello", make_file, "Hello, World!\n" },
+ { "goodbye", make_file, "Goodbye, cruel World!\n" },
+ { }
};
mach_port_t bootstrap;
@@ -40,7 +27,7 @@ int main (int argc, char **argv)
error (1, 0, "Must be started as a translator");
netfs_init ();
- netfs_root_node = procfs_make_node (&ops, NULL);
+ netfs_root_node = procfs_dir_make_node (entries, NULL);
netfs_startup (bootstrap, 0);
for (;;)
diff --git a/procfs_dir.c b/procfs_dir.c
new file mode 100644
index 0000000..4d4faa2
--- /dev/null
+++ b/procfs_dir.c
@@ -0,0 +1,79 @@
+#include <stdlib.h>
+#include <string.h>
+#include "procfs.h"
+#include "procfs_dir.h"
+
+struct procfs_dir_node
+{
+ const struct procfs_dir_entry *entries;
+ void *hook;
+};
+
+static error_t
+procfs_dir_get_contents (void *hook, void **contents, size_t *contents_len)
+{
+ struct procfs_dir_node *dn = hook;
+ const struct procfs_dir_entry *ent;
+ char *pos;
+
+ *contents_len = 0;
+ for (ent = dn->entries; ent->name; ent++)
+ *contents_len += strlen (ent->name) + 1;
+
+ *contents = malloc (*contents_len);
+ if (! *contents)
+ return ENOMEM;
+
+ pos = *contents;
+ for (ent = dn->entries; ent->name; ent++)
+ {
+ strcpy (pos, ent->name);
+ pos += strlen (ent->name) + 1;
+ }
+
+ return 0;
+}
+
+static error_t
+procfs_dir_lookup (void *hook, const char *name, struct node **np)
+{
+ struct procfs_dir_node *dn = hook;
+ const struct procfs_dir_entry *ent;
+
+ for (ent = dn->entries; ent->name && strcmp (name, ent->name); ent++);
+ if (! ent->name)
+ return ENOENT;
+
+ *np = ent->make_node (dn->hook, ent->hook);
+ if (! *np)
+ return ENOMEM;
+
+ return 0;
+}
+
+struct node *
+procfs_dir_make_node (const struct procfs_dir_entry *entries, void *dir_hook)
+{
+ static const struct procfs_node_ops ops = {
+ .get_contents = procfs_dir_get_contents,
+ .lookup = procfs_dir_lookup,
+ .cleanup_contents = free,
+ .cleanup = free,
+ };
+ struct procfs_dir_node *dn;
+ struct node *np;
+
+ dn = malloc (sizeof *dn);
+ if (! dn)
+ return NULL;
+
+ dn->entries = entries;
+ dn->hook = dir_hook;
+
+ np = procfs_make_node (&ops, dn);
+ if (! np)
+ free (dn);
+
+ return np;
+}
+
diff --git a/procfs_dir.h b/procfs_dir.h
new file mode 100644
index 0000000..1ba45ad
--- /dev/null
+++ b/procfs_dir.h
@@ -0,0 +1,15 @@
+
+/* Each entry associates a name with a callback function for creating new
+ nodes corresponding to that entry. */
+struct procfs_dir_entry
+{
+ const char *name;
+ struct node *(*make_node)(void *dir_hook, void *entry_hook);
+ void *hook;
+};
+
+/* A simple directory is built from a table of entries. The table is
+ terminated by a null NAME pointer. */
+struct node *
+procfs_dir_make_node (const struct procfs_dir_entry *entries, void *dir_hook);
+