Mercurial > hg > toybox
annotate lib/llist.c @ 50:63c168b65fa6
Add rewrite(), writeall(),and xwrite() to match the read versions.
author | Rob Landley <rob@landley.net> |
---|---|
date | Mon, 08 Jan 2007 02:49:39 -0500 |
parents | 3981c96f9285 |
children | 630b2e12db16 |
rev | line source |
---|---|
15
2a56fdc40035
Linked list functions, forgot to add this to the repository.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
1 /* vi: set sw=4 ts=4 : |
2a56fdc40035
Linked list functions, forgot to add this to the repository.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
2 * llist.c - Linked list functions |
2a56fdc40035
Linked list functions, forgot to add this to the repository.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
3 * |
2a56fdc40035
Linked list functions, forgot to add this to the repository.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
4 * Linked list structures have a next pointer as their first element. |
2a56fdc40035
Linked list functions, forgot to add this to the repository.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
5 */ |
2a56fdc40035
Linked list functions, forgot to add this to the repository.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
6 |
2a56fdc40035
Linked list functions, forgot to add this to the repository.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
7 #include "toys.h" |
2a56fdc40035
Linked list functions, forgot to add this to the repository.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
8 |
2a56fdc40035
Linked list functions, forgot to add this to the repository.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
9 // Free all the elements of a linked list |
2a56fdc40035
Linked list functions, forgot to add this to the repository.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
10 // if freeit!=NULL call freeit() on each element before freeing it. |
2a56fdc40035
Linked list functions, forgot to add this to the repository.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
11 |
2a56fdc40035
Linked list functions, forgot to add this to the repository.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
12 void llist_free(void *list, void (*freeit)(void *data)) |
2a56fdc40035
Linked list functions, forgot to add this to the repository.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
13 { |
2a56fdc40035
Linked list functions, forgot to add this to the repository.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
14 while (list) { |
20
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
15
diff
changeset
|
15 void *pop = llist_pop(&list); |
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
15
diff
changeset
|
16 if (freeit) freeit(pop); |
15
2a56fdc40035
Linked list functions, forgot to add this to the repository.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
17 } |
2a56fdc40035
Linked list functions, forgot to add this to the repository.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
18 } |
20
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
15
diff
changeset
|
19 |
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
15
diff
changeset
|
20 // Return the first item from the list, advancing the list (which must be called |
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
15
diff
changeset
|
21 // as &list) |
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
15
diff
changeset
|
22 void *llist_pop(void *list) |
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
15
diff
changeset
|
23 { |
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
15
diff
changeset
|
24 // I'd use a void ** for the argument, and even accept the typecast in all |
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
15
diff
changeset
|
25 // callers as documentation you need the &, except the stupid compiler |
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
15
diff
changeset
|
26 // would then scream about type-punned pointers. Screw it. |
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
15
diff
changeset
|
27 void **llist = (void **)list; |
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
15
diff
changeset
|
28 void **next = (void **)*llist; |
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
15
diff
changeset
|
29 *llist = *next; |
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
15
diff
changeset
|
30 |
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
15
diff
changeset
|
31 return (void *)next; |
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
15
diff
changeset
|
32 } |