Vari programmi di test.
[gapil.git] / sources / ProcInfo.c
diff --git a/sources/ProcInfo.c b/sources/ProcInfo.c
new file mode 100644 (file)
index 0000000..eb16fc0
--- /dev/null
@@ -0,0 +1,110 @@
+/* ProcInfo.c
+ * 
+ * Copyright (C) 2001 Simone Piccardi
+ * 
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+/****************************************************************
+ *
+ * Program test_fopen.c: 
+ * Program to test function fopen
+ *
+ * Author: Simone Piccardi
+ * Dec. 2001
+ *
+ * Usage: procinfo -h give all info's
+ *
+ * $Id: ProcInfo.c,v 1.1 2002/01/03 23:44:14 piccardi Exp $
+ *
+ ****************************************************************/
+/* 
+ * Include needed headers
+ */
+#define _GNU_SOURCE
+#include <errno.h>       /* error definitions and routines */ 
+#include <stdlib.h>      /* C standard library */
+#include <unistd.h>      /* unix standard library */
+#include <stdio.h>      /* standard I/O library */
+#include <string.h>      /* string functions */
+
+/* Help printing routine */
+void usage(void);
+
+int main(int argc, char *argv[])
+{
+/* 
+ * Variables definition  
+ */
+    int i;
+    FILE * file;
+    char * ptr;
+    /*
+     * Input section: decode command line parameters 
+     * Use getopt function
+     */
+    opterr = 0;         /* don't want writing to stderr */
+    while ( (i = getopt(argc, argv, "h")) != -1) {
+       switch (i) {
+       /* 
+        * Handling options 
+        */ 
+       case 'h':   /* help option */
+           printf("Wrong -h option use\n");
+           usage();
+           return -1;
+           break;
+       case '?':   /* unrecognized options */
+           printf("Unrecognized options -%c\n",optopt);
+           usage();
+       default:    /* should not reached */
+           usage();
+       }
+    }
+    /* ***********************************************************
+     * 
+     *          Options processing completed
+     *
+     *               Main code beginning
+     * 
+     * ***********************************************************/
+    /* There must be 2 remaing parameters */
+    if ( (argc-optind) != 2 )  {
+       printf("From %d arguments, removed %d options\n", argc, optind);
+       usage();
+    }
+    if ( !(file = fopen(argv[1], argv[2]))) {
+       perror("cannot open file");
+       exit(1);
+    }
+    i = 100;
+    ptr = malloc(i);
+    getline(&ptr, &i, file);
+//    fclean(file);  /* do not exist on Linux */
+    flockfile(file);
+    fclose(file);
+    return 0;
+}
+/*
+ * routine to print usage info and exit
+ */
+void usage(void) {
+    printf("Program testfopen : test fopen for a file  \n");
+    printf("Usage:\n");
+    printf("  testfopen [-h] file mode \n");
+    printf("  -h          print this help\n");
+    
+    exit(1);
+}
+