summaryrefslogtreecommitdiffstats
path: root/usr.bin/basename/basename.c
diff options
context:
space:
mode:
authordes <des@FreeBSD.org>2000-09-03 17:09:41 +0000
committerdes <des@FreeBSD.org>2000-09-03 17:09:41 +0000
commit1846ba7eb2fe7818584ba51ef5cbc47eb8b2337c (patch)
treeb1cd9031cf53255521410ffb4caf274ee3d047da /usr.bin/basename/basename.c
parent107eda6ecbf47cf2df900465531df1faf11a7721 (diff)
downloadFreeBSD-src-1846ba7eb2fe7818584ba51ef5cbc47eb8b2337c.zip
FreeBSD-src-1846ba7eb2fe7818584ba51ef5cbc47eb8b2337c.tar.gz
Use basename(3).
Diffstat (limited to 'usr.bin/basename/basename.c')
-rw-r--r--usr.bin/basename/basename.c66
1 files changed, 4 insertions, 62 deletions
diff --git a/usr.bin/basename/basename.c b/usr.bin/basename/basename.c
index 1a6aa48..d807ed9 100644
--- a/usr.bin/basename/basename.c
+++ b/usr.bin/basename/basename.c
@@ -29,6 +29,8 @@
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
+ *
+ * $FreeBSD$
*/
#ifndef lint
@@ -41,9 +43,8 @@ static const char copyright[] =
static const char sccsid[] = "@(#)basename.c 8.4 (Berkeley) 5/4/95";
#endif /* not lint */
+#include <libgen.h>
#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
#include <unistd.h>
void usage __P((void));
@@ -53,7 +54,6 @@ main(argc, argv)
int argc;
char **argv;
{
- char *p;
int ch;
while ((ch = getopt(argc, argv, "")) != -1)
@@ -68,65 +68,7 @@ main(argc, argv)
if (argc != 1 && argc != 2)
usage();
- /*
- * (1) If string is // it is implementation defined whether steps (2)
- * through (5) are skipped or processed.
- *
- * (2) If string consists entirely of slash characters, string shall
- * be set to a single slash character. In this case, skip steps
- * (3) through (5).
- */
- for (p = *argv;; ++p) {
- if (!*p) {
- if (p > *argv)
- (void)printf("/\n");
- else
- (void)printf("\n");
- exit(0);
- }
- if (*p != '/')
- break;
- }
-
- /*
- * (3) If there are any trailing slash characters in string, they
- * shall be removed.
- */
- for (; *p; ++p)
- continue;
- while (*--p == '/')
- continue;
- *++p = '\0';
-
- /*
- * (4) If there are any slash characters remaining in string, the
- * prefix of string up to an including the last slash character
- * in string shall be removed.
- */
- while (--p >= *argv)
- if (*p == '/')
- break;
- ++p;
-
- /*
- * (5) If the suffix operand is present, is not identical to the
- * characters remaining in string, and is identical to a suffix
- * of the characters remaining in string, the suffix suffix
- * shall be removed from string.
- */
- if (*++argv) {
- int suffixlen, stringlen, off;
-
- suffixlen = strlen(*argv);
- stringlen = strlen(p);
-
- if (suffixlen < stringlen) {
- off = stringlen - suffixlen;
- if (!strcmp(p + off, *argv))
- p[off] = '\0';
- }
- }
- (void)printf("%s\n", p);
+ (void)printf("%s\n", basename(*argv));
exit(0);
}
OpenPOWER on IntegriCloud