]> git.ipfire.org Git - thirdparty/bash.git/blame - examples/loadables/basename.c
bash-4.4 beta release
[thirdparty/bash.git] / examples / loadables / basename.c
CommitLineData
ccc6cda3
JA
1/* basename - return nondirectory portion of pathname */
2
3/* See Makefile for compilation details. */
4
c7e43312
CR
5/*
6 Copyright (C) 1999-2009 Free Software Foundation, Inc.
7
8 This file is part of GNU Bash.
9 Bash is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 Bash is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with Bash. If not, see <http://www.gnu.org/licenses/>.
21*/
22
ccc6cda3
JA
23#include "config.h"
24
25#if defined (HAVE_UNISTD_H)
26# include <unistd.h>
27#endif
28
29#include <stdio.h>
30#include "builtins.h"
31#include "shell.h"
29f3080f 32#include "common.h"
ccc6cda3 33
54a5fbe1 34int
ccc6cda3
JA
35basename_builtin (list)
36 WORD_LIST *list;
37{
38 int slen, sufflen, off;
39 char *string, *suffix, *fn;
40
41 if (list == 0)
42 {
43 builtin_usage ();
44 return (EX_USAGE);
45 }
46
47 if (no_options (list))
48 return (EX_USAGE);
49
50 string = list->word->word;
51 suffix = (char *)NULL;
52 if (list->next)
53 {
54 list = list->next;
55 suffix = list->word->word;
56 }
57
58 if (list->next)
59 {
60 builtin_usage ();
61 return (EX_USAGE);
62 }
63
64 slen = strlen (string);
65
66 /* Strip trailing slashes */
67 while (slen > 0 && string[slen - 1] == '/')
68 slen--;
69
70 /* (2) If string consists entirely of slash characters, string shall be
71 set to a single slash character. In this case, skip steps (3)
72 through (5). */
73 if (slen == 0)
74 {
75 fputs ("/\n", stdout);
76 return (EXECUTION_SUCCESS);
77 }
78
79 /* (3) If there are any trailing slash characters in string, they
80 shall be removed. */
81 string[slen] = '\0';
82
83 /* (4) If there are any slash characters remaining in string, the prefix
84 of string up to an including the last slash character in string
85 shall be removed. */
86 while (--slen >= 0)
87 if (string[slen] == '/')
88 break;
89
90 fn = string + slen + 1;
91
92 /* (5) If the suffix operand is present, is not identical to the
93 characters remaining in string, and is identical to a suffix
94 of the characters remaining in string, the suffix suffix
95 shall be removed from string. Otherwise, string shall not be
96 modified by this step. */
97 if (suffix)
98 {
99 sufflen = strlen (suffix);
100 slen = strlen (fn);
101 if (sufflen < slen)
102 {
103 off = slen - sufflen;
104 if (strcmp (fn + off, suffix) == 0)
105 fn[off] = '\0';
106 }
107 }
108 printf ("%s\n", fn);
109 return (EXECUTION_SUCCESS);
110}
111
112char *basename_doc[] = {
6a8fd0ed
CR
113 "Return non-directory portion of pathname.",
114 "",
ccc6cda3
JA
115 "The STRING is converted to a filename corresponding to the last",
116 "pathname component in STRING. If the suffix string SUFFIX is",
117 "supplied, it is removed.",
118 (char *)NULL
119};
120
121/* The standard structure describing a builtin command. bash keeps an array
122 of these structures. */
123struct builtin basename_struct = {
124 "basename", /* builtin name */
125 basename_builtin, /* function implementing the builtin */
126 BUILTIN_ENABLED, /* initial flags for builtin */
127 basename_doc, /* array of long documentation strings. */
128 "basename string [suffix]", /* usage synopsis */
129 0 /* reserved for internal use */
130};