All pastes #11065 Raw Edit

Something

public c v1 · immutable
#11065 ·published 2005-05-05 21:06 UTC
rendered paste body
/*************************************************************************** *            cron_client.c * *  Wed May  4 20:03:01 2005 *  Copyright  2005  tw03d024 *  Email tw03d024@technikum-wien.at ****************************************************************************/#include <stdio.h>#include <stdlib.h>#include <sys/types.h>#include <sys/ipc.h>#include <sys/msg.h>#include <time.h>#include <string.h>#define KEY 0410257051#define PERM 0666#define MAX_DATA 255struct tm tm_now;typedef struct{long mType;char mText[MAX_DATA];} message_t;int main(int argc, char *argv[]){    message_t msg;    int msgid = -1;    char command[MAX_DATA];    int i;    char delim[] = ":";    char *token = NULL;    int new_hour;    int new_min;            if (argc < 2)    {      fprintf(stderr,"Usage: %s hh:mm program [arg1] [agr2...argn]\n",argv[0]);      return EXIT_FAILURE;      }   	for(i=2; i<argc; i++)	{		strcat(command, argv[i]);		strcat(command, " ");	}        token = strtok( argv[1], delim );    new_hour = atoi(token);        token = strtok( NULL, delim);    new_min = atoi(token);        fprintf(stdout, " %d%d \n",new_hour, new_min);        tm_now.tm_hour = new_hour;    tm_now.tm_min = new_min;        mktime(&tm_now);        msg.mType = (long)&tm_now;       /* Message Queue oeffnen */   if( (msgid = msgget(KEY,PERM))==-1 )   {      /* error handling */      fprintf(stderr,"%s: Can't access message queue\n",argv[0]);      return EXIT_FAILURE;      }	/*Kommandozeile ins struct schreiben*/	strncpy(msg.mText,command,MAX_DATA);			     /* Nachricht verschicken */     if (msgsnd(msgid,&msg,sizeof(msg)-sizeof(long),0) == -1)     {         /* error handling */         fprintf(stderr,"%s: Can't send message\n",argv[0]);         return EXIT_FAILURE;      }      fprintf(stdout, "Message sent %s\n"msg.mText);   return EXIT_SUCCESS;}/*************************************************************************** *            cron_server.c * *  Wed May  4 20:03:01 2005 *  Copyright  2005  tw03d024 *  Email tw03d024@technikum-wien.at ****************************************************************************/#include <stdio.h>#include <stdlib.h>#include <sys/types.h>#include <sys/ipc.h>#include <sys/msg.h>#include <time.h>#include <string.h>#include <signal.h>#define KEY 0410257051#define PERM 0666#define MAX_DATA 255typedef struct{long mType;char mText[MAX_DATA];} message_t;void myhandler(int sig)void myhandler(int sig){   if((msgctl(msgid, IPC_RMID, NULL) == -1)   {   fprintf("Message Queue couldn't be closed!\n",sig);   fflush(stdout);   }   fprintf("Message Queue was shut down properly!\n");   fflusch(stdout);      }int main(int argc, char *argv[]){    message_t msg;    int msgid = -1;    time_t start, stop;    double diff;        start = time(NULL);        if ( (msgid = msgget( KEY,PERM | IPC_CREAT | IPC_EXCL  )) == -1 )    {         fprintf(stderr, "%s: MessageQueue couldn't be started!\n", argv[0]);         return EXIT_FAILURE;         }        diff=difftime(stop=time(NULL),start);        while(1)    {    	(void) signal(SIGINT,myhandler);	    	fprintf(stdout, "Time: %d\n", (int)diff);	fflush(stdout);   	while((diff=difftime(stop=time(NULL),start)) != 5);         {              if (msgrcv(msgid,&msg,sizeof(msg)-sizeof(long), -1, 0) == -1)               {                   fprintf(stderr, "%s: Message couldn't be recieved!\n", argv[0]);                   return EXIT_FAILURE;              }	      	fprintf(stdout, "Message: %s %ld\n", msg.mText, (long)msg.mType);		fflush (stdout);			}	    }  system("PAUSE");	  return 0;}