-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfopen_directory.c
78 lines (56 loc) · 1.52 KB
/
fopen_directory.c
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
/*
Program test how C stdlib handles directories opened
with function fopen.
Compilation:
$ gcc -Wall fopen_directory.c -o your_favorite_name
Usage:
$ ./your_favorite_name directory_path
Author : Wojciech Muła
Date : 2013-12-25
License : public domain
*/
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h> // for strerror
void fopen_directory(const char* path);
void print_errno(const char* prefix);
int main(int argc, char* argv[]) {
int i;
if (argc > 1) {
for (i=1; i < argc; i++)
fopen_directory(argv[i]);
return EXIT_SUCCESS;
} else {
printf("usage: %s directory\n", argv[0]);
return EXIT_FAILURE;
}
}
void fopen_directory(const char* path) {
FILE* f;
printf("testing '%s'...\n", path);
print_errno("fopen");
f = fopen(path, "rb");
if (f == NULL) {
return;
}
int seek_res = fseek(f, 0, SEEK_END);
print_errno("fseek");
printf("fseek result: %d\n", seek_res);
long tell_res = ftell(f);
print_errno("ftell");
printf("ftell result: %ld\n", tell_res);
int feof_res = feof(f);
print_errno("feof");
printf("feof result: %d (EOF=%s)\n", feof_res, feof_res == EOF ? "yes" : "no");
int fgetc_res = fgetc(f);
print_errno("fgetc");
printf("fgetc result: %d (EOF=%s)\n", fgetc_res, fgetc_res == EOF ? "yes" : "no");
char fread_buffer[1024];
int fread_result = fread(fread_buffer, 1024, 1, f);
print_errno("fread");
printf("fread result: %d\n", fread_result);
}
void print_errno(const char* prefix) {
printf("%s: %s [errno=%d]\n", prefix, strerror(errno), errno);
}