summaryrefslogtreecommitdiff
path: root/src/basename.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/basename.c')
-rw-r--r--src/basename.c60
1 files changed, 54 insertions, 6 deletions
diff --git a/src/basename.c b/src/basename.c
index f8d9e17f6..01e3d4375 100644
--- a/src/basename.c
+++ b/src/basename.c
@@ -27,6 +27,9 @@
#include <stdio.h>
#include <sys/types.h>
+#include <getopt.h>
+
+#include "version.h"
#include "system.h"
char *basename ();
@@ -34,25 +37,70 @@ void strip_trailing_slashes ();
static void remove_suffix ();
+/* The name this program was run with. */
+char *program_name;
+
+/* If non-zero, display usage information and exit. */
+static int show_help;
+
+/* If non-zero, print the version on standard error. */
+static int show_version;
+
+static struct option const long_options[] =
+{
+ {"help", no_argument, &show_help, 1},
+ {"version", no_argument, &show_version, 1},
+ {0, 0, 0, 0}
+};
+
+static void
+usage ()
+{
+ fprintf (stderr, "Usage: %s [{--help,--version}] name [suffix]\n",
+ program_name);
+ exit (1);
+}
+
void
main (argc, argv)
int argc;
char **argv;
{
char *name;
+ int c;
+
+ program_name = argv[0];
+
+ while ((c = getopt_long (argc, argv, "", long_options, (int *) 0)) != EOF)
+ {
+ switch (c)
+ {
+ case 0:
+ break;
+
+ default:
+ usage ();
+ }
+ }
- if (argc == 1 || argc > 3)
+ if (show_version)
{
- fprintf (stderr, "Usage: %s name [suffix]\n", argv[0]);
- exit (1);
+ printf ("%s\n", version_string);
+ exit (0);
}
- strip_trailing_slashes (argv[1]);
+ if (show_help)
+ usage ();
+
+ if (argc - optind == 0 || argc - optind > 2)
+ usage ();
+
+ strip_trailing_slashes (argv[optind]);
- name = basename (argv[1]);
+ name = basename (argv[optind]);
if (argc == 3)
- remove_suffix (name, argv[2]);
+ remove_suffix (name, argv[optind + 1]);
puts (name);