41 lines
704 B
C
41 lines
704 B
C
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <libgen.h>
|
|
|
|
void remove_suffix(char *base, const char *suffix) {
|
|
char *ptr = base + strlen(base) - strlen(suffix);
|
|
if (!strcmp(ptr, suffix))
|
|
*ptr = '\0';
|
|
}
|
|
|
|
int main(const int argc, char **argv) {
|
|
char *suffix = NULL;
|
|
char *base = NULL;
|
|
|
|
int i;
|
|
for (i = 1; i < argc; i++) {
|
|
if (argv[i][0] != '-')
|
|
break;
|
|
|
|
else if (!strcmp(argv[i], "--help")) {
|
|
printf("basename [name] [suffix]\n");
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
if (argc - i < 1) {
|
|
fprintf(stderr, "basename: missing operand\n");
|
|
return 1;
|
|
}
|
|
|
|
else if (argc - i == 2)
|
|
suffix = argv[i + 1];
|
|
|
|
base = basename(argv[i]);
|
|
if (suffix)
|
|
remove_suffix(base, suffix);
|
|
|
|
puts(base);
|
|
return 0;
|
|
}
|