Fri Jul 30 22:19:51 2021 UTC ()
make: merge duplicate code for sorting strings and numbers

No functional change.


(rillig)
diff -r1.940 -r1.941 src/usr.bin/make/var.c

cvs diff -r1.940 -r1.941 src/usr.bin/make/var.c (expand / switch to unified diff)

--- src/usr.bin/make/var.c 2021/07/30 22:16:09 1.940
+++ src/usr.bin/make/var.c 2021/07/30 22:19:51 1.941
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: var.c,v 1.940 2021/07/30 22:16:09 rillig Exp $ */ 1/* $NetBSD: var.c,v 1.941 2021/07/30 22:19:51 rillig Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1988, 1989, 1990, 1993 4 * Copyright (c) 1988, 1989, 1990, 1993
5 * The Regents of the University of California. All rights reserved. 5 * The Regents of the University of California. All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to Berkeley by 7 * This code is derived from software contributed to Berkeley by
8 * Adam de Boor. 8 * Adam de Boor.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
@@ -130,27 +130,27 @@ @@ -130,27 +130,27 @@
130#include <regex.h> 130#include <regex.h>
131#endif 131#endif
132#include <errno.h> 132#include <errno.h>
133#include <inttypes.h> 133#include <inttypes.h>
134#include <limits.h> 134#include <limits.h>
135#include <time.h> 135#include <time.h>
136 136
137#include "make.h" 137#include "make.h"
138#include "dir.h" 138#include "dir.h"
139#include "job.h" 139#include "job.h"
140#include "metachar.h" 140#include "metachar.h"
141 141
142/* "@(#)var.c 8.3 (Berkeley) 3/19/94" */ 142/* "@(#)var.c 8.3 (Berkeley) 3/19/94" */
143MAKE_RCSID("$NetBSD: var.c,v 1.940 2021/07/30 22:16:09 rillig Exp $"); 143MAKE_RCSID("$NetBSD: var.c,v 1.941 2021/07/30 22:19:51 rillig Exp $");
144 144
145/* 145/*
146 * Variables are defined using one of the VAR=value assignments. Their 146 * Variables are defined using one of the VAR=value assignments. Their
147 * value can be queried by expressions such as $V, ${VAR}, or with modifiers 147 * value can be queried by expressions such as $V, ${VAR}, or with modifiers
148 * such as ${VAR:S,from,to,g:Q}. 148 * such as ${VAR:S,from,to,g:Q}.
149 * 149 *
150 * There are 3 kinds of variables: scope variables, environment variables, 150 * There are 3 kinds of variables: scope variables, environment variables,
151 * undefined variables. 151 * undefined variables.
152 * 152 *
153 * Scope variables are stored in a GNode.scope. The only way to undefine 153 * Scope variables are stored in a GNode.scope. The only way to undefine
154 * a scope variable is using the .undef directive. In particular, it must 154 * a scope variable is using the .undef directive. In particular, it must
155 * not be possible to undefine a variable during the evaluation of an 155 * not be possible to undefine a variable during the evaluation of an
156 * expression, or Var.name might point nowhere. 156 * expression, or Var.name might point nowhere.
@@ -3305,43 +3305,39 @@ num_val(const char *s) @@ -3305,43 +3305,39 @@ num_val(const char *s)
3305static int 3305static int
3306num_cmp_asc(const void *sa, const void *sb) 3306num_cmp_asc(const void *sa, const void *sb)
3307{ 3307{
3308 NUM_TYPE a, b; 3308 NUM_TYPE a, b;
3309 3309
3310 a = num_val(*(const char *const *)sa); 3310 a = num_val(*(const char *const *)sa);
3311 b = num_val(*(const char *const *)sb); 3311 b = num_val(*(const char *const *)sb);
3312 return (a > b) ? 1 : (b > a) ? -1 : 0; 3312 return (a > b) ? 1 : (b > a) ? -1 : 0;
3313} 3313}
3314 3314
3315static int 3315static int
3316num_cmp_desc(const void *sa, const void *sb) 3316num_cmp_desc(const void *sa, const void *sb)
3317{ 3317{
3318 NUM_TYPE a, b; 3318 return num_cmp_asc(sb, sa);
3319 
3320 a = num_val(*(const char *const *)sa); 
3321 b = num_val(*(const char *const *)sb); 
3322 return (a > b) ? -1 : (b > a) ? 1 : 0; 
3323} 3319}
3324 3320
3325static int 3321static int
3326str_cmp_asc(const void *a, const void *b) 3322str_cmp_asc(const void *a, const void *b)
3327{ 3323{
3328 return strcmp(*(const char *const *)a, *(const char *const *)b); 3324 return strcmp(*(const char *const *)a, *(const char *const *)b);
3329} 3325}
3330 3326
3331static int 3327static int
3332str_cmp_desc(const void *a, const void *b) 3328str_cmp_desc(const void *a, const void *b)
3333{ 3329{
3334 return strcmp(*(const char *const *)b, *(const char *const *)a); 3330 return str_cmp_asc(b, a);
3335} 3331}
3336 3332
3337static void 3333static void
3338ShuffleStrings(char **strs, size_t n) 3334ShuffleStrings(char **strs, size_t n)
3339{ 3335{
3340 size_t i; 3336 size_t i;
3341 3337
3342 for (i = n - 1; i > 0; i--) { 3338 for (i = n - 1; i > 0; i--) {
3343 size_t rndidx = (size_t)random() % (i + 1); 3339 size_t rndidx = (size_t)random() % (i + 1);
3344 char *t = strs[i]; 3340 char *t = strs[i];
3345 strs[i] = strs[rndidx]; 3341 strs[i] = strs[rndidx];
3346 strs[rndidx] = t; 3342 strs[rndidx] = t;
3347 } 3343 }