aboutsummaryrefslogtreecommitdiff
path: root/src/basename.c
blob: e07a7ee89dbb57de6968c22cf92e499086448ddd (plain)
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <getopt.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define BASENAME_OUT_FMT "%s\n"
#define BASENAME_USAGE 	"Usage: basename NAME [SUFFIX]\n"
#define BASENAME_USE "Return non-directory portion of a pathname\n"

static char *remove_trailing_slashes(char *pathname) {
	size_t j = strlen(pathname);
	char *last_char = pathname + j - 1;
	for (;last_char > pathname; last_char--) {
		if (*last_char != '/') {
			break;
		}
		*last_char = '\0';
	}

	return last_char;
}

int basename_main(int argc, char *argv[]) {
	char *result = NULL;
	char *suffix = NULL;
	char *last_slash = NULL;
	char *last_char = NULL;
	char *suffix_end = NULL;
	bool suffix_match = true;
	size_t suffix_len = 0;
	char *cursor = NULL;


	if (getopt(argc, argv, "") != -1) {
		fprintf(stderr, BASENAME_USAGE);
		return EXIT_FAILURE;
	}

	if (argc < 2 || argc > 3) {
		fprintf(stderr, "%s%s", BASENAME_USAGE, BASENAME_USE);
		return EXIT_FAILURE;
	}

	result = argv[1];
	suffix = argc == 3 ? argv[2] : NULL;
	
	/* Return "" on empty input string */
	if (result[0] == '\0') {
		printf(BASENAME_OUT_FMT, "");
		return EXIT_SUCCESS;
	}
	
	/* Remove trailing whitespace and return "/" if the path was all slashes */
	last_char = remove_trailing_slashes(result);
	if (last_char == result) {
		printf(BASENAME_OUT_FMT, "/");
		return EXIT_SUCCESS;
	}

	/* Keep only the last node */
	last_slash = strrchr(result, '/'); 
	result = last_slash == NULL ? result : last_slash + 1;


	/* Remove suffix or return it as is if it's equal to last_node */
	if (suffix == NULL) {
		printf(BASENAME_OUT_FMT, result);
		return EXIT_SUCCESS;
	}

	suffix_len = strlen(suffix);
	if (suffix_len > strlen(result)) {
		printf(BASENAME_OUT_FMT, result);
		return EXIT_SUCCESS;
	}

	cursor = last_char;
	for (suffix_end = suffix + suffix_len - 1; suffix_end > suffix; suffix_end--) {
		if (*cursor != *suffix_end) {
			suffix_match = false;
			break;	
		}
		cursor--;
	}

	if (suffix_match && cursor != result) {
		*cursor = '\0';
	}

	printf(BASENAME_OUT_FMT, result);
	return EXIT_SUCCESS;
}