Aggiornamento note copyright
[gapil.git] / sources / WriteShm.c
1 /* WriteShm.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  * File WriteShm.c: 
22  *
23  * An example of POSIX shared memory use: write some content to a
24  * shared memory segment
25  *
26  * Author: S. Piccardi Jan. 2003
27  *
28  *****************************************************************************/
29 #include <sys/types.h>   /* primitive system data types */
30 #include <sys/stat.h>    /* file characteristics constants and functions */
31 #include <dirent.h>      /* directory operation constants and functions */
32 #include <stdlib.h>      /* C standard library */
33 #include <unistd.h>      /* unix standard library */
34 #include <stdio.h>       /* standard I/O library */
35
36 #include "Gapil.h"
37 #include "macros.h"
38
39 /* Help printing routine */
40 void usage(void);
41
42 int main(int argc, char *argv[]) 
43 {
44     int i;
45     int size = 4096;
46     char * file_name = NULL;
47     int fd = 0;
48     int mutex;
49     char * mutex_file;
50     void * shm_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, "hs:f:")) != -1) {
57         switch (i) {
58         /* 
59          * Handling options 
60          */ 
61         case 's':   /* set pause (in sec.) */
62             size = strtol(optarg, NULL, 10);
63             break;
64         case 'f':   /* set pause (in sec.) */
65             file_name = optarg;
66             break;
67         case 'h':   /* help option */
68             printf("Wrong -h option use\n");
69             usage();
70             return -1;
71             break;
72         case '?':   /* unrecognized options */
73             printf("Unrecognized options -%c\n",optopt);
74             usage();
75         default:    /* should not reached */
76             usage();
77         }
78     }
79     /* ***********************************************************
80      * 
81      *           Options processing completed
82      *
83      *                Main code beginning
84      * 
85      * ***********************************************************/
86     if ((argc - optind) != 1) {  /* There must be remaing parameters */
87         printf("Wrong number of arguments %d\n", argc - optind);
88         usage();
89     }
90     /* get a Mutex */
91     mutex_file = tempnam("/tmp","mutex");
92     if ((mutex = CreateMutex(mutex_file)) == -1) { 
93         perror("Cannot create mutex");
94         goto errmutex;
95     }
96     /* main loop */
97     shm_ptr = CreateShm(argv[1], size, 0644, 0);
98     if (shm_ptr == NULL) {
99         perror("Error creating shared memory");
100         goto errshared;
101     }
102     if (file_name) {
103         fd = open(file_name, O_RDONLY);
104     }
105     FullRead(fd, shm_ptr, size);
106 //    RemoveShm(argv[1]);
107     RemoveMutex(mutex_file);
108     exit(0);
109  errshared:
110     RemoveShm(argv[1]);
111     RemoveMutex(mutex_file);
112  errmutex:
113     exit(1);
114 }
115 /*
116  * routine to print usage info and exit
117  */
118 void usage(void) {
119     printf("Program myls: list file in a directory \n");
120     printf("Usage:\n");
121     printf("  WriteShm [-h] [-s size] [-f file] name \n");
122     printf("  -h           print this help\n");
123     printf("  -s size      size of segment \n");
124     printf("  -f file      filename to read contents \n");
125     exit(1);
126 }