Merge branch 'master' of ssh://gapil.gnulinux.it/srv/git/gapil
[gapil.git] / sources / message_setter.c
1 /* message_setter.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 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 message_setter: Set a message in a shared memory segment,
22  * protecting it with a semaphore. 
23  *
24  * Author: S. Piccardi Jan. 2010
25  *
26  *****************************************************************************/
27 #include <sys/types.h>   /* primitive system data types */
28 #include <sys/stat.h>    /* file characteristics constants and functions */
29 #include <dirent.h>      /* directory operation constants and functions */  
30 #include <unistd.h>      /* unix standard library */
31 #include <stdlib.h>      /* C standard library */
32 #include <string.h>      /* C strings library */
33
34 #include <semaphore.h>
35 #include "Gapil.h"
36 /*
37  * Program message_setter
38  *
39  * List files and their size inside a given directory
40  */
41 /* Help printing routine */
42 void usage(void);
43
44 #define MSGMAXSIZE 256
45
46 int main(int argc, char *argv[]) 
47 {
48     int i, t = 0;
49     sem_t * sem;
50     char *shmname = "messages";
51     char *semname = "messages";
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, "hf:s:t:")) != -1) {
59         switch (i) {
60         /* 
61          * Handling options 
62          */ 
63         case 'h':                                            /* help option */
64             printf("Wrong -h option use\n");
65             usage();
66             return -1;
67             break;
68         case 'f':
69             shmname = optarg;
70             break;
71         case 's':
72             semname = optarg;
73             break;
74         case 't':
75             t = strtol(optarg, NULL, 10);
76             break;
77         case '?':                                    /* unrecognized options */
78             printf("Unrecognized options -%c\n",optopt);
79             usage();
80         default:                                       /* should not reached */
81             usage();
82         }
83     }
84     /* ***********************************************************
85      * 
86      *           Options processing completed
87      *
88      *                Main code beginning
89      * 
90      * ***********************************************************/
91     if ((argc - optind) != 1) {          /* There must be remaing parameters */
92         printf("Wrong number of arguments %d\n", argc - optind);
93         usage();
94     }
95     // get shared memory segment
96     shm_ptr = FindShm(shmname, MSGMAXSIZE);
97     if ( shm_ptr == NULL) {
98         perror("Cannot find shared memory");
99         exit(1);
100     }
101     // open semaphore
102     if ( (sem = sem_open(semname, 0)) == SEM_FAILED ) {
103         perror("Cannot open semaphore");
104         exit(1);
105     }
106     // get semaphore
107     if ( sem_wait(sem) != 0) {
108         perror("cannot use semaphore");
109         exit(1);
110     }
111     strncpy((char *) shm_ptr, argv[optind],  MSGMAXSIZE); // modify message
112     printf("Sleeping for %i seconds\n", t);               // print sleep value
113     sleep(t);                                             // sleep
114     // release semaphore
115     if ( sem_post(sem) != 0) {
116         perror("cannot release semaphore");
117         exit(1);
118     }
119     exit(0);
120 }
121
122 /*
123  * routine to print usage info and exit
124  */
125 void usage(void) {
126     printf("Program message_setter: put a message in shared memory segment \n");
127     printf("Usage:\n");
128     printf("  message_setter [-h] [-f shmname]  message \n");
129     printf("  -h           print this help\n");
130     printf("  -f shmname   use shmname memory segment\n");
131     printf("  -s semname   use semname semaphore\n");
132     printf("  -n time      seconds to wait\n");
133     exit(1);
134 }