annotate toys/basename.c @ 279:0ca2fcee572b

Spent the five minutes to implement "cat".
author Rob Landley <rob@landley.net>
date Fri, 04 Apr 2008 12:19:21 -0500
parents 163498bf547b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 186
diff changeset
1 /* vi: set sw=4 ts=4:
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 186
diff changeset
2 *
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 186
diff changeset
3 * basename.c - print non-directory portion of path
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 186
diff changeset
4 *
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 186
diff changeset
5 * Copyright 2007 Charlie Shepherd <masterdriverz@gentoo.org>
177
00bbb7f65b6f Remove a few bytes from basename and add 'em back (and more) in the help string.
Rob Landley <rob@landley.net>
parents: 176
diff changeset
6 *
00bbb7f65b6f Remove a few bytes from basename and add 'em back (and more) in the help string.
Rob Landley <rob@landley.net>
parents: 176
diff changeset
7 * See http://www.opengroup.org/onlinepubs/009695399/utilities/basename.html
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 186
diff changeset
8
234
163498bf547b Move NEWTOY() list from end of toylist.h to generated/newtoys.h.
Rob Landley <rob@landley.net>
parents: 233
diff changeset
9 USE_BASENAME(NEWTOY(basename, "<1>2", TOYFLAG_BIN))
163498bf547b Move NEWTOY() list from end of toylist.h to generated/newtoys.h.
Rob Landley <rob@landley.net>
parents: 233
diff changeset
10
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 186
diff changeset
11 config BASENAME
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 186
diff changeset
12 bool "basename"
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 186
diff changeset
13 default y
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 186
diff changeset
14 help
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 186
diff changeset
15 usage: basename path [suffix]
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 186
diff changeset
16
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 186
diff changeset
17 Print the part of path after the last slash, optionally minus suffix.
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 186
diff changeset
18 */
177
00bbb7f65b6f Remove a few bytes from basename and add 'em back (and more) in the help string.
Rob Landley <rob@landley.net>
parents: 176
diff changeset
19
176
07533cabeede Patch from Charlie Shepherd to add basename and dirname. (Fixed up to apply.)
Rob Landley <rob@landley.net>
parents:
diff changeset
20 #include "toys.h"
07533cabeede Patch from Charlie Shepherd to add basename and dirname. (Fixed up to apply.)
Rob Landley <rob@landley.net>
parents:
diff changeset
21
186
25447caf1b4b Change command main() functions to return void, and exit(toys.exitval) from
Rob Landley <rob@landley.net>
parents: 177
diff changeset
22 void basename_main(void)
176
07533cabeede Patch from Charlie Shepherd to add basename and dirname. (Fixed up to apply.)
Rob Landley <rob@landley.net>
parents:
diff changeset
23 {
177
00bbb7f65b6f Remove a few bytes from basename and add 'em back (and more) in the help string.
Rob Landley <rob@landley.net>
parents: 176
diff changeset
24 char *name = basename(*toys.optargs);
00bbb7f65b6f Remove a few bytes from basename and add 'em back (and more) in the help string.
Rob Landley <rob@landley.net>
parents: 176
diff changeset
25 char *suffix = toys.optargs[1];
00bbb7f65b6f Remove a few bytes from basename and add 'em back (and more) in the help string.
Rob Landley <rob@landley.net>
parents: 176
diff changeset
26 if (suffix) {
00bbb7f65b6f Remove a few bytes from basename and add 'em back (and more) in the help string.
Rob Landley <rob@landley.net>
parents: 176
diff changeset
27 char *end = name+strlen(name)-strlen(suffix);
00bbb7f65b6f Remove a few bytes from basename and add 'em back (and more) in the help string.
Rob Landley <rob@landley.net>
parents: 176
diff changeset
28 if (end>name && !strcmp(end,suffix)) *end=0;
176
07533cabeede Patch from Charlie Shepherd to add basename and dirname. (Fixed up to apply.)
Rob Landley <rob@landley.net>
parents:
diff changeset
29 }
07533cabeede Patch from Charlie Shepherd to add basename and dirname. (Fixed up to apply.)
Rob Landley <rob@landley.net>
parents:
diff changeset
30 puts(name);
07533cabeede Patch from Charlie Shepherd to add basename and dirname. (Fixed up to apply.)
Rob Landley <rob@landley.net>
parents:
diff changeset
31 }