Skip to content

Instantly share code, notes, and snippets.

@flatcap
Created May 3, 2018 11:28
Show Gist options
  • Save flatcap/4b2bee027c15579c30aecfc56c027218 to your computer and use it in GitHub Desktop.
Save flatcap/4b2bee027c15579c30aecfc56c027218 to your computer and use it in GitHub Desktop.
Test dirname wrapper
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <libgen.h>
#define mutt_array_size(x) (sizeof(x) / sizeof((x)[0]))
void *mutt_mem_malloc(size_t size)
{
void *p = NULL;
if (size == 0)
return NULL;
p = malloc(size);
if (!p)
exit(1);
return p;
}
char *mutt_str_strdup(const char *str)
{
if (!str || !*str)
return NULL;
const size_t len = strlen(str) + 1;
char *copy = mutt_mem_malloc(len);
memcpy(copy, str, len);
return copy;
}
char *mutt_file_dirname(const char *path)
{
char *copy = mutt_str_strdup(path);
char *dir = dirname(copy);
if (dir != copy)
{
free(copy);
dir = strdup(dir);
}
return dir;
}
int main()
{
const char *s = NULL;
char *d = NULL;
char *test[] = {
"/usr/share",
"/usr/share/",
"mutt",
".",
"..",
"/",
"//",
NULL
};
for (size_t i = 0; i < mutt_array_size(test); i++)
{
s = test[i];
d = mutt_file_dirname(s);
printf("%s -> %s\n", s, d);
free(d);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment