Varie correzioni, completata revisione capitolo sull'I/O su file
[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  ****************************************************************/
30 /* 
31  * Include needed headers
32  */
33 #define _GNU_SOURCE
34 #include <errno.h>       /* error definitions and routines */ 
35 #include <stdlib.h>      /* C standard library */
36 #include <unistd.h>      /* unix standard library */
37 #include <string.h>      /* C strings library */
38 #include <limits.h>      /* system limits constants, types and functions */
39 #include <stdio.h>       /* standard I/O library */
40 #include <time.h>        /* date and time constants, types and functions */
41
42
43 /* Table of constants for sysconf() */
44 char *sc_names[]={"_SC_ARG_MAX",
45                   "_SC_CHILD_MAX",
46                   "_SC_OPEN_MAX",
47                   "_SC_STREAM_MAX",
48                   "_SC_TZNAME_MAX",
49                   "_SC_NGROUPS_MAX",
50                   "_SC_SSIZE_MAX",
51                   "_SC_CLK_TCK",
52                   "_SC_JOB_CONTROL",
53                   "_SC_SAVED_IDS",
54                   "_SC_VERSION"};
55
56 int sc_argument[]={_SC_ARG_MAX,
57                    _SC_CHILD_MAX,
58                    _SC_OPEN_MAX,
59                    _SC_STREAM_MAX,
60                    _SC_TZNAME_MAX,
61                    _SC_NGROUPS_MAX,
62                    _SC_SSIZE_MAX,
63                    _SC_CLK_TCK,
64                    _SC_JOB_CONTROL,
65                    _SC_SAVED_IDS,
66                    _SC_VERSION};
67
68 /* 
69    Set the defined[] array to true if the macro is defined else set it
70    to false and define the macro to -1 as a marker
71 */
72 int defined[]={
73 #ifdef  ARG_MAX
74     1,
75 #else
76 #define ARG_MAX               -1  
77     0,
78 #endif
79 #ifdef  CHILD_MAX
80     1,
81 #else
82 #define CHILD_MAX             -1
83     0,
84 #endif
85 #ifdef  OPEN_MAX
86     1,
87 #else
88 #define OPEN_MAX              -1 
89     0,
90 #endif
91 #ifdef  STREAM_MAX
92     1,
93 #else
94 #define STREAM_MAX            -1
95     0,
96 #endif
97 #ifdef  NGROUPS_MAX
98     1,
99 #else
100 #define NGROUPS_MAX           -1
101     0,
102 #endif
103 #ifdef  TZNAME_MAX
104     1,
105 #else
106 #define TZNAME_MAX            -1
107     0,
108 #endif
109 #ifdef  SSIZE_MAX
110     1,
111 #else
112 #define SSIZE_MAX             -1
113     0
114 #endif
115 };
116
117
118
119 /* values of stadard macros */
120 long values[]={ARG_MAX, 
121                CHILD_MAX,
122                OPEN_MAX,
123                STREAM_MAX,
124                TZNAME_MAX,
125                NGROUPS_MAX,
126                SSIZE_MAX,
127                CLOCKS_PER_SEC,
128                _POSIX_JOB_CONTROL,
129                _POSIX_SAVED_IDS,
130                _POSIX_VERSION};
131
132 /* Help printing routine */
133 void usage(void);
134
135 int main(int argc, char *argv[])
136 {
137 /* 
138  * Variables definition  
139  */
140     int i;
141     /*
142      * Input section: decode command line parameters 
143      * Use getopt function
144      */
145     opterr = 0;  /* don't want writing to stderr */
146     while ( (i = getopt(argc, argv, "h")) != -1) {
147         switch (i) {
148         /* 
149          * Handling options 
150          */ 
151         case 'h':   /* help option */
152             printf("Wrong -h option use\n");
153             usage();
154             return -1;
155             break;
156         case '?':   /* unrecognized options */
157             printf("Unrecognized options -%c\n",optopt);
158             usage();
159         default:    /* should not reached */
160             usage();
161         }
162     }
163     /* ***********************************************************
164      * 
165      *           Options processing completed
166      *
167      *                Main code beginning
168      * 
169      * ***********************************************************/
170     if ( (argc-optind) != 1 )  {
171         printf("From %d arguments, removed %d options\n", argc, optind);
172         usage();
173     }
174     for (i=0; i<=4; i++) {
175         printf("Response for %s is %ld, values is %ld\n",sc_names[i], 
176                sysconf(sc_argument[i]), values[i]);
177     }
178     return 0;
179 }
180 /*
181  * routine to print usage info and exit
182  */
183 void usage(void) {
184     printf("Program getparam : test fopen for a file  \n");
185     printf("Usage:\n");
186     printf("  getparam [-h] parameter \n");
187     printf("  -h           print this help\n");
188     
189     exit(1);
190 }
191