1992-11-01 05:44:29 +00:00
|
|
|
/* dirname -- strip filename suffix from pathname
|
2001-01-28 21:58:19 +00:00
|
|
|
Copyright (C) 1990-1997, 1999, 2000, 2001 Free Software Foundation, Inc.
|
1992-11-01 05:44:29 +00:00
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
|
any later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
1996-03-24 18:33:12 +00:00
|
|
|
along with this program; if not, write to the Free Software Foundation,
|
|
|
|
|
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
|
1992-11-01 05:44:29 +00:00
|
|
|
|
|
|
|
|
/* Written by David MacKenzie and Jim Meyering. */
|
|
|
|
|
|
1993-10-12 14:49:11 +00:00
|
|
|
#include <config.h>
|
1992-11-01 05:44:29 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <sys/types.h>
|
1993-09-08 18:23:12 +00:00
|
|
|
|
1992-11-01 05:44:29 +00:00
|
|
|
#include "system.h"
|
1994-02-13 20:27:08 +00:00
|
|
|
#include "long-options.h"
|
1994-12-20 05:26:44 +00:00
|
|
|
#include "error.h"
|
2000-10-25 08:39:29 +00:00
|
|
|
#include "dirname.h"
|
2000-05-07 14:51:07 +00:00
|
|
|
#include "closeout.h"
|
2000-12-08 07:55:05 +00:00
|
|
|
#include "xalloc.h"
|
1992-11-01 05:44:29 +00:00
|
|
|
|
1999-03-31 04:11:35 +00:00
|
|
|
/* The official name of this program (e.g., no `g' prefix). */
|
|
|
|
|
#define PROGRAM_NAME "dirname"
|
|
|
|
|
|
2001-08-14 07:10:10 +00:00
|
|
|
#define AUTHORS N_ ("David MacKenzie and Jim Meyering")
|
1999-03-31 04:11:35 +00:00
|
|
|
|
1993-09-08 18:23:12 +00:00
|
|
|
/* The name this program was run with. */
|
|
|
|
|
char *program_name;
|
|
|
|
|
|
1999-01-25 14:33:38 +00:00
|
|
|
void
|
1996-01-06 11:44:05 +00:00
|
|
|
usage (int status)
|
1993-09-08 18:23:12 +00:00
|
|
|
{
|
1993-10-17 03:57:04 +00:00
|
|
|
if (status != 0)
|
1995-08-08 04:37:34 +00:00
|
|
|
fprintf (stderr, _("Try `%s --help' for more information.\n"),
|
1993-10-17 03:57:04 +00:00
|
|
|
program_name);
|
|
|
|
|
else
|
1993-10-26 00:11:14 +00:00
|
|
|
{
|
1995-08-08 04:37:34 +00:00
|
|
|
printf (_("\
|
1995-05-15 04:53:56 +00:00
|
|
|
Usage: %s NAME\n\
|
1994-02-14 21:19:12 +00:00
|
|
|
or: %s OPTION\n\
|
1995-08-08 04:37:34 +00:00
|
|
|
"),
|
1994-02-14 21:19:12 +00:00
|
|
|
program_name, program_name);
|
2001-12-15 20:46:30 +00:00
|
|
|
fputs (_("\
|
1995-05-15 04:53:56 +00:00
|
|
|
Print NAME with its trailing /component removed; if NAME contains no /'s,\n\
|
|
|
|
|
output `.' (meaning the current directory).\n\
|
1993-10-17 03:57:04 +00:00
|
|
|
\n\
|
2001-12-15 20:46:30 +00:00
|
|
|
"), stdout);
|
|
|
|
|
fputs (HELP_OPTION_DESCRIPTION, stdout);
|
|
|
|
|
fputs (VERSION_OPTION_DESCRIPTION, stdout);
|
1998-09-19 17:32:35 +00:00
|
|
|
puts (_("\nReport bugs to <bug-sh-utils@gnu.org>."));
|
1993-10-26 00:11:14 +00:00
|
|
|
}
|
1993-10-17 03:57:04 +00:00
|
|
|
exit (status);
|
1993-09-08 18:23:12 +00:00
|
|
|
}
|
|
|
|
|
|
1996-03-21 22:47:02 +00:00
|
|
|
int
|
1996-01-06 11:44:05 +00:00
|
|
|
main (int argc, char **argv)
|
1992-11-01 05:44:29 +00:00
|
|
|
{
|
2001-05-12 16:03:19 +00:00
|
|
|
static char const dot = '.';
|
2001-05-27 15:03:09 +00:00
|
|
|
char const *result;
|
2001-05-12 16:03:19 +00:00
|
|
|
size_t len;
|
1992-11-01 05:44:29 +00:00
|
|
|
|
1993-09-08 18:23:12 +00:00
|
|
|
program_name = argv[0];
|
1996-03-12 23:49:29 +00:00
|
|
|
setlocale (LC_ALL, "");
|
|
|
|
|
bindtextdomain (PACKAGE, LOCALEDIR);
|
|
|
|
|
textdomain (PACKAGE);
|
1993-09-08 18:23:12 +00:00
|
|
|
|
2000-05-07 14:51:07 +00:00
|
|
|
atexit (close_stdout);
|
|
|
|
|
|
1999-03-31 04:11:35 +00:00
|
|
|
parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
|
|
|
|
|
AUTHORS, usage);
|
1999-05-01 02:28:54 +00:00
|
|
|
/* The above handles --help and --version.
|
|
|
|
|
Since there is no other invocation of getopt, handle `--' here. */
|
|
|
|
|
if (argc > 1 && STREQ (argv[1], "--"))
|
|
|
|
|
{
|
|
|
|
|
--argc;
|
|
|
|
|
++argv;
|
|
|
|
|
}
|
1993-09-08 18:23:12 +00:00
|
|
|
|
1994-02-13 20:27:08 +00:00
|
|
|
if (argc != 2)
|
1994-10-21 05:25:03 +00:00
|
|
|
{
|
1996-04-30 03:29:01 +00:00
|
|
|
error (0, 0, argc < 2 ? _("too few arguments")
|
|
|
|
|
: _("too many arguments"));
|
1994-10-21 05:25:03 +00:00
|
|
|
usage (1);
|
|
|
|
|
}
|
1993-09-08 18:23:12 +00:00
|
|
|
|
2001-05-12 16:03:19 +00:00
|
|
|
result = argv[1];
|
|
|
|
|
len = dir_len (result);
|
|
|
|
|
|
|
|
|
|
if (! len)
|
|
|
|
|
{
|
|
|
|
|
result = ˙
|
|
|
|
|
len = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fwrite (result, 1, len, stdout);
|
|
|
|
|
putchar ('\n');
|
1992-11-01 05:44:29 +00:00
|
|
|
|
|
|
|
|
exit (0);
|
|
|
|
|
}
|