Import sources from 2011-03-30 iso image
This commit is contained in:
commit
e5888a1ffd
7810 changed files with 2489717 additions and 0 deletions
44
sys/src/ape/cmd/basename.c
Executable file
44
sys/src/ape/cmd/basename.c
Executable file
|
@ -0,0 +1,44 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
char *f, *b, *s;
|
||||
int n;
|
||||
|
||||
if(argc < 2 || argc > 3){
|
||||
fprintf(stderr, "Usage: basename string [suffix]\n");
|
||||
exit(1);
|
||||
}
|
||||
s = argv[1];
|
||||
b = s + strlen(s) - 1;
|
||||
while(b > s && *b == '/')
|
||||
b--;
|
||||
*++b = 0;
|
||||
if(b == s+1 && s[0] == '/') {
|
||||
printf("/");
|
||||
exit(0);
|
||||
}
|
||||
/* now b is after last char of string, trailing slashes removed */
|
||||
|
||||
for(f = b; f >= s; f--)
|
||||
if(*f == '/'){
|
||||
f++;
|
||||
break;
|
||||
}
|
||||
if(f < s)
|
||||
f = s;
|
||||
|
||||
/* now f is first char after last remaining slash, or first char */
|
||||
|
||||
if(argc == 3){
|
||||
n = strlen(argv[2]);
|
||||
if(n < b-f && strncmp(b-n, argv[2], n) == 0){
|
||||
b -= n;
|
||||
*b = 0;
|
||||
}
|
||||
}
|
||||
printf("%s\n", f);
|
||||
exit(0);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue