int Convert_File_To_List(log_t* Link_list,char * filename, unsigned long long int bytesToTransfer){ //FILE* NOW; FILE* article; unsigned long long int num_byte = 0; unsigned long long int total_byte = 0; char Chunk_data[PACKAGE_SIZE]; article = fopen (filename, "rb"); //NOW = fopen("NOW","wb"); unsigned long long int s_length = 0; /* if(bytesToTransfer < sizeof(Chunk_data)){ s_length = bytesToTransfer; } else{ s_length = sizeof(Chunk_data); } */ s_length = min(bytesToTransfer, sizeof(Chunk_data)); //printf("in_f\n"); unsigned long long int transfer_length = bytesToTransfer; if (article){ memset(Chunk_data, 0, sizeof Chunk_data); while (((num_byte = fread(Chunk_data, sizeof(char), s_length, article)) > 0) && (total_byte < transfer_length)){ total_byte += num_byte; //Chunk_data[num_byte] = '\0';///////// log_push(Link_list, Chunk_data, num_byte); //fwrite(Chunk_data, sizeof(char), strlen(buffer), out); /* if(bytesToTransfer < sizeof(Chunk_data)){ s_length = bytesToTransfer; } else{ s_length = sizeof(Chunk_data); } */ bytesToTransfer = bytesToTransfer - num_byte; s_length = min(bytesToTransfer, sizeof(Chunk_data)); total++; memset(Chunk_data, 0, sizeof Chunk_data); } } else{ printf("file not exist\n"); return FAIL; } fclose(article); return OK; }
int main() { log_t l; log_init( &l ); while(1) { size_t length = 1; char * buff = calloc( length, sizeof( char ) ); char * cwd = getcwd( buff, length ); while( cwd == NULL ) { length *= 2; buff = realloc( buff, length * sizeof(char) ); cwd = getcwd( buff, length ); } pid_t pid = getpid(); printf("(pid=%d)%s$ ", pid, cwd); free( buff ); char * input = NULL; size_t leng = 0; size_t read = 0; read = getline( &input, &leng, stdin); if( strcmp( input, "\n" ) == 0) { free( input ); continue; } if( *input == '!' ) { printf("Command executed by pid=%d\n", pid); if( strcmp( input, "!#\n" ) == 0 ) { int i; for( i = 0; i < l.num; i++ ) { printf("%s", l.histo[i] ); } free( input ); continue; } else { char * result = log_search( &l, input +1 ); if( result == NULL ) { puts("No Match"); free( input ); continue; } char * temp = malloc( strlen( input + 1) ); int k; for(k = 0; k < strlen( input + 1 ) - 1; k ++) { temp[k] = input[k+1]; } temp[k] = '\0'; printf("%s matches %s", temp, result); free(temp); log_push( &l, result ); free( input ); input = calloc( strlen( result ) + 1, sizeof( char ) ); strcpy( input, result ); } } else { log_push( &l, input ); } /*if( feof( stdin ) ) { log_destroy( &l ); exit( 0 ); }*/ if( strcmp( input, "exit\n" ) == 0 ) { printf("Command executed by pid=%d\n", pid); free( input ); log_destroy( &l ); exit( 0 ); } char * token = strtok( input, " \n" ); char * first = token; char ** follower = calloc( 100, sizeof( char * ) ); int i = 1; while( token ) { token = strtok( NULL, " \n" ); follower[i] = token; i++; } follower[0] = first; follower[i] = NULL; if( strcmp( first, "cd") == 0 ) { printf("Command executed by pid=%d\n", pid); if( chdir( follower[1] ) ) printf("%s: No such file or directory\n", follower[1] ); free( follower ); free( input ); continue; } pid_t child; int status; if ( ( child = fork() ) < 0 ) { perror("Filed to fork"); exit(1); } else if ( child == 0 ) { pid_t pid = getpid(); printf("Command executed by pid=%d\n", pid); execvp( first, follower ); printf("%s: not found\n", first); free(follower); free( input ); log_destroy( &l ); exit(1); } else { child = wait( &status ); } free( follower ); free( input ); } log_destroy( &l ); return 0; }
/** * Starting point for shell. */ int main() { log_init(&Log); char cwd[1024]; int pid; size_t size= 0; while(1){ pid= getpid(); getcwd(cwd, 1024); printf("(pid=%d)%s$ ", pid, cwd); char * line= NULL; getline(&line, &size, stdin); line[strlen(line)-1]= '\0'; if(strstr(line, "cd") == line){ const char * path= line+3; int change= chdir(path); if(change==0){ int pid= getpid(); printf("Command executed by pid=%d\n", pid); log_push(&Log, line); continue; } else{ printf("%s: No such file or directory\n", path); log_push(&Log, line); continue; } } if(strstr(line, "exit") == line){ int pid= getpid(); printf("Command executed by pid=%d\n", pid); log_push(&Log, line); log_destroy(&Log); exit(1); } if(strstr(line, "!#")){ log_entry * curr= Log.head; while(curr){ printf("%s\n", curr->data); curr= curr->next; } free(line); continue; } if(strstr(line, "!") == line){ const char * query= line+1; char * match= NULL; if((&Log)->size > 0) match= log_search(&Log, query); if(match){ printf("%s matches %s\n", query, match); char * line2= malloc(strlen(match)+1); strcpy(line2, match); log_push(&Log, line2); free(line); if(strstr(match, "cd ")){ char * path= match + 3; int change= chdir(path); if(change == 0){ int pid= getpid(); printf("Command executed by pid=%d\n", pid); continue; } else{ printf("%s: No such file or directory\n",path); continue; } } char * array[5]= {NULL, NULL, NULL, NULL, NULL}; char * temp2= malloc(strlen(match)+1); strcpy(temp2, match); char * str= strtok(temp2, " "); if(!str){ array[0] = malloc(strlen(temp2)+1); strcpy(array[0], temp2); } else{ int i=0; for(i=0; i < 5 && str; i++){ array[i]= malloc(strlen(str)+1); strcpy(array[i], str); str= strtok(NULL, " "); } } int status; pid_t child= fork(); if(child >= 0){ if(child == 0){ execvp(array[0], array); printf("%s: not found\n", line); exit(1); } else{ pid_t pid= getpid(); while(pid != child){ pid= wait(&status); } printf("Command executed by pid=%d\n", pid); } } else{ printf("fork failed\n"); exit(1); } free(temp2); free(str); int i=0; for(i=0; i < 5; i++){ free(array[i]); } continue; } else printf("No Match\n"); continue; } char * array[5]= {NULL, NULL, NULL, NULL, NULL}; char * temp= malloc(strlen(line)+1); strcpy(temp, line); char * str= strtok(temp, " "); if(!str){ array[0] = malloc(strlen(temp)+1); strcpy(array[0], temp); } else{ int i=0; for(i=0; i < 5 && str; i++){ array[i]= malloc(strlen(str)+1); strcpy(array[i], str); str= strtok(NULL, " "); } } int status; pid_t child= fork(); if(child == 0){ execvp(array[0], array); printf("%s: not found\n", temp); exit(1); } else{ pid_t pid= getpid(); while(pid != child){ pid= wait(&status); } printf("Command executed by pid=%d\n", pid); log_push(&Log, line); } int i=0; for(i=0; i < 5; i++){ free(array[i]); } free(str); free(temp); } return 0; }