int main(int argc, char * argv[]) { read_char(); read_line(); read_file_content(); get_file_meta_data("demo.txt"); open_same_file_again("demo.txt"); fork_read("demo.txt"); test_dup2("demo.txt"); return 0; }
int main(int argc, char** argv) { GLFWwindow window; int ch, iter; double dt; double last_update_time; int frame; float f; GLint uloc_modelview; GLint uloc_project; char* vertex_shader_path = NULL; char* fragment_shader_path = NULL; char* vertex_shader_src = NULL; char* fragment_shader_src = NULL; GLuint shader_program; while ((ch = getopt(argc, argv, "f:v:h")) != -1) { switch (ch) { case 'f': fragment_shader_path = optarg; break; case 'v': vertex_shader_path = optarg; break; case 'h': usage(); exit(EXIT_SUCCESS); default: usage(); exit(EXIT_FAILURE); } } if (fragment_shader_path) { vertex_shader_src = read_file_content(fragment_shader_path); if (!fragment_shader_src) { fprintf(stderr, "ERROR: unable to load fragment shader from '%s'\n", fragment_shader_path); exit(EXIT_FAILURE); } } if (vertex_shader_path) { vertex_shader_src = read_file_content(vertex_shader_path); if (!vertex_shader_src) { fprintf(stderr, "ERROR: unable to load vertex shader from '%s'\n", fragment_shader_path); exit(EXIT_FAILURE); } } if (!glfwInit()) { fprintf(stderr, "ERROR: Unable to initialize GLFW\n"); usage(); free(vertex_shader_src); free(fragment_shader_src); exit(EXIT_FAILURE); } glfwOpenWindowHint(GLFW_WINDOW_RESIZABLE, GL_FALSE); glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3); glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 2); glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwOpenWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_FALSE); window = glfwOpenWindow(800, 600, GLFW_WINDOWED, "GLFW OpenGL3 Heightmap demo", NULL); if (! window ) { fprintf(stderr, "ERROR: Unable to create the OpenGL context and associated window\n"); usage(); free(vertex_shader_src); free(fragment_shader_src); exit(EXIT_FAILURE); } glfwSetWindowCloseCallback(window_close_callback); glfwSetKeyCallback(key_callback); /* Register events callback */ if (GL_TRUE != init_opengl()) { fprintf(stderr, "ERROR: unable to resolve OpenGL function pointers\n"); free(vertex_shader_src); free(fragment_shader_src); exit(EXIT_FAILURE); } /* Prepare opengl resources for rendering */ shader_program = make_shader_program(vertex_shader_src , fragment_shader_src); free(vertex_shader_src); free(fragment_shader_src); if (shader_program == 0u) { fprintf(stderr, "ERROR: during creation of the shader program\n"); usage(); exit(EXIT_FAILURE); } pglUseProgram(shader_program); uloc_project = pglGetUniformLocation(shader_program, "project"); uloc_modelview = pglGetUniformLocation(shader_program, "modelview"); /* Compute the projection matrix */ f = 1.0f / tanf(view_angle / 2.0f); projection_matrix[0] = f / aspect_ratio; projection_matrix[5] = f; projection_matrix[10] = (z_far + z_near)/ (z_near - z_far); projection_matrix[11] = -1.0f; projection_matrix[14] = 2.0f * (z_far * z_near) / (z_near - z_far); pglUniformMatrix4fv(uloc_project, 1, GL_FALSE, projection_matrix); /* Set the camera position */ modelview_matrix[12] = -5.0f; modelview_matrix[13] = -5.0f; modelview_matrix[14] = -20.0f; pglUniformMatrix4fv(uloc_modelview, 1, GL_FALSE, modelview_matrix); /* Create mesh data */ init_map(); make_mesh(shader_program); /* Create vao + vbo to store the mesh */ /* Create the vbo to store all the information for the grid and the height */ /* setup the scene ready for rendering */ glViewport(0, 0, 800, 600); glClearColor(0.0f, 0.0f, 0.0f, 0.0f); /* main loop */ frame = 0; iter = 0; dt = last_update_time = glfwGetTime(); while (running) { ++frame; /* render the next frame */ glClear(GL_COLOR_BUFFER_BIT); glDrawElements(GL_LINES, 2* MAP_NUM_LINES , GL_UNSIGNED_INT, 0); /* display and process events through callbacks */ glfwSwapBuffers(); glfwPollEvents(); /* Check the frame rate and update the heightmap if needed */ dt = glfwGetTime(); if ((dt - last_update_time) > 0.2) { /* generate the next iteration of the heightmap */ if (iter < MAX_ITER) { update_map(NUM_ITER_AT_A_TIME); update_mesh(); iter += NUM_ITER_AT_A_TIME; } last_update_time = dt; frame = 0; } } exit(EXIT_SUCCESS); }
/** * Read a replay moment 'STEP' from file. * @param remain: Rest of line (after STEP keyword). * @param in: file to read from. * @param name: name to print in errors. * @param lineno: incremented as lines are read. * @param ttl: for readentry * @param or: for readentry * @param prev: for readentry * @return: range object to add to list, or NULL on error. */ static struct replay_moment* replay_moment_read(char* remain, FILE* in, const char* name, int* lineno, uint32_t* ttl, ldns_rdf** or, ldns_rdf** prev) { struct replay_moment* mom = (struct replay_moment*)malloc( sizeof(struct replay_moment)); int skip = 0; int readentry = 0; if(!mom) return NULL; memset(mom, 0, sizeof(*mom)); if(sscanf(remain, " %d%n", &mom->time_step, &skip) != 1) { log_err("%d: cannot read number: %s", *lineno, remain); free(mom); return NULL; } remain += skip; while(isspace((int)*remain)) remain++; if(parse_keyword(&remain, "NOTHING")) { mom->evt_type = repevt_nothing; } else if(parse_keyword(&remain, "QUERY")) { mom->evt_type = repevt_front_query; readentry = 1; if(!extstrtoaddr("127.0.0.1", &mom->addr, &mom->addrlen)) fatal_exit("internal error"); } else if(parse_keyword(&remain, "CHECK_ANSWER")) { mom->evt_type = repevt_front_reply; readentry = 1; } else if(parse_keyword(&remain, "CHECK_OUT_QUERY")) { mom->evt_type = repevt_back_query; readentry = 1; } else if(parse_keyword(&remain, "REPLY")) { mom->evt_type = repevt_back_reply; readentry = 1; } else if(parse_keyword(&remain, "TIMEOUT")) { mom->evt_type = repevt_timeout; } else if(parse_keyword(&remain, "TIME_PASSES")) { mom->evt_type = repevt_time_passes; while(isspace((int)*remain)) remain++; if(parse_keyword(&remain, "EVAL")) { while(isspace((int)*remain)) remain++; mom->string = strdup(remain); if(!mom->string) fatal_exit("out of memory"); if(strlen(mom->string)>0) mom->string[strlen(mom->string)-1]=0; remain += strlen(mom->string); } } else if(parse_keyword(&remain, "CHECK_AUTOTRUST")) { mom->evt_type = repevt_autotrust_check; while(isspace((int)*remain)) remain++; if(strlen(remain)>0 && remain[strlen(remain)-1]=='\n') remain[strlen(remain)-1] = 0; mom->autotrust_id = strdup(remain); if(!mom->autotrust_id) fatal_exit("out of memory"); read_file_content(in, lineno, mom); } else if(parse_keyword(&remain, "ERROR")) { mom->evt_type = repevt_error; } else if(parse_keyword(&remain, "TRAFFIC")) { mom->evt_type = repevt_traffic; } else if(parse_keyword(&remain, "ASSIGN")) { mom->evt_type = repevt_assign; read_assign_step(remain, mom); } else if(parse_keyword(&remain, "INFRA_RTT")) { char *s, *m; mom->evt_type = repevt_infra_rtt; while(isspace((int)*remain)) remain++; s = remain; remain = strchr(s, ' '); if(!remain) fatal_exit("expected three args for INFRA_RTT"); remain[0] = 0; remain++; while(isspace((int)*remain)) remain++; m = strchr(remain, ' '); if(!m) fatal_exit("expected three args for INFRA_RTT"); m[0] = 0; m++; while(isspace((int)*m)) m++; if(!extstrtoaddr(s, &mom->addr, &mom->addrlen)) fatal_exit("bad infra_rtt address %s", s); if(strlen(m)>0 && m[strlen(m)-1]=='\n') m[strlen(m)-1] = 0; mom->variable = strdup(remain); mom->string = strdup(m); if(!mom->string) fatal_exit("out of memory"); if(!mom->variable) fatal_exit("out of memory"); } else { log_err("%d: unknown event type %s", *lineno, remain); free(mom); return NULL; } while(isspace((int)*remain)) remain++; if(parse_keyword(&remain, "ADDRESS")) { while(isspace((int)*remain)) remain++; if(strlen(remain) > 0) /* remove \n */ remain[strlen(remain)-1] = 0; if(!extstrtoaddr(remain, &mom->addr, &mom->addrlen)) { log_err("line %d: could not parse ADDRESS: %s", *lineno, remain); free(mom); return NULL; } } if(parse_keyword(&remain, "ELAPSE")) { double sec; errno = 0; sec = strtod(remain, &remain); if(sec == 0. && errno != 0) { log_err("line %d: could not parse ELAPSE: %s (%s)", *lineno, remain, strerror(errno)); free(mom); return NULL; } #ifndef S_SPLINT_S mom->elapse.tv_sec = (int)sec; mom->elapse.tv_usec = (int)((sec - (double)mom->elapse.tv_sec) *1000000. + 0.5); #endif } if(readentry) { mom->match = read_entry(in, name, lineno, ttl, or, prev, 1); if(!mom->match) { free(mom); return NULL; } } return mom; }
int fcache_createval(fcache_context * pfcache, const char * filename, fcache_value ** ppvalue) { int result = NONFATAL; fcache_value * pvalue = NULL; HANDLE hFile = INVALID_HANDLE_VALUE; void * buffer = NULL; unsigned int filesize = 0; unsigned char * psec = NULL; unsigned int openflags = 0; unsigned int attributes = 0; unsigned int fileflags = 0; BY_HANDLE_FILE_INFORMATION bhfi = {0}; dprintverbose("start fcache_createval"); _ASSERT(pfcache != NULL); _ASSERT(filename != NULL); _ASSERT(ppvalue != NULL); *ppvalue = NULL; /* Allocate memory for cache entry in shared memory */ pvalue = (fcache_value *)alloc_smalloc(pfcache->palloc, sizeof(fcache_value)); if(pvalue == NULL) { result = FATAL_OUT_OF_SMEMORY; goto Finished; } pvalue->file_sec = 0; pvalue->file_content = 0; /* Open the file in shared read mode */ openflags |= FILE_ATTRIBUTE_ENCRYPTED; openflags |= FILE_FLAG_OVERLAPPED; openflags |= FILE_FLAG_BACKUP_SEMANTICS; openflags |= FILE_FLAG_SEQUENTIAL_SCAN; hFile = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, openflags, NULL); if(hFile == INVALID_HANDLE_VALUE) { error_setlasterror(); result = FATAL_FCACHE_CREATEFILE; goto Finished; } if(!GetFileInformationByHandle(hFile, &bhfi)) { result = FATAL_FCACHE_BYHANDLE_INFO; goto Finished; } attributes = bhfi.dwFileAttributes; if(attributes & FILE_ATTRIBUTE_DIRECTORY) { fileflags |= FILE_IS_FOLDER; } else { /* Dont cache files bigger than maxfsize */ if(bhfi.nFileSizeHigh > 0 || bhfi.nFileSizeLow > pfcache->maxfsize) { result = WARNING_FCACHE_TOOBIG; goto Finished; } /* File size is just the low file size */ filesize = bhfi.nFileSizeLow; /* Allocate memory for entire file in shared memory */ buffer = alloc_smalloc(pfcache->palloc, filesize); if(buffer == NULL) { result = FATAL_OUT_OF_SMEMORY; goto Finished; } /* If filesize is greater than 0, read file content */ if(filesize > 0) { result = read_file_content(hFile, filesize, &buffer); if(FAILED(result)) { goto Finished; } } } /* Read security information */ result = read_file_security(pfcache, filename, &psec); if(FAILED(result)) { goto Finished; } fileflags |= FILE_IS_RUNAWARE; fileflags |= FILE_IS_WUNAWARE; pvalue->file_size = filesize; pvalue->file_sec = psec - pfcache->memaddr; pvalue->file_content = ((buffer == NULL) ? 0 : ((char *)buffer - pfcache->memaddr)); pvalue->file_flags = fileflags; pvalue->is_deleted = 0; pvalue->hitcount = 0; pvalue->refcount = 0; InterlockedIncrement(&pfcache->header->itemcount); InterlockedIncrement(&pfcache->header->misscount); /* Decrement the hitcounts here. They will be incremented*/ /* back in refinc so that hitcount remains the same */ InterlockedDecrement(&pfcache->header->hitcount); InterlockedDecrement(&pvalue->hitcount); *ppvalue = pvalue; _ASSERT(SUCCEEDED(result)); Finished: if(hFile != INVALID_HANDLE_VALUE) { CloseHandle(hFile); hFile = INVALID_HANDLE_VALUE; } if(FAILED(result)) { dprintimportant("failure %d in fcache_createval", result); _ASSERT(result > WARNING_COMMON_BASE); if(buffer != NULL) { alloc_sfree(pfcache->palloc, buffer); buffer = NULL; } if(psec != NULL) { alloc_sfree(pfcache->palloc, psec); psec = NULL; } if(pvalue != NULL) { pvalue->file_sec = 0; pvalue->file_content = 0; alloc_sfree(pfcache->palloc, pvalue); pvalue = NULL; } } dprintverbose("end fcache_createval"); return result; }
/* * This will handle connection for each client * */ void *connection_handler(void *socket_desc) { //Get the socket descriptor int sock = *(int*)socket_desc; int read_size; char *message , client_message[2000]; char received_cmd; char editted_filename[100]; char Copied_filename[100]; //Send some messages to the client //message = "Greetings! I am your connection handler\n"; //write(sock , message , strlen(message)); //message = "Now type something and i shall repeat what you type \n"; //write(sock , message , strlen(message)); //Receive a message from client while( (read_size = recv(sock , client_message , 2000 , 0)) > 0 ) { ////////////////recv C cmd if(strcmp ( client_message,"C")==0 ) { received_cmd='C';//it means we have to do C cmd exec later. //memset(client_message,0,sizeof(client_message)); strcpy(client_message,"What's the name of your new file?\n"); //Send the message back to client if(write(sock , client_message , strlen(client_message))<0) { puts("Send failed"); return 1; } memset(client_message,0,sizeof(client_message)); } ///////////////recv C end ////////////////recv R cmd else if(strcmp (client_message,"R")==0) { received_cmd='R';//it means we have to do R cmd exec later. strcpy(client_message,"Which file do you want to delete?\n"); //Send the message back to client if(write(sock , client_message , strlen(client_message))<0) { puts("Send failed"); return 1; } memset(client_message,0,sizeof(client_message)); } ///////////////recv R end ///////////////recv L cmd else if(strcmp (client_message,"L")==0) { received_cmd='L';//it means we have to do L cmd exec later. strcpy(client_message,"The followings are the files created on server side.\n"); //call ls do_list(client_message); strcat(client_message,"The aboves are the files created on server side.\n"); //Send the message back to client if(write(sock , client_message , strlen(client_message))<0) { puts("Send failed"); return 1; } memset(client_message,0,sizeof(client_message)); } ///////////////recv L end ///////////////recv D cmd else if(strcmp (client_message,"D")==0) { received_cmd='D';//it means we have to do D cmd exec later. strcpy(client_message,"Which file do you want to download?\n"); //Send the message back to client if(write(sock , client_message , strlen(client_message))<0) { puts("Send failed"); return 1; } memset(client_message,0,sizeof(client_message)); } /////////////////end recv D cmd //////////////////recv E cmd else if(strcmp (client_message,"E")==0) { received_cmd='E';//it means we have to do E cmd exec later. strcpy(client_message,"Which file do you want to edit in \"vi\"?\n"); if(write(sock , client_message, strlen(client_message)) < 0) { puts("Send failed"); return 1; } memset(client_message,0,sizeof(client_message)); } else if(strcmp (client_message,"Copy")==0) { received_cmd='P';//it means we have to do Copy cmd exec later. strcpy(client_message,"Which file do you want to copy?\n"); //Send the message back to client if(write(sock , client_message , strlen(client_message))<0) { puts("Send failed"); return 1; } memset(client_message,0,sizeof(client_message)); } //////////////////////////end recv E cmd ///////////////////////////recv write editted content into file //get the editted content and write them to the file. else if(strcmp (client_message,"Read file after editted in vi.")==0) { strcpy(client_message,"Ok send your file."); if(write(sock , client_message , strlen(client_message))<0) { puts("Send failed"); return 1; } memset(client_message,0,sizeof(client_message)); received_cmd='e'; } ///////////////////////////end writing editted content into file ////////////////////////////////client send a file name according to the previous command. else { ///////////////recv C cmd if(received_cmd=='C') { //int file_exist; //printf("this is new file info before check %s \n",client_message); //if the this file doesn't exist on server side, create_file(client_message); //Send the message back to client if(write(sock , client_message , strlen(client_message))<0) { puts("Send failed"); return 1; } memset(client_message,0,sizeof(client_message)); //printf("this is new file info %s \n",client_message); received_cmd=' '; } /////////////////////end creating new file exec /////////////////////recv removing file cmd //remove an existing file else if(received_cmd=='R') { if(do_remove(client_message)==0) { strcat(client_message," has been deleted!\n"); } else strcat(client_message," does not exist or can't be deleted!\n"); if(write(sock , client_message, strlen(client_message)) < 0) { puts("Send failed"); return 1; } memset(client_message,0,sizeof(client_message)); received_cmd=' '; } ///////////////////////////end exec removing file //////////////////////////exec Download cmd else if(received_cmd=='D') { //if_file_exist int exist_or_not; if(exist_or_not=whether_file_exist(client_message)==0) { //read the content of the file which user wants to download. read_file_content(client_message); } //download error :if the file does not exist. else { strcpy(client_message," does not exist!\n"); printf("this is D result: %s ",client_message); } //return the file content or the download error msg. if(write(sock , client_message, strlen(client_message)) < 0) { puts("Send failed"); return 1; } memset(client_message,0,sizeof(client_message)); received_cmd=' '; } ////////////////////////end exec Download cmd /////////////////////////recv Edit cmd else if(received_cmd=='E') { //To record the editted_filename because we have to store the editted content back to it. strcpy(editted_filename,client_message); //received_cmd='B'; received_cmd=' '; //if_file_exists printf("Edit file name: %s",editted_filename); int exist_or_not; //if file exists if(exist_or_not=whether_file_exist(client_message)==0) { //read the content of the file which user wants to download. read_file_content(client_message); } //download error :if the file does not exist. else { strcpy(client_message,"File does not exist.You can't edit it\n"); } //return the file content or the download error msg. if(write(sock , client_message, strlen(client_message)) < 0) { puts("Send failed"); return 1; } memset(client_message,0,strlen(client_message)); } /////to write the editted content back to the file on server side. else if(received_cmd=='e') { //get the editted content and write them to the file. write_editted_content_to_file(editted_filename,client_message); printf("this is editted content form client %s\n",client_message); printf("write_editted_content_to_file %s ",editted_filename); memset(client_message,0,sizeof(client_message)); received_cmd=' '; } ///////////////////////end exec E cmd ////////////////////////exec Copy cmd else if(received_cmd=='P') { //int file_exist; //printf("this is new file info before check %s \n",client_message); //if the this file doesn't exist on server side, int exist_or_not; if(exist_or_not=whether_file_exist(client_message)==0) { strcpy(Copied_filename,client_message); strcpy(client_message,"What's the name of your new file which is the copy of "); strcat(client_message,Copied_filename); } else { strcat(client_message," doesn't exist! You can't copy it.\n"); } //Send the message back to client if(write(sock , client_message , strlen(client_message))<0) { puts("Send failed"); return 1; } memset(client_message,0,sizeof(client_message)); //printf("this is new file info %s \n",client_message); received_cmd='p'; } else if(received_cmd=='p') { copy_file(Copied_filename, client_message); strcat(client_message," has been created by coping from "); strcat(client_message,Copied_filename); //Send the message back to client if(write(sock , client_message , strlen(client_message))<0) { puts("Send failed"); return 1; } memset(client_message,0,sizeof(client_message)); //printf("this is new file info %s \n",client_message); received_cmd=' '; } ////////////////////////end Copy cmd exec ///////////////////////////recv trash cmd else { printf("illegal cmd before send %s \n",client_message); //strcat( client_message," is created!\n"); strcpy(client_message,"This command doesn't exist!\n"); if(write(sock , client_message, strlen(client_message)) < 0) { puts("Send failed"); return 1; } printf("illegal cmd %s \n",client_message); memset(client_message,0,sizeof(client_message)); received_cmd=' '; } ///////////////////////////end exec trash cmd } //memset(client_message,0,sizeof(client_message)); } if(read_size == 0) { puts("Client disconnected"); fflush(stdout); } else if(read_size == -1) { perror("recv failed"); } //Free the socket pointer free(socket_desc); return 0; }