logoISU  

CS 456 - Systems Programming

Spring 2024

Displaying ./code/jan31/fd3.c

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>

int main (int argc, char **argv){

	if (argc < 4){
	   fprintf(stderr, "Usage %s file1 file2 file3\n",argv[0]);
	   exit(1);
	}

	//open a file, print it's file descriptor, then exit)

	int fd1 = open(argv[1], O_RDONLY);
	int fd2 = open(argv[2], O_RDONLY);
	int fd3 = open(argv[3], O_RDONLY);	

	printf("fd of %s: %d\n", argv[1], fd1);
	printf("fd of %s: %d\n", argv[2], fd2);
	printf("fd of %s: %d\n", argv[3], fd3);

	close(fd1);
	close(fd2);

	fd2 = open(argv[2], O_RDONLY);
	printf("fd of %s again: %d\n", argv[2], fd2);
	printf("fd of %s again: %d\n", argv[3], fd3);
	close(fd2);
	close(fd3);


	return 0;

}