Browse Source Download (without any required ccan dependencies)
take
routines to mark pointers to be consumed by called functions.
Rusty Russell <rusty@rustcorp.com.au>
This code helps to implement ownership transfer on a per-arg basis: the caller wraps the pointer argument in take() and the callee checks taken() to see if it should consume it.
// Given foo/bar.c outputs basename is bar.c
#include <ccan/take/take.h>
#include <string.h>
// Dumb basename program and driver.
static char *base(const char *file)
{
const char *p = strrchr(file, '/');
if (!p)
p = file;
else
p++;
// Use arg in place if we're allowed.
if (taken(file))
return memmove((char *)file, p, strlen(p)+1);
else
return strdup(p);
}
int main(int argc, char *argv[])
{
char *b;
if (argv[1]) // Mangle in place.
b = base(take(argv[1]));
else
b = base("test/string");
printf("basename is %s\n", b);
return 0;
}
CC0 (Public domain)