-
Notifications
You must be signed in to change notification settings - Fork 0
/
main3.c
119 lines (102 loc) · 2.86 KB
/
main3.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
/*
// This version was created by Gemini assistent based on my original version.
*/
#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: -h, --help, -v, --version, -n, --number, -E, --show-ends, -T, --show-tabs";
// Function prototypes
void print_help(void);
void print_version(void);
int print_file(const char *filename, int flags);
int main(int argc, char *argv[]) {
int opt;
int flags = 0; // Initialize flags to 0
// Parse options using getopt
while ((opt = getopt(argc, argv, "hvnET")) != -1) {
switch (opt) {
case 'h':
case ':': // Handle missing argument for short options
print_help();
return EXIT_SUCCESS;
case 'v':
case '-': // Handle long options prefixed with '-'
print_version();
return EXIT_SUCCESS;
case 'n':
case 'E':
case 'T':
flags |= (1 << (opt - 'n')); // Set corresponding flag bit
break;
default:
fprintf(stderr, "Invalid option '-%c'\n", opt);
return EXIT_FAILURE;
}
}
// Check for remaining non-option arguments (filenames)
if (optind >= argc) {
fprintf(stderr, "Missing filename\n");
return EXIT_FAILURE;
}
// Process each file based on flags
for (int i = optind; i < argc; i++) {
if (print_file(argv[i], flags) != 0) {
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}
// Function to print help message
void print_help(void) {
printf("%s\n", HelpString);
// ... add detailed help message here ...
}
// Function to print version information
void print_version(void) {
printf("%s\n", VersionString);
}
// Function to open, read, and print a file with flags
int print_file(const char *filename, int flags) {
FILE *fp = fopen(filename, "r");
if (fp == NULL) {
perror("fopen");
return -1;
}
char buffer[BUFFER_SIZE];
int nreads;
// Print line numbers if --number flag is set
if (flags & 1) {
int line_number = 1;
while ((nreads = fread(buffer, 1, sizeof(buffer), fp)) > 0) {
for (int i = 0; i < nreads; i++) {
if (buffer[i] == '\n') {
printf("%d ", line_number++);
}
putchar(buffer[i]);
}
}
} else {
// Normal read and write
while ((nreads = fread(buffer, 1, sizeof(buffer), fp)) > 0) {
fwrite(buffer, 1, nreads, stdout);
}
}
if (ferror(fp)) {
perror("fread");
fclose(fp);
return -1;
}
// Print newline and show line ending if flags are set
if (!(flags & 2)) { // Check if show-ends flag is not set
putchar('\n');
}
if (flags & 4) { // Check if show-tabs flag is set
printf("$");
}
fclose(fp);
return 0;
}