annotate toys/basename.c @ 404:ad5ffc45aa62

Initial 'basename' implementation
author Tryn Mirell <tryn@mirell.org>
date Sun, 15 Jan 2012 23:20:06 -0600
parents
children a8b14410e784
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 *
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
3 * basename.c
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
4
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
5 USE_BASENAME(NEWTOY(basename, NULL, TOYFLAG_USR|TOYFLAG_BIN))
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
6
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
7 config BASENAME
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
8 bool "basename"
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
9 default n
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
10 help
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
11 usage: basename string [suffix]
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
12 Return non-directory portion of a pathname
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
13 */
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
14
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
15 #include "toys.h"
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
16
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
17 void basename_main(void)
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 char *arg, *suffix, *base;
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
20 int arglen;
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
21
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
22 arg = toys.optargs[0];
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
23 suffix = toys.optargs[1];
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
24
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
25 // return null string if nothing provided
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
26 if (!arg) return;
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
27
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
28 arglen = strlen(arg);
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
29
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
30 // handle the case where we only have single slash
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
31 if (arglen == 1 && arg[0] == '/') {
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
32 puts("/");
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
33 return;
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
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
36 // remove trailing slash
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
37 if (arg[arglen - 1] == '/') {
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
38 arg[arglen - 1] = 0;
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
39 }
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
40
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
41 // get everything past the last /
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
42 base = strrchr(arg, '/') + 1;
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 // handle the case where we have all slashes
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
45 if (base[0] == 0) base = "/";
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
46
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
47 // chop off the suffix if provided
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
48 if (suffix) {
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
49 strstr(base, suffix)[0] = 0;
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
50 }
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
51
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
52 puts(base);
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
53 }