Varie correzioni, completata revisione capitolo sull'I/O su file
[gapil.git] / sources / mygetclock.c
1 /* mygetclock.c
2  * 
3  * Copyright (C) 2010 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 3 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  * File mygetclock.c: An example host command
22  *
23  * Author: S. Piccardi May. 2010
24  *
25  *****************************************************************************/
26 #include <stdlib.h>      /* C standard library */
27 #include <stdio.h>       /* standard I/O library */
28 #include <unistd.h>      /* unix standard library */
29 #include <time.h>        /* time library */
30
31
32 /*
33  * Program mygetclock
34  */
35 /* Help printing routine */
36 void usage(void);
37
38 int main(int argc, char *argv[]) 
39 {
40 /* 
41  * Variables definition
42  */
43     int i;
44     clockid_t clockid;
45     pid_t pid;
46     struct timespec time;
47     /*
48      * Input section: decode command line parameters 
49      * Use getopt function
50      */
51     opterr = 0;  /* don't want writing to stderr */
52     while ( (i = getopt(argc, argv, "h")) != -1) {
53         switch (i) {
54         /* 
55          * Handling options 
56          */ 
57         case 'h':                                            /* help option */
58             printf("Wrong -h option use\n");
59             usage();
60             return -1;
61             break;
62         case '?':                                    /* unrecognized options */
63             printf("Unrecognized options -%c\n",optopt);
64             usage();
65         default:                                       /* should not reached */
66             usage();
67         }
68     }
69     /* ***********************************************************
70      * 
71      *           Options processing completed
72      *
73      *                Main code beginning
74      * 
75      * ***********************************************************/
76     if ((argc - optind) != 1) {
77         printf("Wrong number of arguments %d\n", argc - optind);
78         usage();
79     }
80     
81     pid = atoi(argv[optind]);
82     if (pid > 0) {
83         if (clock_getcpuclockid(pid, &clockid) != 0) {
84             perror("Cannot get clockid");
85             exit(1);
86         }
87     } else {
88         clockid = CLOCK_PROCESS_CPUTIME_ID;
89     }
90     if (clock_gettime(clockid, &time) != 0) {
91         perror("Cannot get time");
92         exit(1);
93     } else {
94         printf("Tempo %s", ctime(&time.tv_sec));
95     }
96     exit(0);
97 }
98 /*
99  * routine to print usage info and exit
100  */
101 void usage(void) {
102     printf("Program mygetclock: prende un orologio \n");
103     printf("Usage:\n");
104     printf("  mygetclock [-h] PID \n");
105     printf("  -h   print this help\n");
106     exit(1);
107 }