1
0
mirror of git://git.sv.gnu.org/coreutils.git synced 2026-04-20 02:36:16 +02:00
Files
coreutils/src/dirname.c

109 lines
2.7 KiB
C
Raw Normal View History

1992-11-01 05:44:29 +00:00
/* dirname -- strip filename suffix from pathname
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>
1992-11-01 05:44:29 +00:00
#include "system.h"
1994-02-13 20:27:08 +00:00
#include "long-options.h"
#include "error.h"
#include "dirname.h"
#include "closeout.h"
#include "xalloc.h"
1992-11-01 05:44:29 +00:00
/* The official name of this program (e.g., no `g' prefix). */
#define PROGRAM_NAME "dirname"
#define AUTHORS N_ ("David MacKenzie and Jim Meyering")
/* 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-10-17 03:57:04 +00:00
if (status != 0)
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
{
printf (_("\
Usage: %s NAME\n\
1994-02-14 21:19:12 +00:00
or: %s OPTION\n\
"),
1994-02-14 21:19:12 +00:00
program_name, program_name);
fputs (_("\
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\
"), 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);
}
int
1996-01-06 11:44:05 +00:00
main (int argc, char **argv)
1992-11-01 05:44:29 +00:00
{
static char const dot = '.';
char const *result;
size_t len;
1992-11-01 05:44:29 +00:00
program_name = argv[0];
setlocale (LC_ALL, "");
bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE);
atexit (close_stdout);
parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
AUTHORS, usage);
/* The above handles --help and --version.
Since there is no other invocation of getopt, handle `--' here. */
if (argc > 1 && STREQ (argv[1], "--"))
{
--argc;
++argv;
}
1994-02-13 20:27:08 +00:00
if (argc != 2)
1994-10-21 05:25:03 +00:00
{
error (0, 0, argc < 2 ? _("too few arguments")
: _("too many arguments"));
1994-10-21 05:25:03 +00:00
usage (1);
}
result = argv[1];
len = dir_len (result);
if (! len)
{
result = &dot;
len = 1;
}
fwrite (result, 1, len, stdout);
putchar ('\n');
1992-11-01 05:44:29 +00:00
exit (0);
}