예제 #1
0
void read_file_word_by_word(char *filename, list_t *list) {
    check(filename != NULL,
          "Given filename was null.");
    check(list != NULL,
          "list was NULL.");
    FILE *fp1 = NULL;
    char string[500];
    char c;
    fp1 = fopen(filename, "r");
    check(fp1 != NULL,
          "Could not open file: %s.", filename);
    do {
        c = fscanf(fp1, "%s", string);
        if(c != EOF) {
            string_to_lower(string);
            if(validate_word(string) != 0) {
                char *temp_word = string;
                check(dl_insert(list, temp_word) == 1,
                      "Did not insert '%s' properly,",
                      temp_word);
            }
        }
    } while(c != EOF);

    fclose(fp1);
    return;
error:
    if(list != NULL) {
        dl_clear_destroy(list);
    }
    exit(1);
}
예제 #2
0
파일: syscall.c 프로젝트: coweatyou/school
/* Pops the next argument off the stack and checks that it is a valid pointer. */
static void *next_arg() {
  ASSERT(lock_held_by_current_thread(&offset_lock));

  validate_word(offset);
  void *ret_val = *(uint32_t *) offset;
  offset += sizeof(uint32_t);
  return ret_val;
}
예제 #3
0
bool read_file_word_by_word(FILE *fp, list_t *list, 
		int actual_offset, int child_read_size, int size){
	check(list != NULL, 
		"list was NULL.");
	check(fp != NULL,
		"file was NULL");
	int offset = fseek(fp, actual_offset, SEEK_SET);
	check_debug(offset == 0, 
		"fseek returned non-zero.");
	if(actual_offset != 0){
		offset = fseek(fp, -1, SEEK_CUR);
		int cur_character;
		do {
			cur_character = fgetc(fp);
			if(isspace(cur_character) != 0){
				break;
			} else {
				offset = fseek(fp, -2, SEEK_CUR);
			}
		} while(isspace(cur_character) == 0
			&& cur_character != EOF
			&& offset == 0);
		actual_offset = ftell(fp);
		if((size - (actual_offset + child_read_size)) < child_read_size){
			child_read_size = size - actual_offset;
		}
	}
	int byte_count = 0;
	char string[512];
	int c;
	do {
		c = fscanf(fp, "%s", string);
		byte_count = ftell(fp) - actual_offset;
		if(byte_count < child_read_size
			|| (actual_offset + byte_count) == size){
			if(c != EOF){
				string_to_lower(string);
				if(validate_word(string) != 0){
					char *temp_word = string;
					check(dl_insert(list, temp_word) == 1,
						"Did not insert '%s' properly,",
						temp_word);
				}
			}
		} else {

		}
	} while(c != EOF && byte_count < child_read_size);
	return 1;
error:
	return 0;
}
예제 #4
0
int main(int argc, char *argv[]){
	int pid = 0;
	int i = 0;
	int child = 0;
	int status;
	
	char *first_param = "2";
	int num_children = atoi(first_param);
		
	int children[num_children];

	struct stat st;
	char *file_name = "./dllist.c";
	int size = 0;
	stat(file_name, &st);
	size = st.st_size;
	printf("file_name: %s, file_size: %d\n", file_name, size);
	FILE *fp = fopen(file_name, "r");
	int child_read_size = size / num_children;	
	int child_segment_size = 0;
	for(i = 0; i < NUM_ELEMS(children); i++){
		child_segment_size += child_read_size;
		pid = fork();
		if(pid != 0){
			children[i] = pid;
			printf("child_pid: %d\n", pid);
			waitpid(children[i], &status, 0);
		} else {
			child = 1;
			break;
		}
	}
	if(child == 0){
		printf("parent process, pid: %d\n", getpid());
	} else {
		printf("child process, pid: %d, ppid: %d\n", pid, getppid());
		printf("\tchild_start: %d, child_end: %d\n", 
			child_segment_size - child_read_size,
			child_segment_size);
		int actual_offset = child_segment_size - child_read_size;
		int offset = fseek(fp, actual_offset, SEEK_SET);
		printf("\toffset: %d\n", offset);
		int ch = 0;
		int byte_count = 0;
		char string_buffer[512];
		if(actual_offset != 0){
			int cur_character;
			do{
				cur_character = fgetc(fp);
				if(isspace(cur_character) == 0){
					offset = fseek(fp, -2, SEEK_CUR);
				} else {
					offset = fseek(fp, -1, SEEK_CUR);
				}
			} while(isspace(cur_character) == 0
				&& (cur_character != EOF
				&& offset == 0));
			printf("\t\tftell(fp): %lu\n", ftell(fp));
		}
		int word_count = 0;
		list_t *list = NULL;
		list = dl_init();
		do {		
			ch = fscanf(fp, "%s", string_buffer);
			if(actual_offset != 0 && byte_count == 0){
				printf("\t\tfirst_word: %s\n", string_buffer);
			}
			byte_count = ftell(fp) - (actual_offset);
			if(string_buffer[0] == '\0'){
				printf("\t\twhitespace_at_bytecount: %d\n", byte_count);
			}
			if(byte_count < child_read_size
				|| (actual_offset + byte_count) == size){
				if(ch != EOF){
					string_to_lower(string_buffer);
					if(validate_word(string_buffer) != 0){
						char *temp_word = string_buffer;
						check(dl_insert(list, temp_word) == 1,
							"Did not insert '%s' properly.",
							temp_word);
						word_count++;
					}
				} else {
					printf("\t\tlast_word_file: %s\n", string_buffer);
				}
			} else {
				printf("\t\tlast_word: %s\n", string_buffer);	
			}
		} while( ch != EOF && byte_count < child_read_size);
		dl_clear_destroy(list);	
		printf("\tbyte_count: %d, word_count: %d\n", byte_count, word_count);
		fclose(fp);
		exit(0);
	}
	rewind(fp);
	int temp;
	char temp_buffer[512];
	int true_wordcount = 0;
	do{
		temp = fscanf(fp, "%s", temp_buffer); 
		if(temp != EOF){
			true_wordcount++;
		}
	} while(temp != EOF);
	fclose(fp);
	printf("true_wordcount: %d\n", true_wordcount);
	return 0;
error:
	return 1;
}