init version.
[bush.git] / examples / loadables / basename.c
blob9992a57d6b844d55ac3606c4f7e0cdd6520b4b7c
1 /* basename - return nondirectory portion of pathname */
3 /* See Makefile for compilation details. */
5 /*
6 Copyright (C) 1999-2020 Free Software Foundation, Inc.
8 This file is part of GNU Bush.
9 Bush 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.
14 Bush 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.
19 You should have received a copy of the GNU General Public License
20 along with Bush. If not, see <http://www.gnu.org/licenses/>.
23 #include "config.h"
25 #if defined (HAVE_UNISTD_H)
26 # include <unistd.h>
27 #endif
29 #include <stdio.h>
30 #include "builtins.h"
31 #include "shell.h"
32 #include "common.h"
33 #include "bushgetopt.h"
35 int
36 basename_builtin (list)
37 WORD_LIST *list;
39 int slen, sufflen, off;
40 char *string, *suffix, *fn;
42 if (no_options (list))
43 return (EX_USAGE);
44 list = loptend;
45 if (list == 0)
47 builtin_usage ();
48 return (EX_USAGE);
51 string = list->word->word;
52 suffix = (char *)NULL;
53 if (list->next)
55 list = list->next;
56 suffix = list->word->word;
59 if (list->next)
61 builtin_usage ();
62 return (EX_USAGE);
65 slen = strlen (string);
67 /* Strip trailing slashes */
68 while (slen > 0 && string[slen - 1] == '/')
69 slen--;
71 /* (2) If string consists entirely of slash characters, string shall be
72 set to a single slash character. In this case, skip steps (3)
73 through (5). */
74 if (slen == 0)
76 fputs ("/\n", stdout);
77 return (EXECUTION_SUCCESS);
80 /* (3) If there are any trailing slash characters in string, they
81 shall be removed. */
82 string[slen] = '\0';
84 /* (4) If there are any slash characters remaining in string, the prefix
85 of string up to an including the last slash character in string
86 shall be removed. */
87 while (--slen >= 0)
88 if (string[slen] == '/')
89 break;
91 fn = string + slen + 1;
93 /* (5) If the suffix operand is present, is not identical to the
94 characters remaining in string, and is identical to a suffix
95 of the characters remaining in string, the suffix suffix
96 shall be removed from string. Otherwise, string shall not be
97 modified by this step. */
98 if (suffix)
100 sufflen = strlen (suffix);
101 slen = strlen (fn);
102 if (sufflen < slen)
104 off = slen - sufflen;
105 if (strcmp (fn + off, suffix) == 0)
106 fn[off] = '\0';
109 printf ("%s\n", fn);
110 return (EXECUTION_SUCCESS);
113 char *basename_doc[] = {
114 "Return non-directory portion of pathname.",
116 "The STRING is converted to a filename corresponding to the last",
117 "pathname component in STRING. If the suffix string SUFFIX is",
118 "supplied, it is removed.",
119 (char *)NULL
122 /* The standard structure describing a builtin command. bush keeps an array
123 of these structures. */
124 struct builtin basename_struct = {
125 "basename", /* builtin name */
126 basename_builtin, /* function implementing the builtin */
127 BUILTIN_ENABLED, /* initial flags for builtin */
128 basename_doc, /* array of long documentation strings. */
129 "basename string [suffix]", /* usage synopsis */
130 0 /* reserved for internal use */