*
* Example:
* // buf param may be freed by this; need return value!
- * static char *WARN_UNUSED_RESULT enlarge(const char *buf, unsigned *size)
+ * static char *WARN_UNUSED_RESULT enlarge(char *buf, unsigned *size)
* {
* return realloc(buf, (*size) *= 2);
* }
* {
* struct child *c;
*
- * printf("%s (%u children):\n", p->name, parent->num_children);
+ * printf("%s (%u children):\n", p->name, p->num_children);
* list_check(&p->children, "bad child list");
* list_for_each(&p->children, c, list)
* printf(" -> %s\n", c->name);
#define debug_list(h) (h)
#endif
+/**
+ * LIST_HEAD - define and initalize an empty list_head
+ * @name: the name of the list.
+ *
+ * The LIST_HEAD macro defines a list_head and initializes it to an empty
+ * list. It can be prepended by "static" to define a static list_head.
+ *
+ * Example:
+ * static LIST_HEAD(my_global_list);
+ */
+#define LIST_HEAD(name) \
+ struct list_head name = { { &name.n, &name.n } }
+
/**
* list_head_init - initialize a list_head
* @h: the list_head to set to the empty list
*
* Example:
+ * ...
+ * struct parent *parent = malloc(sizeof(*parent));
+ *
* list_head_init(&parent->children);
* parent->num_children = 0;
*/
h->n.next = h->n.prev = &h->n;
}
-/**
- * LIST_HEAD - define and initalized empty list_head
- * @name: the name of the list.
- *
- * The LIST_HEAD macro defines a list_head and initializes it to an empty
- * list. It can be prepended by "static" to define a static list_head.
- *
- * Example:
- * // Header:
- * extern struct list_head my_list;
- *
- * // C file:
- * LIST_HEAD(my_list);
- */
-#define LIST_HEAD(name) \
- struct list_head name = { { &name.n, &name.n } }
-
/**
* list_add - add an entry at the start of a linked list.
* @h: the list_head to add the node to
*
* The list_node does not need to be initialized; it will be overwritten.
* Example:
+ * struct child *child;
+ *
* list_add(&parent->children, &child->list);
* parent->num_children++;
*/
* @member: the list_node member of the type
*
* Example:
- * struct child *c;
* // First list entry is children.next; convert back to child.
- * c = list_entry(parent->children.next, struct child, list);
+ * child = list_entry(parent->children.n.next, struct child, list);
+ *
+ * See Also:
+ * list_top(), list_for_each()
*/
#define list_entry(n, type, member) container_of(n, type, member)
* a for loop, so you can break and continue as normal.
*
* Example:
- * struct child *c;
- * list_for_each(&parent->children, c, list)
- * printf("Name: %s\n", c->name);
+ * list_for_each(&parent->children, child, list)
+ * printf("Name: %s\n", child->name);
*/
#define list_for_each(h, i, member) \
for (i = container_of_var(debug_list(h)->n.next, i, member); \
* @nxt is used to hold the next element, so you can delete @i from the list.
*
* Example:
- * struct child *c, *n;
- * list_for_each_safe(&parent->children, c, n, list) {
- * list_del(&c->list);
+ * struct child *next;
+ * list_for_each_safe(&parent->children, child, next, list) {
+ * list_del(&child->list);
* parent->num_children--;
* }
*/
* #include <errno.h>
* #include <ccan/noerr/noerr.h>
*
- * bool write_string_to_file(const char *file, const char *string)
+ * static bool write_string_to_file(const char *file, const char *string)
* {
* int ret, fd = open(file, O_WRONLY|O_CREAT|O_EXCL, 0600);
* if (fd < 0)
* static unsigned int values[] = { 1, 7, 11, 1235, 99999 };
*
* // Return true if this value is in set, and remove it.
- * bool remove_from_values(unsigned int val)
+ * static bool remove_from_values(unsigned int val)
* {
* unsigned int *p;
* // We use 5 here, but ccan/array_size.h is better!
* static unsigned int values[] = { 1, 7, 11, 1235, 99999 };
*
* // Return true if this value is in set, and remove it.
- * bool remove_from_values(unsigned int val)
+ * static bool remove_from_values(unsigned int val)
* {
* unsigned int *p;
* p = sparse_bsearch(&val, values, 5, val_cmp, val_valid);
* This macro is arguably more readable than "!strcmp(a, b)".
*
* Example:
- * if (streq(str, ""))
+ * if (streq(somestring, ""))
* printf("String is empty!\n");
*/
#define streq(a,b) (strcmp((a),(b)) == 0)
* @prefix: prefix to look for at start of str
*
* Example:
- * if (strstarts(str, "foo"))
- * printf("String %s begins with 'foo'!\n", str);
+ * if (strstarts(somestring, "foo"))
+ * printf("String %s begins with 'foo'!\n", somestring);
*/
#define strstarts(str,prefix) (strncmp((str),(prefix),strlen(prefix)) == 0)
* @postfix: postfix to look for at end of str
*
* Example:
- * if (strends(str, "foo"))
- * printf("String %s end with 'foo'!\n", str);
+ * if (strends(somestring, "foo"))
+ * printf("String %s end with 'foo'!\n", somestring);
*/
static inline bool strends(const char *str, const char *postfix)
{
* @nump to find the array length.
*
* Example:
- * unsigned int count_long_lines(const char *text)
+ * #include <ccan/talloc/talloc.h>
+ * #include <ccan/str_talloc/str_talloc.h>
+ * ...
+ * static unsigned int count_long_lines(const char *string)
* {
* char **lines;
* unsigned int i, long_lines = 0;
*
* Example:
* // Append the string "--EOL" to each line.
- * char *append_to_all_lines(const char *string)
+ * static char *append_to_all_lines(const char *string)
* {
* char **lines, *ret;
- * unsigned int i, num, newnum;
*
* lines = strsplit(NULL, string, "\n", NULL);
* ret = strjoin(NULL, lines, "-- EOL\n");
*
* Example:
*
- * #include <ccan/stringmap/stringmap.h>
+ * #include <ccan/stringmap/stringmap.h>
*
- * static const char *get_string(void) {
- * static char buffer[4096];
- * char *tail;
- * if (!fgets(buffer, sizeof(buffer), stdin))
- * return NULL;
- * tail = strchr(buffer, 0);
- * if (tail>buffer && tail[-1]=='\n')
- * *--tail = 0;
- * if (!*buffer)
- * return NULL;
- * return buffer;
- * }
+ * static const char *get_string(void) {
+ * static char buffer[4096];
+ * char *tail;
+ * if (!fgets(buffer, sizeof(buffer), stdin))
+ * return NULL;
+ * tail = strchr(buffer, 0);
+ * if (tail>buffer && tail[-1]=='\n')
+ * *--tail = 0;
+ * if (!*buffer)
+ * return NULL;
+ * return buffer;
+ * }
*
- * int main(void) {
+ * int main(void) {
* stringmap(int) map = stringmap_new(NULL);
* const char *string;
*
* return 0;
* }
*
- * Authors: Joey Adams, Anders Magnusson
- * License: BSD
+ * Authors: Joey Adams, Anders Magnusson
+ * License: BSD
*/
int main(int argc, char *argv[])
{
* static struct upcache *cache;
* static unsigned int cache_hits = 0;
* #define CACHE_SIZE 4
- * void init_upcase(void)
+ * static void init_upcase(void)
* {
* cache = talloc_zero_array(NULL, struct upcache, CACHE_SIZE);
* }
* }
*
* // If you want to keep the result, talloc_link it.
- * const char *get_upcase(const char *str)
+ * static const char *get_upcase(const char *str)
* {
* struct upcache *uc = lookup_upcase(str);
* if (!uc)
* return uc->upstr;
* }
*
- * void exit_upcase(void)
+ * static void exit_upcase(void)
* {
* talloc_free(cache);
* printf("Cache hits: %u\n", cache_hits);
* #include <err.h>
* #include <stdio.h>
*
- * static void usage(void)
+ * static void usage(const char *argv0)
* {
* errx(1, "Usage: %s fetch <dbfile> <key>\n"
- * "OR %s store <dbfile> <key> <data>");
+ * "OR %s store <dbfile> <key> <data>", argv0, argv0);
* }
*
* int main(int argc, char *argv[])
* TDB_DATA key, value;
*
* if (argc < 4)
- * usage();
+ * usage(argv[0]);
*
* tdb = tdb_open(argv[2], 1024, TDB_DEFAULT, O_CREAT|O_RDWR,
* 0600);
*
* if (streq(argv[1], "fetch")) {
* if (argc != 4)
- * usage();
+ * usage(argv[0]);
* value = tdb_fetch(tdb, key);
* if (!value.dptr)
* errx(1, "fetch %s: %s",
* free(value.dptr);
* } else if (streq(argv[1], "store")) {
* if (argc != 5)
- * usage();
+ * usage(argv[0]);
* value.dptr = (void *)argv[4];
* value.dsize = strlen(argv[4]);
* if (tdb_store(tdb, key, value, 0) != 0)
* errx(1, "store %s: %s",
* argv[3], tdb_errorstr(tdb));
* } else
- * usage();
+ * usage(argv[0]);
*
* return 0;
* }
* #include <err.h>
* #include <stdio.h>
*
- * static void usage(void)
+ * static void usage(const char *argv0)
* {
* errx(1, "Usage: %s fetch <dbfile> <key>\n"
- * "OR %s store <dbfile> <key> <data>");
+ * "OR %s store <dbfile> <key> <data>", argv0, argv0);
* }
*
* int main(int argc, char *argv[])
* TDB_DATA key, value;
*
* if (argc < 4)
- * usage();
+ * usage(argv[0]);
*
- * tdb = tdb_open(argv[2], 1024, TDB_DEFAULT, O_CREAT|O_RDWR,
- * 0600);
+ * tdb = tdb_open(argv[2], TDB_DEFAULT, O_CREAT|O_RDWR,0600, NULL);
* if (!tdb)
* err(1, "Opening %s", argv[2]);
*
*
* if (streq(argv[1], "fetch")) {
* if (argc != 4)
- * usage();
+ * usage(argv[0]);
* value = tdb_fetch(tdb, key);
* if (!value.dptr)
* errx(1, "fetch %s: %s",
* free(value.dptr);
* } else if (streq(argv[1], "store")) {
* if (argc != 5)
- * usage();
+ * usage(argv[0]);
* value.dptr = (void *)argv[4];
* value.dsize = strlen(argv[4]);
* if (tdb_store(tdb, key, value, 0) != 0)
* errx(1, "store %s: %s",
* argv[3], tdb_errorstr(tdb));
* } else
- * usage();
+ * usage(argv[0]);
*
* return 0;
* }
*
* // Define several silly callbacks. Note they don't use void *!
* #define DEF_CALLBACK(name, op) \
- * static int name(int val, const int *arg)\
+ * static int name(int val, int *arg) \
* { \
* printf("%s", #op); \
* return val op *arg; \