micro-utils/src/coreutils/basename/basename.c

33 lines
527 B
C

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <libgen.h>
int main(int argc, char **argv) {
int opt;
while ((opt = getopt(argc, argv, "")) != -1) {
fprintf(stderr, "basename [text] [suffix]\n");
return 1;
}
argv += optind;
argc -= optind;
char *suffix = NULL;
if (argc == 2)
suffix = argv[1];
char *base = basename(argv[0]);
if (suffix) {
char *ptr = base + strlen(base) - strlen(suffix);
if (!strcmp(ptr, suffix))
*ptr = '\0';
}
puts(base);
return 0;
}