(invalid_arg): Use a single fprintf and whole
[gnulib.git] / lib / argmatch.c
1 /* argmatch.c -- find a match for a string in an array
2    Copyright (C) 1990, 1997 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2, or (at your option)
7    any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; see the file COPYING.
16    If not, write to the Free Software Foundation,
17    59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
18
19 /* Written by David MacKenzie <djm@gnu.ai.mit.edu> */
20
21 #if HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
25 #include <argmatch.h>
26
27 #include <sys/types.h>
28
29 #include <stdio.h>
30 #if HAVE_STRING_H
31 # include <string.h>
32 #else
33 # include <strings.h>
34 #endif
35
36 /* If ARG is an unambiguous match for an element of the
37    null-terminated array OPTLIST, return the index in OPTLIST
38    of the matched element, else -1 if it does not match any element
39    or -2 if it is ambiguous (is a prefix of more than one element).  */
40
41 int
42 argmatch (const char *arg, const char *const *optlist)
43 {
44   int i;                        /* Temporary index in OPTLIST.  */
45   size_t arglen;                /* Length of ARG.  */
46   int matchind = -1;            /* Index of first nonexact match.  */
47   int ambiguous = 0;            /* If nonzero, multiple nonexact match(es).  */
48
49   arglen = strlen (arg);
50
51   /* Test all elements for either exact match or abbreviated matches.  */
52   for (i = 0; optlist[i]; i++)
53     {
54       if (!strncmp (optlist[i], arg, arglen))
55         {
56           if (strlen (optlist[i]) == arglen)
57             /* Exact match found.  */
58             return i;
59           else if (matchind == -1)
60             /* First nonexact match found.  */
61             matchind = i;
62           else
63             /* Second nonexact match found.  */
64             ambiguous = 1;
65         }
66     }
67   if (ambiguous)
68     return -2;
69   else
70     return matchind;
71 }
72
73 /* Error reporting for argmatch.
74    KIND is a description of the type of entity that was being matched.
75    VALUE is the invalid value that was given.
76    PROBLEM is the return value from argmatch.  */
77
78 void
79 invalid_arg (const char *kind, const char *value, int problem)
80 {
81   const char *fmt = (problem == -1
82                      ? "%s: invalid %s `%s'\n"
83                      : "%s: ambiguous %s `%s'\n");
84   fprintf (stderr, fmt, program_name, kind, value);
85 }