~vcs-imports/logrotate/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <stdlib.h>
#include <string.h>

#include "basenames.h"

/* Return NAME with any leading path stripped off.  */

char *ourBaseName(char *name)
{
    char *base;

    base = strrchr(name, '/');
    return base ? base + 1 : name;
}

static void stripTrailingSlashes(char *path)
{
    char *last;

    last = path + strlen(path) - 1;
    while (last > path && *last == '/')
	*last-- = '\0';
}

char *ourDirName(char *origname)
{
    char *slash;
    char *name;

    name = strdup(origname);

    stripTrailingSlashes(name);

    slash = strrchr(name, '/');

    if (!slash) {
	/* No slash, must be current directory */
	free(name);
	/* strdup used, as the return value will be free()ed at some point */
	return strdup(".");
    } else {
	/* Remove any trailing slashes and final element. */
	while (slash > name && *slash == '/')
	    --slash;
	slash[1] = '\0';
	return name;
    }
}