logoISU  

CS 456 - Systems Programming

Spring 2024

Displaying ./code/feb15/dir3.c

// open and read the current directory

#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>

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

	DIR *mydir; //defines a pointer to a directory
	struct dirent *file; //accessing the dirent struct, defined in dirent.h
	struct stat st; // defines the stat struct

	mydir = opendir(argv[1]); //opens specified directory

	// then run through directory, printing it's contents
	while((file = readdir(mydir)) != NULL){
		stat(file->d_name, &st);
		printf("%s %lld\n", file->d_name, st.st_size);
	}

	//close the directory
	closedir(mydir);

	return 0;

}