readhappy.c revision 1.1.20.2 1 1.1.20.2 jdolecek /* $NetBSD: readhappy.c,v 1.1.20.2 2017/12/03 11:38:53 jdolecek Exp $ */
2 1.1.20.2 jdolecek
3 1.1.20.2 jdolecek /*-
4 1.1.20.2 jdolecek * Copyright (c) 2015 The NetBSD Foundation, Inc.
5 1.1.20.2 jdolecek * All rights reserved.
6 1.1.20.2 jdolecek *
7 1.1.20.2 jdolecek * Redistribution and use in source and binary forms, with or without
8 1.1.20.2 jdolecek * modification, are permitted provided that the following conditions
9 1.1.20.2 jdolecek * are met:
10 1.1.20.2 jdolecek * 1. Redistributions of source code must retain the above copyright
11 1.1.20.2 jdolecek * notice, this list of conditions and the following disclaimer.
12 1.1.20.2 jdolecek * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.20.2 jdolecek * notice, this list of conditions and the following disclaimer in the
14 1.1.20.2 jdolecek * documentation and/or other materials provided with the distribution.
15 1.1.20.2 jdolecek *
16 1.1.20.2 jdolecek * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1.20.2 jdolecek * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1.20.2 jdolecek * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1.20.2 jdolecek * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1.20.2 jdolecek * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1.20.2 jdolecek * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1.20.2 jdolecek * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1.20.2 jdolecek * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1.20.2 jdolecek * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1.20.2 jdolecek * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1.20.2 jdolecek * POSSIBILITY OF SUCH DAMAGE.
27 1.1.20.2 jdolecek */
28 1.1.20.2 jdolecek
29 1.1.20.2 jdolecek #include <sys/cdefs.h>
30 1.1.20.2 jdolecek __KERNEL_RCSID(0, "$NetBSD: readhappy.c,v 1.1.20.2 2017/12/03 11:38:53 jdolecek Exp $");
31 1.1.20.2 jdolecek
32 1.1.20.2 jdolecek #include <sys/param.h>
33 1.1.20.2 jdolecek #include <sys/conf.h>
34 1.1.20.2 jdolecek #include <sys/device.h>
35 1.1.20.2 jdolecek #include <sys/kernel.h>
36 1.1.20.2 jdolecek #include <sys/module.h>
37 1.1.20.2 jdolecek
38 1.1.20.2 jdolecek /*
39 1.1.20.2 jdolecek * Create a device /dev/happy from which you can read sequential
40 1.1.20.2 jdolecek * happy numbers.
41 1.1.20.2 jdolecek *
42 1.1.20.2 jdolecek * To use this device you need to do:
43 1.1.20.2 jdolecek * mknod /dev/happy c 210 0
44 1.1.20.2 jdolecek *
45 1.1.20.2 jdolecek * Commentary:
46 1.1.20.2 jdolecek * A happy number is a number defined by the following process: Starting with
47 1.1.20.2 jdolecek * any positive integer, replace the number by the sum of the squares of its
48 1.1.20.2 jdolecek * digits, and repeat the process until the number equals 1 (where it will
49 1.1.20.2 jdolecek * stay), or it loops endlessly in a cycle which does not include 1. Those
50 1.1.20.2 jdolecek * numbers for which this process ends in 1 are happy numbers, while those that
51 1.1.20.2 jdolecek * do not end in 1 are unhappy numbers (or sad numbers).
52 1.1.20.2 jdolecek *
53 1.1.20.2 jdolecek * For more information on happy numbers, and the algorithms, see
54 1.1.20.2 jdolecek * http://en.wikipedia.org/wiki/Happy_number
55 1.1.20.2 jdolecek *
56 1.1.20.2 jdolecek * The happy number generator is here only to have something that the user
57 1.1.20.2 jdolecek * can read from our device. Any other arbitrary data generator could
58 1.1.20.2 jdolecek * have been used. The algorithm is not critical to the implementation
59 1.1.20.2 jdolecek * of the module.
60 1.1.20.2 jdolecek */
61 1.1.20.2 jdolecek
62 1.1.20.2 jdolecek
63 1.1.20.2 jdolecek #define HAPPY_NUMBER 1
64 1.1.20.2 jdolecek
65 1.1.20.2 jdolecek /* If n is not happy then its sequence ends in the cycle:
66 1.1.20.2 jdolecek * 4, 16, 37, 58, 89, 145, 42, 20, 4, ... */
67 1.1.20.2 jdolecek #define SAD_NUMBER 4
68 1.1.20.2 jdolecek
69 1.1.20.2 jdolecek /* Calculate the sum of the squares of the digits of n */
70 1.1.20.2 jdolecek static unsigned
71 1.1.20.2 jdolecek dsum(unsigned n)
72 1.1.20.2 jdolecek {
73 1.1.20.2 jdolecek unsigned sum, x;
74 1.1.20.2 jdolecek for (sum = 0; n; n /= 10) {
75 1.1.20.2 jdolecek x = n % 10;
76 1.1.20.2 jdolecek sum += x * x;
77 1.1.20.2 jdolecek }
78 1.1.20.2 jdolecek return sum;
79 1.1.20.2 jdolecek }
80 1.1.20.2 jdolecek
81 1.1.20.2 jdolecek static int
82 1.1.20.2 jdolecek check_happy(unsigned n)
83 1.1.20.2 jdolecek {
84 1.1.20.2 jdolecek for (;;) {
85 1.1.20.2 jdolecek unsigned total = dsum(n);
86 1.1.20.2 jdolecek
87 1.1.20.2 jdolecek if (total == HAPPY_NUMBER)
88 1.1.20.2 jdolecek return 1;
89 1.1.20.2 jdolecek if (total == SAD_NUMBER)
90 1.1.20.2 jdolecek return 0;
91 1.1.20.2 jdolecek
92 1.1.20.2 jdolecek n = total;
93 1.1.20.2 jdolecek }
94 1.1.20.2 jdolecek }
95 1.1.20.2 jdolecek
96 1.1.20.2 jdolecek dev_type_open(happy_open);
97 1.1.20.2 jdolecek dev_type_close(happy_close);
98 1.1.20.2 jdolecek dev_type_read(happy_read);
99 1.1.20.2 jdolecek
100 1.1.20.2 jdolecek static struct cdevsw happy_cdevsw = {
101 1.1.20.2 jdolecek .d_open = happy_open,
102 1.1.20.2 jdolecek .d_close = happy_close,
103 1.1.20.2 jdolecek .d_read = happy_read,
104 1.1.20.2 jdolecek .d_write = nowrite,
105 1.1.20.2 jdolecek .d_ioctl = noioctl,
106 1.1.20.2 jdolecek .d_stop = nostop,
107 1.1.20.2 jdolecek .d_tty = notty,
108 1.1.20.2 jdolecek .d_poll = nopoll,
109 1.1.20.2 jdolecek .d_mmap = nommap,
110 1.1.20.2 jdolecek .d_kqfilter = nokqfilter,
111 1.1.20.2 jdolecek .d_discard = nodiscard,
112 1.1.20.2 jdolecek .d_flag = D_OTHER
113 1.1.20.2 jdolecek };
114 1.1.20.2 jdolecek
115 1.1.20.2 jdolecek
116 1.1.20.2 jdolecek struct happy_softc {
117 1.1.20.2 jdolecek int refcnt;
118 1.1.20.2 jdolecek unsigned last;
119 1.1.20.2 jdolecek };
120 1.1.20.2 jdolecek
121 1.1.20.2 jdolecek static struct happy_softc sc;
122 1.1.20.2 jdolecek
123 1.1.20.2 jdolecek int
124 1.1.20.2 jdolecek happy_open(dev_t self __unused, int flag __unused, int mode __unused,
125 1.1.20.2 jdolecek struct lwp *l __unused)
126 1.1.20.2 jdolecek {
127 1.1.20.2 jdolecek if (sc.refcnt > 0)
128 1.1.20.2 jdolecek return EBUSY;
129 1.1.20.2 jdolecek
130 1.1.20.2 jdolecek sc.last = 0;
131 1.1.20.2 jdolecek ++sc.refcnt;
132 1.1.20.2 jdolecek
133 1.1.20.2 jdolecek return 0;
134 1.1.20.2 jdolecek }
135 1.1.20.2 jdolecek
136 1.1.20.2 jdolecek int
137 1.1.20.2 jdolecek happy_close(dev_t self __unused, int flag __unused, int mode __unused,
138 1.1.20.2 jdolecek struct lwp *l __unused)
139 1.1.20.2 jdolecek {
140 1.1.20.2 jdolecek --sc.refcnt;
141 1.1.20.2 jdolecek
142 1.1.20.2 jdolecek return 0;
143 1.1.20.2 jdolecek }
144 1.1.20.2 jdolecek
145 1.1.20.2 jdolecek int
146 1.1.20.2 jdolecek happy_read(dev_t self __unused, struct uio *uio, int flags __unused)
147 1.1.20.2 jdolecek {
148 1.1.20.2 jdolecek char line[80];
149 1.1.20.2 jdolecek
150 1.1.20.2 jdolecek /* Get next happy number */
151 1.1.20.2 jdolecek while (check_happy(++sc.last) == 0)
152 1.1.20.2 jdolecek continue;
153 1.1.20.2 jdolecek
154 1.1.20.2 jdolecek /* Print it into line[] with trailing \n */
155 1.1.20.2 jdolecek int len = snprintf(line, sizeof(line), "%u\n", sc.last);
156 1.1.20.2 jdolecek
157 1.1.20.2 jdolecek /* Is there room? */
158 1.1.20.2 jdolecek if (uio->uio_resid < len) {
159 1.1.20.2 jdolecek --sc.last; /* Step back */
160 1.1.20.2 jdolecek return EINVAL;
161 1.1.20.2 jdolecek }
162 1.1.20.2 jdolecek
163 1.1.20.2 jdolecek /* Send it to User-Space */
164 1.1.20.2 jdolecek int e;
165 1.1.20.2 jdolecek if ((e = uiomove(line, len, uio)))
166 1.1.20.2 jdolecek return e;
167 1.1.20.2 jdolecek
168 1.1.20.2 jdolecek return 0;
169 1.1.20.2 jdolecek }
170 1.1.20.2 jdolecek
171 1.1.20.2 jdolecek MODULE(MODULE_CLASS_MISC, happy, NULL);
172 1.1.20.2 jdolecek
173 1.1.20.2 jdolecek static int
174 1.1.20.2 jdolecek happy_modcmd(modcmd_t cmd, void *arg __unused)
175 1.1.20.2 jdolecek {
176 1.1.20.2 jdolecek /* The major should be verified and changed if needed to avoid
177 1.1.20.2 jdolecek * conflicts with other devices. */
178 1.1.20.2 jdolecek int cmajor = 210, bmajor = -1;
179 1.1.20.2 jdolecek
180 1.1.20.2 jdolecek switch (cmd) {
181 1.1.20.2 jdolecek case MODULE_CMD_INIT:
182 1.1.20.2 jdolecek if (devsw_attach("happy", NULL, &bmajor, &happy_cdevsw,
183 1.1.20.2 jdolecek &cmajor))
184 1.1.20.2 jdolecek return ENXIO;
185 1.1.20.2 jdolecek return 0;
186 1.1.20.2 jdolecek case MODULE_CMD_FINI:
187 1.1.20.2 jdolecek if (sc.refcnt > 0)
188 1.1.20.2 jdolecek return EBUSY;
189 1.1.20.2 jdolecek
190 1.1.20.2 jdolecek devsw_detach(NULL, &happy_cdevsw);
191 1.1.20.2 jdolecek return 0;
192 1.1.20.2 jdolecek default:
193 1.1.20.2 jdolecek return ENOTTY;
194 1.1.20.2 jdolecek }
195 1.1.20.2 jdolecek }
196