Importing usr.bin/basename

Change-Id: I2c71c83bc6a420390c4aed015373f78ebf425903
This commit is contained in:
Lionel Sambuc 2013-08-23 19:35:37 +02:00
parent 211b53e442
commit 0b8a9e801d
11 changed files with 207 additions and 122 deletions

View file

@ -2,7 +2,7 @@
.include <bsd.own.mk>
SUBDIR= add_route arp ash at backup basename btrace \
SUBDIR= add_route arp ash at backup btrace \
cawf cd cdprobe cpp \
chmod chown ci cleantmp cmp co \
comm compress cp crc cron crontab cut \

View file

@ -1,4 +0,0 @@
PROG= basename
MAN=
.include <bsd.prog.mk>

View file

@ -1,76 +0,0 @@
/* basename - print last part of a path Authors: B. Garfolo & P. Nelson */
/* Basename - print the last part of a path.
*
* For MINIX -- Conforms to POSIX - P1003.2/D10
* Exception -- it ignores the LC environment variables.
*
* Original MINIX author: Blaine Garfolo
* POSIX rewrite author: Philip A. Nelson
*
* POSIX version - October 20, 1990
* Feb 14, 1991: changed rindex to strrchr. (PAN)
*
*/
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#define EOS '\0'
int main(int argc, char **argv);
int main(argc, argv)
int argc;
char *argv[];
{
char *result_string; /* The pointer into argv[1]. */
char *temp; /* Used to move around in argv[1]. */
int suffix_len; /* Length of the suffix. */
int suffix_start; /* Where the suffix should start. */
/* Check for the correct number of arguments. */
if ((argc < 2) || (argc > 3)) {
fprintf(stderr, "Usage: basename string [suffix] \n");
exit(1);
}
/* Check for all /'s */
for (temp = argv[1]; *temp == '/'; temp++) /* Move to next char. */
;
if (*temp == EOS) {
printf("/\n");
exit(0);
}
/* Build the basename. */
result_string = argv[1];
/* Find the last /'s */
temp = strrchr(result_string, '/');
if (temp != NULL) {
/* Remove trailing /'s. */
while ((*(temp + 1) == EOS) && (*temp == '/')) *temp-- = EOS;
/* Set result_string to last part of path. */
if (*temp != '/') temp = strrchr(result_string, '/');
if (temp != NULL && *temp == '/') result_string = temp + 1;
}
/* Remove the suffix, if any. */
if (argc > 2) {
suffix_len = strlen(argv[2]);
suffix_start = strlen(result_string) - suffix_len;
if (suffix_start > 0)
if (strcmp(result_string + suffix_start, argv[2]) == EOS)
*(result_string + suffix_start) = EOS;
}
/* Print the resultant string. */
printf("%s\n", result_string);
return(0);
}

View file

@ -1823,6 +1823,7 @@
./usr/man/man1/df.1 minix-sys
./usr/man/man1/dhrystone.1 minix-sys
./usr/man/man1/diff.1 minix-sys
./usr/man/man1/dirname.1 minix-sys
./usr/man/man1/dosdir.1 minix-sys
./usr/man/man1/dosread.1 minix-sys
./usr/man/man1/doswrite.1 minix-sys

View file

@ -1,4 +1,4 @@
MAN= ash.1 at.1 basename.1 \
MAN= ash.1 at.1 \
bsfilt.1 cawf.1 chgrp.1 \
chmod.1 cmp.1 comm.1 compress.1 \
cp.1 crc.1 crontab.1 dd.1 \

View file

@ -1,39 +0,0 @@
.TH BASENAME 1
.SH NAME
basename, dirname \- strip off file prefixes and suffixes
.SH SYNOPSIS
\fBbasename \fIfile\fR [\fIsuffix\fR]\fR
.br
\fBdirname \fIfile\fR
.de FL
.TP
\\fB\\$1\\fR
\\$2
..
.de EX
.TP 20
\\fB\\$1\\fR
# \\$2
..
.SH EXAMPLES
.TP 20
.B basename /user/ast/file.c
# Strips path to yield \fIfile.c\fP
.TP 20
.B basename /user/file.c .c
# Strips path and \fI.c\fP to yield \fIfile\fP
.TP 20
.B dirname /user/file.c
# Strips basename to yield \fI/user\fP
.SH DESCRIPTION
.PP
.I Basename
removes the initial directory names (if any) yielding the name of the
file itself.
If a second argument is present, it is interpreted as a suffix and is
also stripped, if present.
.PP
.I Dirname
removes the final component of a path, yielding the directory a file is in.
.PP
These programs are primarily used in shell scripts.

View file

@ -143,6 +143,7 @@
2012/10/17 12:00:00,tools/tsort
2009/05/08 12:48:43,usr.bin/apropos
2012/10/17 12:00:00,usr.bin/banner
2012/10/17 12:00:00,usr.bin/basename
2013/05/31 12:00:00,usr.bin/bdes
2012/10/17 12:00:00,usr.bin/bzip2
2012/10/17 12:00:00,usr.bin/bzip2recover

View file

@ -4,7 +4,7 @@
.include <bsd.own.mk>
SUBDIR= \
banner bdes \
banner basename bdes \
bzip2 bzip2recover \
cal chpass cksum \
col ctags \

View file

@ -0,0 +1,7 @@
# $NetBSD: Makefile,v 1.6 2009/04/14 22:15:17 lukem Exp $
# @(#)Makefile 8.1 (Berkeley) 6/6/93
PROG= basename
MLINKS= basename.1 dirname.1
.include <bsd.prog.mk>

View file

@ -0,0 +1,92 @@
.\" $NetBSD: basename.1,v 1.17 2003/09/06 20:56:40 kleink Exp $
.\"
.\" Copyright (c) 1990, 1993, 1994
.\" The Regents of the University of California. All rights reserved.
.\"
.\" This code is derived from software contributed to Berkeley by
.\" the Institute of Electrical and Electronics Engineers, Inc.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. Neither the name of the University nor the names of its contributors
.\" may be used to endorse or promote products derived from this software
.\" without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" 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.
.\"
.\" @(#)basename.1 8.2 (Berkeley) 4/18/94
.\"
.Dd April 18, 1994
.Dt BASENAME 1
.Os
.Sh NAME
.Nm basename ,
.Nm dirname
.Nd return filename or directory portion of pathname
.Sh SYNOPSIS
.Nm
.Ar string
.Op Ar suffix
.Nm dirname
.Ar string
.Sh DESCRIPTION
.Nm
deletes any prefix ending with the last slash
.Ql \&/
character present in
.Ar string ,
and a
.Ar suffix ,
if given.
The resulting filename is written to the standard output.
A non-existent suffix is ignored.
.Pp
.Nm dirname
deletes the filename portion, beginning
with the last slash
.Ql \&/
character to the end of
.Ar string ,
and writes the result to the standard output.
.Sh EXIT STATUS
Both the
.Nm
and
.Nm dirname
utilities
exit 0 on success, and \*[Gt]0 if an error occurs.
.Sh EXAMPLES
The following line sets the shell variable
.Ev FOO
to
.Pa /usr/bin .
.Pp
.Dl FOO=`dirname /usr/bin/trail`
.Sh SEE ALSO
.Xr csh 1 ,
.Xr sh 1 ,
.Xr basename 3 ,
.Xr dirname 3
.Sh STANDARDS
The
.Nm
and
.Nm dirname
utilities conform to
.St -p1003.2-92 .

103
usr.bin/basename/basename.c Normal file
View file

@ -0,0 +1,103 @@
/* $NetBSD: basename.c,v 1.15 2011/08/29 14:24:03 joerg Exp $ */
/*-
* Copyright (c) 1991, 1993, 1994
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* 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.
*/
#include <sys/cdefs.h>
#ifndef lint
__COPYRIGHT("@(#) Copyright (c) 1991, 1993, 1994\
The Regents of the University of California. All rights reserved.");
#endif /* not lint */
#ifndef lint
#if 0
static char sccsid[] = "@(#)basename.c 8.4 (Berkeley) 5/4/95";
#endif
__RCSID("$NetBSD: basename.c,v 1.15 2011/08/29 14:24:03 joerg Exp $");
#endif /* not lint */
#include <err.h>
#include <libgen.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
__dead static void usage(void);
int
main(int argc, char **argv)
{
char *p;
int ch;
setlocale(LC_ALL, "");
while ((ch = getopt(argc, argv, "")) != -1)
switch (ch) {
case '?':
default:
usage();
}
argc -= optind;
argv += optind;
if (argc != 1 && argc != 2)
usage();
if (**argv == '\0') {
(void)printf("\n");
exit(0);
}
if ((p = basename(*argv)) == NULL)
err(1, "%s", *argv);
if (*++argv != '\0') {
int suffixlen, stringlen, off;
suffixlen = strlen(*argv);
stringlen = strlen(p);
if (suffixlen < stringlen) {
off = stringlen - suffixlen;
if (strcmp(p + off, *argv) == 0)
p[off] = '\0';
}
}
(void)printf("%s\n", p);
exit(0);
}
static void
usage(void)
{
(void)fprintf(stderr, "usage: basename string [suffix]\n");
exit(1);
}