/*Define here what the action characters on the map will do when entered on their area*/ void pAAction(int aChar) { if (strcmp(mapName, "menu") == 0) { switch (aChar) { case 1: initializeSort(); switchMap("filtering"); break; case 2: initializeSort(); switchMap("sorting"); break; case 3: teleport(aChar); break; case 4: closeProg(); break; } } else if (strcmp(mapName, "sorting") == 0) { switch (aChar) { case A: teleport(B); scrollData(UP); break; case C: teleport(B); scrollData(DOWN); break; } } else if (strcmp(mapName, "filtering") == 0) { switch (aChar) { case 1: teleport(2); scrollData(UP); break; case 3: teleport(2); scrollData(DOWN); break; case 7: closeDoor(Q, DOWN); break; case 8: closeDoor(W, UP); break; } } else { perror ("'mapName' out of speck"); SC(mapName); assert(!TRUE); } }
int main(int argc,char** argv){ int totalBytes=0; int sp=0; //File descriptor for source program int tp=0; //File descriptor for target program int numBytes=0; //Number of bytes read or written char * buf=NULL; //Buffer that has NOBYTES (number of bytes); used to read and write if((buf = malloc(NOBYTES))==0) error(OTHER); //sigaction() is more portable than signal(): struct sigaction sigAct; sigAct.sa_handler = copyStatus; sigAct.sa_flags = SA_RESTART; //Use this to print if the file takes a long time to copy. if(sigaction(SIGALRM,&sigAct,NULL)<0){ error(OTHER); } if(alarm(1)<0) error(OTHER); //If there are not exactly three arguments, send an error code to the user and exit. if(argc!=3){ error(ARGER); }else if(strcmp(argv[1],argv[2])==0){ error(AR2ER); }else{ //Open the source file if((sp = open(argv[1],O_RDONLY))==-1){ //Error, value of -1 returned error(REAER); }else{ //Want to open the file we're going to write to: if((tp=creat(argv[2],S_IRUSR|S_IWUSR))==-1){ //Error creating file. error(WRIER); }else{ //Here, we have successfully opened both files. //Want to read from file: do{ memset(buf,0,strlen(buf)); #ifdef DEBUG printf("Buffer: ####%s####\n",buf); #endif if((numBytes = read(sp,buf,NOBYTES))==-1){ //Error reading from file //Deal with error here. //Check to see if it was from the signalAction if(errno == EINTR){ //Try to read again. continue; }else{ error(REAER); } }else{ //Increment the total number of bytes copied: totalBytes+=numBytes; if(numBytes < NOBYTES){ #ifdef DEBUG //Print out results: printf("@@@@%s@@@@",buf); printf("%d : string length\n",strlen(buf)); #endif if((write(tp,buf,strlen(buf)))==-1){ //If it's an interrupt, try writing again, otherwise, exit. if(errno == EINTR){ //Try to read again. continue; }else{ error(WRIER); } } numBytes = 0; }else{ #ifdef DEBUG //Print out results: printf("@@@@%s@@@@",buf); printf("%d : string length\n",strlen(buf)); #endif //Take the buffer and write to the other file if((write(tp,buf,strlen(buf)))==-1){ //If it's an interrupt, try writing again, otherwise, exit. if(errno == EINTR){ //Try to read again. continue; }else{ error(WRIER); } } } } #ifdef DEBUG printf("Bytes read: %d\n",numBytes); sleep(1000); #endif }while (numBytes>0); } //Need to close the files: if(close(sp)<0) error(OTHER); if(close(tp)<0) error(OTHER); } } //Deallocate memory and give a closing message to the user. free(buf); closeProg(totalBytes,argv[1],argv[2]); exit(0); }