-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOS lab1.1.c
91 lines (81 loc) · 2.09 KB
/
OS lab1.1.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
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <stdio.h>
#include <stdlib.h>
typedef enum {
fsc_ok,
fsc_invalid_num_arg,
fsc_error_output
} function_status_code;
int print_all_file(FILE* stream) {
printf("Empty: _ptr: %p, _base: %p, _bufsiz: %d, _charbuf: %d, _cnt: %d, _file: %d, _flag: %d\n", stream->_ptr,
stream->_base,
stream->_bufsiz, stream->_charbuf,
stream->_cnt, stream->_file, stream->_flag);
char buff[1];
do {
fread(buff, sizeof(char), 1, stream);
printf("%d: _ptr: %p, _base: %p, _bufsiz: %d, _charbuf: %d, _cnt: %d, _file: %d, _flag: %d\n", buff[0],
stream->_ptr,
stream->_base,
stream->_bufsiz, stream->_charbuf,
stream->_cnt, stream->_file, stream->_flag);
} while (!feof(stream));
return 0;
}
int print_n_bytes_from_file(FILE* stream, int n) {
char* buff = (char*) malloc(sizeof(char) * (n));
if (buff == NULL) {
return -1;
}
fread(buff, sizeof(char), n, stream);
buff[n] = 0;
printf("%d bytes ", n);
for(int i = 0; i < n; i++){
printf("%d ", buff[i]);
}
return 0;
}
function_status_code execute(char* filename) {
FILE* stream = fopen(filename, "wb");
if (stream == NULL) {
return fsc_error_output;
}
char buff[11] = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
fwrite(buff, sizeof(char), 11, stream);
fclose(stream);
stream = fopen(filename, "rb");
if (stream == NULL) {
return fsc_error_output;
}
print_all_file(stream);
fclose(stream);
stream = fopen(filename, "rb");
if (stream == NULL) {
return fsc_error_output;
}
fseek(stream, 3, SEEK_SET);
print_n_bytes_from_file(stream, 4);
fclose(stream);
return fsc_ok;
}
function_status_code input(int argc, char* argv[]) {
if (argc != 2) {
return fsc_invalid_num_arg;
}
return execute(argv[1]);
}
int main(int argc, char* argv[]) {
switch (input(argc, argv)) {
case fsc_ok:
printf("All is ok");
break;
case fsc_invalid_num_arg:
printf("invalid argc\n");
break;
case fsc_error_output:
printf("couldn't open file\n");
break;
default:
printf("stc unknown\n");
}
return 0;
}