nrelease: Clean up a bit the 'clean' target
[dragonfly.git] / usr.bin / calendar / utils.c
blob9f308264993cae2a68f564892ed41c121ca65370
1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2019-2020 The DragonFly Project. All rights reserved.
6 * This code is derived from software contributed to The DragonFly Project
7 * by Aaron LI <aly@aaronly.me>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 * 3. Neither the name of The DragonFly Project nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific, prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
36 * Reference:
37 * Calendrical Calculations, The Ultimate Edition (4th Edition)
38 * by Edward M. Reingold and Nachum Dershowitz
39 * 2018, Cambridge University Press
42 #include <err.h>
43 #include <math.h>
44 #include <stdbool.h>
45 #include <stddef.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
50 #include "utils.h"
54 * Calculate the polynomial: c[0] + c[1] * x + ... + c[n-1] * x^(n-1)
56 double
57 poly(double x, const double *coefs, size_t n)
59 double p = 0.0;
60 double t = 1.0;
61 for (size_t i = 0; i < n; i++) {
62 p += t * coefs[i];
63 t *= x;
65 return p;
69 * Use bisection search to find the inverse of the given angular function
70 * $f(x) at value $y (degrees) within time interval [$a, $b].
71 * Ref: Sec.(1.8), Eq.(1.36)
73 double
74 invert_angular(double (*f)(double), double y, double a, double b)
76 static const double eps = 1e-6;
77 double x;
79 do {
80 x = (a + b) / 2.0;
81 if (mod_f(f(x) - y, 360) < 180.0)
82 b = x;
83 else
84 a = x;
85 } while (fabs(a-b) >= eps);
87 return x;
92 * Like malloc(3) but exit if allocation fails.
94 void *
95 xmalloc(size_t size)
97 void *ptr = malloc(size);
98 if (ptr == NULL)
99 errx(1, "mcalloc(%zu): out of memory", size);
100 return ptr;
104 * Like calloc(3) but exit if allocation fails.
106 void *
107 xcalloc(size_t number, size_t size)
109 void *ptr = calloc(number, size);
110 if (ptr == NULL)
111 errx(1, "xcalloc(%zu, %zu): out of memory", number, size);
112 return ptr;
116 * Like realloc(3) but exit if allocation fails.
118 void *
119 xrealloc(void *ptr, size_t size)
121 ptr = realloc(ptr, size);
122 if (ptr == NULL)
123 errx(1, "xrealloc: out of memory (size: %zu)", size);
124 return ptr;
128 * Like strdup(3) but exit if fail.
130 char *
131 xstrdup(const char *str)
133 char *p = strdup(str);
134 if (p == NULL)
135 errx(1, "xstrdup: out of memory (length: %zu)", strlen(str));
136 return p;
141 * Linked list implementation
144 struct node {
145 char *name;
146 void *data;
147 struct node *next;
151 * Create a new list node with the given $name and $data.
153 struct node *
154 list_newnode(char *name, void *data)
156 struct node *newp;
158 newp = xcalloc(1, sizeof(*newp));
159 newp->name = name;
160 newp->data = data;
162 return newp;
166 * Add $newp to the front of list $listp.
168 struct node *
169 list_addfront(struct node *listp, struct node *newp)
171 newp->next = listp;
172 return newp;
176 * Lookup the given $name in the list $listp.
177 * The $cmp function compares two names and return 0 if they equal.
178 * Return the associated data with the found node, otherwise NULL.
180 bool
181 list_lookup(struct node *listp, const char *name,
182 int (*cmp)(const char *, const char *), void **data_out)
184 for ( ; listp; listp = listp->next) {
185 if ((*cmp)(name, listp->name) == 0) {
186 if (data_out)
187 *data_out = listp->data;
188 return true;
192 return false;
196 * Free all nodes of list $listp.
198 void
199 list_freeall(struct node *listp,
200 void (*free_name)(void *),
201 void (*free_data)(void *))
203 struct node *cur;
205 while (listp) {
206 cur = listp;
207 listp = listp->next;
208 if (free_name)
209 (*free_name)(cur->name);
210 if (free_data)
211 (*free_data)(cur->data);
212 free(cur);