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