Esempio n. 1
0
File: eval.c Progetto: gvlx/gawk
int
cmp_nodes(NODE *t1, NODE *t2)
{
    int ret = 0;
    size_t len1, len2;
    int l, ldiff;

    if (t1 == t2)
        return 0;

    if (t1->flags & MAYBE_NUM)
        (void) force_number(t1);
    if (t2->flags & MAYBE_NUM)
        (void) force_number(t2);
    if (t1->flags & INTIND)
        t1 = force_string(t1);
    if (t2->flags & INTIND)
        t2 = force_string(t2);

    if ((t1->flags & NUMBER) && (t2->flags & NUMBER))
        return cmp_numbers(t1, t2);

    (void) force_string(t1);
    (void) force_string(t2);
    len1 = t1->stlen;
    len2 = t2->stlen;
    ldiff = len1 - len2;
    if (len1 == 0 || len2 == 0)
        return ldiff;

    if (do_posix)
        return posix_compare(t1, t2);

    l = (ldiff <= 0 ? len1 : len2);
    if (IGNORECASE) {
        const unsigned char *cp1 = (const unsigned char *) t1->stptr;
        const unsigned char *cp2 = (const unsigned char *) t2->stptr;

#if MBS_SUPPORT
        if (gawk_mb_cur_max > 1) {
            ret = strncasecmpmbs((const unsigned char *) cp1,
                                 (const unsigned char *) cp2, l);
        } else
#endif
            /* Could use tolower() here; see discussion above. */
            for (ret = 0; l-- > 0 && ret == 0; cp1++, cp2++)
                ret = casetable[*cp1] - casetable[*cp2];
    } else
        ret = memcmp(t1->stptr, t2->stptr, l);

    ret = ret == 0 ? ldiff : ret;
    return ret;
}
Esempio n. 2
0
/**
 * fdisk_partition_cmp_start:
 * @a: partition
 * @b: partition
 *
 * Compares partitions according to start offset, See fdisk_table_sort_partitions().
 *
 * Return: 0 if the same, <0 if @b greater, >0 if @a greater.
 */
int fdisk_partition_cmp_start(struct fdisk_partition *a,
			      struct fdisk_partition *b)
{
	int no_a = FDISK_IS_UNDEF(a->start),
	    no_b = FDISK_IS_UNDEF(b->start);

	if (no_a && no_b)
		return 0;
	if (no_a)
		return -1;
	if (no_b)
		return 1;

	return cmp_numbers(a->start, b->start);
}