Download | Plain Text | No Line Numbers
- /*
- * Name: insertfile (client)
- * Author: Manuel Mausz, 0728348
- * Description: Server writes data read from client via IPC message queue to
- * file. If filesize is reached server quits automatically. Client
- * sends data read from file via IPC message queue to server
- * Offset specifies file offset to start writing for server.
- * Created: 12.03.2009
- * Exercise: 1h B
- */
-
- #include <unistd.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <errno.h>
- #include <limits.h>
- #include "global.h"
-
- /*
- * NAME: sighandler
- * PURPOSE:
- * signal handler sets global error flag to 1
- *
- * PARAMETERS:
- * int signum ... signal number
- *
- * RETURN VALUE:
- * void
- *
- * GLOBAL VARS:
- * error
- */
- static void sighandler(int signum)
- {
- #ifdef DEBUG
- #endif
- error = 1;
- }
-
- /*
- * NAME: usage
- * PURPOSE:
- * prints program usage to stderr and terminates with EXIT_FAILURE
- *
- * PARAMETERS:
- * void
- *
- * RETURN VALUE:
- * void
- *
- * GLOBAL VARS:
- * me
- */
- static void usage(void)
- {
- }
-
- /*
- * NAME: read_send
- * PURPOSE:
- * sends data read from stream pointed to by fp via IPC msg queue to server.
- * offset specifies absolute file offset to start writing for the server.
- * functions returns after all data has been sent or on error.
- *
- * PARAMETERS:
- * FILE *fp ... pointer to stream to read from
- * int msgqid ... msg queue id
- * long offset ... offset
- *
- * RETURN VALUE:
- * void
- *
- * GLOBAL VARS:
- * error
- */
- static void read_send(FILE *fp, int msgqid, long offset)
- {
- int len;
- msg_t msg;
-
- /* read + send */
- if (!error)
- {
- msg.mtype = offset + 1;
- {
- /* read from file */
- {
- (void) printferr("Error while reading from file");
- break;
- }
- msg.len = len;
-
- #ifdef DEBUG
- #endif
-
- /* send message */
- if (!error && msgsnd(msgqid, &msg, MSGSIZE, 0) == -1)
- (void) printferr("Couldn't send message");
-
- /* increase offset */
- msg.mtype += len;
- }
- (void) printferr("Error while reading from file");
- }
- }
-
- /*
- * NAME: main
- * PURPOSE:
- * install signal handler, parse commandline options,
- * get msg queue id and call read_send
- *
- * PARAMETERS:
- * standard parameters of main
- *
- * RETURN VALUE:
- * int ... 0 on success, 1 on error
- *
- * GLOBAL VARS:
- * me, error
- */
- int main(int argc, char *argv[])
- {
- int opt, msgqid = -1;
- long offset = -1;
- char *endptr;
- char *filename = NULL;
- FILE *fp = stdin;
- struct sigaction new_action, old_action;
-
- /* save me */
- me = argv[0];
-
- /* install signal handler */
- new_action.sa_handler = sighandler;
- sigemptyset(&new_action.sa_mask);
- new_action.sa_flags = 0; /* to avoid restart of blocking syscalls */
- INSTALL_SIGNAL(SIGINT, new_action, old_action)
- INSTALL_SIGNAL(SIGQUIT, new_action, old_action)
- INSTALL_SIGNAL(SIGTERM, new_action, old_action)
-
- /* parse cmdline options */
- while ((opt = getopt(argc, argv, "o:")) != -1)
- {
- switch(opt)
- {
- case 'o':
-
- if ((errno == ERANGE && (offset == LONG_MAX || offset == LONG_MIN))
- || (errno != 0 && offset == 0))
- (void) bailout("Invalid offset value");
-
- if (*endptr != '\0' || offset < 0)
- (void) bailout("Invalid offset value. Only positive numbers allowed.");
-
- break;
- default:
- (void) usage();
- break;
- }
- }
- argv += optind;
-
- /* parse next commandline option */
- if (argv)
- filename = *argv;
-
- /* check required option value */
- if (offset < 0)
- (void) usage();
-
- #ifdef DEBUG
- #endif
-
- /* open file to read from */
- (void) bailout("%s", filename);
-
- /* get message queue id */
- if (!error && (msgqid = msgget(IPC_KEY, 0)) == -1)
- {
- if (errno == ENOENT)
- {
- errno = 0;
- (void) printferr("Couldn't get message queue. Server not running?");
- }
- else
- (void) printferr("Couldn't get message queue");
- }
- #ifdef DEBUG
- if (msgqid != -1)
- #endif
-
- /* read and send data */
- if (!error)
- (void) read_send(fp, msgqid, offset);
-
- /* cleanup stuff */
- #ifdef DEBUG
- #endif
-
- /* close open fds */
- if (filename != NULL && fp != stdin)
-
- return (error) ? 1 : 0;
- }
-
- /* vim: set et sw=2 ts=2: */
-