FENIX_coreutils/dirname.c

49 lines
784 B
C
Executable file

#include <stdio.h>
#include <string.h>
int main(int argc, char ** argv) {
int i, last_slash = -1, length;
if(argc == 1) {
printf("/\n");
return 0;
}
if(strcmp(argv[1], "//") == 0) {
printf("/\n");
return 0;
}
length = strlen(argv[1]);
if(argv[1][length - 1] == '/') {
argv[1][length - 1] = '\0';
}
for(i = 0; i < length; i++) {
if(argv[1][i] == '/') {
last_slash = i;
}
}
if(last_slash == -1) {
printf(".\n");
return 0;
}
argv[1][last_slash] = '\0';
if(strcmp(argv[1], "//") == 0 || strcmp(argv[1], "") == 0 ||
strcmp(argv[1], "/") == 0) {
printf("/\n");
return 0;
}
for(i = last_slash - 1; argv[1][i] == '/'; i--) {
argv[1][i] = '\0';
}
printf("%s\n", argv[1]);
return 0;
}