Un po' di materiale su {{{splice}}} e inizio della ripulitura degli
[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: wwrite 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>
31 #include <dirent.h>        /* directory */
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     mutex_file = tempnam("/tmp","mutex");
91     if ((mutex = CreateMutex(mutex_file)) == -1) {            /* get a Mutex */
92         perror("Cannot create mutex");
93         goto errmutex;
94     }
95     /* main loop */
96     shm_ptr = CreateShm(argv[1], size, 0644, 0);
97     if (shm_ptr == NULL) {
98         perror("Error creating shared memory");
99         goto errshared;
100     }
101     if (file_name) {
102         fd = open(file_name, O_RDONLY);
103     }
104     FullRead(fd, shm_ptr, size);
105 //    RemoveShm(argv[1]);
106     RemoveMutex(mutex_file);
107     exit(0);
108  errshared:
109     RemoveShm(argv[1]);
110     RemoveMutex(mutex_file);
111  errmutex:
112     exit(1);
113 }
114 /*
115  * routine to print usage info and exit
116  */
117 void usage(void) {
118     printf("Program myls: list file in a directory \n");
119     printf("Usage:\n");
120     printf("  WriteShm [-h] [-s size] [-f file] name \n");
121     printf("  -h           print this help\n");
122     printf("  -s size      size of segment \n");
123     printf("  -f file      filename to read contents \n");
124     exit(1);
125 }