Initial import from FreeBSD RELENG_4:
[games.git] / release / sysinstall / devices.c
1 /*
2  * The new sysinstall program.
3  *
4  * This is probably the last program in the `sysinstall' line - the next
5  * generation being essentially a complete rewrite.
6  *
7  * $FreeBSD: src/release/sysinstall/devices.c,v 1.117.2.19 2003/01/29 20:59:10 trhodes Exp $
8  *
9  * Copyright (c) 1995
10  *      Jordan Hubbard.  All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer,
17  *    verbatim and that no modifications are made prior to this
18  *    point in the file.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  *
23  * THIS SOFTWARE IS PROVIDED BY JORDAN HUBBARD ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL JORDAN HUBBARD OR HIS PETS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  */
36
37 #include "sysinstall.h"
38 #include <sys/fcntl.h>
39 #include <sys/param.h>
40 #include <sys/socket.h>
41 #include <sys/ioctl.h>
42 #include <sys/errno.h>
43 #include <sys/time.h>
44 #include <net/if.h>
45 #include <net/if_var.h>
46 #include <net/if_dl.h>
47 #include <netinet/in.h>
48 #include <netinet/in_var.h>
49 #include <arpa/inet.h>
50 #include <ctype.h>
51
52 /* how much to bias minor number for a given /dev/<ct#><un#>s<s#> slice */
53 #define SLICE_DELTA     (0x10000)
54
55 static Device *Devices[DEV_MAX];
56 static int numDevs;
57
58 static struct _devname {
59     DeviceType type;
60     char *name;
61     char *description;
62     int major, minor, delta, max;
63 } device_names[] = {
64     { DEVICE_TYPE_CDROM,        "cd%dc",        "SCSI CDROM drive",     15, 2, 8, 4                             },
65     { DEVICE_TYPE_CDROM,        "mcd%da",       "Mitsumi (old model) CDROM drive",      29, 0, 8, 4             },
66     { DEVICE_TYPE_CDROM,        "scd%da",       "Sony CDROM drive - CDU31/33A type",    45, 0, 8, 4             },
67 #ifdef notdef
68     { DEVICE_TYPE_CDROM,        "matcd%da",     "Matsushita CDROM ('sound blaster' type)", 46, 0, 8, 4          },
69 #endif
70     { DEVICE_TYPE_CDROM,        "acd%dc",       "ATAPI/IDE CDROM",      117, 0, 8, 4                            },
71     { DEVICE_TYPE_TAPE,         "rsa%d",        "SCSI tape drive",      14, 0, 16, 4                            },
72     { DEVICE_TYPE_TAPE,         "rwt%d",        "Wangtek tape drive",   10, 0, 1, 4                             },
73     { DEVICE_TYPE_DISK,         "da%d",         "SCSI disk device",     13, 65538, 8, 16                        },
74     { DEVICE_TYPE_DISK,         "ad%d",         "ATA/IDE disk device",  116, 65538, 8, 16                       },
75     { DEVICE_TYPE_DISK,         "ar%d",         "ATA/IDE RAID device",  157, 65538, 8, 16                       },
76     { DEVICE_TYPE_DISK,         "fla%d",        "M-Systems DiskOnChip Flash devicee",   102, 65538, 8, 16       },
77     { DEVICE_TYPE_DISK,         "afd%d",        "ATAPI/IDE floppy device",      118, 65538, 8, 4                },
78     { DEVICE_TYPE_DISK,         "mlxd%d",       "Mylex RAID disk",      131, 65538, 8, 4                        },
79     { DEVICE_TYPE_DISK,         "amrd%d",       "AMI MegaRAID drive",   133, 65538, 8, 4                        },
80     { DEVICE_TYPE_DISK,         "idad%d",       "Compaq RAID array",    109, 65538, 8, 4                        },
81     { DEVICE_TYPE_DISK,         "twed%d",       "3ware ATA RAID array", 147, 65538, 8, 4                        },
82     { DEVICE_TYPE_DISK,         "aacd%d",       "Adaptec FSA RAID array", 151, 65538, 8, 4                      },
83     { DEVICE_TYPE_FLOPPY,       "fd%d",         "floppy drive unit A",  9, 0, 64, 4                             },
84     { DEVICE_TYPE_NETWORK,      "an",           "Aironet 4500/4800 802.11 wireless adapter"                     },
85     { DEVICE_TYPE_NETWORK,      "aue",          "ADMtek USB ethernet adapter"                                   },
86     { DEVICE_TYPE_NETWORK,      "bge",          "Broadcom BCM570x PCI gigabit ethernet card"                            },
87     { DEVICE_TYPE_NETWORK,      "cue",          "CATC USB ethernet adapter"                                     },
88     { DEVICE_TYPE_NETWORK,      "fpa",          "DEC DEFPA PCI FDDI card"                                       },
89     { DEVICE_TYPE_NETWORK,      "sr",           "SDL T1/E1 sync serial PCI card"                                },
90     { DEVICE_TYPE_NETWORK,      "cc3i",         "SDL HSSI sync serial PCI card"                                 },
91     { DEVICE_TYPE_NETWORK,      "en",           "Efficient Networks ATM PCI card"                               },
92     { DEVICE_TYPE_NETWORK,      "dc",           "DEC/Intel 21143 (and clones) PCI fast ethernet card"           },
93     { DEVICE_TYPE_NETWORK,      "de",           "DEC DE435 PCI NIC or other DC21040-AA based card"              },
94     { DEVICE_TYPE_NETWORK,      "fxp",          "Intel EtherExpress Pro/100B PCI Fast Ethernet card"            },
95     { DEVICE_TYPE_NETWORK,      "ed",           "Novell NE1000/2000; 3C503; NE2000-compatible PCMCIA"           },
96     { DEVICE_TYPE_NETWORK,      "ep",           "3Com 3C509 ethernet card/3C589 PCMCIA"                         },
97     { DEVICE_TYPE_NETWORK,      "el",           "3Com 3C501 ethernet card"                                      },
98     { DEVICE_TYPE_NETWORK,      "em",           "Intel(R) PRO/1000 ethernet card"                               },
99     { DEVICE_TYPE_NETWORK,      "ex",           "Intel EtherExpress Pro/10 ethernet card"                       },
100     { DEVICE_TYPE_NETWORK,      "fe",           "Fujitsu MB86960A/MB86965A ethernet card"                       },
101     { DEVICE_TYPE_NETWORK,      "ie",           "AT&T StarLAN 10 and EN100; 3Com 3C507; NI5210"                 },
102     { DEVICE_TYPE_NETWORK,      "ix",           "Intel Etherexpress ethernet card"                              },
103     { DEVICE_TYPE_NETWORK,      "kue",          "Kawasaki LSI USB ethernet adapter"                             },
104     { DEVICE_TYPE_NETWORK,      "le",           "DEC EtherWorks 2 or 3 ethernet card"                           },
105     { DEVICE_TYPE_NETWORK,      "lnc",          "Lance/PCnet (Isolan/Novell NE2100/NE32-VL) ethernet"           },
106     { DEVICE_TYPE_NETWORK,      "lge",          "Level 1 LXT1001 gigabit ethernet card"                         },
107     { DEVICE_TYPE_NETWORK,      "nge",          "NatSemi PCI gigabit ethernet card"                             },
108     { DEVICE_TYPE_NETWORK,      "pcn",          "AMD Am79c79x PCI ethernet card"                                },
109     { DEVICE_TYPE_NETWORK,      "ray",          "Raytheon Raylink 802.11 wireless adaptor"                      },
110     { DEVICE_TYPE_NETWORK,      "rl",           "RealTek 8129/8139 PCI ethernet card"                           },
111     { DEVICE_TYPE_NETWORK,      "sf",           "Adaptec AIC-6915 PCI ethernet card"                            },
112     { DEVICE_TYPE_NETWORK,      "sis",          "SiS 900/SiS 7016 PCI ethernet card"                            },
113 #ifdef PC98
114     { DEVICE_TYPE_NETWORK,      "snc",          "SONIC ethernet card"                                           },
115 #endif
116     { DEVICE_TYPE_NETWORK,      "sn",           "SMC/Megahertz ethernet card"                                   },
117     { DEVICE_TYPE_NETWORK,      "ste",          "Sundance ST201 PCI ethernet card"                              },
118     { DEVICE_TYPE_NETWORK,      "sk",           "SysKonnect PCI gigabit ethernet card"                          },
119     { DEVICE_TYPE_NETWORK,      "tx",           "SMC 9432TX ethernet card"                                      },
120     { DEVICE_TYPE_NETWORK,      "txp",          "3Com 3cR990 ethernet card"                                     },
121     { DEVICE_TYPE_NETWORK,      "ti",           "Alteon Networks PCI gigabit ethernet card"                     },
122     { DEVICE_TYPE_NETWORK,      "tl",           "Texas Instruments ThunderLAN PCI ethernet card"                },
123     { DEVICE_TYPE_NETWORK,      "vr",           "VIA VT3043/VT86C100A Rhine PCI ethernet card"                  },
124     { DEVICE_TYPE_NETWORK,      "vx",           "3COM 3c590 / 3c595 ethernet card"                              },
125     { DEVICE_TYPE_NETWORK,      "wb",           "Winbond W89C840F PCI ethernet card"                            },
126     { DEVICE_TYPE_NETWORK,      "wi",           "Lucent WaveLAN/IEEE 802.11 wireless adapter"                   },
127     { DEVICE_TYPE_NETWORK,      "wx",           "Intel Gigabit Ethernet (82452) card"                   },
128     { DEVICE_TYPE_NETWORK,      "xe",           "Xircom/Intel EtherExpress Pro100/16 ethernet card"             },
129     { DEVICE_TYPE_NETWORK,      "xl",           "3COM 3c90x / 3c90xB PCI ethernet card"                         },
130     { DEVICE_TYPE_NETWORK,      "cuaa%d",       "%s on device %s (COM%d)",      28, 128, 1, 16                  },
131     { DEVICE_TYPE_NETWORK,      "lp",           "Parallel Port IP (PLIP) peer connection"                       },
132     { DEVICE_TYPE_NETWORK,      "lo",           "Loop-back (local) network interface"                           },
133 #ifdef PC98
134     { DEVICE_TYPE_DISK,         "wd%d",         "IDE disk device",              3, 65538, 8, 16                 },
135     { DEVICE_TYPE_CDROM,        "wcd%dc",       "ATAPI IDE CDROM",              69, 2, 8, 4                     },
136     { DEVICE_TYPE_FLOPPY,       "wfd%d",        "ATAPI floppy drive unit A",    87, 0, 8, 4                     },
137     { DEVICE_TYPE_DISK,         "wfd%d",        "ATAPI floppy device",          87, 65538, 8, 4                 },
138 #endif
139     { 0 },
140 };
141
142 Device *
143 new_device(char *name)
144 {
145     Device *dev;
146
147     dev = safe_malloc(sizeof(Device));
148     bzero(dev, sizeof(Device));
149     if (name)
150         SAFE_STRCPY(dev->name, name);
151     return dev;
152 }
153
154 /* Stubs for unimplemented strategy routines */
155 Boolean
156 dummyInit(Device *dev)
157 {
158     return TRUE;
159 }
160
161 FILE *
162 dummyGet(Device *dev, char *dist, Boolean probe)
163 {
164     return NULL;
165 }
166
167 void
168 dummyShutdown(Device *dev)
169 {
170     return;
171 }
172
173 static int
174 deviceTry(struct _devname dev, char *try, int i)
175 {
176     int fd;
177     char unit[80];
178     mode_t m;
179     dev_t d;
180     int fail;
181
182     snprintf(unit, sizeof unit, dev.name, i);
183     snprintf(try, FILENAME_MAX, "/dev/%s", unit);
184     if (isDebug())
185         msgDebug("deviceTry: attempting to open %s\n", try);
186     fd = open(try, O_RDONLY);
187     if (fd >= 0) {
188         if (isDebug())
189             msgDebug("deviceTry: open of %s succeeded on first try.\n", try);
190         return fd;
191     }
192     m = 0640 | S_IFCHR;
193     d = makedev(dev.major, dev.minor + (i * dev.delta));
194     if (isDebug())
195         msgDebug("deviceTry: Making %s device for %s [%d, %d]\n", m & S_IFCHR ? "raw" : "block", try, dev.major, dev.minor + (i * dev.delta));
196     fail = mknod(try, m, d);
197     fd = open(try, O_RDONLY);
198     if (fd >= 0) {
199         if (isDebug())
200             msgDebug("deviceTry: open of %s succeeded on second try.\n", try);
201         return fd;
202     }
203     else if (!fail)
204         (void)unlink(try);
205     /* Don't try a "make-under" here since we're using a fixit floppy in this case */
206     snprintf(try, FILENAME_MAX, "/mnt/dev/%s", unit);
207     fd = open(try, O_RDONLY);
208     if (isDebug())
209         msgDebug("deviceTry: final attempt for %s returns %d\n", try, fd);
210     return fd;
211 }
212
213 /* Register a new device in the devices array */
214 Device *
215 deviceRegister(char *name, char *desc, char *devname, DeviceType type, Boolean enabled,
216                Boolean (*init)(Device *), FILE * (*get)(Device *, char *, Boolean),
217                void (*shutdown)(Device *), void *private)
218 {
219     Device *newdev = NULL;
220
221     if (numDevs == DEV_MAX)
222         msgFatal("Too many devices found!");
223     else {
224         newdev = new_device(name);
225         newdev->description = desc;
226         newdev->devname = devname;
227         newdev->type = type;
228         newdev->enabled = enabled;
229         newdev->init = init ? init : dummyInit;
230         newdev->get = get ? get : dummyGet;
231         newdev->shutdown = shutdown ? shutdown : dummyShutdown;
232         newdev->private = private;
233         Devices[numDevs] = newdev;
234         Devices[++numDevs] = NULL;
235     }
236     return newdev;
237 }
238
239 /* Reset the registered device chain */
240 void
241 deviceReset(void)
242 {
243     int i;
244
245     for (i = 0; i < numDevs; i++) {
246         DEVICE_SHUTDOWN(Devices[i]);
247
248         /* XXX this potentially leaks Devices[i]->private if it's being
249          * used to point to something dynamic, but you're not supposed
250          * to call this routine at such times that some open instance
251          * has its private ptr pointing somewhere anyway. XXX
252          */
253         free(Devices[i]);
254     }
255     Devices[numDevs = 0] = NULL;
256 }
257
258 /* Get all device information for devices we have attached */
259 void
260 deviceGetAll(void)
261 {
262     int i, j, fd, s;
263     struct ifconf ifc;
264     struct ifreq *ifptr, *end;
265     int ifflags;
266     char buffer[INTERFACE_MAX * sizeof(struct ifreq)];
267     char **names;
268
269     msgNotify("Probing devices, please wait (this can take a while)...");
270     /* First go for the network interfaces.  Stolen shamelessly from ifconfig! */
271     ifc.ifc_len = sizeof(buffer);
272     ifc.ifc_buf = buffer;
273
274     s = socket(AF_INET, SOCK_DGRAM, 0);
275     if (s < 0)
276         goto skipif;    /* Jump over network iface probing */
277
278     if (ioctl(s, SIOCGIFCONF, (char *) &ifc) < 0)
279         goto skipif;    /* Jump over network iface probing */
280
281     close(s);
282     ifflags = ifc.ifc_req->ifr_flags;
283     end = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
284     for (ifptr = ifc.ifc_req; ifptr < end; ifptr++) {
285         char *descr;
286
287         /* If it's not a link entry, forget it */
288         if (ifptr->ifr_ifru.ifru_addr.sa_family != AF_LINK)
289             goto loopend;
290
291         /* Eliminate network devices that don't make sense */
292         if (!strncmp(ifptr->ifr_name, "lo", 2))
293             goto loopend;
294
295         /* If we have a slip device, don't register it */
296         if (!strncmp(ifptr->ifr_name, "sl", 2)) {
297             goto loopend;
298         }
299         /* And the same for ppp */
300         if (!strncmp(ifptr->ifr_name, "tun", 3) || !strncmp(ifptr->ifr_name, "ppp", 3)) {
301             goto loopend;
302         }
303         /* Try and find its description */
304         for (i = 0, descr = NULL; device_names[i].name; i++) {
305             int len = strlen(device_names[i].name);
306
307             if (!ifptr->ifr_name || !ifptr->ifr_name[0])
308                 continue;
309             else if (!strncmp(ifptr->ifr_name, device_names[i].name, len)) {
310                 descr = device_names[i].description;
311                 break;
312             }
313         }
314         if (!descr)
315             descr = "<unknown network interface type>";
316
317         deviceRegister(ifptr->ifr_name, descr, strdup(ifptr->ifr_name), DEVICE_TYPE_NETWORK, TRUE,
318                        mediaInitNetwork, NULL, mediaShutdownNetwork, NULL);
319         if (isDebug())
320             msgDebug("Found a network device named %s\n", ifptr->ifr_name);
321         close(s);
322         if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
323             continue;
324
325 loopend:
326         if (ifptr->ifr_addr.sa_len)     /* I'm not sure why this is here - it's inherited */
327             ifptr = (struct ifreq *)((caddr_t)ifptr + ifptr->ifr_addr.sa_len - sizeof(struct sockaddr));
328         close(s);
329     }
330
331 skipif:
332     /* Next, try to find all the types of devices one might need
333      * during the second stage of the installation.
334      */
335     for (i = 0; device_names[i].name; i++) {
336         for (j = 0; j < device_names[i].max; j++) {
337             char try[FILENAME_MAX];
338
339             switch(device_names[i].type) {
340             case DEVICE_TYPE_CDROM:
341                 fd = deviceTry(device_names[i], try, j);
342                 if (fd >= 0 || errno == EBUSY) {        /* EBUSY if already mounted */
343                     char n[BUFSIZ];
344
345                     if (fd >= 0) close(fd);
346                     snprintf(n, sizeof n, device_names[i].name, j);
347                     deviceRegister(strdup(n), device_names[i].description, strdup(try),
348                                          DEVICE_TYPE_CDROM, TRUE, mediaInitCDROM, mediaGetCDROM,
349                                          mediaShutdownCDROM, NULL);
350                     if (isDebug())
351                         msgDebug("Found a CDROM device for %s\n", try);
352                 }
353                 break;
354
355             case DEVICE_TYPE_TAPE:
356                 fd = deviceTry(device_names[i], try, j);
357                 if (fd >= 0) {
358                     char n[BUFSIZ];
359
360                     close(fd);
361                     snprintf(n, sizeof n, device_names[i].name, j);
362                     deviceRegister(strdup(n), device_names[i].description, strdup(try),
363                                    DEVICE_TYPE_TAPE, TRUE, mediaInitTape, mediaGetTape, mediaShutdownTape, NULL);
364                     if (isDebug())
365                         msgDebug("Found a TAPE device for %s\n", try);
366                 }
367                 break;
368
369             case DEVICE_TYPE_DISK:
370                 fd = deviceTry(device_names[i], try, j);
371                 if (fd >= 0 && RunningAsInit) {
372                     dev_t d;
373                     mode_t m;
374                     int s, fail;
375                     char unit[80], slice[80];
376
377                     close(fd);
378                     /* Make associated slice entries */
379                     for (s = 1; s < 8; s++) {
380                         snprintf(unit, sizeof unit, device_names[i].name, j);
381                         snprintf(slice, sizeof slice, "/dev/%ss%d", unit, s);
382                         d = makedev(device_names[i].major, device_names[i].minor +
383                                     (j * device_names[i].delta) + (s * SLICE_DELTA));
384                         m = 0640 | S_IFCHR;
385                         fail = mknod(slice, m, d);
386                         fd = open(slice, O_RDONLY);
387                         if (fd >= 0)
388                             close(fd);
389                         else if (!fail)
390                             (void)unlink(slice);
391                     }
392                 }
393                 break;
394
395             case DEVICE_TYPE_FLOPPY:
396                 fd = deviceTry(device_names[i], try, j);
397                 if (fd >= 0) {
398                     char n[BUFSIZ];
399
400                     close(fd);
401                     snprintf(n, sizeof n, device_names[i].name, j);
402                     deviceRegister(strdup(n), device_names[i].description, strdup(try),
403                                    DEVICE_TYPE_FLOPPY, TRUE, mediaInitFloppy, mediaGetFloppy,
404                                    mediaShutdownFloppy, NULL);
405                     if (isDebug())
406                         msgDebug("Found a floppy device for %s\n", try);
407                 }
408                 break;
409
410             case DEVICE_TYPE_NETWORK:
411                 fd = deviceTry(device_names[i], try, j);
412                 /* The only network devices that you can open this way are serial ones */
413                 if (fd >= 0) {
414                     char *newdesc, *cp;
415
416                     close(fd);
417                     cp = device_names[i].description;
418                     /* Serial devices get a slip and ppp device each, if supported */
419                     newdesc = safe_malloc(strlen(cp) + 40);
420                     sprintf(newdesc, cp, "SLIP interface", try, j + 1);
421                     deviceRegister("sl0", newdesc, strdup(try), DEVICE_TYPE_NETWORK, TRUE, mediaInitNetwork,
422                                    NULL, mediaShutdownNetwork, NULL);
423                     msgDebug("Add mapping for %s to sl0\n", try);
424                     newdesc = safe_malloc(strlen(cp) + 50);
425                     sprintf(newdesc, cp, "PPP interface", try, j + 1);
426                     deviceRegister("ppp0", newdesc, strdup(try), DEVICE_TYPE_NETWORK, TRUE, mediaInitNetwork,
427                                    NULL, mediaShutdownNetwork, NULL);
428                     if (isDebug())
429                         msgDebug("Add mapping for %s to ppp0\n", try);
430                 }
431                 break;
432
433             default:
434                 break;
435             }
436         }
437     }
438
439     /* Finally, go get the disks and look for DOS partitions to register */
440     if ((names = Disk_Names()) != NULL) {
441         int i;
442
443         for (i = 0; names[i]; i++) {
444             Chunk *c1;
445             Disk *d;
446
447             /* Ignore memory disks */
448             if (!strncmp(names[i], "md", 2))
449                 continue;
450
451             d = Open_Disk(names[i]);
452             if (!d) {
453                 msgDebug("Unable to open disk %s", names[i]);
454                 continue;
455             }
456
457             deviceRegister(names[i], names[i], d->name, DEVICE_TYPE_DISK, FALSE,
458                            dummyInit, dummyGet, dummyShutdown, d);
459             if (isDebug())
460                 msgDebug("Found a disk device named %s\n", names[i]);
461
462             /* Look for existing DOS partitions to register as "DOS media devices" */
463             for (c1 = d->chunks->part; c1; c1 = c1->next) {
464                 if (c1->type == fat || c1->type == extended) {
465                     Device *dev;
466                     char devname[80];
467
468                     /* Got one! */
469                     snprintf(devname, sizeof devname, "/dev/%s", c1->name);
470                     dev = deviceRegister(c1->name, c1->name, strdup(devname), DEVICE_TYPE_DOS, TRUE,
471                                          mediaInitDOS, mediaGetDOS, mediaShutdownDOS, NULL);
472                     dev->private = c1;
473                     if (isDebug())
474                         msgDebug("Found a DOS partition %s on drive %s\n", c1->name, d->name);
475                 }
476             }
477         }
478         free(names);
479     }
480     dialog_clear_norefresh();
481 }
482
483 /* Rescan all devices, after closing previous set - convenience function */
484 void
485 deviceRescan(void)
486 {
487     deviceReset();
488     deviceGetAll();
489 }
490
491 /*
492  * Find all devices that match the criteria, allowing "wildcarding" as well
493  * by allowing NULL or ANY values to match all.  The array returned is static
494  * and may be used until the next invocation of deviceFind().
495  */
496 Device **
497 deviceFind(char *name, DeviceType class)
498 {
499     static Device *found[DEV_MAX];
500     int i, j;
501
502     j = 0;
503     for (i = 0; i < numDevs; i++) {
504         if ((!name || !strcmp(Devices[i]->name, name))
505             && (class == DEVICE_TYPE_ANY || class == Devices[i]->type))
506             found[j++] = Devices[i];
507     }
508     found[j] = NULL;
509     return j ? found : NULL;
510 }
511
512 Device **
513 deviceFindDescr(char *name, char *desc, DeviceType class)
514 {
515     static Device *found[DEV_MAX];
516     int i, j;
517
518     j = 0;
519     for (i = 0; i < numDevs; i++) {
520         if ((!name || !strcmp(Devices[i]->name, name)) &&
521             (!desc || !strcmp(Devices[i]->description, desc)) &&
522             (class == DEVICE_TYPE_ANY || class == Devices[i]->type))
523             found[j++] = Devices[i];
524     }
525     found[j] = NULL;
526     return j ? found : NULL;
527 }
528
529 int
530 deviceCount(Device **devs)
531 {
532     int i;
533
534     if (!devs)
535         return 0;
536     for (i = 0; devs[i]; i++);
537     return i;
538 }
539
540 /*
541  * Create a menu listing all the devices of a certain type in the system.
542  * The passed-in menu is expected to be a "prototype" from which the new
543  * menu is cloned.
544  */
545 DMenu *
546 deviceCreateMenu(DMenu *menu, DeviceType type, int (*hook)(dialogMenuItem *d), int (*check)(dialogMenuItem *d))
547 {
548     Device **devs;
549     int numdevs;
550     DMenu *tmp = NULL;
551     int i, j;
552
553     devs = deviceFind(NULL, type);
554     numdevs = deviceCount(devs);
555     if (!numdevs)
556         return NULL;
557     tmp = (DMenu *)safe_malloc(sizeof(DMenu) + (sizeof(dialogMenuItem) * (numdevs + 1)));
558     bcopy(menu, tmp, sizeof(DMenu));
559     for (i = 0; devs[i]; i++) {
560         tmp->items[i].prompt = devs[i]->name;
561         for (j = 0; j < numDevs; j++) {
562             if (devs[i] == Devices[j]) {
563                 tmp->items[i].title = Devices[j]->description;
564                 break;
565             }
566         }
567         if (j == numDevs)
568             tmp->items[i].title = "<unknown device type>";
569         tmp->items[i].fire = hook;
570         tmp->items[i].checked = check;
571     }
572     tmp->items[i].title = NULL;
573     return tmp;
574 }