6410bb572e60fc27e63c9ff52b2a82b36f7fa2cd
[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 read system parameters
23  *
24  * Author: Simone Piccardi
25  * Jan. 2002
26  *
27  * Usage: getparam -h give all info's
28  *
29  * $Id: getparam.c,v 1.3 2003/05/02 09:55: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 #endif
118 };
119
120
121
122 /* values of stadard macros */
123 long values[]={ARG_MAX, 
124                CHILD_MAX,
125                OPEN_MAX,
126                STREAM_MAX,
127                TZNAME_MAX,
128                NGROUPS_MAX,
129                SSIZE_MAX,
130                CLOCKS_PER_SEC,
131                _POSIX_JOB_CONTROL,
132                _POSIX_SAVED_IDS,
133                _POSIX_VERSION};
134
135 /* Help printing routine */
136 void usage(void);
137
138 int main(int argc, char *argv[])
139 {
140 /* 
141  * Variables definition  
142  */
143     int i;
144     /*
145      * Input section: decode command line parameters 
146      * Use getopt function
147      */
148     opterr = 0;  /* don't want writing to stderr */
149     while ( (i = getopt(argc, argv, "h")) != -1) {
150         switch (i) {
151         /* 
152          * Handling options 
153          */ 
154         case 'h':   /* help option */
155             printf("Wrong -h option use\n");
156             usage();
157             return -1;
158             break;
159         case '?':   /* unrecognized options */
160             printf("Unrecognized options -%c\n",optopt);
161             usage();
162         default:    /* should not reached */
163             usage();
164         }
165     }
166     /* ***********************************************************
167      * 
168      *           Options processing completed
169      *
170      *                Main code beginning
171      * 
172      * ***********************************************************/
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 getparam : test fopen for a file  \n");
188     printf("Usage:\n");
189     printf("  getparam [-h] parameter \n");
190     printf("  -h           print this help\n");
191     
192     exit(1);
193 }
194