toaruos/apps/basename.c

32 lines
680 B
C
Raw Permalink Normal View History

/**
* @brief basename - print file name
*
* @copyright
2018-10-09 15:04:35 +03:00
* This file is part of ToaruOS and is released under the terms
* of the NCSA / University of Illinois License - see LICENSE.md
* Copyright (C) 2018 K. Lange
*/
#include <stdio.h>
#include <string.h>
2018-11-18 03:47:23 +03:00
#include <libgen.h>
2018-10-09 15:04:35 +03:00
int main(int argc, char * argv[]) {
if (argc < 2) {
fprintf(stderr, "%s: expected argument\n", argv[0]);
return 1;
}
2018-11-18 03:47:23 +03:00
char * c = basename(argv[1]);
2018-10-09 15:04:35 +03:00
if (argc > 2) {
char * suffix = argv[2];
char * found = strstr(c + strlen(c) - strlen(suffix), suffix);
if (found && (found - c == (int)(strlen(c)-strlen(suffix)))) {
*found = '\0';
}
}
fprintf(stdout, "%s\n", c);
return 0;
}