init version.
[bush.git] / examples / loadables / dirname.c
blob25cc08c4de7dce37e8c0899f72527a87fe48c22a
1 /* dirname - return directory portion of pathname */
3 /* See Makefile for compilation details. */
5 /*
6 Copyright (C) 1999-2009 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 dirname_builtin (list)
37 WORD_LIST *list;
39 int slen;
40 char *string;
42 if (no_options (list))
43 return (EX_USAGE);
44 list = loptend;
46 if (list == 0 || list->next)
48 builtin_usage ();
49 return (EX_USAGE);
52 string = list->word->word;
53 slen = strlen (string);
55 /* Strip trailing slashes */
56 while (slen > 0 && string[slen - 1] == '/')
57 slen--;
59 /* (2) If string consists entirely of slash characters, string shall be
60 set to a single slash character. In this case, skip steps (3)
61 through (8). */
62 if (slen == 0)
64 fputs ("/\n", stdout);
65 return (EXECUTION_SUCCESS);
68 /* (3) If there are any trailing slash characters in string, they
69 shall be removed. */
70 string[slen] = '\0';
72 /* (4) If there are no slash characters remaining in string, string
73 shall be set to a single period character. In this case, skip
74 steps (5) through (8).
76 (5) If there are any trailing nonslash characters in string,
77 they shall be removed. */
79 while (--slen >= 0)
80 if (string[slen] == '/')
81 break;
83 if (slen < 0)
85 fputs (".\n", stdout);
86 return (EXECUTION_SUCCESS);
89 /* (7) If there are any trailing slash characters in string, they
90 shall be removed. */
91 while (--slen >= 0)
92 if (string[slen] != '/')
93 break;
94 string[++slen] = '\0';
96 /* (8) If the remaining string is empty, string shall be set to a single
97 slash character. */
98 printf ("%s\n", (slen == 0) ? "/" : string);
99 return (EXECUTION_SUCCESS);
102 char *dirname_doc[] = {
103 "Display directory portion of pathname.",
105 "The STRING is converted to the name of the directory containing",
106 "the filename corresponding to the last pathname component in STRING.",
107 (char *)NULL
110 /* The standard structure describing a builtin command. bush keeps an array
111 of these structures. */
112 struct builtin dirname_struct = {
113 "dirname", /* builtin name */
114 dirname_builtin, /* function implementing the builtin */
115 BUILTIN_ENABLED, /* initial flags for builtin */
116 dirname_doc, /* array of long documentation strings. */
117 "dirname string", /* usage synopsis */
118 0 /* reserved for internal use */