/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * COPYING NOTES
 *
 * test.c -- minimal test for shared memory managing library
 * 
 * Copyright (C) 1995 Scott Burkett <scottb@IntNet.net>.
 *   as heavily derived from the Chapter 6 of The Linux Programmer's Guide
 * Copyright (C) 2002 Roberto A. Foglietta <robang@libero.it>
 * Copyright (C) 2002 GEA-Automotive <fogliettar@gea-automotive.com>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 */

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
 * REVISION NOTES:
 * released 17-10-2002 by Roberto A. Foglietta
 * modified 29-05-2003 by Roberto A. Foglietta
 */
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdlib.h>
#include <ctype.h>

#include "shmtool.h"
int usage(void);

int main(int argc, char *argv[])
{
        int   shmid;
        char *buf;

        if(argc == 1)
                        return usage();

        /* RAF 2003-05-29: Create unique key via call to ftok() */
        key_t key = ftok (".", 'S');

        /* Open the shared memory segment - create if necessary */
        shmid = getidshm(key);
        
        if(shmid == -1 && argv[1][0] != 'c') {
                fprintf(stderr, "\nBefore anything else you have to create the shared memory\n");
                return -1;
        }
                
        switch(tolower(argv[1][0]))
        {
                        case 'c': shmid = initshm(key, 0);
                                          break;
                        case 'w': writeshm(shmid, argv[2]);
                                          break;
                        case 'r': buf = readshm(shmid);
                                          printf("Shared memory contenent:\n%s\n",buf);
                                          free(buf);
                                          break;
                        case 'd': removeshm(shmid);
                                          break;
                        case 'm': changemode(shmid, argv[2]);
                                          break;
                        default : return usage();

        }
        return 0;
}

int usage(void)
{
        fprintf(stderr, "shmtool - A utility for tinkering with shared memory\n");
            fprintf(stderr, "\nUSAGE:  shmtool (c)reate\n");
        fprintf(stderr, "                (w)rite <text>\n");
        fprintf(stderr, "                (r)ead\n");
        fprintf(stderr, "                (d)elete\n");
        fprintf(stderr, "                (m)ode change <octal mode>\n");
                fprintf(stderr, "\nBefore anything else you have to create the shared memory\n");
        return 1;
}