summaryrefslogtreecommitdiffstats
path: root/old/skeletons/main.c.erb
blob: 1767fbfad53fa34d11c079bf04140b3eed9b4bcd (plain)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<%= header 'c.hdr' %>

#include "main.h"

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdarg.h>
#include <string.h>
#include <getopt.h>


static int verbose = 0;

/**
 * my_error	-	print an error message on stderr then : if(code) exit(code)
 */
static void my_error( int code, const char *fmt, ... )
{
	va_list ap;

	if ( *fmt ) {
	    va_start( ap, fmt );
	    (void)vfprintf( stderr, fmt, ap );
	    va_end( ap );
		fmt += strlen( fmt );
		if ( fmt[-1] != '\n' )
			(void) fputc( '\n', stderr );
	}
	if( code ) exit( code );
	/* NOTREACHED */
}


/**
 * usage	-	print usage on stderr
 */
static void usage( const char *program_name, const char* optstring )
{
	fprintf( stderr, "usage : %s [-%s]\n", program_name, optstring );
	fprintf( stderr, "\t --verbose (-v)        : verbose mode.\n"
			"\t --version (-V)        : print version number.\n"
			"\t --help    (-h)        : print this screen.\n"
			"\t --arg     (-a) <arg>  : a numeric argument.\n"
			"\n" );
}


/**
 * MAIN
 */
int main( int argc, char **argv )
{
	int optch;	/* returned by getopts_long */
	int index;	/* index of longopts[] */

	int a;

	/**
	 * name - has_arg (bool) - flag (int *) - val (int)
	 *
	 * 	if(!longopts[x].flag)
	 * 		return longopts[x].val;
	 * 	else
	 * 		*(longopts[x].flag) = longopts[x].val;
	 * 	return 0;
	 *
	 */
	static struct option longopts[] = {
		{ "help", 0, 0, 'h' },
		{ "version", 0, 0, 'V' },
		{ "verbose", 0, &verbose, 1 },
		{ "arg", 1, 0, 'a' },
		{ 0, 0, 0, 0, }
	};

	static char *optstring = "hvVa:";

	static const char *program_name = PACKAGE_NAME;
	/*
	const char *program_name = file_name( argv[0] );
	*/

	opterr=0;	/* no default advise for bad options */
	optind=0;

	a=0;

#ifdef DEBUG
	printf( "DEBUG mode is set.\n" );
#endif

	/* process options */
	while( ( optch = getopt_long( argc, argv, optstring, longopts, &index ) ) !=-1 ) {
		switch( optch ){
			case 'h':
				usage( program_name, optstring );
				return EXIT_FAILURE;
			case 'V':
				fprintf( stdout, " %s version : %s\n", program_name, VERSION );
				return EXIT_FAILURE;
			case 'v':
				verbose = 1;
				break;
			case 'a':
				a = atoi( optarg );
				break;
			case 0:
				/* longopt used flag */
				break;
			default:
				my_error(EXIT_FAILURE,"invalid option %s\n\ttype : %s --help\n",argv[optind-1],program_name);
		}
	}

	printf("a : %d\n",a);

	/* process other arguments */
	while(optind<argc){
		printf("argv[%d]='%s'\n",optind,argv[optind]);
		optind++;
	}

	if(verbose)printf("verbose mode is set.\n");

	/**
	 * insert some fucking code here.
	 */

	/* end reached */
	return EXIT_SUCCESS;
}