54212e35b386e7c31c807f54f3963673f42e879e
[gapil.git] / sources / getparam.c
1 /* getparam.c
2  * 
3  * Copyright (C) 2002 Simone Piccardi
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or (at
8  * your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 /****************************************************************
20  *
21  * Program getparam.c: 
22  * Program to test function fopen
23  *
24  * Author: Simone Piccardi
25  * Jan. 2002
26  *
27  * Usage: getparam -h give all info's
28  *
29  * $Id: getparam.c,v 1.1 2002/01/03 23:44:14 piccardi Exp $
30  *
31  ****************************************************************/
32 /* 
33  * Include needed headers
34  */
35 #define _GNU_SOURCE
36 #include <errno.h>       /* error definitions and routines */ 
37 #include <stdlib.h>      /* C standard library */
38 #include <unistd.h>      /* unix standard library */
39 #include <string.h>      /* string functions */
40 #include <limits.h>
41 #include <stdio.h>
42
43
44 #include <time.h>        /* for CLK_TCK e CLOCKS_PER_SEC */
45
46 /* Table of constants for sysconf() */
47 char *sc_names[]={"_SC_ARG_MAX",
48                   "_SC_CHILD_MAX",
49                   "_SC_OPEN_MAX",
50                   "_SC_STREAM_MAX",
51                   "_SC_TZNAME_MAX",
52                   "_SC_NGROUPS_MAX",
53                   "_SC_SSIZE_MAX",
54                   "_SC_CLK_TCK",
55                   "_SC_JOB_CONTROL",
56                   "_SC_SAVED_IDS",
57                   "_SC_VERSION"};
58
59 int sc_argument[]={_SC_ARG_MAX,
60                    _SC_CHILD_MAX,
61                    _SC_OPEN_MAX,
62                    _SC_STREAM_MAX,
63                    _SC_TZNAME_MAX,
64                    _SC_NGROUPS_MAX,
65                    _SC_SSIZE_MAX,
66                    _SC_CLK_TCK,
67                    _SC_JOB_CONTROL,
68                    _SC_SAVED_IDS,
69                    _SC_VERSION};
70
71 /* 
72    Set the defined[] array to true if the macro is defined else set it
73    to false and define the macro to -1 as a marker
74 */
75 int defined[]={
76 #ifdef  ARG_MAX
77     1,
78 #else
79 #define ARG_MAX               -1  
80     0,
81 #endif
82 #ifdef  CHILD_MAX
83     1,
84 #else
85 #define CHILD_MAX             -1
86     0,
87 #endif
88 #ifdef  OPEN_MAX
89     1,
90 #else
91 #define OPEN_MAX              -1 
92     0,
93 #endif
94 #ifdef  STREAM_MAX
95     1,
96 #else
97 #define STREAM_MAX            -1
98     0,
99 #endif
100 #ifdef  NGROUPS_MAX
101     1,
102 #else
103 #define NGROUPS_MAX           -1
104     0,
105 #endif
106 #ifdef  TZNAME_MAX
107     1,
108 #else
109 #define TZNAME_MAX            -1
110     0,
111 #endif
112 #ifdef  SSIZE_MAX
113     1
114 #else
115 #define SSIZE_MAX             -1
116     0
117 };
118
119
120
121 /* values of stadard macros */
122 long values[]={ARG_MAX, 
123                CHILD_MAX,
124                OPEN_MAX,
125                STREAM_MAX,
126                TZNAME_MAX,
127                NGROUPS_MAX,
128                SSIZE_MAX,
129                CLOCKS_PER_SEC,
130                _POSIX_JOB_CONTROL,
131                _POSIX_SAVED_IDS,
132                _POSIX_VERSION};
133
134 /* Help printing routine */
135 void usage(void);
136
137 int main(int argc, char *argv[])
138 {
139 /* 
140  * Variables definition  
141  */
142     int i;
143     /*
144      * Input section: decode command line parameters 
145      * Use getopt function
146      */
147     opterr = 0;  /* don't want writing to stderr */
148     while ( (i = getopt(argc, argv, "h")) != -1) {
149         switch (i) {
150         /* 
151          * Handling options 
152          */ 
153         case 'h':   /* help option */
154             printf("Wrong -h option use\n");
155             usage();
156             return -1;
157             break;
158         case '?':   /* unrecognized options */
159             printf("Unrecognized options -%c\n",optopt);
160             usage();
161         default:    /* should not reached */
162             usage();
163         }
164     }
165     /* ***********************************************************
166      * 
167      *           Options processing completed
168      *
169      *                Main code beginning
170      * 
171      * ***********************************************************/
172     /* There must be 2 remaing parameters */
173     if ( (argc-optind) != 1 )  {
174         printf("From %d arguments, removed %d options\n", argc, optind);
175         usage();
176     }
177     for (i=0; i<=4; i++) {
178         printf("Response for %s is %ld, values is %ld\n", names[i], 
179                sysconf(argument[i]), values[i]);
180     }
181     return 0;
182 }
183 /*
184  * routine to print usage info and exit
185  */
186 void usage(void) {
187     printf("Program testfopen : test fopen for a file  \n");
188     printf("Usage:\n");
189     printf("  testfopen [-h] file mode \n");
190     printf("  -h           print this help\n");
191     
192     exit(1);
193 }
194