-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain2.c
83 lines (69 loc) · 1.95 KB
/
main2.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
// #
// This version was created by Gemini assistent based on my original version.
/*
Switches:
--help
--version
--number
--show-tabs
--show-ends
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#define BUFFER_SIZE (4 * 1024)
const char *VersionString = "Version 1.0";
const char *HelpString = "options: --help, --version, --number";
// Function prototypes (if using separate functions for help/version)
int main(int argc, char *argv[]) {
FILE *fp;
char *buffer = malloc(BUFFER_SIZE); // Allocate buffer dynamically
int fd, nreads, nwrites;
size_t bytes_read = 0;
int i;
// Flag handling (using a loop for simplicity)
int fNumber = 0, fShowTabs = 0, fShowEnds = 0;
for (i = 1; i < argc; i++) {
if (strcmp(argv[i], "--help") == 0) {
// Call help function (if implemented)
return EXIT_SUCCESS;
} else if (strcmp(argv[i], "--version") == 0) {
// Call version function (if implemented)
return EXIT_SUCCESS;
} else if (strcmp(argv[i], "--number") == 0) {
fNumber = 1;
} else if (strcmp(argv[i], "--show-tabs") == 0) {
fShowTabs = 1;
} else if (strcmp(argv[i], "--show-ends") == 0) {
fShowEnds = 1;
} else {
// Open the file
fd = open(argv[i], O_RDONLY);
if (fd < 0) {
perror("open");
goto fail;
}
// Read and write in a loop
while ((nreads = read(fd, buffer, BUFFER_SIZE)) > 0) {
nwrites = write(1, buffer, nreads);
if (nwrites != nreads) {
perror("write");
goto fail;
}
bytes_read += nreads;
}
if (nreads < 0) {
perror("read");
goto fail;
}
close(fd); // Close the file
}
}
free(buffer); // Free the allocated buffer
return EXIT_SUCCESS;
fail:
free(buffer); // Free even on error
return EXIT_FAILURE;
}