annotate toys/basename.c @ 408:8506c538f26a

Comment changes, and add a blank line to the help text.
author Rob Landley <rob@landley.net>
date Wed, 18 Jan 2012 20:32:52 -0600
parents a8b14410e784
children 55598a9b8f21
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
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
10 USE_BASENAME(NEWTOY(basename, NULL, TOYFLAG_USR|TOYFLAG_BIN))
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 {
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
25 char *arg, *suffix, *base;
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
26 int arglen;
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 arg = toys.optargs[0];
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
29 suffix = toys.optargs[1];
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
30
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
31 // return null string if nothing provided
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
32 if (!arg) return;
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
33
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
34 arglen = strlen(arg);
405
a8b14410e784 'basename': Handle where we have no / passed
Tryn Mirell <tryn@mirell.org>
parents: 404
diff changeset
35
404
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
36 // handle the case where we only have single slash
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
37 if (arglen == 1 && arg[0] == '/') {
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
38 puts("/");
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
39 return;
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
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
42 // remove trailing slash
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
43 if (arg[arglen - 1] == '/') {
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
44 arg[arglen - 1] = 0;
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
45 }
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 // get everything past the last /
405
a8b14410e784 'basename': Handle where we have no / passed
Tryn Mirell <tryn@mirell.org>
parents: 404
diff changeset
48 base = strrchr(arg, '/');
a8b14410e784 'basename': Handle where we have no / passed
Tryn Mirell <tryn@mirell.org>
parents: 404
diff changeset
49
a8b14410e784 'basename': Handle where we have no / passed
Tryn Mirell <tryn@mirell.org>
parents: 404
diff changeset
50 if (!base) base = arg;
a8b14410e784 'basename': Handle where we have no / passed
Tryn Mirell <tryn@mirell.org>
parents: 404
diff changeset
51 else base++;
404
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
52
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
53 // handle the case where we have all slashes
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
54 if (base[0] == 0) base = "/";
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
55
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
56 // chop off the suffix if provided
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
57 if (suffix) {
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
58 strstr(base, suffix)[0] = 0;
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
59 }
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
60
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
61 puts(base);
ad5ffc45aa62 Initial 'basename' implementation
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
62 }