1 /**************************************************************************************************
2 	$Id: getoptstr.c,v 1.11 2005/04/20 16:49:11 bboy Exp $
3 
4 	Copyright (C) 2002-2005  Don Moore <bboy@bboy.net>
5 
6 	This program is free software; you can redistribute it and/or modify
7 	it under the terms of the GNU General Public License as published by
8 	the Free Software Foundation; either version 2 of the License, or
9 	(at Your option) any later version.
10 
11 	This program is distributed in the hope that it will be useful,
12 	but WITHOUT ANY WARRANTY; without even the implied warranty of
13 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 	GNU General Public License for more details.
15 
16 	You should have received a copy of the GNU General Public License
17 	along with this program; if not, write to the Free Software
18 	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 **************************************************************************************************/
20 
21 #include "mydnsutil.h"
22 
23 
24 /**************************************************************************************************
25 	GETOPTSTR
26 	Given a list of long opts, constructs the opt string automatically, since I always forget to
27 	update it.
28 **************************************************************************************************/
29 char *
getoptstr(struct option const longopts[])30 getoptstr(struct option const longopts[]) {
31   static char *optstr = NULL;
32   char *c;
33   register int n, opts;
34   int optsize = 0;
35 
36   /* Count number of options */
37   for (opts = 0; longopts[opts].name; opts++) {
38     optsize += 1 + ((longopts[opts].has_arg == required_argument)
39 		    ?1
40 		    :(longopts[opts].has_arg == optional_argument)
41 		    ?2
42 		    :0);
43   }
44 
45 #if DEBUG_ENABLED
46   Debug(_("Optstring is %d in size with %d elements"), optsize, opts);
47 #endif
48 
49   /* Allocate optstr */
50   optstr = REALLOCATE(optstr, optsize + 1, char[]);
51   memset(optstr, 0, optsize + 1);
52 
53   /* Build optstr */
54   for (c = optstr, n = 0; n < opts && c - optstr < optsize; n++) {
55     if (longopts[n].val) {
56       *c++ = longopts[n].val;
57       if (longopts[n].has_arg == required_argument)
58 	*c++ = ':';
59       else if (longopts[n].has_arg == optional_argument) {
60 	*c++ = ':';
61 	*c++ = ':';
62       }
63     }
64   }
65   return (optstr);
66 }
67 /*--- getoptstr() -------------------------------------------------------------------------------*/
68 
69 /* vi:set ts=3: */
70