1 /* cut.c - Cut from a file.
3 * Copyright 2012 Ranjan Kumar <ranjankumar.bth@gmail.com>
4 * Copyright 2012 Kyungwan Han <asura321@gmail.com>
6 * http://pubs.opengroup.org/onlinepubs/9699919799/utilities/cut.html
10 USE_CUT(NEWTOY(cut, "b:|c:|f:|d:sn[!cbf]", TOYFLAG_USR|TOYFLAG_BIN))
16 usage: cut OPTION... [FILE]...
18 Print selected parts of lines from each FILE to standard output.
20 -b LIST select only these bytes from LIST.
21 -c LIST select only these characters from LIST.
22 -f LIST select only these fields.
23 -d DELIM use DELIM instead of TAB for field delimiter.
24 -s do not print lines not containing delimiters.
25 -n don't split multibyte characters (Ignored).
38 void (*do_cut)(int fd);
46 static void add_to_list(int start, int end)
48 struct slist *current, *head_ref, *temp1_node;
50 head_ref = TT.slist_head;
51 temp1_node = xzalloc(sizeof(struct slist));
52 temp1_node->start = start;
53 temp1_node->end = end;
55 /* Special case for the head end */
56 if (!head_ref || head_ref->start >= start) {
57 temp1_node->next = head_ref;
58 head_ref = temp1_node;
60 /* Locate the node before the point of insertion */
62 while (current->next && current->next->start < temp1_node->start)
63 current = current->next;
64 temp1_node->next = current->next;
65 current->next = temp1_node;
67 TT.slist_head = head_ref;
70 // parse list and add to slist.
71 static void parse_list(char *list)
74 char *ctoken = strsep(&list, ","), *dtoken;
75 int start = 0, end = INT_MAX;
78 if (!*ctoken) continue;
80 // Get start position.
81 if (*(dtoken = strsep(&ctoken, "-"))) {
82 start = atolx_range(dtoken, 0, INT_MAX);
83 start = (start?(start-1):start);
87 if (!ctoken) end = -1; //case e.g. 1,2,3
88 else if (*ctoken) {//case e.g. N-M
89 end = atolx_range(ctoken, 0, INT_MAX);
90 if (!end) end = INT_MAX;
92 if(end == start) end = -1;
94 add_to_list(start, end);
97 // if list is missing in command line.
98 if (!TT.nelem) error_exit("missing positions list");
102 * retrive data from the file/s.
104 static void get_data(void)
106 char **argv = toys.optargs; //file name.
107 toys.exitval = EXIT_SUCCESS;
109 if(!*argv) TT.do_cut(0); //for stdin
111 for(; *argv; ++argv) {
112 if(strcmp(*argv, "-") == 0) TT.do_cut(0); //for stdin
114 int fd = open(*argv, O_RDONLY, 0);
115 if (fd < 0) {//if file not present then continue with other files.
116 perror_msg_raw(*argv);
126 // perform cut operation on the given delimiter.
127 static void do_fcut(int fd)
129 char *buff, *pfield = 0, *delimiter = TT.delim;
133 int start, ndelimiters = -1;
134 int nprinted_fields = 0;
135 struct slist *temp_node = TT.slist_head;
140 if (!(buff = get_line(fd))) break;
142 //does line have any delimiter?.
143 if (strrchr(buff, (int)delimiter[0]) == NULL) {
144 //if not then print whole line and move to next line.
145 if (!(toys.optflags & FLAG_s)) xputs(buff);
149 pfield = xzalloc(strlen(buff) + 1);
152 //process list on each line.
153 while (cpos < TT.nelem && buff) {
154 if (!temp_node) break;
155 start = temp_node->start;
159 //count number of delimeters per line.
161 if (ndelimiters < start) {
163 field = strsep(&buff, delimiter);
166 //print field (if not yet printed).
167 if (!pfield[ndelimiters]) {
168 if (ndelimiters == start) {
170 if (nprinted_fields++ > 0) xputc(delimiter[0]);
171 if (field) fputs(field, stdout);
172 //make sure this field won't print again.
173 pfield[ndelimiters] = (char) 0x23; //put some char at this position.
177 if ((temp_node->end < 0) || !buff) break;
178 } while(start <= temp_node->end);
179 temp_node = temp_node->next;
187 // perform cut operation char or byte.
188 static void do_bccut(int fd)
192 while ((buff = get_line(fd)) != NULL) {
194 int buffln = strlen(buff);
195 char *pfield = xzalloc(buffln + 1);
196 struct slist *temp_node = TT.slist_head;
198 if (temp_node != NULL) {
199 while (cpos < TT.nelem) {
202 if (!temp_node) break;
203 start = temp_node->start;
204 while (start < buffln) {
205 //to avoid duplicate field printing.
207 if (++start <= temp_node->end) continue;
208 temp_node = temp_node->next;
211 //make sure this field won't print again.
212 pfield[start] = (char) 0x23; //put some char at this position.
215 if (++start > temp_node->end) {
216 temp_node = temp_node->next;
231 char delimiter = '\t'; //default delimiter.
235 TT.slist_head = NULL;
237 //Get list and assign the function.
238 if (toys.optflags & FLAG_f) {
241 } else if (toys.optflags & FLAG_c) {
243 TT.do_cut = do_bccut;
246 TT.do_cut = do_bccut;
249 if (toys.optflags & FLAG_d) {
250 //delimiter must be 1 char.
251 if(TT.delim[0] && TT.delim[1])
252 perror_exit("the delimiter must be a single character");
253 delimiter = TT.delim[0];
256 if(!(toys.optflags & FLAG_d) && (toys.optflags & FLAG_f)) {
257 TT.delim = xzalloc(2);
258 TT.delim[0] = delimiter;
261 //when field is not specified, cutting has some special handling.
262 if (!(toys.optflags & FLAG_f)) {
263 if (toys.optflags & FLAG_s)
264 perror_exit("suppressing non-delimited lines operating on fields");
265 if (delimiter != '\t')
266 perror_exit("an input delimiter may be specified only when operating on fields");
271 if (!(toys.optflags & FLAG_d) && (toys.optflags & FLAG_f)) {
275 llist_traverse(TT.slist_head, free);