File: getoptstr.c

package info (click to toggle)
yencode 0.46-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,260 kB
  • ctags: 1,019
  • sloc: ansic: 9,628; sh: 3,516; makefile: 371; yacc: 318
file content (62 lines) | stat: -rw-r--r-- 2,069 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**************************************************************************************************
	$Header: /pub/cvsroot/yencode/lib/getoptstr.c,v 1.2 2002/03/10 16:22:19 bboy Exp $

	Copyright (C) 2002  Don Moore <bboy@bboy.net>

	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 of the License, 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
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
**************************************************************************************************/

#include "misc.h"


/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	GETOPTSTR
	Given a list of long opts, constructs the opt string automatically, since I always forget to
	update it.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
char *
getoptstr(struct option const longopts[])
{
	char *optstr, *c;
	register int n, opts;

	// Count number of options
	for (opts = 0; longopts[opts].name; opts++)
		;

	// Allocate optstr
	optstr = (char *)xmalloc(((opts * 3) + 1) * sizeof(char));
	memset(optstr, 0, (opts * 3) + 1);

	// Build optstr
	for (c = optstr, n = 0; n < opts; n++)
	{
		if (longopts[n].val)
		{
			*c++ = longopts[n].val;
			if (longopts[n].has_arg == required_argument)
				*c++ = ':';
			else if (longopts[n].has_arg == optional_argument)
			{
				*c++ = ':';
				*c++ = ':';
			}
		}
	}
	return (optstr);
}
/*--- getoptstr() -------------------------------------------------------------------------------*/

/* vi:set ts=3: */