]> git.ipfire.org Git - thirdparty/bash.git/blame - examples/loadables/basename.c
Imported from ../bash-4.0-rc1.tar.gz.
[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
5#include "config.h"
6
7#if defined (HAVE_UNISTD_H)
8# include <unistd.h>
9#endif
10
11#include <stdio.h>
12#include "builtins.h"
13#include "shell.h"
3185942a 14#include "common.h"
ccc6cda3
JA
15
16basename_builtin (list)
17 WORD_LIST *list;
18{
19 int slen, sufflen, off;
20 char *string, *suffix, *fn;
21
22 if (list == 0)
23 {
24 builtin_usage ();
25 return (EX_USAGE);
26 }
27
28 if (no_options (list))
29 return (EX_USAGE);
30
31 string = list->word->word;
32 suffix = (char *)NULL;
33 if (list->next)
34 {
35 list = list->next;
36 suffix = list->word->word;
37 }
38
39 if (list->next)
40 {
41 builtin_usage ();
42 return (EX_USAGE);
43 }
44
45 slen = strlen (string);
46
47 /* Strip trailing slashes */
48 while (slen > 0 && string[slen - 1] == '/')
49 slen--;
50
51 /* (2) If string consists entirely of slash characters, string shall be
52 set to a single slash character. In this case, skip steps (3)
53 through (5). */
54 if (slen == 0)
55 {
56 fputs ("/\n", stdout);
57 return (EXECUTION_SUCCESS);
58 }
59
60 /* (3) If there are any trailing slash characters in string, they
61 shall be removed. */
62 string[slen] = '\0';
63
64 /* (4) If there are any slash characters remaining in string, the prefix
65 of string up to an including the last slash character in string
66 shall be removed. */
67 while (--slen >= 0)
68 if (string[slen] == '/')
69 break;
70
71 fn = string + slen + 1;
72
73 /* (5) If the suffix operand is present, is not identical to the
74 characters remaining in string, and is identical to a suffix
75 of the characters remaining in string, the suffix suffix
76 shall be removed from string. Otherwise, string shall not be
77 modified by this step. */
78 if (suffix)
79 {
80 sufflen = strlen (suffix);
81 slen = strlen (fn);
82 if (sufflen < slen)
83 {
84 off = slen - sufflen;
85 if (strcmp (fn + off, suffix) == 0)
86 fn[off] = '\0';
87 }
88 }
89 printf ("%s\n", fn);
90 return (EXECUTION_SUCCESS);
91}
92
93char *basename_doc[] = {
3185942a
JA
94 "Return non-directory portion of pathname.",
95 "",
ccc6cda3
JA
96 "The STRING is converted to a filename corresponding to the last",
97 "pathname component in STRING. If the suffix string SUFFIX is",
98 "supplied, it is removed.",
99 (char *)NULL
100};
101
102/* The standard structure describing a builtin command. bash keeps an array
103 of these structures. */
104struct builtin basename_struct = {
105 "basename", /* builtin name */
106 basename_builtin, /* function implementing the builtin */
107 BUILTIN_ENABLED, /* initial flags for builtin */
108 basename_doc, /* array of long documentation strings. */
109 "basename string [suffix]", /* usage synopsis */
110 0 /* reserved for internal use */
111};