From 0b8a9e801d87951bf6562d7043594da801f85a64 Mon Sep 17 00:00:00 2001 From: Lionel Sambuc Date: Fri, 23 Aug 2013 19:35:37 +0200 Subject: [PATCH] Importing usr.bin/basename Change-Id: I2c71c83bc6a420390c4aed015373f78ebf425903 --- commands/Makefile | 2 +- commands/basename/Makefile | 4 -- commands/basename/basename.c | 76 -------------------------- distrib/sets/lists/minix/mi | 1 + man/man1/Makefile | 2 +- man/man1/basename.1 | 39 ------------- releasetools/nbsd_ports | 1 + usr.bin/Makefile | 2 +- usr.bin/basename/Makefile | 7 +++ usr.bin/basename/basename.1 | 92 +++++++++++++++++++++++++++++++ usr.bin/basename/basename.c | 103 +++++++++++++++++++++++++++++++++++ 11 files changed, 207 insertions(+), 122 deletions(-) delete mode 100644 commands/basename/Makefile delete mode 100644 commands/basename/basename.c delete mode 100644 man/man1/basename.1 create mode 100644 usr.bin/basename/Makefile create mode 100644 usr.bin/basename/basename.1 create mode 100644 usr.bin/basename/basename.c diff --git a/commands/Makefile b/commands/Makefile index ac031bd4b..79486fe01 100644 --- a/commands/Makefile +++ b/commands/Makefile @@ -2,7 +2,7 @@ .include -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 \ diff --git a/commands/basename/Makefile b/commands/basename/Makefile deleted file mode 100644 index 7a5acc21d..000000000 --- a/commands/basename/Makefile +++ /dev/null @@ -1,4 +0,0 @@ -PROG= basename -MAN= - -.include diff --git a/commands/basename/basename.c b/commands/basename/basename.c deleted file mode 100644 index bd170837e..000000000 --- a/commands/basename/basename.c +++ /dev/null @@ -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 -#include -#include - -#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); -} diff --git a/distrib/sets/lists/minix/mi b/distrib/sets/lists/minix/mi index 8795d61d0..726e1778d 100644 --- a/distrib/sets/lists/minix/mi +++ b/distrib/sets/lists/minix/mi @@ -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 diff --git a/man/man1/Makefile b/man/man1/Makefile index fd4cc970e..a38ddaba1 100644 --- a/man/man1/Makefile +++ b/man/man1/Makefile @@ -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 \ diff --git a/man/man1/basename.1 b/man/man1/basename.1 deleted file mode 100644 index 5f2c89549..000000000 --- a/man/man1/basename.1 +++ /dev/null @@ -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. diff --git a/releasetools/nbsd_ports b/releasetools/nbsd_ports index 72bb403f4..9bde208b3 100644 --- a/releasetools/nbsd_ports +++ b/releasetools/nbsd_ports @@ -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 diff --git a/usr.bin/Makefile b/usr.bin/Makefile index fd22dcc3b..27999760d 100644 --- a/usr.bin/Makefile +++ b/usr.bin/Makefile @@ -4,7 +4,7 @@ .include SUBDIR= \ - banner bdes \ + banner basename bdes \ bzip2 bzip2recover \ cal chpass cksum \ col ctags \ diff --git a/usr.bin/basename/Makefile b/usr.bin/basename/Makefile new file mode 100644 index 000000000..455b287b6 --- /dev/null +++ b/usr.bin/basename/Makefile @@ -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 diff --git a/usr.bin/basename/basename.1 b/usr.bin/basename/basename.1 new file mode 100644 index 000000000..3cbb8b223 --- /dev/null +++ b/usr.bin/basename/basename.1 @@ -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 . diff --git a/usr.bin/basename/basename.c b/usr.bin/basename/basename.c new file mode 100644 index 000000000..5bd8f9f15 --- /dev/null +++ b/usr.bin/basename/basename.c @@ -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 +#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 +#include +#include +#include +#include +#include +#include + +__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); +} -- 2.44.0