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