student-distrib/system_call.c
int32_t syscall_open(const uint8_t* filename) {

	//null check
	if((filename == NULL) || (*filename == NULL))
		return -1;

	//get current file descriptor array from pcb
	pcb_t* pcb = get_pcb(tasks_running);
	file_descriptor_t* fd_array = pcb->fd;

	//error out if fd table is invalid
	if(fd_array == NULL)
		return -1;

	//find an empty position in fda to open
	int fd_index = 2;
	while(fd_index < 8){

		//flags=0 means open spot
		if(fd_array[fd_index].flags == 0)
			break;

		fd_index++;
	}

	//if no free space in fda, error out
	if(fd_index > 7)
		return -1;

	//char* f = filename;
	//detect if stdin or stdout is called and set correct fops table
	if(strncmp("stdin", (const char*)filename, 6) == 0)
		fd_array[fd_index].fops_table = &stdin_fops;
	else if(strncmp("stdout", (const char*)filename, 7) == 0)
		fd_array[fd_index].fops_table = &stdout_fops;

	dentry_t dentry;

	//check if file exists
	if(read_dentry_by_name((char*) filename, &dentry) == 0){

		//set correct fops table for each file type
		if(dentry.type == 0)	//rtc
			fd_array[fd_index].fops_table = &rtc_fops;
		else if(dentry.type == 1)	//folder
			fd_array[fd_index].fops_table = &dir_fops;
		else if(dentry.type == 2)	//file
			fd_array[fd_index].fops_table = &file_fops;
	}
	else
		return -1;	//return error if file is non-existent

	//if open fails
	if((*fd_array[fd_index].fops_table->open)(&fd_array[fd_index].inode, (char*) filename) == -1)
		return -1;

	//set flag to 1 signifying index is filled
	fd_array[fd_index].flags = 1;
	//return the fd index where file was initialized
	return fd_index;
}
/* system_open() - added by mlee148
 * Input: file name to be opened
 * Returns: -1 if cannot be execute
 * Effect:
 */
added by mlee148
Input: file name to be opened
Returns: -1 if cannot be execute
Effect.
added by mlee148 Input : file name to be opened Returns : - 1 if cannot be execute Effect .
int32_t syscall_open(const uint8_t* filename) {
	if((filename == NULL) || (*filename == NULL))
		return -1;
	pcb_t* pcb = get_pcb(tasks_running);
	file_descriptor_t* fd_array = pcb->fd;
	if(fd_array == NULL)
		return -1;
	int fd_index = 2;
	while(fd_index < 8){
		if(fd_array[fd_index].flags == 0)
			break;
		fd_index++;
	}
	if(fd_index > 7)
		return -1;
	if(strncmp("stdin", (const char*)filename, 6) == 0)
		fd_array[fd_index].fops_table = &stdin_fops;
	else if(strncmp("stdout", (const char*)filename, 7) == 0)
		fd_array[fd_index].fops_table = &stdout_fops;
	dentry_t dentry;
	if(read_dentry_by_name((char*) filename, &dentry) == 0){
		if(dentry.type == 0)	
			fd_array[fd_index].fops_table = &rtc_fops;
		else if(dentry.type == 1)	
			fd_array[fd_index].fops_table = &dir_fops;
		else if(dentry.type == 2)	
			fd_array[fd_index].fops_table = &file_fops;
	}
	else
		return -1;	
	if((*fd_array[fd_index].fops_table->open)(&fd_array[fd_index].inode, (char*) filename) == -1)
		return -1;
	fd_array[fd_index].flags = 1;
	return fd_index;
}
int32_t syscall_open ( const uint8_t * filename ) { if ( ( filename == NULL ) || ( * filename == NULL ) ) return -1 ; pcb_t * pcb = get_pcb ( tasks_running ) ; file_descriptor_t * fd_array = pcb -> fd ; if ( fd_array == NULL ) return -1 ; int fd_index = 2 ; while ( fd_index < 8 ) { if ( fd_array [ fd_index ] . flags == 0 ) break ; fd_index ++ ; } if ( fd_index > 7 ) return -1 ; if ( strncmp ( " " , ( const char * ) filename , 6 ) == 0 ) fd_array [ fd_index ] . fops_table = & stdin_fops ; else if ( strncmp ( " " , ( const char * ) filename , 7 ) == 0 ) fd_array [ fd_index ] . fops_table = & stdout_fops ; dentry_t dentry ; if ( read_dentry_by_name ( ( char * ) filename , & dentry ) == 0 ) { if ( dentry . type == 0 ) fd_array [ fd_index ] . fops_table = & rtc_fops ; else if ( dentry . type == 1 ) fd_array [ fd_index ] . fops_table = & dir_fops ; else if ( dentry . type == 2 ) fd_array [ fd_index ] . fops_table = & file_fops ; } else return -1 ; if ( ( * fd_array [ fd_index ] . fops_table -> open ) ( & fd_array [ fd_index ] . inode , ( char * ) filename ) == -1 ) return -1 ; fd_array [ fd_index ] . flags = 1 ; return fd_index ; }
system_open() - added by mlee148
 Input: file name to be opened
 Returns: -1 if cannot be execute
 Effect:
system_open () - added by mlee148 Input : file name to be opened Returns : - 1 if cannot be execute Effect :
//null check //get current file descriptor array from pcb //error out if fd table is invalid //find an empty position in fda to open //flags=0 means open spot //if no free space in fda, error out //char* f = filename; //detect if stdin or stdout is called and set correct fops table //check if file exists //set correct fops table for each file type //rtc //folder //file //return error if file is non-existent //if open fails //set flag to 1 signifying index is filled //return the fd index where file was initialized