Mercurial > hg > toybox
annotate toys/posix/cut.c @ 1477:91767d247a50 draft
Cleanup pass on cut, more to do.
Cut predates the "pending" directory, so was checked in as-is. Ashwini
Sharma's recent static analysis fixes touched this file, but there's a lot
more to do than the static analyzer found.
author | Rob Landley <rob@landley.net> |
---|---|
date | Wed, 17 Sep 2014 08:26:07 -0500 |
parents | fc1bb49e58a9 |
children | 434c4ae19f05 |
rev | line source |
---|---|
706
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
1 /* cut.c - Cut from a file. |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
2 * |
1477
91767d247a50
Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents:
1333
diff
changeset
|
3 * Copyright 2012 Ranjan Kumar <ranjankumar.bth@gmail.com> |
91767d247a50
Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents:
1333
diff
changeset
|
4 * Copyright 2012 Kyungwan Han <asura321@gmail.com> |
706
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
5 * |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
6 * http://pubs.opengroup.org/onlinepubs/9699919799/utilities/cut.html |
1067
2bfdd63382b4
First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
7 |
2bfdd63382b4
First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
8 USE_CUT(NEWTOY(cut, "b:|c:|f:|d:sn[!cbf]", TOYFLAG_BIN)) |
706
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
9 |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
10 config CUT |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
11 bool "cut" |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
12 default y |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
13 help |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
14 usage: cut OPTION... [FILE]... |
1333
fc1bb49e58a9
Help text should have a blank line after usage: lines, and a couple other whitespace tweaks.
Rob Landley <rob@landley.net>
parents:
1134
diff
changeset
|
15 |
706
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
16 Print selected parts of lines from each FILE to standard output. |
1333
fc1bb49e58a9
Help text should have a blank line after usage: lines, and a couple other whitespace tweaks.
Rob Landley <rob@landley.net>
parents:
1134
diff
changeset
|
17 |
1477
91767d247a50
Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents:
1333
diff
changeset
|
18 -b LIST select only these bytes from LIST. |
91767d247a50
Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents:
1333
diff
changeset
|
19 -c LIST select only these characters from LIST. |
91767d247a50
Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents:
1333
diff
changeset
|
20 -f LIST select only these fields. |
91767d247a50
Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents:
1333
diff
changeset
|
21 -d DELIM use DELIM instead of TAB for field delimiter. |
91767d247a50
Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents:
1333
diff
changeset
|
22 -s do not print lines not containing delimiters. |
91767d247a50
Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents:
1333
diff
changeset
|
23 -n don't split multibyte characters (Ignored). |
706
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
24 */ |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
25 #define FOR_cut |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
26 #include "toys.h" |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
27 |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
28 GLOBALS( |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
29 char *delim; |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
30 char *flist; |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
31 char *clist; |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
32 char *blist; |
1477
91767d247a50
Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents:
1333
diff
changeset
|
33 |
91767d247a50
Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents:
1333
diff
changeset
|
34 void *slist_head; |
706
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
35 unsigned nelem; |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
36 ) |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
37 |
1477
91767d247a50
Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents:
1333
diff
changeset
|
38 struct slist { |
91767d247a50
Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents:
1333
diff
changeset
|
39 struct slist *next; |
91767d247a50
Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents:
1333
diff
changeset
|
40 int start, end; |
91767d247a50
Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents:
1333
diff
changeset
|
41 }; |
91767d247a50
Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents:
1333
diff
changeset
|
42 |
706
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
43 void (*do_cut)(int); |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
44 static void do_fcut(int fd); |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
45 static void do_bccut(int fd); |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
46 |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
47 static void add_to_list(int start, int end) |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
48 { |
1477
91767d247a50
Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents:
1333
diff
changeset
|
49 struct slist *current, *head_ref, *temp1_node; |
706
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
50 |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
51 head_ref = TT.slist_head; |
1477
91767d247a50
Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents:
1333
diff
changeset
|
52 temp1_node = xzalloc(sizeof(struct slist)); |
91767d247a50
Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents:
1333
diff
changeset
|
53 temp1_node->start = start; |
91767d247a50
Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents:
1333
diff
changeset
|
54 temp1_node->end = end; |
706
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
55 |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
56 /* Special case for the head end */ |
1477
91767d247a50
Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents:
1333
diff
changeset
|
57 if (!head_ref || head_ref->start >= start) { |
706
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
58 temp1_node->next = head_ref; |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
59 head_ref = temp1_node; |
1067
2bfdd63382b4
First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
60 } else { |
706
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
61 /* Locate the node before the point of insertion */ |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
62 current = head_ref; |
1477
91767d247a50
Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents:
1333
diff
changeset
|
63 while (current->next && current->next->start < temp1_node->start) |
706
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
64 current = current->next; |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
65 temp1_node->next = current->next; |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
66 current->next = temp1_node; |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
67 } |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
68 TT.slist_head = head_ref; |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
69 } |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
70 |
1067
2bfdd63382b4
First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
71 // parse list and add to slist. |
706
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
72 static void parse_list(char *list) |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
73 { |
1067
2bfdd63382b4
First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
74 for (;;) { |
2bfdd63382b4
First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
75 char *ctoken = strsep(&list, ","), *dtoken; |
2bfdd63382b4
First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
76 int start = 0, end = INT_MAX; |
2bfdd63382b4
First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
77 |
2bfdd63382b4
First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
78 if (!ctoken) break; |
2bfdd63382b4
First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
79 if (!*ctoken) continue; |
706
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
80 |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
81 //Get start position. |
1067
2bfdd63382b4
First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
82 if (*(dtoken = strsep(&ctoken, "-"))) { |
1134 | 83 start = atolx_range(dtoken, 0, INT_MAX); |
706
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
84 start = (start?(start-1):start); |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
85 } |
1067
2bfdd63382b4
First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
86 |
706
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
87 //Get end position. |
1067
2bfdd63382b4
First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
88 if (!ctoken) end = -1; //case e.g. 1,2,3 |
2bfdd63382b4
First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
89 else if (*ctoken) {//case e.g. N-M |
1134 | 90 end = atolx_range(ctoken, 0, INT_MAX); |
1067
2bfdd63382b4
First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
91 if (!end) end = INT_MAX; |
2bfdd63382b4
First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
92 end--; |
2bfdd63382b4
First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
93 if(end == start) end = -1; |
706
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
94 } |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
95 add_to_list(start, end); |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
96 TT.nelem++; |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
97 } |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
98 //if list is missing in command line. |
1067
2bfdd63382b4
First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
99 if (!TT.nelem) error_exit("missing positions list"); |
706
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
100 } |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
101 |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
102 /* |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
103 * retrive data from the file/s. |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
104 */ |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
105 static void get_data(void) |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
106 { |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
107 char **argv = toys.optargs; //file name. |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
108 toys.exitval = EXIT_SUCCESS; |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
109 |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
110 if(!*argv) do_cut(0); //for stdin |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
111 else { |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
112 for(; *argv; ++argv) { |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
113 if(strcmp(*argv, "-") == 0) do_cut(0); //for stdin |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
114 else { |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
115 int fd = open(*argv, O_RDONLY, 0); |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
116 if(fd < 0) {//if file not present then continue with other files. |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
117 perror_msg(*argv); |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
118 continue; |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
119 } |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
120 do_cut(fd); |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
121 xclose(fd); |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
122 } |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
123 } |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
124 } |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
125 } |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
126 |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
127 void cut_main(void) |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
128 { |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
129 char delimiter = '\t'; //default delimiter. |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
130 char *list; |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
131 |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
132 TT.nelem = 0; |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
133 TT.slist_head = NULL; |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
134 |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
135 //Get list and assign the function. |
1067
2bfdd63382b4
First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
136 if (toys.optflags & FLAG_f) { |
706
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
137 list = TT.flist; |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
138 do_cut = do_fcut; |
1067
2bfdd63382b4
First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
139 } else if (toys.optflags & FLAG_c) { |
706
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
140 list = TT.clist; |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
141 do_cut = do_bccut; |
1067
2bfdd63382b4
First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
142 } else { |
706
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
143 list = TT.blist; |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
144 do_cut = do_bccut; |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
145 } |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
146 |
1067
2bfdd63382b4
First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
147 if (toys.optflags & FLAG_d) { |
706
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
148 //delimiter must be 1 char. |
1067
2bfdd63382b4
First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
149 if(TT.delim[0] && TT.delim[1]) |
2bfdd63382b4
First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
150 perror_exit("the delimiter must be a single character"); |
706
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
151 delimiter = TT.delim[0]; |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
152 } |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
153 |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
154 if(!(toys.optflags & FLAG_d) && (toys.optflags & FLAG_f)) { |
1067
2bfdd63382b4
First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
155 TT.delim = xzalloc(2); |
706
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
156 TT.delim[0] = delimiter; |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
157 } |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
158 |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
159 //when field is not specified, cutting has some special handling. |
1067
2bfdd63382b4
First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
160 if (!(toys.optflags & FLAG_f)) { |
2bfdd63382b4
First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
161 if (toys.optflags & FLAG_s) |
2bfdd63382b4
First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
162 perror_exit("suppressing non-delimited lines operating on fields"); |
2bfdd63382b4
First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
163 if (delimiter != '\t') |
2bfdd63382b4
First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
164 perror_exit("an input delimiter may be specified only when operating on fields"); |
706
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
165 } |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
166 |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
167 parse_list(list); |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
168 get_data(); |
1067
2bfdd63382b4
First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
169 if (!(toys.optflags & FLAG_d) && (toys.optflags & FLAG_f)) { |
706
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
170 free(TT.delim); |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
171 TT.delim = NULL; |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
172 } |
1477
91767d247a50
Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents:
1333
diff
changeset
|
173 llist_traverse(TT.slist_head, free); |
706
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
174 } |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
175 |
1067
2bfdd63382b4
First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
176 // perform cut operation on the given delimiter. |
706
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
177 static void do_fcut(int fd) |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
178 { |
1477
91767d247a50
Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents:
1333
diff
changeset
|
179 char *buff, *pfield = 0, *delimiter = TT.delim; |
1067
2bfdd63382b4
First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
180 |
1477
91767d247a50
Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents:
1333
diff
changeset
|
181 for (;;) { |
91767d247a50
Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents:
1333
diff
changeset
|
182 unsigned cpos = 0; |
91767d247a50
Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents:
1333
diff
changeset
|
183 int start, ndelimiters = -1; |
91767d247a50
Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents:
1333
diff
changeset
|
184 int nprinted_fields = 0; |
91767d247a50
Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents:
1333
diff
changeset
|
185 struct slist *temp_node = TT.slist_head; |
91767d247a50
Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents:
1333
diff
changeset
|
186 |
91767d247a50
Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents:
1333
diff
changeset
|
187 free(pfield); |
91767d247a50
Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents:
1333
diff
changeset
|
188 pfield = 0; |
91767d247a50
Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents:
1333
diff
changeset
|
189 |
91767d247a50
Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents:
1333
diff
changeset
|
190 if (!(buff = get_line(fd))) break; |
91767d247a50
Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents:
1333
diff
changeset
|
191 |
706
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
192 //does line have any delimiter?. |
1067
2bfdd63382b4
First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
193 if (strrchr(buff, (int)delimiter[0]) == NULL) { |
706
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
194 //if not then print whole line and move to next line. |
1067
2bfdd63382b4
First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
195 if (!(toys.optflags & FLAG_s)) xputs(buff); |
706
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
196 continue; |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
197 } |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
198 |
1477
91767d247a50
Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents:
1333
diff
changeset
|
199 pfield = xzalloc(strlen(buff) + 1); |
706
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
200 |
1477
91767d247a50
Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents:
1333
diff
changeset
|
201 if (temp_node) { |
706
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
202 //process list on each line. |
1067
2bfdd63382b4
First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
203 while (cpos < TT.nelem && buff) { |
2bfdd63382b4
First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
204 if (!temp_node) break; |
1477
91767d247a50
Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents:
1333
diff
changeset
|
205 start = temp_node->start; |
706
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
206 do { |
1477
91767d247a50
Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents:
1333
diff
changeset
|
207 char *field = 0; |
91767d247a50
Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents:
1333
diff
changeset
|
208 |
706
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
209 //count number of delimeters per line. |
1067
2bfdd63382b4
First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
210 while (buff) { |
2bfdd63382b4
First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
211 if (ndelimiters < start) { |
706
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
212 ndelimiters++; |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
213 field = strsep(&buff, delimiter); |
1067
2bfdd63382b4
First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
214 } else break; |
706
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
215 } |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
216 //print field (if not yet printed). |
1067
2bfdd63382b4
First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
217 if (!pfield[ndelimiters]) { |
2bfdd63382b4
First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
218 if (ndelimiters == start) { |
706
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
219 //put delimiter. |
1067
2bfdd63382b4
First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
220 if (nprinted_fields++ > 0) xputc(delimiter[0]); |
2bfdd63382b4
First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
221 if (field) fputs(field, stdout); |
706
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
222 //make sure this field won't print again. |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
223 pfield[ndelimiters] = (char) 0x23; //put some char at this position. |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
224 } |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
225 } |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
226 start++; |
1477
91767d247a50
Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents:
1333
diff
changeset
|
227 if ((temp_node->end < 0) || !buff) break; |
91767d247a50
Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents:
1333
diff
changeset
|
228 } while(start <= temp_node->end); |
706
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
229 temp_node = temp_node->next; |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
230 cpos++; |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
231 } |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
232 } |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
233 xputc('\n'); |
1477
91767d247a50
Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents:
1333
diff
changeset
|
234 } |
706
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
235 } |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
236 |
1067
2bfdd63382b4
First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
237 // perform cut operation char or byte. |
706
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
238 static void do_bccut(int fd) |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
239 { |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
240 char *buff; |
1067
2bfdd63382b4
First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
241 |
2bfdd63382b4
First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
242 while ((buff = get_line(fd)) != NULL) { |
706
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
243 unsigned cpos = 0; |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
244 int buffln = strlen(buff); |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
245 char *pfield = xzalloc(buffln + 1); |
1477
91767d247a50
Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents:
1333
diff
changeset
|
246 struct slist *temp_node = TT.slist_head; |
1067
2bfdd63382b4
First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
247 |
2bfdd63382b4
First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
248 if (temp_node != NULL) { |
2bfdd63382b4
First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
249 while (cpos < TT.nelem) { |
706
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
250 int start; |
1067
2bfdd63382b4
First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
251 |
2bfdd63382b4
First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
252 if (!temp_node) break; |
1477
91767d247a50
Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents:
1333
diff
changeset
|
253 start = temp_node->start; |
1067
2bfdd63382b4
First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
254 while (start < buffln) { |
706
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
255 //to avoid duplicate field printing. |
1067
2bfdd63382b4
First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
256 if (pfield[start]) { |
1477
91767d247a50
Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents:
1333
diff
changeset
|
257 if (++start <= temp_node->end) continue; |
706
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
258 temp_node = temp_node->next; |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
259 break; |
1067
2bfdd63382b4
First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
260 } else { |
706
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
261 //make sure this field won't print again. |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
262 pfield[start] = (char) 0x23; //put some char at this position. |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
263 xputc(buff[start]); |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
264 } |
1477
91767d247a50
Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents:
1333
diff
changeset
|
265 if (++start > temp_node->end) { |
706
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
266 temp_node = temp_node->next; |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
267 break; |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
268 } |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
269 } |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
270 cpos++; |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
271 } |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
272 xputc('\n'); |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
273 } |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
274 free(pfield); |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
275 pfield = NULL; |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
276 } |
35a9f9c5b53f
Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
277 } |