455f5cc4d65d3b8fcac38f04fe8d67611ad0bbc5
[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  * $Id: WriteShm.c,v 1.1 2003/02/03 18:07:38 piccardi Exp $
29  *
30  *****************************************************************************/
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <dirent.h>        /* directory */
34 #include <stdlib.h>        /* C standard library */
35 #include <unistd.h>
36 #include <stdio.h>
37
38 #include "Gapil.h"
39 #include "macros.h"
40
41 /* Help printing routine */
42 void usage(void);
43
44 int main(int argc, char *argv[]) 
45 {
46     int i;
47     int size = 4096;
48     char * file_name = NULL;
49     int fd = 0;
50     int mutex;
51     char * mutex_file;
52     void * shm_ptr;
53     /*
54      * Input section: decode command line parameters 
55      * Use getopt function
56      */
57     opterr = 0;  /* don't want writing to stderr */
58     while ( (i = getopt(argc, argv, "hs:f:")) != -1) {
59         switch (i) {
60         /* 
61          * Handling options 
62          */ 
63         case 's':                                     /* set pause (in sec.) */
64             size = strtol(optarg, NULL, 10);
65             break;
66         case 'f':                                     /* set pause (in sec.) */
67             file_name = optarg;
68             break;
69         case 'h':                                             /* help option */
70             printf("Wrong -h option use\n");
71             usage();
72             return -1;
73             break;
74         case '?':                                    /* unrecognized options */
75             printf("Unrecognized options -%c\n",optopt);
76             usage();
77         default:                                       /* should not reached */
78             usage();
79         }
80     }
81     /* ***********************************************************
82      * 
83      *           Options processing completed
84      *
85      *                Main code beginning
86      * 
87      * ***********************************************************/
88     if ((argc - optind) != 1) {          /* There must be remaing parameters */
89         printf("Wrong number of arguments %d\n", argc - optind);
90         usage();
91     }
92     mutex_file = tempnam("/tmp","mutex");
93     if ((mutex = CreateMutex(mutex_file)) == -1) {            /* get a Mutex */
94         perror("Cannot create mutex");
95         goto errmutex;
96     }
97     /* main loop */
98     shm_ptr = CreateShm(argv[1], size, 0644, 0);
99     if (shm_ptr == NULL) {
100         perror("Error creating shared memory");
101         goto errshared;
102     }
103     if (file_name) {
104         fd = open(file_name, O_RDONLY);
105     }
106     FullRead(fd, shm_ptr, size);
107 //    RemoveShm(argv[1]);
108     RemoveMutex(mutex_file);
109     exit(0);
110  errshared:
111     RemoveShm(argv[1]);
112     RemoveMutex(mutex_file);
113  errmutex:
114     exit(1);
115 }
116 /*
117  * routine to print usage info and exit
118  */
119 void usage(void) {
120     printf("Program myls: list file in a directory \n");
121     printf("Usage:\n");
122     printf("  WriteShm [-h] [-s size] [-f file] name \n");
123     printf("  -h           print this help\n");
124     printf("  -s size      size of segment \n");
125     printf("  -f file      filename to read contents \n");
126     exit(1);
127 }