-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
91 lines (73 loc) · 2.43 KB
/
Copy pathmain.c
File metadata and controls
91 lines (73 loc) · 2.43 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
/* The following define only used as a workaround for wasi support of outter-os file flags,
* This will be replaced when gnfd has is own defined header for out-call wrappers.
*/
#define __wasilibc_unmodified_upstream 1
#ifndef O_CREAT
#define O_CREAT 00000100
#endif
#ifndef O_TRUNC
#define O_TRUNC 00001000
#endif
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#define BUFFER_SIZE 1024
int countWords(int fd) {
char buffer[BUFFER_SIZE];
int bytesRead, totalWords = 0;
int inWord = 0;
while ((bytesRead = read(fd, buffer, BUFFER_SIZE)) > 0) {
for (int i = 0; i < bytesRead; i++) {
if (buffer[i] == ' ' || buffer[i] == '\t' || buffer[i] == '\n') {
inWord = 0;
} else {
if (!inWord) {
inWord = 1;
totalWords++;
}
}
}
}
return totalWords;
}
int main(int argc, char *argv[]) {
if (argc < 3) {
printf("Usage: %s <input_file> <output_file>\n", argv[0]);
return 1;
}
char *inputFile = argv[1];
char *outputFile = argv[2];
int flag = O_RDONLY;
int mode = 0;
printf("App: trying to open file %s with flag 0x%x, mode %d\n", inputFile, flag, mode);
int inputFd = open(inputFile, flag);
if (inputFd == -1) {
printf("Failed to open the input file: %s\n", inputFile);
return 1;
}
int wordCount = countWords(inputFd);
flag = O_RDWR | O_CREAT | O_TRUNC;
mode = 0644;
printf("App: trying to open file %s with flag 0x%x, mode %d\n", outputFile, flag, mode);
int outputFd = open(outputFile, flag, mode);
if (outputFd == -1) {
printf("Failed to open the output file: %s\n", outputFile);
return 1;
}
char resultBuffer[20]; // Buffer to hold the result as a string
int resultLength = snprintf(resultBuffer, sizeof(resultBuffer), "%d", wordCount);
printf("App: Trying to write \"%s\" to outputfile\n", resultBuffer);
if (write(outputFd, resultBuffer, resultLength) != resultLength) {
printf("Failed to write the result to the output file: %s\n", outputFile);
return 1;
}
if (close(inputFd) == -1) {
printf("Failed to close the input file: %s\n", inputFile);
return 1;
}
if (close(outputFd) == -1) {
printf("Failed to close the output file: %s\n", outputFile);
return 1;
}
return 0;
}