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 |
This commit is contained in:
parent
cf749bc10c
commit
0d42ddff70
135
coreutils/sort.c
135
coreutils/sort.c
@ -39,11 +39,10 @@ static int global_flags;
|
|||||||
#define FLAG_bb 32768 /* Ignore trailing blanks */
|
#define FLAG_bb 32768 /* Ignore trailing blanks */
|
||||||
|
|
||||||
|
|
||||||
#ifdef CONFIG_FEATURE_SORT_BIG
|
#if ENABLE_FEATURE_SORT_BIG
|
||||||
static char key_separator;
|
static char key_separator;
|
||||||
|
|
||||||
static struct sort_key
|
static struct sort_key {
|
||||||
{
|
|
||||||
struct sort_key *next_key; /* linked list */
|
struct sort_key *next_key; /* linked list */
|
||||||
unsigned short range[4]; /* start word, start char, end word, end char */
|
unsigned short range[4]; /* start word, start char, end word, end char */
|
||||||
int flags;
|
int flags;
|
||||||
@ -55,12 +54,16 @@ static char *get_key(char *str, struct sort_key *key, int flags)
|
|||||||
|
|
||||||
/* Special case whole string, so we don't have to make a copy */
|
/* Special case whole string, so we don't have to make a copy */
|
||||||
if (key->range[0] == 1 && !key->range[1] && !key->range[2] && !key->range[3]
|
if (key->range[0] == 1 && !key->range[1] && !key->range[2] && !key->range[3]
|
||||||
&& !(flags&(FLAG_b&FLAG_d&FLAG_f&FLAG_i&FLAG_bb))) return str;
|
&& !(flags & (FLAG_b | FLAG_d | FLAG_f | FLAG_i | FLAG_bb))
|
||||||
|
) {
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
/* Find start of key on first pass, end on second pass*/
|
/* Find start of key on first pass, end on second pass*/
|
||||||
len = strlen(str);
|
len = strlen(str);
|
||||||
|
|
||||||
for (j = 0; j < 2; j++) {
|
for (j = 0; j < 2; j++) {
|
||||||
if(!key->range[2*j]) end=len;
|
if (!key->range[2*j])
|
||||||
|
end = len;
|
||||||
/* Loop through fields */
|
/* Loop through fields */
|
||||||
else {
|
else {
|
||||||
end = 0;
|
end = 0;
|
||||||
@ -68,25 +71,33 @@ static char *get_key(char *str, struct sort_key *key, int flags)
|
|||||||
/* Skip leading blanks or first separator */
|
/* Skip leading blanks or first separator */
|
||||||
if (str[end]) {
|
if (str[end]) {
|
||||||
if (!key_separator && isspace(str[end]))
|
if (!key_separator && isspace(str[end]))
|
||||||
|
/* TODO: remove "&& isspace(str[end])" */
|
||||||
while (isspace(str[end])) end++;
|
while (isspace(str[end])) end++;
|
||||||
}
|
}
|
||||||
/* Skip body of key */
|
/* Skip body of key */
|
||||||
for (; str[end]; end++) {
|
for (; str[end]; end++) {
|
||||||
if (key_separator) {
|
if (key_separator) {
|
||||||
if(str[end]==key_separator) break;
|
if (str[end] == key_separator)
|
||||||
} else if(isspace(str[end])) break;
|
break;
|
||||||
|
} else {
|
||||||
|
if (isspace(str[end]))
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!j) start = end;
|
if (!j) start = end;
|
||||||
}
|
}
|
||||||
/* Key with explicit separator starts after separator */
|
/* Key with explicit separator starts after separator */
|
||||||
if(key_separator && str[start]==key_separator) start++;
|
if (key_separator && str[start] == key_separator)
|
||||||
|
start++;
|
||||||
/* Strip leading whitespace if necessary */
|
/* Strip leading whitespace if necessary */
|
||||||
//XXX: skip_whitespace()
|
//XXX: skip_whitespace()
|
||||||
if(flags&FLAG_b) while(isspace(str[start])) start++;
|
if (flags & FLAG_b)
|
||||||
|
while (isspace(str[start])) start++;
|
||||||
/* Strip trailing whitespace if necessary */
|
/* Strip trailing whitespace if necessary */
|
||||||
if(flags&FLAG_bb) while(end>start && isspace(str[end-1])) end--;
|
if (flags & FLAG_bb)
|
||||||
|
while (end > start && isspace(str[end-1])) end--;
|
||||||
/* Handle offsets on start and end */
|
/* Handle offsets on start and end */
|
||||||
if (key->range[3]) {
|
if (key->range[3]) {
|
||||||
end += key->range[3] - 1;
|
end += key->range[3] - 1;
|
||||||
@ -102,17 +113,21 @@ static char *get_key(char *str, struct sort_key *key, int flags)
|
|||||||
/* Handle -d */
|
/* Handle -d */
|
||||||
if (flags & FLAG_d) {
|
if (flags & FLAG_d) {
|
||||||
for (start = end = 0; str[end]; end++)
|
for (start = end = 0; str[end]; end++)
|
||||||
if(isspace(str[end]) || isalnum(str[end])) str[start++]=str[end];
|
if (isspace(str[end]) || isalnum(str[end]))
|
||||||
str[start]=0;
|
str[start++] = str[end];
|
||||||
|
str[start] = '\0';
|
||||||
}
|
}
|
||||||
/* Handle -i */
|
/* Handle -i */
|
||||||
if (flags & FLAG_i) {
|
if (flags & FLAG_i) {
|
||||||
for (start = end = 0; str[end]; end++)
|
for (start = end = 0; str[end]; end++)
|
||||||
if(isprint(str[end])) str[start++]=str[end];
|
if (isprint(str[end]))
|
||||||
str[start]=0;
|
str[start++] = str[end];
|
||||||
|
str[start] = '\0';
|
||||||
}
|
}
|
||||||
/* Handle -f */
|
/* Handle -f */
|
||||||
if(flags*FLAG_f) for(i=0;str[i];i++) str[i]=toupper(str[i]);
|
if (flags & FLAG_f)
|
||||||
|
for (i = 0; str[i]; i++)
|
||||||
|
str[i] = toupper(str[i]);
|
||||||
|
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
@ -120,12 +135,15 @@ static char *get_key(char *str, struct sort_key *key, int flags)
|
|||||||
static struct sort_key *add_key(void)
|
static struct sort_key *add_key(void)
|
||||||
{
|
{
|
||||||
struct sort_key **pkey = &key_list;
|
struct sort_key **pkey = &key_list;
|
||||||
while(*pkey) pkey=&((*pkey)->next_key);
|
while (*pkey)
|
||||||
|
pkey = &((*pkey)->next_key);
|
||||||
return *pkey = xzalloc(sizeof(struct sort_key));
|
return *pkey = xzalloc(sizeof(struct sort_key));
|
||||||
}
|
}
|
||||||
|
|
||||||
#define GET_LINE(fp) (global_flags&FLAG_z) ? bb_get_chunk_from_file(fp,NULL) \
|
#define GET_LINE(fp) \
|
||||||
: xmalloc_getline(fp)
|
((global_flags & FLAG_z) \
|
||||||
|
? bb_get_chunk_from_file(fp, NULL) \
|
||||||
|
: xmalloc_getline(fp))
|
||||||
#else
|
#else
|
||||||
#define GET_LINE(fp) xmalloc_getline(fp)
|
#define GET_LINE(fp) xmalloc_getline(fp)
|
||||||
#endif
|
#endif
|
||||||
@ -136,7 +154,7 @@ static int compare_keys(const void *xarg, const void *yarg)
|
|||||||
int flags = global_flags, retval = 0;
|
int flags = global_flags, retval = 0;
|
||||||
char *x, *y;
|
char *x, *y;
|
||||||
|
|
||||||
#ifdef CONFIG_FEATURE_SORT_BIG
|
#if ENABLE_FEATURE_SORT_BIG
|
||||||
struct sort_key *key;
|
struct sort_key *key;
|
||||||
|
|
||||||
for (key = key_list; !retval && key; key = key->next_key) {
|
for (key = key_list; !retval && key; key = key->next_key) {
|
||||||
@ -160,27 +178,33 @@ static int compare_keys(const void *xarg, const void *yarg)
|
|||||||
case 0:
|
case 0:
|
||||||
retval = strcmp(x, y);
|
retval = strcmp(x, y);
|
||||||
break;
|
break;
|
||||||
#ifdef CONFIG_FEATURE_SORT_BIG
|
#if ENABLE_FEATURE_SORT_BIG
|
||||||
case FLAG_g:
|
case FLAG_g: {
|
||||||
{
|
|
||||||
char *xx, *yy;
|
char *xx, *yy;
|
||||||
double dx = strtod(x, &xx), dy = strtod(y, &yy);
|
double dx = strtod(x, &xx), dy = strtod(y, &yy);
|
||||||
/* not numbers < NaN < -infinity < numbers < +infinity) */
|
/* not numbers < NaN < -infinity < numbers < +infinity) */
|
||||||
if(x==xx) retval=(y==yy ? 0 : -1);
|
if (x == xx)
|
||||||
else if(y==yy) retval=1;
|
retval = (y == yy ? 0 : -1);
|
||||||
|
else if (y == yy)
|
||||||
|
retval = 1;
|
||||||
/* Check for isnan */
|
/* Check for isnan */
|
||||||
else if(dx != dx) retval = (dy != dy) ? 0 : -1;
|
else if (dx != dx)
|
||||||
else if(dy != dy) retval = 1;
|
retval = (dy != dy) ? 0 : -1;
|
||||||
|
else if (dy != dy)
|
||||||
|
retval = 1;
|
||||||
/* Check for infinity. Could underflow, but it avoids libm. */
|
/* Check for infinity. Could underflow, but it avoids libm. */
|
||||||
else if (1.0 / dx == 0.0) {
|
else if (1.0 / dx == 0.0) {
|
||||||
if(dx<0) retval=((1.0/dy == 0.0 && dy<0) ? 0 : -1);
|
if (dx < 0)
|
||||||
else retval=((1.0/dy == 0.0 && dy>0) ? 0 : 1);
|
retval = (1.0 / dy == 0.0 && dy < 0) ? 0 : -1;
|
||||||
} else if(1.0/dy == 0.0) retval=dy<0 ? 1 : -1;
|
else
|
||||||
else retval=dx>dy ? 1 : (dx<dy ? -1 : 0);
|
retval = (1.0 / dy == 0.0 && dy > 0) ? 0 : 1;
|
||||||
|
} else if (1.0 / dy == 0.0)
|
||||||
|
retval = (dy < 0) ? 1 : -1;
|
||||||
|
else
|
||||||
|
retval = (dx > dy) ? 1 : ((dx < dy) ? -1 : 0);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case FLAG_M:
|
case FLAG_M: {
|
||||||
{
|
|
||||||
struct tm thyme;
|
struct tm thyme;
|
||||||
int dx;
|
int dx;
|
||||||
char *xx, *yy;
|
char *xx, *yy;
|
||||||
@ -188,16 +212,18 @@ static int compare_keys(const void *xarg, const void *yarg)
|
|||||||
xx = strptime(x, "%b", &thyme);
|
xx = strptime(x, "%b", &thyme);
|
||||||
dx = thyme.tm_mon;
|
dx = thyme.tm_mon;
|
||||||
yy = strptime(y, "%b", &thyme);
|
yy = strptime(y, "%b", &thyme);
|
||||||
if(!xx) retval=(!yy ? 0 : -1);
|
if (!xx)
|
||||||
else if(!yy) retval=1;
|
retval = (!yy) ? 0 : -1;
|
||||||
else retval=(dx==thyme.tm_mon ? 0 : dx-thyme.tm_mon);
|
else if (!yy)
|
||||||
|
retval = 1;
|
||||||
|
else
|
||||||
|
retval = (dx==thyme.tm_mon) ? 0 : dx-thyme.tm_mon;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
/* Full floating point version of -n */
|
/* Full floating point version of -n */
|
||||||
case FLAG_n:
|
case FLAG_n: {
|
||||||
{
|
|
||||||
double dx = atof(x), dy =atof(y);
|
double dx = atof(x), dy =atof(y);
|
||||||
retval=dx>dy ? 1 : (dx<dy ? -1 : 0);
|
retval = (dx > dy) ? 1 : ((dx < dy) ? -1 : 0);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -216,7 +242,9 @@ static int compare_keys(const void *xarg, const void *yarg)
|
|||||||
/* Perform fallback sort if necessary */
|
/* Perform fallback sort if necessary */
|
||||||
if (!retval && !(global_flags & FLAG_s))
|
if (!retval && !(global_flags & FLAG_s))
|
||||||
retval = strcmp(*(char **)xarg, *(char **)yarg);
|
retval = strcmp(*(char **)xarg, *(char **)yarg);
|
||||||
return ((flags&FLAG_r)?-1:1)*retval;
|
|
||||||
|
if (flags & FLAG_r) return -retval;
|
||||||
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
int sort_main(int argc, char **argv)
|
int sort_main(int argc, char **argv)
|
||||||
@ -232,7 +260,7 @@ int sort_main(int argc, char **argv)
|
|||||||
line = strchr(optlist, c);
|
line = strchr(optlist, c);
|
||||||
if (!line) bb_show_usage();
|
if (!line) bb_show_usage();
|
||||||
switch (*line) {
|
switch (*line) {
|
||||||
#ifdef CONFIG_FEATURE_SORT_BIG
|
#if ENABLE_FEATURE_SORT_BIG
|
||||||
case 'o':
|
case 'o':
|
||||||
if (outfile) bb_error_msg_and_die("too many -o");
|
if (outfile) bb_error_msg_and_die("too many -o");
|
||||||
outfile = xfopen(optarg, "w");
|
outfile = xfopen(optarg, "w");
|
||||||
@ -243,8 +271,7 @@ int sort_main(int argc, char **argv)
|
|||||||
key_separator = *optarg;
|
key_separator = *optarg;
|
||||||
break;
|
break;
|
||||||
/* parse sort key */
|
/* parse sort key */
|
||||||
case 'k':
|
case 'k': {
|
||||||
{
|
|
||||||
struct sort_key *key = add_key();
|
struct sort_key *key = add_key();
|
||||||
char *temp, *temp2;
|
char *temp, *temp2;
|
||||||
|
|
||||||
@ -264,7 +291,7 @@ int sort_main(int argc, char **argv)
|
|||||||
flag = (1 << (temp2 - optlist));
|
flag = (1 << (temp2 - optlist));
|
||||||
if (!temp2 || (flag > FLAG_M && flag < FLAG_b))
|
if (!temp2 || (flag > FLAG_M && flag < FLAG_b))
|
||||||
bb_error_msg_and_die("unknown key option");
|
bb_error_msg_and_die("unknown key option");
|
||||||
/* b after , means strip _trailing_ space */
|
/* b after ',' means strip _trailing_ space */
|
||||||
if (i && flag == FLAG_b) flag = FLAG_bb;
|
if (i && flag == FLAG_b) flag = FLAG_bb;
|
||||||
key->flags |= flag;
|
key->flags |= flag;
|
||||||
}
|
}
|
||||||
@ -281,8 +308,9 @@ int sort_main(int argc, char **argv)
|
|||||||
}
|
}
|
||||||
/* Open input files and read data */
|
/* Open input files and read data */
|
||||||
for (i = argv[optind] ? optind : optind-1; argv[i]; i++) {
|
for (i = argv[optind] ? optind : optind-1; argv[i]; i++) {
|
||||||
if(i<optind || (*argv[i]=='-' && !argv[i][1])) fp=stdin;
|
fp = stdin;
|
||||||
else fp=xfopen(argv[i],"r");
|
if (i >= optind && (argv[i][0] != '-' || argv[i][1]))
|
||||||
|
fp = xfopen(argv[i], "r");
|
||||||
for (;;) {
|
for (;;) {
|
||||||
line = GET_LINE(fp);
|
line = GET_LINE(fp);
|
||||||
if (!line) break;
|
if (!line) break;
|
||||||
@ -292,9 +320,10 @@ int sort_main(int argc, char **argv)
|
|||||||
}
|
}
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
}
|
}
|
||||||
#ifdef CONFIG_FEATURE_SORT_BIG
|
#if ENABLE_FEATURE_SORT_BIG
|
||||||
/* if no key, perform alphabetic sort */
|
/* if no key, perform alphabetic sort */
|
||||||
if(!key_list) add_key()->range[0]=1;
|
if (!key_list)
|
||||||
|
add_key()->range[0] = 1;
|
||||||
/* handle -c */
|
/* handle -c */
|
||||||
if (global_flags & FLAG_c) {
|
if (global_flags & FLAG_c) {
|
||||||
int j = (global_flags & FLAG_u) ? -1 : 0;
|
int j = (global_flags & FLAG_u) ? -1 : 0;
|
||||||
@ -311,13 +340,17 @@ int sort_main(int argc, char **argv)
|
|||||||
/* handle -u */
|
/* handle -u */
|
||||||
if (global_flags & FLAG_u) {
|
if (global_flags & FLAG_u) {
|
||||||
for (flag = 0, i = 1; i < linecount; i++) {
|
for (flag = 0, i = 1; i < linecount; i++) {
|
||||||
if(!compare_keys(&lines[flag],&lines[i])) free(lines[i]);
|
if (!compare_keys(&lines[flag], &lines[i]))
|
||||||
else lines[++flag]=lines[i];
|
free(lines[i]);
|
||||||
|
else
|
||||||
|
lines[++flag] = lines[i];
|
||||||
}
|
}
|
||||||
if (linecount) linecount = flag+1;
|
if (linecount) linecount = flag+1;
|
||||||
}
|
}
|
||||||
/* Print it */
|
/* Print it */
|
||||||
if (!outfile) outfile = stdout;
|
if (!outfile) outfile = stdout;
|
||||||
for(i=0;i<linecount;i++) fprintf(outfile,"%s\n",lines[i]);
|
for (i = 0; i < linecount; i++)
|
||||||
|
fprintf(outfile, "%s\n", lines[i]);
|
||||||
|
|
||||||
fflush_stdout_and_exit(EXIT_SUCCESS);
|
fflush_stdout_and_exit(EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user