2 * ircd-ratbox: A slightly useful ircd.
3 * getopt.c: Uses getopt to fetch the command line options.
5 * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
6 * Copyright (C) 1996-2002 Hybrid Development Team
7 * Copyright (C) 2002-2005 ircd-ratbox development team
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
24 * $Id: getopt.c 26 2006-09-20 18:02:06Z spb $
29 #include "ircd_getopt.h"
34 parseargs(int *argc
, char ***argv
, struct lgetopt
*opts
)
37 char *progname
= (*argv
)[0];
39 /* loop through each argument */
52 /* check if it *is* an arg.. */
53 if((*argv
)[0][0] != OPTCHAR
)
60 /* search through our argument list, and see if it matches */
61 for (i
= 0; opts
[i
].opt
; i
++)
63 if(!strcmp(opts
[i
].opt
, (*argv
)[0]))
65 /* found our argument */
68 switch (opts
[i
].argtype
)
71 *((int *) opts
[i
].argloc
) = 1;
77 "Error: option '%c%s' requires an argument\n",
78 OPTCHAR
, opts
[i
].opt
);
82 *((int *) opts
[i
].argloc
) = atoi((*argv
)[1]);
91 "error: option '%c%s' requires an argument\n",
92 OPTCHAR
, opts
[i
].opt
);
96 *((char **) opts
[i
].argloc
) =
97 malloc(strlen((*argv
)[1]) + 1);
98 strcpy(*((char **) opts
[i
].argloc
), (*argv
)[1]);
106 /*NOTREACHED*/ default:
108 "Error: internal error in parseargs() at %s:%d\n",
116 fprintf(stderr
, "error: unknown argument '%c%s'\n", OPTCHAR
, (*argv
)[0]);
127 fprintf(stderr
, "Usage: %s [options]\n", name
);
128 fprintf(stderr
, "Where valid options are:\n");
130 for (i
= 0; myopts
[i
].opt
; i
++)
132 fprintf(stderr
, "\t%c%-10s %-20s%s\n", OPTCHAR
,
133 myopts
[i
].opt
, (myopts
[i
].argtype
== YESNO
134 || myopts
[i
].argtype
==
135 USAGE
) ? "" : myopts
[i
].argtype
==
136 INTEGER
? "<number>" : "<string>", myopts
[i
].desc
);