-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcatpy.c
169 lines (154 loc) · 4.04 KB
/
catpy.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* catpy.c
*
* Author: shane kenny
* Date: 17-08-2022
*
* Notes: My first time programming :)
*/
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <assert.h>
#define STD_OUT 1
#define STD_ERR 2
size_t get_str_sz(const char*);
#define SUCCESS_MESSAGE "\nCats can pee:)\n"
#define SUCC_SIZE get_str_sz(SUCCESS_MESSAGE)
#define _ASSERT_MESSAGE(test, message) assert(((void)message, test))
size_t get_str_sz(const char* buffer)
{
size_t sz = 0;
while (buffer[sz] != '\0') ++sz;
return sz;
}
void write_error(const char* error_message)
{
size_t count = get_str_sz(error_message);
write(STD_ERR, error_message, count);
}
void write_file(const char* buffer, size_t count)
{
ssize_t result = write(STD_OUT, buffer, count);
if (result == -1) {
write_error("Error: Cat cannot py because cat has no thumbs!");
}
}
void slurp_file(const char* file_name)
{
// int open(const char *pathname, int flags);
int fd = open(file_name, O_RDONLY);
if (fd == -1) {
write_error("Error: Cat cannot py because file cannot be opened!\n");
}
// find the size of the file at fd
struct stat statbuf;
int result = fstat(fd, &statbuf);
off_t count = statbuf.st_size;
if (result == -1) {
write_error("Cat cannot py because it does not know when to stop!");
}
// syscall read that number of bytes into a malloced cstr
// count is the number of bytes that we want to eventually process
#define BUF_CAP 256
char buf[BUF_CAP];
int unread_bytes = count;
while (unread_bytes) {
if (unread_bytes >= BUF_CAP) {
read(fd, buf, BUF_CAP);
write_file(buf, BUF_CAP);
unread_bytes -= BUF_CAP;
} else {
read(fd, buf, unread_bytes);
write_file(buf, unread_bytes);
unread_bytes = 0;
}
}
close(fd);
}
void print_usage() {
const char *usage = "Usage:./catpy <filename>\n\t-m <message>\n";
write_error(usage);
exit(1);
}
int convert_count_string(char *count_string) {
int result = 0, len = -1;
while (count_string[++len]!='\0') {
if (count_string[len] > '9' || count_string[len] < '0') {
print_usage();
exit(1);
}
}
int d = 0;
while (d < len) {
result *= 10;
result += (count_string[d] - (int)'0');
d++;
}
return result;
}
// There are five system calls that generate file descriptors: create, open, fcntl, dup and pipe.
// lseek(2)
// O_RDONLY and not O_CREAT(auto not specified)
int main(int argc, char* argv[])
{
if (argc < 2 || argc%2 != 0) {
print_usage();
return 1;
}
if (argc == 2) {
const char *filename = argv[1];
slurp_file(filename);
write_file(SUCCESS_MESSAGE, SUCC_SIZE);
return 0;
}
// we can read them all in and anything that isn't paired with a -* is the filename
int filename_found = 0, message_len = SUCC_SIZE, count = 1;
char *message, *pot_filename, *count_string;
for (int i = 1; i < argc; ++i) {
if (argv[i][0] == '-') {
// this is a flag and we can switch on the character
const char c = argv[i][1];
switch (c) {
case '\0' :
print_usage();
return 1;
break;
case 'm':
message = argv[++i];
int j=-1;
while (message[++j]!='\0');
message_len = j;
break;
case 'c':
count_string = argv[++i];
count = convert_count_string(count_string);
break;
default:
print_usage();
exit(1);
}
} else {
pot_filename = argv[i];
if (access(pot_filename, F_OK) != 0) {
// the file does not exist
write_error("Cat cannot py because the cat cannot find a file to py.");
return 1;
}
}
}
const char *filename = pot_filename;
if (access(filename, F_OK) != 0) {
// the file does not exist
write_error("Cat cannot py because the cat cannot find a file to py.\n");
return 1;
}
for (int i = 0; i < count; ++i) {
slurp_file(filename);
write_file("\n", 1);
}
write_file(message, message_len);
write_file("\n", 1);
return 0;
}