eb16fc0eb2dd5564834b41831b194c5218b1b855
[gapil.git] / sources / ProcInfo.c
1 /* ProcInfo.c
2  * 
3  * Copyright (C) 2001 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 test_fopen.c: 
22  * Program to test function fopen
23  *
24  * Author: Simone Piccardi
25  * Dec. 2001
26  *
27  * Usage: procinfo -h give all info's
28  *
29  * $Id: ProcInfo.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 <stdio.h>       /* standard I/O library */
40 #include <string.h>      /* string functions */
41
42 /* Help printing routine */
43 void usage(void);
44
45 int main(int argc, char *argv[])
46 {
47 /* 
48  * Variables definition  
49  */
50     int i;
51     FILE * file;
52     char * ptr;
53     /*
54      * Input section: decode command line parameters 
55      * Use getopt function
56      */
57     opterr = 0;  /* don't want writing to stderr */
58     while ( (i = getopt(argc, argv, "h")) != -1) {
59         switch (i) {
60         /* 
61          * Handling options 
62          */ 
63         case 'h':   /* help option */
64             printf("Wrong -h option use\n");
65             usage();
66             return -1;
67             break;
68         case '?':   /* unrecognized options */
69             printf("Unrecognized options -%c\n",optopt);
70             usage();
71         default:    /* should not reached */
72             usage();
73         }
74     }
75     /* ***********************************************************
76      * 
77      *           Options processing completed
78      *
79      *                Main code beginning
80      * 
81      * ***********************************************************/
82     /* There must be 2 remaing parameters */
83     if ( (argc-optind) != 2 )  {
84         printf("From %d arguments, removed %d options\n", argc, optind);
85         usage();
86     }
87     if ( !(file = fopen(argv[1], argv[2]))) {
88         perror("cannot open file");
89         exit(1);
90     }
91     i = 100;
92     ptr = malloc(i);
93     getline(&ptr, &i, file);
94 //    fclean(file);  /* do not exist on Linux */
95     flockfile(file);
96     fclose(file);
97     return 0;
98 }
99 /*
100  * routine to print usage info and exit
101  */
102 void usage(void) {
103     printf("Program testfopen : test fopen for a file  \n");
104     printf("Usage:\n");
105     printf("  testfopen [-h] file mode \n");
106     printf("  -h           print this help\n");
107     
108     exit(1);
109 }
110