forked from uicsystems/directory_reading_example
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.c
More file actions
111 lines (93 loc) · 2.66 KB
/
example.c
File metadata and controls
111 lines (93 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/* -----------------------------------------------
CS 361 - C reading contents of Directory Example
Program reads in contents of a directory and prints
out contents to std out. Pass directory as argument
to this program. Example:
Terminal Input:
"./example test_dir"
Terminal Output:
Contents of test_dir are:
.
..
test1.c
test2.txt
sub_dir
------------------------------------------------
*/
#include <dirent.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#define PATH_ARG 1
#define DIRECTORY_LISTING_MAX_CHAR 1013 // can get overflow if have a large directory and not enough characters
/*
*
* Note: Returns raw pointer to malloc'ed space. User is responsible
* for freeing up this pointer after its use
*
*/
char* get_directory_contents(char* directory_path)
{
char* directory_listing = NULL;
// open directory path up
DIR* path = opendir(directory_path);
// check to see if opening up directory was successful
if(path != NULL)
{
directory_listing = (char*) malloc(sizeof(char)*DIRECTORY_LISTING_MAX_CHAR);
directory_listing[0] = '\0';
// stores underlying info of files and sub_directories of directory_path
struct dirent* underlying_file = NULL;
// iterate through all of the underlying files of directory_path
while((underlying_file = readdir(path)) != NULL)
{
strcat(directory_listing, underlying_file->d_name);
strcat(directory_listing, "\n");
}
closedir(path);
}
return directory_listing;
}
/*
* Program requires one argument
* 1) the directory to print out its contents
*
*/
int main(int argc, char** argv)
{
//Check for enough arguments
if(argc != 2)
{
fprintf(stderr, "Arguments to Program not correct. Exiting.\n");
exit(-1);
}
//check if file exists
struct stat file_stat;
if (stat(argv[PATH_ARG], &file_stat) != 0) {
printf("%s does not exist! Program exiting\n", argv[PATH_ARG]);
exit(-1);
}
//check if file is a directory
if (S_ISDIR(file_stat.st_mode)) {
printf("%s is a directory\n", argv[PATH_ARG]);
} else {
printf("%s is NOT a directory! Program exiting\n", argv[PATH_ARG]);
exit(-1);
}
// read in contents and check for read in errors
char* directory_contents_str = get_directory_contents(argv[PATH_ARG]);
if(directory_contents_str != NULL)
{
printf("Contents of %s are:\n", argv[PATH_ARG]);
printf("%s\n", directory_contents_str);
}
else
{
fprintf(stderr, "Error reading contents of %s\n", argv[PATH_ARG]);
exit(-2);
}
return 0;
}