File: basenames.c

package info (click to toggle)
logrotate 3.7-5
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 368 kB
  • ctags: 174
  • sloc: ansic: 1,987; sh: 987; makefile: 173
file content (45 lines) | stat: -rw-r--r-- 895 bytes parent folder | download | duplicates (3)
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
#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;
    }
}