Mercurial > hg > toybox
annotate toys/basename.c @ 455:6c62d5b6675a
Add test for basename, fix issue where suffix is wrongfully applied if it appears in the middle of the filename
author | Elie De Brauwer <eliedebrauwer@gmail.com> |
---|---|
date | Sun, 12 Feb 2012 14:14:58 +0100 |
parents | eda61bcf575a |
children | dd24f86d5ad5 |
rev | line source |
---|---|
404
ad5ffc45aa62
Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff
changeset
|
1 /* vi: set sw=4 ts=4: |
ad5ffc45aa62
Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff
changeset
|
2 * |
408
8506c538f26a
Comment changes, and add a blank line to the help text.
Rob Landley <rob@landley.net>
parents:
405
diff
changeset
|
3 * basename.c - Return non-directory portion of a pathname |
8506c538f26a
Comment changes, and add a blank line to the help text.
Rob Landley <rob@landley.net>
parents:
405
diff
changeset
|
4 * |
8506c538f26a
Comment changes, and add a blank line to the help text.
Rob Landley <rob@landley.net>
parents:
405
diff
changeset
|
5 * Copyright 2012 Tryn Mirell <tryn@mirell.org> |
8506c538f26a
Comment changes, and add a blank line to the help text.
Rob Landley <rob@landley.net>
parents:
405
diff
changeset
|
6 * |
8506c538f26a
Comment changes, and add a blank line to the help text.
Rob Landley <rob@landley.net>
parents:
405
diff
changeset
|
7 * See http://opengroup.org/onlinepubs/9699919799/utilities/basename.html |
8506c538f26a
Comment changes, and add a blank line to the help text.
Rob Landley <rob@landley.net>
parents:
405
diff
changeset
|
8 |
404
ad5ffc45aa62
Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff
changeset
|
9 |
409
55598a9b8f21
'env' and 'basename' refactored
Tryn Mirell <tryn@mirell.org>
parents:
408
diff
changeset
|
10 USE_BASENAME(NEWTOY(basename, "<1>2", TOYFLAG_USR|TOYFLAG_BIN)) |
404
ad5ffc45aa62
Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff
changeset
|
11 |
ad5ffc45aa62
Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff
changeset
|
12 config BASENAME |
ad5ffc45aa62
Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff
changeset
|
13 bool "basename" |
420
eda61bcf575a
Basename and env are usable, default them to y.
Rob Landley <rob@landley.net>
parents:
409
diff
changeset
|
14 default y |
404
ad5ffc45aa62
Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff
changeset
|
15 help |
ad5ffc45aa62
Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff
changeset
|
16 usage: basename string [suffix] |
408
8506c538f26a
Comment changes, and add a blank line to the help text.
Rob Landley <rob@landley.net>
parents:
405
diff
changeset
|
17 |
455
6c62d5b6675a
Add test for basename, fix issue where suffix is wrongfully applied if it appears in the middle of the filename
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
420
diff
changeset
|
18 Return non-directory portion of a pathname removing suffix |
404
ad5ffc45aa62
Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff
changeset
|
19 */ |
ad5ffc45aa62
Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff
changeset
|
20 |
ad5ffc45aa62
Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff
changeset
|
21 #include "toys.h" |
ad5ffc45aa62
Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff
changeset
|
22 |
ad5ffc45aa62
Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff
changeset
|
23 void basename_main(void) |
ad5ffc45aa62
Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff
changeset
|
24 { |
409
55598a9b8f21
'env' and 'basename' refactored
Tryn Mirell <tryn@mirell.org>
parents:
408
diff
changeset
|
25 char *arg = toys.optargs[0], *suffix = toys.optargs[1], *base; |
404
ad5ffc45aa62
Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff
changeset
|
26 |
409
55598a9b8f21
'env' and 'basename' refactored
Tryn Mirell <tryn@mirell.org>
parents:
408
diff
changeset
|
27 while ((base = strrchr(arg, '/'))) { |
55598a9b8f21
'env' and 'basename' refactored
Tryn Mirell <tryn@mirell.org>
parents:
408
diff
changeset
|
28 if (base == arg) break; |
55598a9b8f21
'env' and 'basename' refactored
Tryn Mirell <tryn@mirell.org>
parents:
408
diff
changeset
|
29 if (!base[1]) *base = 0; |
55598a9b8f21
'env' and 'basename' refactored
Tryn Mirell <tryn@mirell.org>
parents:
408
diff
changeset
|
30 else { |
55598a9b8f21
'env' and 'basename' refactored
Tryn Mirell <tryn@mirell.org>
parents:
408
diff
changeset
|
31 base++; |
55598a9b8f21
'env' and 'basename' refactored
Tryn Mirell <tryn@mirell.org>
parents:
408
diff
changeset
|
32 break; |
55598a9b8f21
'env' and 'basename' refactored
Tryn Mirell <tryn@mirell.org>
parents:
408
diff
changeset
|
33 } |
404
ad5ffc45aa62
Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff
changeset
|
34 } |
ad5ffc45aa62
Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff
changeset
|
35 |
405
a8b14410e784
'basename': Handle where we have no / passed
Tryn Mirell <tryn@mirell.org>
parents:
404
diff
changeset
|
36 if (!base) base = arg; |
404
ad5ffc45aa62
Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff
changeset
|
37 |
ad5ffc45aa62
Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff
changeset
|
38 // chop off the suffix if provided |
ad5ffc45aa62
Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff
changeset
|
39 if (suffix) { |
455
6c62d5b6675a
Add test for basename, fix issue where suffix is wrongfully applied if it appears in the middle of the filename
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
420
diff
changeset
|
40 int suflen = strlen(suffix); |
6c62d5b6675a
Add test for basename, fix issue where suffix is wrongfully applied if it appears in the middle of the filename
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
420
diff
changeset
|
41 int reslen = strlen(base); |
6c62d5b6675a
Add test for basename, fix issue where suffix is wrongfully applied if it appears in the middle of the filename
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
420
diff
changeset
|
42 if (suflen < reslen && !strcmp( base+reslen-suflen, suffix)) |
6c62d5b6675a
Add test for basename, fix issue where suffix is wrongfully applied if it appears in the middle of the filename
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
420
diff
changeset
|
43 base[reslen-suflen] = 0; |
404
ad5ffc45aa62
Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff
changeset
|
44 } |
455
6c62d5b6675a
Add test for basename, fix issue where suffix is wrongfully applied if it appears in the middle of the filename
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
420
diff
changeset
|
45 |
404
ad5ffc45aa62
Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff
changeset
|
46 puts(base); |
ad5ffc45aa62
Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff
changeset
|
47 } |