Mercurial > hg > toybox
annotate toys/posix/basename.c @ 1008:a55854bde872
su: cleanery
* make help message more like others
* s/TT\.(.)Argu/TT.\1/g
* move environ to toys.h
* simplify failure messages
* clear password before quit
* not check what execve returns
* -lc
author | Strake <strake888@gmail.com> |
---|---|
date | Sat, 17 Aug 2013 02:54:58 -0500 |
parents | b300eb824c70 |
children |
rev | line source |
---|---|
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
653
diff
changeset
|
1 /* basename.c - Return non-directory portion of a pathname |
408
8506c538f26a
Comment changes, and add a blank line to the help text.
Rob Landley <rob@landley.net>
parents:
405
diff
changeset
|
2 * |
8506c538f26a
Comment changes, and add a blank line to the help text.
Rob Landley <rob@landley.net>
parents:
405
diff
changeset
|
3 * 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
|
4 * |
8506c538f26a
Comment changes, and add a blank line to the help text.
Rob Landley <rob@landley.net>
parents:
405
diff
changeset
|
5 * 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
|
6 |
404
ad5ffc45aa62
Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff
changeset
|
7 |
409
55598a9b8f21
'env' and 'basename' refactored
Tryn Mirell <tryn@mirell.org>
parents:
408
diff
changeset
|
8 USE_BASENAME(NEWTOY(basename, "<1>2", TOYFLAG_USR|TOYFLAG_BIN)) |
404
ad5ffc45aa62
Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff
changeset
|
9 |
ad5ffc45aa62
Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff
changeset
|
10 config BASENAME |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
653
diff
changeset
|
11 bool "basename" |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
653
diff
changeset
|
12 default y |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
653
diff
changeset
|
13 help |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
653
diff
changeset
|
14 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
|
15 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
653
diff
changeset
|
16 Return non-directory portion of a pathname removing suffix |
404
ad5ffc45aa62
Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff
changeset
|
17 */ |
ad5ffc45aa62
Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff
changeset
|
18 |
ad5ffc45aa62
Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff
changeset
|
19 #include "toys.h" |
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 void basename_main(void) |
ad5ffc45aa62
Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff
changeset
|
22 { |
781
b300eb824c70
Make basename use basename().
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
23 char *base = basename(*toys.optargs), *suffix = toys.optargs[1]; |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
653
diff
changeset
|
24 |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
653
diff
changeset
|
25 // chop off the suffix if provided |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
653
diff
changeset
|
26 if (suffix) { |
781
b300eb824c70
Make basename use basename().
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
27 char *s = base + strlen(base) - strlen(suffix); |
b300eb824c70
Make basename use basename().
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
28 if (s > base && !strcmp(s, suffix)) *s = 0; |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
653
diff
changeset
|
29 } |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
653
diff
changeset
|
30 |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
653
diff
changeset
|
31 puts(base); |
404
ad5ffc45aa62
Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff
changeset
|
32 } |