static void closePendingFiles() { if(listSize(fileCloseList) > 0) { int size = 0; do { struct FileWriteObject *object; struct node *listObject; pthread_mutex_lock(&writeMutex); size = listSize(fileCloseList) ; if(size > 0) listObject = popData(fileCloseList); else // The fileCloseList is empty, nothing to do. break; pthread_mutex_unlock(&writeMutex); object = (struct FileWriteObject*)listObject->data; destroyList(listObject); listObject = NULL; // Remove the object from the writing list. popElement(fileWriteList,object); flushOutstandingDataInObjectToFile(object); if(object->wav == NULL) fclose(object->stream); else closeWavSink(object->wav,object->stream); destroyWriteObject(object); size --; }while(size > 0); } }
/** * Process HTML tag * Properties -> Properties of tag being checked * Stack -> Stack used for parsing */ int processTag(char* properties, Stack* stack) { // Check if a singleton tag (only one should appear) if (!isSingletonTag(properties)) { // If not a closing tag, push onto stack if (properties[0] != '/') push(stack, properties); else { // Top tag on stack should correspond to this closing tag char* openingTag = popElement(stack); if (openingTag) { // If opening tag corresponds to closing tag, return failure if (!tagCompare(openingTag, properties)) return 0; // Otherwise return success else return 1; } } } // Indicate the tag is properly formatted return 1; }
/* extract files from the archive as specified by the names argument in argv */ void extractFar(Stack stk, Stack errStk, char **argv) { FILE *achv = fopen(argv[ARCHIVE], "r"); if(!achv) { //archive does not exist fprintf(stderr,"(%s) does not exist\n", argv[ARCHIVE]); exit(EXIT_FAILURE); } while(!feof(achv)) { char tempName[PATH_MAX]; int numBytes = -1; int fileMode = -1; if(fscanf(achv, "%s%d%d", tempName, &numBytes, &fileMode) && numBytes >= 0) { char fileName[strlen(tempName)]; strcpy(fileName,tempName); char c; if((c = fgetc(achv)) != '\n' && c != EOF) //skip newline { DIE("%s is corrupt\n",argv[ARCHIVE]); } //write contents of buffer to new file if exists in stack or empty stack given (extract all) if(existsInArchive(stk,fileName) || !stk) { //create all the required folders if they don't exist and then write to file if(fileMode == 0) //skip directories { createDir(fileName); FILE *f = fopen(fileName,"w"); if(f) { int i; for(i = 0; i < numBytes; i++) { fputc(fgetc(achv),f); } fclose(f); } } else //if fileName is dir then just make a new directory { mkdir(fileName,0777); } if(errStk) { //if the file is extracted, pop out its pathName from the error stack popElement(&errStk,fileName); } } else { fseek(achv,numBytes,SEEK_CUR); } } } while(errStk) { //print out all the errors //for checking whether the path is a directory or a file struct stat fileStat; //check if the path in stack is a directory..in which case don't print to stderr if(!stat(errStk->fullPath,&fileStat) && !S_ISDIR(fileStat.st_mode)) { fprintf(stderr,"cannot extract the directory %s\n", errStk->fullPath); } errStk = popStack(errStk); } destroyStack(stk); }
void executeProgram(Program* program) { Stack stack; int arg_pos; initStack(&stack, program->size * 16, MAX_ARG_SIZE); if(stack.elements == NULL) return; program->lineNumber = 0; while(program->lineNumber < program->size) { char* line; line = &program->lines[program->lineNumber*MAX_LINE_SIZE]; if((arg_pos = parseCmd(PUSH_CMD, line)) > 0) { pushElement(&stack, line+arg_pos); program->lineNumber++; } else if((arg_pos = parseCmd(POP_CMD, line)) > 0) { char* element; int len; element = (char *) popElement(&stack); len = strnlen(element, MAX_ARG_SIZE); memset(element, 0, len); program->lineNumber++; } else if((arg_pos = parseCmd(ADD_CMD, line)) > 0) { int a, b, ret; char *a_str; char *a_element; char *b_element; int len; a_element = popElement(&stack); b_element = popElement(&stack); a = strn2int(a_element, MAX_ARG_SIZE); b = strn2int(b_element, MAX_ARG_SIZE); a += b; a_str = itoaB10(a); len = strnlen(a_element, MAX_ARG_SIZE); memset(a_element, 0, len); len = strnlen(b_element, MAX_ARG_SIZE); memset(b_element, 0, len); len = strlen(a_str); pushElement(&stack, a_str); ret = deallocate(a_str, len); if (ret != 0) _terminate(11); program->lineNumber++; } else if((arg_pos = parseCmd(PRINT_CMD, line)) > 0) { int ret, len; char *element; element = (char *) popElement(&stack); ret = transmit_all(STDOUT, element, strnlen(element, MAX_ARG_SIZE)); if (ret != 0) _terminate(14); ret = transmit_all(STDOUT, NEWLINE, strnlen(NEWLINE, MAX_ARG_SIZE)); if (ret != 0) _terminate(15); len = strnlen(element, MAX_ARG_SIZE); memset(element, 0, len); program->lineNumber++; } else if((arg_pos = parseCmd(COPY_CMD, line)) > 0) { int copy_num; int ret, i; copy_num = strn2int(line+arg_pos, MAX_ARG_SIZE); if(copy_num < 1) { program->lineNumber++; continue; } char *element; element = popElement(&stack); for(i=0; i<copy_num; i++) { pushElement(&stack, element); } program->lineNumber++; } else program->lineNumber++; } destroyStack(&stack); }
/* delete file/dir names from the archive as per the names given in argv */ void deleteFar(Stack stk, char **argv) { FILE *oldAchv = fopen(argv[ARCHIVE],"r"); if(!oldAchv) { //archive does not exist fprintf(stderr,"(%s) does not exist\n", argv[ARCHIVE]); exit(EXIT_FAILURE); } else { FILE *achv; achv = fopen("temp.Far","w"); while(achv && !feof(oldAchv)) { char tempName[PATH_MAX]; int numBytes = -1; int fileMode = -1; if(fscanf(oldAchv, "%s%d%d", tempName, &numBytes, &fileMode) && numBytes >= 0) { char fileName[strlen(tempName)]; strcpy(fileName,tempName); char c; if((c = fgetc(achv)) != '\n' && c != EOF) //skip newline { DIE("%s is corrupt\n",argv[ARCHIVE]); } //write to temp archive if no modification to old archive needed if(!existsInArchive(stk,fileName)) { fputs(fileName,achv); fprintf(achv, "\n%d\n%d\n", numBytes, fileMode); int i; for(i = 0; i < numBytes; i++) { fputc(fgetc(oldAchv),achv); } fputs("\n",achv); } else { //if the file is deleted then pop out its pathName from the stack popElement(&stk,fileName); //skip bytes in oldAchv fseek(oldAchv,numBytes,SEEK_CUR); } } } //close and delete the old archive file fclose(oldAchv); unlink(argv[ARCHIVE]); //close temp.Far file and rename it if(achv) { fclose(achv); rename("temp.Far",argv[ARCHIVE]); } while(stk) { //print out all the errors //for checking whether the path is a directory or a file struct stat fileStat; //check if the path in stack is a directory..in which case don't print to stderr if(!stat(stk->fullPath,&fileStat) && !S_ISDIR(fileStat.st_mode)) { fprintf(stderr,"cannot delete the directory %s\n", stk->fullPath); } stk = popStack(stk); } } }