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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
|
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include "7ed.h"
#include "input.h"
#include "i_validation.h"
#include "process_multiples.h"
#include <stdint.h>
#include <sys/stat.h>
extern int clfstdin_doubleprint;
uint8_t new = 0;
int ncat(char filename[]) {
FILE *file;
file = fopen(filename,"r");
if (file == NULL) {
fprintf(stderr, "Cannot open file. ncat\n");
return 1;
}
char buffer[BUF_SZ_2];
size_t bytes_read;
while ((bytes_read = fread(buffer, 1, BUF_SZ_2, file)) > 0) {
fwrite(buffer, 1, bytes_read, stdout);
}
fclose(file);
return 0;
}
int display_name_linecount(char *filename) {
uint64_t Flines;
int returnval = count_lines_in_file(filename, &Flines);
if (returnval == 1) {
return 1;
}
fprintf(stdout,"%s %ld lines\n", filename, Flines);
return 0;
}
int call_singles(char single, uint64_t focus, char *filename) {
switch(single) {
case 'p':
case 'P': {
char *line;
size_t start;
int ret = get_line(filename, focus, &line, &start);
if (ret == 1) {
return EXIT_FAILURE;
}
fprintf(stdout, "%s", line);
free(line);
break; }
case 'e':
case 'E':
editmode(filename, focus);
break;
case 'c':
case 'C': {
if (new == 1) { fprintf(stdout, "[NEW] "); }
int dnl = display_name_linecount(filename);
if (dnl == 1) {
return EXIT_FAILURE;
}
break; }
case 'q':
case 'Q':
if (new == 1) {
//fprintf(stdout, "no change, remove\n");
remove(filename);
}
exit(EXIT_SUCCESS);
break;
case 'a':
case 'A':
ncat(filename);
break;
}
return 0;
}
uint8_t check_if_file_exists(char *filename) { // If the file exists then do nothing. If it does not exist then make it.
FILE *file;
file = fopen(filename,"r"); // Open file
if (file == NULL) {
return FILE_NOT_FOUND;
}
fclose(file);
return FILE_EXISTS;
}
int fe_stop = 0;
int startmode(char filename[]) {
uint8_t file_existence = check_if_file_exists(filename);
if (fe_stop == 0) {
if (file_existence == FILE_NOT_FOUND) {
//fprintf(stdout, "filenotfound\n");
new = 1;
fe_stop = 1;
FILE *file = fopen(filename, "w");
fclose(file);
}
}
struct stat sb;
stat(filename, &sb);
if (sb.st_mode & S_IFDIR) { // check if its a directory
fprintf(stderr, "This is a directory\n");
exit(EXIT_FAILURE);
}
// The entry to the program. Count lines and display the count. Also show which file is being edited.
uint64_t Flines;
if (new == 1) { fprintf(stdout, "[NEW] "); }
int dnl = display_name_linecount(filename);
if (dnl == 1) {
return EXIT_FAILURE;
}
uint64_t focus = 1; // The focus variable. Which is the actual line number we have "selected"
while (1) {
char *multiple;
char single;
int clif = count_lines_in_file(filename, &Flines);
if (clif == 1) {
return EXIT_FAILURE;
}
int smode_input_ret = smode_input(&single, &multiple, focus);
switch (smode_input_ret) {
case _SINGLE:
call_singles(single, focus, filename);
break;
case _MULTIPLE:
switch(multiple[0]) {
case 'l':
case 'L':
focus = call_L(multiple, focus, Flines);
break;
case 'n':
case 'N':
if(call_N(multiple, focus, Flines, filename) == 0) {
new = 0;
}
break;
case 'x':
case 'X':
call_X(multiple, focus, Flines, filename);
break;
case 'd':
case 'D':
focus = call_D(multiple, focus, Flines, filename);
break;
}
free(multiple);
break;
case _FAIL:
if (clfstdin_doubleprint == 0) {
clfstdin_doubleprint = 1;
break;
}
fprintf(stdout, "?\n");
break;
case _RETURN: // if user just preses 'return' button
break;
}
}
}
|