busybox-1.32.0/coreutils/sort.c
static int compare_keys(const void *xarg, const void *yarg)
{
	int flags = option_mask32, retval = 0;
	char *x, *y;

#if ENABLE_FEATURE_SORT_BIG
	struct sort_key *key;

	for (key = key_list; !retval && key; key = key->next_key) {
		flags = key->flags ? key->flags : option_mask32;
		/* Chop out and modify key chunks, handling -dfib */
		x = get_key(*(char **)xarg, key, flags);
		y = get_key(*(char **)yarg, key, flags);
#else
	/* This curly bracket serves no purpose but to match the nesting
	 * level of the for () loop we're not using */
	{
		x = *(char **)xarg;
		y = *(char **)yarg;
#endif
		/* Perform actual comparison */
		switch (flags & (FLAG_n | FLAG_g | FLAG_M | FLAG_V)) {
		default:
			bb_simple_error_msg_and_die("unknown sort type");
			break;
#if defined(HAVE_STRVERSCMP) && HAVE_STRVERSCMP == 1
		case FLAG_V:
			retval = strverscmp(x, y);
			break;
#endif
		/* Ascii sort */
		case 0:
#if ENABLE_LOCALE_SUPPORT
			retval = strcoll(x, y);
#else
			retval = strcmp(x, y);
#endif
			break;
#if ENABLE_FEATURE_SORT_BIG
		case FLAG_g: {
			char *xx, *yy;
			double dx = strtod(x, &xx);
			double dy = strtod(y, &yy);
			/* not numbers < NaN < -infinity < numbers < +infinity) */
			if (x == xx)
				retval = (y == yy ? 0 : -1);
			else if (y == yy)
				retval = 1;
			/* Check for isnan */
			else if (dx != dx)
				retval = (dy != dy) ? 0 : -1;
			else if (dy != dy)
				retval = 1;
			/* Check for infinity.  Could underflow, but it avoids libm. */
			else if (1.0 / dx == 0.0) {
				if (dx < 0)
					retval = (1.0 / dy == 0.0 && dy < 0) ? 0 : -1;
				else
					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;
		}
		case FLAG_M: {
			struct tm thyme;
			int dx;
			char *xx, *yy;

			xx = strptime(x, "%b", &thyme);
			dx = thyme.tm_mon;
			yy = strptime(y, "%b", &thyme);
			if (!xx)
				retval = (!yy) ? 0 : -1;
			else if (!yy)
				retval = 1;
			else
				retval = dx - thyme.tm_mon;
			break;
		}
		/* Full floating point version of -n */
		case FLAG_n: {
			double dx = atof(x);
			double dy = atof(y);
			retval = (dx > dy) ? 1 : ((dx < dy) ? -1 : 0);
			break;
		}
		} /* switch */
		/* Free key copies. */
		if (x != *(char **)xarg) free(x);
		if (y != *(char **)yarg) free(y);
		/* if (retval) break; - done by for () anyway */
#else
		/* Integer version of -n for tiny systems */
		case FLAG_n:
			retval = atoi(x) - atoi(y);
			break;
		}
/* Iterate through keys list and perform comparisons */
Iterate through keys list and perform comparisons
Iterate through keys list and perform comparisons
static int compare_keys(const void *xarg, const void *yarg)
{
	int flags = option_mask32, retval = 0;
	char *x, *y;
#if ENABLE_FEATURE_SORT_BIG
	struct sort_key *key;
	for (key = key_list; !retval && key; key = key->next_key) {
		flags = key->flags ? key->flags : option_mask32;
		x = get_key(*(char **)xarg, key, flags);
		y = get_key(*(char **)yarg, key, flags);
#else
	{
		x = *(char **)xarg;
		y = *(char **)yarg;
#endif
		switch (flags & (FLAG_n | FLAG_g | FLAG_M | FLAG_V)) {
		default:
			bb_simple_error_msg_and_die("unknown sort type");
			break;
#if defined(HAVE_STRVERSCMP) && HAVE_STRVERSCMP == 1
		case FLAG_V:
			retval = strverscmp(x, y);
			break;
#endif
		case 0:
#if ENABLE_LOCALE_SUPPORT
			retval = strcoll(x, y);
#else
			retval = strcmp(x, y);
#endif
			break;
#if ENABLE_FEATURE_SORT_BIG
		case FLAG_g: {
			char *xx, *yy;
			double dx = strtod(x, &xx);
			double dy = strtod(y, &yy);
			if (x == xx)
				retval = (y == yy ? 0 : -1);
			else if (y == yy)
				retval = 1;
			else if (dx != dx)
				retval = (dy != dy) ? 0 : -1;
			else if (dy != dy)
				retval = 1;
			else if (1.0 / dx == 0.0) {
				if (dx < 0)
					retval = (1.0 / dy == 0.0 && dy < 0) ? 0 : -1;
				else
					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;
		}
		case FLAG_M: {
			struct tm thyme;
			int dx;
			char *xx, *yy;
			xx = strptime(x, "%b", &thyme);
			dx = thyme.tm_mon;
			yy = strptime(y, "%b", &thyme);
			if (!xx)
				retval = (!yy) ? 0 : -1;
			else if (!yy)
				retval = 1;
			else
				retval = dx - thyme.tm_mon;
			break;
		}
		case FLAG_n: {
			double dx = atof(x);
			double dy = atof(y);
			retval = (dx > dy) ? 1 : ((dx < dy) ? -1 : 0);
			break;
		}
		} 
		if (x != *(char **)xarg) free(x);
		if (y != *(char **)yarg) free(y);
#else
		case FLAG_n:
			retval = atoi(x) - atoi(y);
			break;
		}
static int compare_keys ( const void * xarg , const void * yarg ) { int flags = option_mask32 , retval = 0 ; char * x , * y ; #if ENABLE_FEATURE_SORT_BIG struct sort_key * key ; for ( key = key_list ; ! retval && key ; key = key -> next_key ) { flags = key -> flags ? key -> flags : option_mask32 ; x = get_key ( * ( char * * ) xarg , key , flags ) ; y = get_key ( * ( char * * ) yarg , key , flags ) ; #else 
	{ x = * ( char * * ) xarg ; y = * ( char * * ) yarg ; #endif 
		switch (flags & (FLAG_n | FLAG_g | FLAG_M | FLAG_V)) { default : bb_simple_error_msg_and_die ( " " ) ; break ; #if defined ( HAVE_STRVERSCMP ) && HAVE_STRVERSCMP == 1 case FLAG_V : retval = strverscmp ( x , y ) ; break ; #endif case 0 : #if ENABLE_LOCALE_SUPPORT retval = strcoll ( x , y ) ; #else retval = strcmp ( x , y ) ; #endif break ; #if ENABLE_FEATURE_SORT_BIG case FLAG_g : { char * xx , * yy ; double dx = strtod ( x , & xx ) ; double dy = strtod ( y , & yy ) ; if ( x == xx ) retval = ( y == yy ? 0 : -1 ) ; else if ( y == yy ) retval = 1 ; else if ( dx != dx ) retval = ( dy != dy ) ? 0 : -1 ; else if ( dy != dy ) retval = 1 ; else if ( 1.0 / dx == 0.0 ) { if ( dx < 0 ) retval = ( 1.0 / dy == 0.0 && dy < 0 ) ? 0 : -1 ; else 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 ; } case FLAG_M : { struct tm thyme ; int dx ; char * xx , * yy ; xx = strptime ( x , " " , & thyme ) ; dx = thyme . tm_mon ; yy = strptime ( y , " " , & thyme ) ; if ( ! xx ) retval = ( ! yy ) ? 0 : -1 ; else if ( ! yy ) retval = 1 ; else retval = dx - thyme . tm_mon ; break ; } case FLAG_n : { double dx = atof ( x ) ; double dy = atof ( y ) ; retval = ( dx > dy ) ? 1 : ( ( dx < dy ) ? -1 : 0 ) ; break ; } } if ( x != * ( char * * ) xarg ) free ( x ) ; if ( y != * ( char * * ) yarg ) free ( y ) ; #else case FLAG_n : retval = atoi ( x ) - atoi ( y ) ; break ; }
Iterate through keys list and perform comparisons
Iterate through keys list and perform comparisons
/* Chop out and modify key chunks, handling -dfib */ 
	/* This curly bracket serves no purpose but to match the nesting
	 * level of the for () loop we're not using */ 
		/* Perform actual comparison */ /* Ascii sort */ /* not numbers < NaN < -infinity < numbers < +infinity) */ /* Check for isnan */ /* Check for infinity.  Could underflow, but it avoids libm. */ /* Full floating point version of -n */ /* switch */ /* Free key copies. */ /* if (retval) break; - done by for () anyway */ /* Integer version of -n for tiny systems */