aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2006-11-26 15:48:03 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2006-11-26 15:48:03 +0000
commit0d42ddff7037c138d1df602158f80f9af914e927 (patch)
tree6877d65d4547c33f91a135284209911a7df61c49
parentcf749bc10c9e7b38217e102d0d3e7044e5515b4f (diff)
downloadbusybox-w32-0d42ddff7037c138d1df602158f80f9af914e927.tar.gz
busybox-w32-0d42ddff7037c138d1df602158f80f9af914e927.tar.bz2
busybox-w32-0d42ddff7037c138d1df602158f80f9af914e927.zip
sort: reformat entire file wrt style.
fix single obvious bug: right hand was 0 here: flags & (FLAG_b&FLAG_d&FLAG_f&FLAG_i&FLAG_bb) fixed to use |
-rw-r--r--coreutils/sort.c397
1 files changed, 215 insertions, 182 deletions
diff --git a/coreutils/sort.c b/coreutils/sort.c
index a6c56ad88..5f91bcb44 100644
--- a/coreutils/sort.c
+++ b/coreutils/sort.c
@@ -39,11 +39,10 @@ static int global_flags;
39#define FLAG_bb 32768 /* Ignore trailing blanks */ 39#define FLAG_bb 32768 /* Ignore trailing blanks */
40 40
41 41
42#ifdef CONFIG_FEATURE_SORT_BIG 42#if ENABLE_FEATURE_SORT_BIG
43static char key_separator; 43static char key_separator;
44 44
45static struct sort_key 45static struct sort_key {
46{
47 struct sort_key *next_key; /* linked list */ 46 struct sort_key *next_key; /* linked list */
48 unsigned short range[4]; /* start word, start char, end word, end char */ 47 unsigned short range[4]; /* start word, start char, end word, end char */
49 int flags; 48 int flags;
@@ -54,270 +53,304 @@ static char *get_key(char *str, struct sort_key *key, int flags)
54 int start = 0, end = 0, len, i, j; 53 int start = 0, end = 0, len, i, j;
55 54
56 /* Special case whole string, so we don't have to make a copy */ 55 /* Special case whole string, so we don't have to make a copy */
57 if(key->range[0]==1 && !key->range[1] && !key->range[2] && !key->range[3] 56 if (key->range[0] == 1 && !key->range[1] && !key->range[2] && !key->range[3]
58 && !(flags&(FLAG_b&FLAG_d&FLAG_f&FLAG_i&FLAG_bb))) return str; 57 && !(flags & (FLAG_b | FLAG_d | FLAG_f | FLAG_i | FLAG_bb))
59 /* Find start of key on first pass, end on second pass*/ 58 ) {
60 len=strlen(str); 59 return str;
60 }
61 61
62 for(j=0;j<2;j++) { 62 /* Find start of key on first pass, end on second pass*/
63 if(!key->range[2*j]) end=len; 63 len = strlen(str);
64 for (j = 0; j < 2; j++) {
65 if (!key->range[2*j])
66 end = len;
64 /* Loop through fields */ 67 /* Loop through fields */
65 else { 68 else {
66 end=0; 69 end = 0;
67 for(i=1;i<key->range[2*j]+j;i++) { 70 for (i = 1; i < key->range[2*j] + j; i++) {
68 /* Skip leading blanks or first separator */ 71 /* Skip leading blanks or first separator */
69 if(str[end]) { 72 if (str[end]) {
70 if(!key_separator && isspace(str[end])) 73 if (!key_separator && isspace(str[end]))
71 while(isspace(str[end])) end++; 74/* TODO: remove "&& isspace(str[end])" */
75 while (isspace(str[end])) end++;
72 } 76 }
73 /* Skip body of key */ 77 /* Skip body of key */
74 for(;str[end];end++) { 78 for (; str[end]; end++) {
75 if(key_separator) { 79 if (key_separator) {
76 if(str[end]==key_separator) break; 80 if (str[end] == key_separator)
77 } else if(isspace(str[end])) break; 81 break;
82 } else {
83 if (isspace(str[end]))
84 break;
85 }
78 } 86 }
79 } 87 }
80 } 88 }
81 if(!j) start=end; 89 if (!j) start = end;
82 } 90 }
83 /* Key with explicit separator starts after separator */ 91 /* Key with explicit separator starts after separator */
84 if(key_separator && str[start]==key_separator) start++; 92 if (key_separator && str[start] == key_separator)
93 start++;
85 /* Strip leading whitespace if necessary */ 94 /* Strip leading whitespace if necessary */
86//XXX: skip_whitespace() 95//XXX: skip_whitespace()
87 if(flags&FLAG_b) while(isspace(str[start])) start++; 96 if (flags & FLAG_b)
97 while (isspace(str[start])) start++;
88 /* Strip trailing whitespace if necessary */ 98 /* Strip trailing whitespace if necessary */
89 if(flags&FLAG_bb) while(end>start && isspace(str[end-1])) end--; 99 if (flags & FLAG_bb)
100 while (end > start && isspace(str[end-1])) end--;
90 /* Handle offsets on start and end */ 101 /* Handle offsets on start and end */
91 if(key->range[3]) { 102 if (key->range[3]) {
92 end+=key->range[3]-1; 103 end += key->range[3] - 1;
93 if(end>len) end=len; 104 if (end > len) end = len;
94 } 105 }
95 if(key->range[1]) { 106 if (key->range[1]) {
96 start+=key->range[1]-1; 107 start += key->range[1] - 1;
97 if(start>len) start=len; 108 if (start > len) start = len;
98 } 109 }
99 /* Make the copy */ 110 /* Make the copy */
100 if(end<start) end=start; 111 if (end < start) end = start;
101 str=xstrndup(str+start,end-start); 112 str = xstrndup(str+start, end-start);
102 /* Handle -d */ 113 /* Handle -d */
103 if(flags&FLAG_d) { 114 if (flags & FLAG_d) {
104 for(start=end=0;str[end];end++) 115 for (start = end = 0; str[end]; end++)
105 if(isspace(str[end]) || isalnum(str[end])) str[start++]=str[end]; 116 if (isspace(str[end]) || isalnum(str[end]))
106 str[start]=0; 117 str[start++] = str[end];
118 str[start] = '\0';
107 } 119 }
108 /* Handle -i */ 120 /* Handle -i */
109 if(flags&FLAG_i) { 121 if (flags & FLAG_i) {
110 for(start=end=0;str[end];end++) 122 for (start = end = 0; str[end]; end++)
111 if(isprint(str[end])) str[start++]=str[end]; 123 if (isprint(str[end]))
112 str[start]=0; 124 str[start++] = str[end];
125 str[start] = '\0';
113 } 126 }
114 /* Handle -f */ 127 /* Handle -f */
115 if(flags*FLAG_f) for(i=0;str[i];i++) str[i]=toupper(str[i]); 128 if (flags & FLAG_f)
129 for (i = 0; str[i]; i++)
130 str[i] = toupper(str[i]);
116 131
117 return str; 132 return str;
118} 133}
119 134
120static struct sort_key *add_key(void) 135static struct sort_key *add_key(void)
121{ 136{
122 struct sort_key **pkey=&key_list; 137 struct sort_key **pkey = &key_list;
123 while(*pkey) pkey=&((*pkey)->next_key); 138 while (*pkey)
139 pkey = &((*pkey)->next_key);
124 return *pkey = xzalloc(sizeof(struct sort_key)); 140 return *pkey = xzalloc(sizeof(struct sort_key));
125} 141}
126 142
127#define GET_LINE(fp) (global_flags&FLAG_z) ? bb_get_chunk_from_file(fp,NULL) \ 143#define GET_LINE(fp) \
128 : xmalloc_getline(fp) 144 ((global_flags & FLAG_z) \
145 ? bb_get_chunk_from_file(fp, NULL) \
146 : xmalloc_getline(fp))
129#else 147#else
130#define GET_LINE(fp) xmalloc_getline(fp) 148#define GET_LINE(fp) xmalloc_getline(fp)
131#endif 149#endif
132 150
133/* Iterate through keys list and perform comparisons */ 151/* Iterate through keys list and perform comparisons */
134static int compare_keys(const void *xarg, const void *yarg) 152static int compare_keys(const void *xarg, const void *yarg)
135{ 153{
136 int flags=global_flags,retval=0; 154 int flags = global_flags, retval = 0;
137 char *x,*y; 155 char *x, *y;
138 156
139#ifdef CONFIG_FEATURE_SORT_BIG 157#if ENABLE_FEATURE_SORT_BIG
140 struct sort_key *key; 158 struct sort_key *key;
141 159
142 for(key=key_list;!retval && key;key=key->next_key) { 160 for (key = key_list; !retval && key; key = key->next_key) {
143 flags=(key->flags) ? key->flags : global_flags; 161 flags = (key->flags) ? key->flags : global_flags;
144 /* Chop out and modify key chunks, handling -dfib */ 162 /* Chop out and modify key chunks, handling -dfib */
145 x=get_key(*(char **)xarg,key,flags); 163 x = get_key(*(char **)xarg, key, flags);
146 y=get_key(*(char **)yarg,key,flags); 164 y = get_key(*(char **)yarg, key, flags);
147#else 165#else
148 /* This curly bracket serves no purpose but to match the nesting 166 /* This curly bracket serves no purpose but to match the nesting
149 level of the for() loop we're not using */ 167 level of the for() loop we're not using */
150 { 168 {
151 x=*(char **)xarg; 169 x = *(char **)xarg;
152 y=*(char **)yarg; 170 y = *(char **)yarg;
153#endif 171#endif
154 /* Perform actual comparison */ 172 /* Perform actual comparison */
155 switch(flags&7) { 173 switch (flags & 7) {
156 default: 174 default:
157 bb_error_msg_and_die("unknown sort type"); 175 bb_error_msg_and_die("unknown sort type");
158 break; 176 break;
159 /* Ascii sort */ 177 /* Ascii sort */
160 case 0: 178 case 0:
161 retval=strcmp(x,y); 179 retval = strcmp(x, y);
162 break; 180 break;
163#ifdef CONFIG_FEATURE_SORT_BIG 181#if ENABLE_FEATURE_SORT_BIG
164 case FLAG_g: 182 case FLAG_g: {
165 { 183 char *xx, *yy;
166 char *xx,*yy; 184 double dx = strtod(x, &xx), dy = strtod(y, &yy);
167 double dx=strtod(x,&xx), dy=strtod(y,&yy); 185 /* not numbers < NaN < -infinity < numbers < +infinity) */
168 /* not numbers < NaN < -infinity < numbers < +infinity) */ 186 if (x == xx)
169 if(x==xx) retval=(y==yy ? 0 : -1); 187 retval = (y == yy ? 0 : -1);
170 else if(y==yy) retval=1; 188 else if (y == yy)
171 /* Check for isnan */ 189 retval = 1;
172 else if(dx != dx) retval = (dy != dy) ? 0 : -1; 190 /* Check for isnan */
173 else if(dy != dy) retval = 1; 191 else if (dx != dx)
174 /* Check for infinity. Could underflow, but it avoids libm. */ 192 retval = (dy != dy) ? 0 : -1;
175 else if(1.0/dx == 0.0) { 193 else if (dy != dy)
176 if(dx<0) retval=((1.0/dy == 0.0 && dy<0) ? 0 : -1); 194 retval = 1;
177 else retval=((1.0/dy == 0.0 && dy>0) ? 0 : 1); 195 /* Check for infinity. Could underflow, but it avoids libm. */
178 } else if(1.0/dy == 0.0) retval=dy<0 ? 1 : -1; 196 else if (1.0 / dx == 0.0) {
179 else retval=dx>dy ? 1 : (dx<dy ? -1 : 0); 197 if (dx < 0)
180 break; 198 retval = (1.0 / dy == 0.0 && dy < 0) ? 0 : -1;
181 } 199 else
182 case FLAG_M: 200 retval = (1.0 / dy == 0.0 && dy > 0) ? 0 : 1;
183 { 201 } else if (1.0 / dy == 0.0)
184 struct tm thyme; 202 retval = (dy < 0) ? 1 : -1;
185 int dx; 203 else
186 char *xx,*yy; 204 retval = (dx > dy) ? 1 : ((dx < dy) ? -1 : 0);
205 break;
206 }
207 case FLAG_M: {
208 struct tm thyme;
209 int dx;
210 char *xx, *yy;
187 211
188 xx=strptime(x,"%b",&thyme); 212 xx = strptime(x, "%b", &thyme);
189 dx=thyme.tm_mon; 213 dx = thyme.tm_mon;
190 yy=strptime(y,"%b",&thyme); 214 yy = strptime(y, "%b", &thyme);
191 if(!xx) retval=(!yy ? 0 : -1); 215 if (!xx)
192 else if(!yy) retval=1; 216 retval = (!yy) ? 0 : -1;
193 else retval=(dx==thyme.tm_mon ? 0 : dx-thyme.tm_mon); 217 else if (!yy)
194 break; 218 retval = 1;
195 } 219 else
196 /* Full floating point version of -n */ 220 retval = (dx==thyme.tm_mon) ? 0 : dx-thyme.tm_mon;
197 case FLAG_n: 221 break;
198 { 222 }
199 double dx=atof(x),dy=atof(y); 223 /* Full floating point version of -n */
200 retval=dx>dy ? 1 : (dx<dy ? -1 : 0); 224 case FLAG_n: {
201 break; 225 double dx = atof(x), dy =atof(y);
202 } 226 retval = (dx > dy) ? 1 : ((dx < dy) ? -1 : 0);
227 break;
228 }
203 } 229 }
204 /* Free key copies. */ 230 /* Free key copies. */
205 if(x!=*(char **)xarg) free(x); 231 if (x != *(char **)xarg) free(x);
206 if(y!=*(char **)yarg) free(y); 232 if (y != *(char **)yarg) free(y);
207 if(retval) break; 233 if (retval) break;
208#else 234#else
209 /* Integer version of -n for tiny systems */ 235 /* Integer version of -n for tiny systems */
210 case FLAG_n: 236 case FLAG_n:
211 retval=atoi(x)-atoi(y); 237 retval = atoi(x) - atoi(y);
212 break; 238 break;
213 } 239 }
214#endif 240#endif
215 } 241 }
216 /* Perform fallback sort if necessary */ 242 /* Perform fallback sort if necessary */
217 if(!retval && !(global_flags&FLAG_s)) 243 if (!retval && !(global_flags & FLAG_s))
218 retval=strcmp(*(char **)xarg, *(char **)yarg); 244 retval = strcmp(*(char **)xarg, *(char **)yarg);
219 return ((flags&FLAG_r)?-1:1)*retval; 245
246 if (flags & FLAG_r) return -retval;
247 return retval;
220} 248}
221 249
222int sort_main(int argc, char **argv) 250int sort_main(int argc, char **argv)
223{ 251{
224 FILE *fp,*outfile=NULL; 252 FILE *fp, *outfile = NULL;
225 int linecount=0,i,flag; 253 int linecount = 0, i, flag;
226 char *line,**lines=NULL,*optlist="ngMucszbrdfimS:T:o:k:t:"; 254 char *line, **lines = NULL, *optlist = "ngMucszbrdfimS:T:o:k:t:";
227 int c; 255 int c;
228 256
229 xfunc_error_retval = 2; 257 xfunc_error_retval = 2;
230 /* Parse command line options */ 258 /* Parse command line options */
231 while((c=getopt(argc,argv,optlist))>0) { 259 while ((c = getopt(argc, argv, optlist)) > 0) {
232 line=strchr(optlist,c); 260 line = strchr(optlist, c);
233 if(!line) bb_show_usage(); 261 if (!line) bb_show_usage();
234 switch(*line) { 262 switch (*line) {
235#ifdef CONFIG_FEATURE_SORT_BIG 263#if ENABLE_FEATURE_SORT_BIG
236 case 'o': 264 case 'o':
237 if(outfile) bb_error_msg_and_die("too many -o"); 265 if (outfile) bb_error_msg_and_die("too many -o");
238 outfile=xfopen(optarg,"w"); 266 outfile = xfopen(optarg, "w");
239 break; 267 break;
240 case 't': 268 case 't':
241 if(key_separator || optarg[1]) 269 if (key_separator || optarg[1])
242 bb_error_msg_and_die("too many -t"); 270 bb_error_msg_and_die("too many -t");
243 key_separator=*optarg; 271 key_separator = *optarg;
244 break; 272 break;
245 /* parse sort key */ 273 /* parse sort key */
246 case 'k': 274 case 'k': {
247 { 275 struct sort_key *key = add_key();
248 struct sort_key *key=add_key(); 276 char *temp, *temp2;
249 char *temp, *temp2;
250 277
251 temp=optarg; 278 temp = optarg;
252 for(i=0;*temp;) { 279 for (i = 0; *temp;) {
253 /* Start of range */ 280 /* Start of range */
254 key->range[2*i]=(unsigned short)strtol(temp,&temp,10); 281 key->range[2*i] = (unsigned short)strtol(temp, &temp, 10);
255 if(*temp=='.') 282 if (*temp == '.')
256 key->range[(2*i)+1]=(unsigned short)strtol(temp+1,&temp,10); 283 key->range[(2*i)+1] = (unsigned short)strtol(temp+1, &temp, 10);
257 for(;*temp;temp++) { 284 for (; *temp; temp++) {
258 if(*temp==',' && !i++) { 285 if (*temp == ',' && !i++) {
259 temp++; 286 temp++;
260 break; 287 break;
261 } /* no else needed: fall through to syntax error 288 } /* no else needed: fall through to syntax error
262 because comma isn't in optlist */ 289 because comma isn't in optlist */
263 temp2=strchr(optlist,*temp); 290 temp2 = strchr(optlist, *temp);
264 flag=(1<<(temp2-optlist)); 291 flag = (1 << (temp2 - optlist));
265 if(!temp2 || (flag>FLAG_M && flag<FLAG_b)) 292 if (!temp2 || (flag > FLAG_M && flag < FLAG_b))
266 bb_error_msg_and_die("unknown key option"); 293 bb_error_msg_and_die("unknown key option");
267 /* b after , means strip _trailing_ space */ 294 /* b after ',' means strip _trailing_ space */
268 if(i && flag==FLAG_b) flag=FLAG_bb; 295 if (i && flag == FLAG_b) flag = FLAG_bb;
269 key->flags|=flag; 296 key->flags |= flag;
270 }
271 } 297 }
272 break;
273 } 298 }
299 break;
300 }
274#endif 301#endif
275 default: 302 default:
276 global_flags|=(1<<(line-optlist)); 303 global_flags |= (1 << (line - optlist));
277 /* global b strips leading and trailing spaces */ 304 /* global b strips leading and trailing spaces */
278 if(global_flags&FLAG_b) global_flags|=FLAG_bb; 305 if (global_flags & FLAG_b) global_flags |= FLAG_bb;
279 break; 306 break;
280 } 307 }
281 } 308 }
282 /* Open input files and read data */ 309 /* Open input files and read data */
283 for(i=argv[optind] ? optind : optind-1;argv[i];i++) { 310 for (i = argv[optind] ? optind : optind-1; argv[i]; i++) {
284 if(i<optind || (*argv[i]=='-' && !argv[i][1])) fp=stdin; 311 fp = stdin;
285 else fp=xfopen(argv[i],"r"); 312 if (i >= optind && (argv[i][0] != '-' || argv[i][1]))
286 for(;;) { 313 fp = xfopen(argv[i], "r");
287 line=GET_LINE(fp); 314 for (;;) {
288 if(!line) break; 315 line = GET_LINE(fp);
289 if(!(linecount&63)) 316 if (!line) break;
290 lines=xrealloc(lines, sizeof(char *)*(linecount+64)); 317 if (!(linecount & 63))
291 lines[linecount++]=line; 318 lines = xrealloc(lines, sizeof(char *) * (linecount + 64));
319 lines[linecount++] = line;
292 } 320 }
293 fclose(fp); 321 fclose(fp);
294 } 322 }
295#ifdef CONFIG_FEATURE_SORT_BIG 323#if ENABLE_FEATURE_SORT_BIG
296 /* if no key, perform alphabetic sort */ 324 /* if no key, perform alphabetic sort */
297 if(!key_list) add_key()->range[0]=1; 325 if (!key_list)
326 add_key()->range[0] = 1;
298 /* handle -c */ 327 /* handle -c */
299 if(global_flags&FLAG_c) { 328 if (global_flags & FLAG_c) {
300 int j=(global_flags&FLAG_u) ? -1 : 0; 329 int j = (global_flags & FLAG_u) ? -1 : 0;
301 for(i=1;i<linecount;i++) 330 for (i = 1; i < linecount; i++)
302 if(compare_keys(&lines[i-1],&lines[i])>j) { 331 if (compare_keys(&lines[i-1], &lines[i]) > j) {
303 fprintf(stderr,"Check line %d\n",i); 332 fprintf(stderr, "Check line %d\n", i);
304 return 1; 333 return 1;
305 } 334 }
306 return 0; 335 return 0;
307 } 336 }
308#endif 337#endif
309 /* Perform the actual sort */ 338 /* Perform the actual sort */
310 qsort(lines,linecount,sizeof(char *),compare_keys); 339 qsort(lines, linecount, sizeof(char *), compare_keys);
311 /* handle -u */ 340 /* handle -u */
312 if(global_flags&FLAG_u) { 341 if (global_flags & FLAG_u) {
313 for(flag=0,i=1;i<linecount;i++) { 342 for (flag = 0, i = 1; i < linecount; i++) {
314 if(!compare_keys(&lines[flag],&lines[i])) free(lines[i]); 343 if (!compare_keys(&lines[flag], &lines[i]))
315 else lines[++flag]=lines[i]; 344 free(lines[i]);
345 else
346 lines[++flag] = lines[i];
316 } 347 }
317 if(linecount) linecount=flag+1; 348 if (linecount) linecount = flag+1;
318 } 349 }
319 /* Print it */ 350 /* Print it */
320 if(!outfile) outfile=stdout; 351 if (!outfile) outfile = stdout;
321 for(i=0;i<linecount;i++) fprintf(outfile,"%s\n",lines[i]); 352 for (i = 0; i < linecount; i++)
353 fprintf(outfile, "%s\n", lines[i]);
354
322 fflush_stdout_and_exit(EXIT_SUCCESS); 355 fflush_stdout_and_exit(EXIT_SUCCESS);
323} 356}