-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
58 lines (50 loc) · 1.63 KB
/
main.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
#include <stdio.h>
#include <string.h>
// Maximum message length
#define MAX_MSG_LENGTH 1024
// Maximum ASCII art size
#define MAX_ASCII_LENGTH 4096
// Maximum message line length for word wrapping
#define MAX_LINE_LENGTH 64
// Word wrap for the message
void wrap_msg(char *message) {
int len = strlen(message);
if (len > MAX_LINE_LENGTH) {
for (int i = MAX_LINE_LENGTH; i < len; i += MAX_LINE_LENGTH) {
memmove(&message[i + 1], &message[i], len - i);
message[i] = '\n';
len++;
}
}
}
int main(int argc, char *argv[]) {
if (argc == 3) { // Expecting two arguments: message and file path
char msg[MAX_MSG_LENGTH];
if (strlen(argv[1]) < MAX_MSG_LENGTH) {
strcpy(msg, argv[1]);
wrap_msg(msg);
FILE *file;
char buffer[MAX_ASCII_LENGTH];
file = fopen(argv[2], "r");
if (file == NULL) {
printf("Error opening file\n");
return 1;
}
size_t asciiArtLength = fread(buffer, 1, sizeof(buffer), file);
if (asciiArtLength > MAX_ASCII_LENGTH) {
printf("ASCII art length overflow: Expected %i, got %zu\n", MAX_ASCII_LENGTH, asciiArtLength);
return 1;
}
fclose(file);
// Print the modified ASCII art with the message
printf(buffer, msg);
printf("\n");
} else {
printf("Error: Message too long. Expected length %i, got %zu\n", MAX_MSG_LENGTH, strlen(argv[1]));
return 1;
}
} else {
printf("Error: Missing arguments\n");
}
return 0;
}