]> git.somenet.org - pub/jan/sysprog.git/blob - gluefile/insertfile.c
all the old sysprog files
[pub/jan/sysprog.git] / gluefile / insertfile.c
1 /**************
2 * Name:         insertfile.c
3 * Author:       Jan Vales (e0726236@student.tuwien.ac.at)
4 * Date:         9.11.2009
5 * Purpose:      Client. Sends file-stubs to server.
6 * Usage:        insertfile -o <offset> [<filename>]
7 * Output:       nothing to stdout, debuginfo and errors to stderr
8 **************/
9
10 #define _SVID_SOURCE
11 #define _XOPEN_SOURCE 600
12 #define TEMPFILECHAR 'd'
13
14 #include <unistd.h>
15 #include <stdio.h>
16 #include <errno.h>
17 #include <stdlib.h>
18 #include <signal.h>
19 #include <assert.h>
20 #include <getopt.h>
21
22 #include <sys/types.h>
23 #include <sys/ipc.h>
24 #include <sys/msg.h>
25
26 #include "aufgb.h"
27
28
29 const char *szCommand = "<not yet set>";        /* cmd name */
30 volatile static int nQueueID = -1;              /* message queue ID */
31
32 /* prints a usage message to stderr */
33 void usage( void )  {
34   (void) fprintf( stderr, "USAGE: %s -o <offset> [<filename>]\n", szCommand);
35 }
36
37 /* prints error messages if an error has occoured */
38 void errorHalt(char* msg){
39   if(errno != 0)(void)fprintf(stderr, "%s: %s = %s\n", szCommand, msg, strerror(errno));
40   else (void)fprintf(stderr, "%s: %s\n", szCommand, msg);
41   exit(EXIT_FAILURE);
42
43
44 /* main program; sends messages via msg-queue to the "server" */
45 int main( int argc, char **argv )  {
46   FILE *fp;
47   int offset;
48   char *filename;
49   char c;
50   int fsize;
51   int read;
52   char mymsgdata[MAXMSGSIZE];
53   int i = 0;
54   szCommand = argv[0];
55
56   while((c = getopt(argc, argv, "o:")) != EOF){
57     switch(c){
58       case 'o':offset = strtol(optarg, 0, 0); 
59         if(offset < 0)errorHalt("offset may not be negative!"); /* bei parse-fehlern wird 0 zurückgegeben - sehr gut! */
60         break;
61       case '?': usage(); return 0;
62       default:assert(0); break;
63     }
64   }
65   c = 0;
66
67   filename = argv[optind];
68   if(!filename){
69     filename = "tempfile.tmp";
70     if((fp = fopen(filename,"w")) == NULL)errorHalt("ERROR while opening temp-file\n");
71     for(;;){
72       c = getchar();
73       if(feof(stdin))break;
74       if(!fputc(c,fp))fprintf( stderr, "Cannot write to temp-file!\n");
75     }
76     if(fclose(fp))errorHalt("Cannot close temp-file!");
77     c = TEMPFILECHAR;
78   }
79
80   if((nQueueID = msgget( KEY, 0)) == -1 )errorHalt("Cannot create/access message queue!\n");
81   if((fp = fopen(filename,"r")) == NULL)errorHalt("ERROR while opening file\n");
82
83   fseek (fp , 0 , SEEK_END);
84   fsize = ftell (fp);
85   rewind (fp);
86
87   for(;ftell(fp) < fsize;){
88     message_t msg;
89     msg.mType = offset+ftell(fp)+1;
90     i = 0;
91     for(;i < MAXMSGSIZE;i++)mymsgdata[i]='\0';
92   
93     read = fread(mymsgdata,1,MAXMSGSIZE-1, fp);
94     mymsgdata[read]='\0';
95     memcpy(msg.mData, mymsgdata, read);
96     msg.mData[read]='\0';
97     if( msgsnd(nQueueID, &msg, sizeof(msg)-sizeof(long), 0) == -1 )errorHalt("Cannot send msg!");
98   }
99
100   if(fclose(fp))errorHalt("Cannot close file!");
101   if(c == TEMPFILECHAR)if(unlink(filename))errorHalt("Cannot unlink temp-file!");
102   (void)fprintf(stderr, "DONE\n");
103   return 0;
104 }
105
106
107
108