annotate toys/basename.c @ 409:55598a9b8f21

'env' and 'basename' refactored
author Tryn Mirell <tryn@mirell.org>
date Fri, 20 Jan 2012 00:02:37 -0600
parents 8506c538f26a
children eda61bcf575a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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"
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
14 default n
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
404
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
18 Return non-directory portion of a pathname
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) {
409
55598a9b8f21 'env' and 'basename' refactored
Tryn Mirell <tryn@mirell.org>
parents: 408
diff changeset
40 char *s = strstr(base, suffix);
55598a9b8f21 'env' and 'basename' refactored
Tryn Mirell <tryn@mirell.org>
parents: 408
diff changeset
41 if (s && s != base) *s = 0;
404
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
42 }
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
43
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
44 puts(base);
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
45 }