3 * Author: Jan Vales (e0726236@student.tuwien.ac.at)
5 * Purpose: Client. Sends file-stubs to server.
6 * Usage: insertfile -o <offset> [<filename>]
7 * Output: nothing to stdout, debuginfo and errors to stderr
11 #define _XOPEN_SOURCE 600
12 #define TEMPFILECHAR 'd'
22 #include <sys/types.h>
29 const char *szCommand = "<not yet set>"; /* cmd name */
30 volatile static int nQueueID = -1; /* message queue ID */
32 /* prints a usage message to stderr */
34 (void) fprintf( stderr, "USAGE: %s -o <offset> [<filename>]\n", szCommand);
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);
44 /* main program; sends messages via msg-queue to the "server" */
45 int main( int argc, char **argv ) {
52 char mymsgdata[MAXMSGSIZE];
56 while((c = getopt(argc, argv, "o:")) != EOF){
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! */
61 case '?': usage(); return 0;
62 default:assert(0); break;
67 filename = argv[optind];
69 filename = "tempfile.tmp";
70 if((fp = fopen(filename,"w")) == NULL)errorHalt("ERROR while opening temp-file\n");
74 if(!fputc(c,fp))fprintf( stderr, "Cannot write to temp-file!\n");
76 if(fclose(fp))errorHalt("Cannot close temp-file!");
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");
83 fseek (fp , 0 , SEEK_END);
87 for(;ftell(fp) < fsize;){
89 msg.mType = offset+ftell(fp)+1;
91 for(;i < MAXMSGSIZE;i++)mymsgdata[i]='\0';
93 read = fread(mymsgdata,1,MAXMSGSIZE-1, fp);
95 memcpy(msg.mData, mymsgdata, read);
97 if( msgsnd(nQueueID, &msg, sizeof(msg)-sizeof(long), 0) == -1 )errorHalt("Cannot send msg!");
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");