comparison toys/posix/cut.c @ 1483:434c4ae19f05 draft

Reorder functions to get rid of unnecessary prototypes, and move a global into GLOBALS.
author Rob Landley <rob@landley.net>
date Thu, 18 Sep 2014 18:07:58 -0500
parents 91767d247a50
children 3ac823675413
comparison
equal deleted inserted replaced
1482:44e72a07fedc 1483:434c4ae19f05
31 char *clist; 31 char *clist;
32 char *blist; 32 char *blist;
33 33
34 void *slist_head; 34 void *slist_head;
35 unsigned nelem; 35 unsigned nelem;
36 void (*do_cut)(int fd);
36 ) 37 )
37 38
38 struct slist { 39 struct slist {
39 struct slist *next; 40 struct slist *next;
40 int start, end; 41 int start, end;
41 }; 42 };
42
43 void (*do_cut)(int);
44 static void do_fcut(int fd);
45 static void do_bccut(int fd);
46 43
47 static void add_to_list(int start, int end) 44 static void add_to_list(int start, int end)
48 { 45 {
49 struct slist *current, *head_ref, *temp1_node; 46 struct slist *current, *head_ref, *temp1_node;
50 47
105 static void get_data(void) 102 static void get_data(void)
106 { 103 {
107 char **argv = toys.optargs; //file name. 104 char **argv = toys.optargs; //file name.
108 toys.exitval = EXIT_SUCCESS; 105 toys.exitval = EXIT_SUCCESS;
109 106
110 if(!*argv) do_cut(0); //for stdin 107 if(!*argv) TT.do_cut(0); //for stdin
111 else { 108 else {
112 for(; *argv; ++argv) { 109 for(; *argv; ++argv) {
113 if(strcmp(*argv, "-") == 0) do_cut(0); //for stdin 110 if(strcmp(*argv, "-") == 0) TT.do_cut(0); //for stdin
114 else { 111 else {
115 int fd = open(*argv, O_RDONLY, 0); 112 int fd = open(*argv, O_RDONLY, 0);
116 if(fd < 0) {//if file not present then continue with other files. 113 if(fd < 0) {//if file not present then continue with other files.
117 perror_msg(*argv); 114 perror_msg(*argv);
118 continue; 115 continue;
119 } 116 }
120 do_cut(fd); 117 TT.do_cut(fd);
121 xclose(fd); 118 xclose(fd);
122 } 119 }
123 } 120 }
124 } 121 }
125 }
126
127 void cut_main(void)
128 {
129 char delimiter = '\t'; //default delimiter.
130 char *list;
131
132 TT.nelem = 0;
133 TT.slist_head = NULL;
134
135 //Get list and assign the function.
136 if (toys.optflags & FLAG_f) {
137 list = TT.flist;
138 do_cut = do_fcut;
139 } else if (toys.optflags & FLAG_c) {
140 list = TT.clist;
141 do_cut = do_bccut;
142 } else {
143 list = TT.blist;
144 do_cut = do_bccut;
145 }
146
147 if (toys.optflags & FLAG_d) {
148 //delimiter must be 1 char.
149 if(TT.delim[0] && TT.delim[1])
150 perror_exit("the delimiter must be a single character");
151 delimiter = TT.delim[0];
152 }
153
154 if(!(toys.optflags & FLAG_d) && (toys.optflags & FLAG_f)) {
155 TT.delim = xzalloc(2);
156 TT.delim[0] = delimiter;
157 }
158
159 //when field is not specified, cutting has some special handling.
160 if (!(toys.optflags & FLAG_f)) {
161 if (toys.optflags & FLAG_s)
162 perror_exit("suppressing non-delimited lines operating on fields");
163 if (delimiter != '\t')
164 perror_exit("an input delimiter may be specified only when operating on fields");
165 }
166
167 parse_list(list);
168 get_data();
169 if (!(toys.optflags & FLAG_d) && (toys.optflags & FLAG_f)) {
170 free(TT.delim);
171 TT.delim = NULL;
172 }
173 llist_traverse(TT.slist_head, free);
174 } 122 }
175 123
176 // perform cut operation on the given delimiter. 124 // perform cut operation on the given delimiter.
177 static void do_fcut(int fd) 125 static void do_fcut(int fd)
178 { 126 {
273 } 221 }
274 free(pfield); 222 free(pfield);
275 pfield = NULL; 223 pfield = NULL;
276 } 224 }
277 } 225 }
226
227 void cut_main(void)
228 {
229 char delimiter = '\t'; //default delimiter.
230 char *list;
231
232 TT.nelem = 0;
233 TT.slist_head = NULL;
234
235 //Get list and assign the function.
236 if (toys.optflags & FLAG_f) {
237 list = TT.flist;
238 TT.do_cut = do_fcut;
239 } else if (toys.optflags & FLAG_c) {
240 list = TT.clist;
241 TT.do_cut = do_bccut;
242 } else {
243 list = TT.blist;
244 TT.do_cut = do_bccut;
245 }
246
247 if (toys.optflags & FLAG_d) {
248 //delimiter must be 1 char.
249 if(TT.delim[0] && TT.delim[1])
250 perror_exit("the delimiter must be a single character");
251 delimiter = TT.delim[0];
252 }
253
254 if(!(toys.optflags & FLAG_d) && (toys.optflags & FLAG_f)) {
255 TT.delim = xzalloc(2);
256 TT.delim[0] = delimiter;
257 }
258
259 //when field is not specified, cutting has some special handling.
260 if (!(toys.optflags & FLAG_f)) {
261 if (toys.optflags & FLAG_s)
262 perror_exit("suppressing non-delimited lines operating on fields");
263 if (delimiter != '\t')
264 perror_exit("an input delimiter may be specified only when operating on fields");
265 }
266
267 parse_list(list);
268 get_data();
269 if (!(toys.optflags & FLAG_d) && (toys.optflags & FLAG_f)) {
270 free(TT.delim);
271 TT.delim = NULL;
272 }
273 llist_traverse(TT.slist_head, free);
274 }