File: argparse.c

package info (click to toggle)
courier 0.60.0-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 52,288 kB
  • ctags: 12,677
  • sloc: ansic: 165,348; cpp: 24,820; sh: 16,410; perl: 6,839; makefile: 3,621; yacc: 289; sed: 16
file content (52 lines) | stat: -rw-r--r-- 877 bytes parent folder | download | duplicates (7)
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
#include	"argparse.h"
#include	<string.h>
#include	<stdlib.h>
#include	<stdio.h>

/*
** Copyright 2000 Double Precision, Inc.
** See COPYING for distribution information.
*/

/* $Id: argparse.c,v 1.2 2000/07/30 01:08:10 mrsam Exp $ */


int argparse(int argc, char **argv, struct args *s)
{
int	argn=1;
int	i;

	while (argn < argc)
	{
	const char *p;
	int	l=0;

		if ( argv[argn][0] != '-')	break;
		if ( argv[argn][1] == 0)
		{
			++argn;
			break;
		}
		for (i=0; s[i].name; i++)
		{
			l=strlen(s[i].name);
			if (strncmp(s[i].name, argv[argn]+1, l) == 0 &&
				(argv[argn][l+1] == 0 ||
				argv[argn][l+1] == '='))	break;
		}
		if (s[i].name == 0)
		{
			fprintf(stderr, "%s: Invalid option: %s\n",
				argv[0], argv[argn]);
			exit(1);
		}
		p=argv[argn]+1+l;
		if (*p)	++p;
		if (s[i].valuep)
			*s[i].valuep=p;
		else
			(*s[i].funcp)(p);
		++argn;
	}
	return (argn);
}