]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/sade/devices.c
This commit was generated by cvs2svn to compensate for changes in r155511,
[FreeBSD/FreeBSD.git] / usr.sbin / sade / 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$
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 #include <libdisk.h>
52
53 /* how much to bias minor number for a given /dev/<ct#><un#>s<s#> slice */
54 #define SLICE_DELTA     (0x10000)
55
56 static Device *Devices[DEV_MAX];
57 static int numDevs;
58
59 static struct _devname {
60     DeviceType type;
61     char *name;
62     char *description;
63     int major, minor, delta, max;
64 } device_names[] = {
65     { DEVICE_TYPE_CDROM,        "cd%d",         "SCSI CDROM drive",     15, 2, 8, 4                             },
66     { DEVICE_TYPE_CDROM,        "mcd%d",        "Mitsumi (old model) CDROM drive",      29, 0, 8, 4             },
67     { DEVICE_TYPE_CDROM,        "scd%d",        "Sony CDROM drive - CDU31/33A type",    45, 0, 8, 4             },
68 #ifdef notdef
69     { DEVICE_TYPE_CDROM,        "matcd%d",      "Matsushita CDROM ('sound blaster' type)", 46, 0, 8, 4          },
70 #endif
71     { DEVICE_TYPE_CDROM,        "acd%d",        "ATAPI/IDE CDROM",      117, 0, 8, 4                            },
72     { DEVICE_TYPE_TAPE,         "sa%d",         "SCSI tape drive",      14, 0, 16, 4                            },
73     { DEVICE_TYPE_TAPE,         "rwt%d",        "Wangtek tape drive",   10, 0, 1, 4                             },
74     { DEVICE_TYPE_DISK,         "da%d",         "SCSI disk device",     13, 65538, 8, 16                        },
75     { DEVICE_TYPE_DISK,         "ad%d",         "ATA/IDE disk device",  116, 65538, 8, 16                       },
76     { DEVICE_TYPE_DISK,         "ar%d",         "ATA/IDE RAID device",  157, 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_DISK,         "ipsd%d",       "IBM ServeRAID RAID array", 176, 65538, 8, 4                    },
84     { DEVICE_TYPE_FLOPPY,       "fd%d",         "floppy drive unit A",  9, 0, 64, 4                             },
85     { DEVICE_TYPE_NETWORK,      "an",           "Aironet 4500/4800 802.11 wireless adapter"                     },
86     { DEVICE_TYPE_NETWORK,      "aue",          "ADMtek USB ethernet adapter"                                   },
87     { DEVICE_TYPE_NETWORK,      "axe",          "ASIX Electronics USB ethernet adapter"                                 },
88     { DEVICE_TYPE_NETWORK,      "bfe",          "Broadcom BCM440x PCI ethernet card"                            },
89     { DEVICE_TYPE_NETWORK,      "bge",          "Broadcom BCM570x PCI gigabit ethernet card"                            },
90     { DEVICE_TYPE_NETWORK,      "cue",          "CATC USB ethernet adapter"                                     },
91     { DEVICE_TYPE_NETWORK,      "fpa",          "DEC DEFPA PCI FDDI card"                                       },
92     { DEVICE_TYPE_NETWORK,      "sr",           "SDL T1/E1 sync serial PCI card"                                },
93     { DEVICE_TYPE_NETWORK,      "cc3i",         "SDL HSSI sync serial PCI card"                                 },
94     { DEVICE_TYPE_NETWORK,      "en",           "Efficient Networks ATM PCI card"                               },
95     { DEVICE_TYPE_NETWORK,      "dc",           "DEC/Intel 21143 (and clones) PCI fast ethernet card"           },
96     { DEVICE_TYPE_NETWORK,      "de",           "DEC DE435 PCI NIC or other DC21040-AA based card"              },
97     { DEVICE_TYPE_NETWORK,      "fxp",          "Intel EtherExpress Pro/100B PCI Fast Ethernet card"            },
98     { DEVICE_TYPE_NETWORK,      "ed",           "Novell NE1000/2000; 3C503; NE2000-compatible PCMCIA"           },
99     { DEVICE_TYPE_NETWORK,      "ep",           "3Com 3C509 ethernet card/3C589 PCMCIA"                         },
100     { DEVICE_TYPE_NETWORK,      "el",           "3Com 3C501 ethernet card"                                      },
101     { DEVICE_TYPE_NETWORK,      "em",           "Intel(R) PRO/1000 ethernet card"                               },
102     { DEVICE_TYPE_NETWORK,      "ex",           "Intel EtherExpress Pro/10 ethernet card"                       },
103     { DEVICE_TYPE_NETWORK,      "fe",           "Fujitsu MB86960A/MB86965A ethernet card"                       },
104     { DEVICE_TYPE_NETWORK,      "gem",          "Apple/Sun GMAC ethernet adapter"                               },
105     { DEVICE_TYPE_NETWORK,      "ie",           "AT&T StarLAN 10 and EN100; 3Com 3C507; NI5210"                 },
106     { DEVICE_TYPE_NETWORK,      "ix",           "Intel Etherexpress ethernet card"                              },
107     { DEVICE_TYPE_NETWORK,      "kue",          "Kawasaki LSI USB ethernet adapter"                             },
108     { DEVICE_TYPE_NETWORK,      "le",           "DEC EtherWorks 2 or 3 ethernet card"                           },
109     { DEVICE_TYPE_NETWORK,      "lnc",          "Lance/PCnet (Isolan/Novell NE2100/NE32-VL) ethernet"           },
110     { DEVICE_TYPE_NETWORK,      "lge",          "Level 1 LXT1001 gigabit ethernet card"                         },
111     { DEVICE_TYPE_NETWORK,      "nge",          "NatSemi PCI gigabit ethernet card"                             },
112     { DEVICE_TYPE_NETWORK,      "pcn",          "AMD Am79c79x PCI ethernet card"                                },
113     { DEVICE_TYPE_NETWORK,      "ray",          "Raytheon Raylink 802.11 wireless adaptor"                      },
114     { DEVICE_TYPE_NETWORK,      "re",           "RealTek 8139C+/8169/8169S/8110S PCI ethernet card"                             },
115     { DEVICE_TYPE_NETWORK,      "rl",           "RealTek 8129/8139 PCI ethernet card"                           },
116     { DEVICE_TYPE_NETWORK,      "rue",          "RealTek USB ethernet card"                             },
117     { DEVICE_TYPE_NETWORK,      "sf",           "Adaptec AIC-6915 PCI ethernet card"                            },
118     { DEVICE_TYPE_NETWORK,      "sis",          "SiS 900/SiS 7016 PCI ethernet card"                            },
119 #ifdef PC98
120     { DEVICE_TYPE_NETWORK,      "snc",          "SONIC ethernet card"                                           },
121 #endif
122     { DEVICE_TYPE_NETWORK,      "sn",           "SMC/Megahertz ethernet card"                                   },
123     { DEVICE_TYPE_NETWORK,      "ste",          "Sundance ST201 PCI ethernet card"                              },
124     { DEVICE_TYPE_NETWORK,      "sk",           "SysKonnect PCI gigabit ethernet card"                          },
125     { DEVICE_TYPE_NETWORK,      "tx",           "SMC 9432TX ethernet card"                                      },
126     { DEVICE_TYPE_NETWORK,      "txp",          "3Com 3cR990 ethernet card"                                     },
127     { DEVICE_TYPE_NETWORK,      "ti",           "Alteon Networks PCI gigabit ethernet card"                     },
128     { DEVICE_TYPE_NETWORK,      "tl",           "Texas Instruments ThunderLAN PCI ethernet card"                },
129     { DEVICE_TYPE_NETWORK,      "vge",          "VIA VT612x PCI gigabit ethernet card"                  },
130     { DEVICE_TYPE_NETWORK,      "vr",           "VIA VT3043/VT86C100A Rhine PCI ethernet card"                  },
131     { DEVICE_TYPE_NETWORK,      "vlan",         "IEEE 802.1Q VLAN network interface"                            },
132     { DEVICE_TYPE_NETWORK,      "vx",           "3COM 3c590 / 3c595 ethernet card"                              },
133     { DEVICE_TYPE_NETWORK,      "wb",           "Winbond W89C840F PCI ethernet card"                            },
134     { DEVICE_TYPE_NETWORK,      "wi",           "Lucent WaveLAN/IEEE 802.11 wireless adapter"                   },
135     { DEVICE_TYPE_NETWORK,      "wx",           "Intel Gigabit Ethernet (82452) card"                   },
136     { DEVICE_TYPE_NETWORK,      "xe",           "Xircom/Intel EtherExpress Pro100/16 ethernet card"             },
137     { DEVICE_TYPE_NETWORK,      "xl",           "3COM 3c90x / 3c90xB PCI ethernet card"                         },
138     { DEVICE_TYPE_NETWORK,      "cuad%d",       "%s on device %s (COM%d)",      28, 128, 1, 16                  },
139     { DEVICE_TYPE_NETWORK,      "fwe",          "FireWire Ethernet emulation"                                   },
140     { DEVICE_TYPE_NETWORK,      "lp",           "Parallel Port IP (PLIP) peer connection"                       },
141     { DEVICE_TYPE_NETWORK,      "lo",           "Loop-back (local) network interface"                           },
142     { DEVICE_TYPE_NETWORK,      "disc",         "Software discard network interface"                            },
143     { 0 },
144 };
145
146 Device *
147 new_device(char *name)
148 {
149     Device *dev;
150
151     dev = safe_malloc(sizeof(Device));
152     bzero(dev, sizeof(Device));
153     if (name)
154         SAFE_STRCPY(dev->name, name);
155     return dev;
156 }
157
158 /* Stubs for unimplemented strategy routines */
159 Boolean
160 dummyInit(Device *dev)
161 {
162     return TRUE;
163 }
164
165 FILE *
166 dummyGet(Device *dev, char *dist, Boolean probe)
167 {
168     return NULL;
169 }
170
171 void
172 dummyShutdown(Device *dev)
173 {
174     return;
175 }
176
177 static int
178 deviceTry(struct _devname dev, char *try, int i)
179 {
180     int fd;
181     char unit[80];
182
183     snprintf(unit, sizeof unit, dev.name, i);
184     snprintf(try, FILENAME_MAX, "/dev/%s", unit);
185     if (isDebug())
186         msgDebug("deviceTry: attempting to open %s\n", try);
187     fd = open(try, O_RDONLY);
188     if (fd >= 0) {
189         if (isDebug())
190             msgDebug("deviceTry: open of %s succeeded on first try.\n", try);
191     } else {
192         if (isDebug())
193             msgDebug("deviceTry: open of %s failed.\n", try);
194     }
195     return fd;
196 }
197
198 /* Register a new device in the devices array */
199 Device *
200 deviceRegister(char *name, char *desc, char *devname, DeviceType type, Boolean enabled,
201                Boolean (*init)(Device *), FILE * (*get)(Device *, char *, Boolean),
202                void (*shutdown)(Device *), void *private)
203 {
204     Device *newdev = NULL;
205
206     if (numDevs == DEV_MAX)
207         msgFatal("Too many devices found!");
208     else {
209         newdev = new_device(name);
210         newdev->description = desc;
211         newdev->devname = devname;
212         newdev->type = type;
213         newdev->enabled = enabled;
214         newdev->init = init ? init : dummyInit;
215         newdev->get = get ? get : dummyGet;
216         newdev->shutdown = shutdown ? shutdown : dummyShutdown;
217         newdev->private = private;
218         Devices[numDevs] = newdev;
219         Devices[++numDevs] = NULL;
220     }
221     return newdev;
222 }
223
224 /* Reset the registered device chain */
225 void
226 deviceReset(void)
227 {
228     int i;
229
230     for (i = 0; i < numDevs; i++) {
231         DEVICE_SHUTDOWN(Devices[i]);
232
233         /* XXX this potentially leaks Devices[i]->private if it's being
234          * used to point to something dynamic, but you're not supposed
235          * to call this routine at such times that some open instance
236          * has its private ptr pointing somewhere anyway. XXX
237          */
238         free(Devices[i]);
239     }
240     Devices[numDevs = 0] = NULL;
241 }
242
243 /* Get all device information for devices we have attached */
244 void
245 deviceGetAll(void)
246 {
247     int i, j, fd, s;
248     struct ifconf ifc;
249     struct ifreq *ifptr, *end;
250     int ifflags;
251     char buffer[INTERFACE_MAX * sizeof(struct ifreq)];
252     char **names;
253
254     msgNotify("Probing devices, please wait (this can take a while)...");
255     /* First go for the network interfaces.  Stolen shamelessly from ifconfig! */
256     ifc.ifc_len = sizeof(buffer);
257     ifc.ifc_buf = buffer;
258
259     s = socket(AF_INET, SOCK_DGRAM, 0);
260     if (s < 0)
261         goto skipif;    /* Jump over network iface probing */
262
263     if (ioctl(s, SIOCGIFCONF, (char *) &ifc) < 0)
264         goto skipif;    /* Jump over network iface probing */
265
266     close(s);
267     ifflags = ifc.ifc_req->ifr_flags;
268     end = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
269     for (ifptr = ifc.ifc_req; ifptr < end; ifptr++) {
270         char *descr;
271
272         /* If it's not a link entry, forget it */
273         if (ifptr->ifr_ifru.ifru_addr.sa_family != AF_LINK)
274             goto loopend;
275
276         /* Eliminate network devices that don't make sense */
277         if (!strncmp(ifptr->ifr_name, "lo", 2))
278             goto loopend;
279
280         /* If we have a slip device, don't register it */
281         if (!strncmp(ifptr->ifr_name, "sl", 2)) {
282             goto loopend;
283         }
284         /* And the same for ppp */
285         if (!strncmp(ifptr->ifr_name, "tun", 3) || !strncmp(ifptr->ifr_name, "ppp", 3)) {
286             goto loopend;
287         }
288         /* Try and find its description */
289         for (i = 0, descr = NULL; device_names[i].name; i++) {
290             int len = strlen(device_names[i].name);
291
292             if (!ifptr->ifr_name || !ifptr->ifr_name[0])
293                 continue;
294             else if (!strncmp(ifptr->ifr_name, device_names[i].name, len)) {
295                 descr = device_names[i].description;
296                 break;
297             }
298         }
299         if (!descr)
300             descr = "<unknown network interface type>";
301
302         deviceRegister(ifptr->ifr_name, descr, strdup(ifptr->ifr_name), DEVICE_TYPE_NETWORK, TRUE,
303                        mediaInitNetwork, NULL, mediaShutdownNetwork, NULL);
304         if (isDebug())
305             msgDebug("Found a network device named %s\n", ifptr->ifr_name);
306         close(s);
307         if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
308             continue;
309
310 loopend:
311         if (ifptr->ifr_addr.sa_len)     /* I'm not sure why this is here - it's inherited */
312             ifptr = (struct ifreq *)((caddr_t)ifptr + ifptr->ifr_addr.sa_len - sizeof(struct sockaddr));
313         close(s);
314     }
315
316 skipif:
317     /* Next, try to find all the types of devices one might need
318      * during the second stage of the installation.
319      */
320     for (i = 0; device_names[i].name; i++) {
321         for (j = 0; j < device_names[i].max; j++) {
322             char try[FILENAME_MAX];
323
324             switch(device_names[i].type) {
325             case DEVICE_TYPE_CDROM:
326                 fd = deviceTry(device_names[i], try, j);
327                 if (fd >= 0 || errno == EBUSY) {        /* EBUSY if already mounted */
328                     char n[BUFSIZ];
329
330                     if (fd >= 0) close(fd);
331                     snprintf(n, sizeof n, device_names[i].name, j);
332                     deviceRegister(strdup(n), device_names[i].description, strdup(try),
333                                          DEVICE_TYPE_CDROM, TRUE, mediaInitCDROM, mediaGetCDROM,
334                                          mediaShutdownCDROM, NULL);
335                     if (isDebug())
336                         msgDebug("Found a CDROM device for %s\n", try);
337                 }
338                 break;
339
340             case DEVICE_TYPE_TAPE:
341                 fd = deviceTry(device_names[i], try, j);
342                 if (fd >= 0) {
343                     char n[BUFSIZ];
344
345                     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_TAPE, TRUE, mediaInitTape, mediaGetTape, mediaShutdownTape, NULL);
349                     if (isDebug())
350                         msgDebug("Found a TAPE device for %s\n", try);
351                 }
352                 break;
353
354             case DEVICE_TYPE_DISK:
355                 /* nothing to do */
356                 break;
357
358             case DEVICE_TYPE_FLOPPY:
359                 fd = deviceTry(device_names[i], try, j);
360                 if (fd >= 0) {
361                     char n[BUFSIZ];
362
363                     close(fd);
364                     snprintf(n, sizeof n, device_names[i].name, j);
365                     deviceRegister(strdup(n), device_names[i].description, strdup(try),
366                                    DEVICE_TYPE_FLOPPY, TRUE, mediaInitFloppy, mediaGetFloppy,
367                                    mediaShutdownFloppy, NULL);
368                     if (isDebug())
369                         msgDebug("Found a floppy device for %s\n", try);
370                 }
371                 break;
372
373             case DEVICE_TYPE_NETWORK:
374                 fd = deviceTry(device_names[i], try, j);
375                 /* The only network devices that you can open this way are serial ones */
376                 if (fd >= 0) {
377                     char *newdesc, *cp;
378
379                     close(fd);
380                     cp = device_names[i].description;
381                     /* Serial devices get a slip and ppp device each, if supported */
382                     newdesc = safe_malloc(strlen(cp) + 40);
383                     sprintf(newdesc, cp, "SLIP interface", try, j + 1);
384                     deviceRegister("sl0", newdesc, strdup(try), DEVICE_TYPE_NETWORK, TRUE, mediaInitNetwork,
385                                    NULL, mediaShutdownNetwork, NULL);
386                     msgDebug("Add mapping for %s to sl0\n", try);
387                     newdesc = safe_malloc(strlen(cp) + 50);
388                     sprintf(newdesc, cp, "PPP interface", try, j + 1);
389                     deviceRegister("ppp0", newdesc, strdup(try), DEVICE_TYPE_NETWORK, TRUE, mediaInitNetwork,
390                                    NULL, mediaShutdownNetwork, NULL);
391                     if (isDebug())
392                         msgDebug("Add mapping for %s to ppp0\n", try);
393                 }
394                 break;
395
396             default:
397                 break;
398             }
399         }
400     }
401
402     /* Finally, go get the disks and look for DOS partitions to register */
403     if ((names = Disk_Names()) != NULL) {
404         int i;
405
406         for (i = 0; names[i]; i++) {
407             Chunk *c1;
408             Disk *d;
409
410             /* Ignore memory disks */
411             if (!strncmp(names[i], "md", 2))
412                 continue;
413
414             /*
415              * XXX 
416              *  Due to unknown reasons, Disk_Names() returns SCSI CDROM as a
417              * valid disk. This is main reason why sysinstall presents SCSI
418              * CDROM to available disks in Fdisk/Label menu. In addition,
419              * adding a blank SCSI CDROM to the menu generates floating point
420              * exception in sparc64. Disk_Names() just extracts sysctl
421              * "kern.disks". Why GEOM treats SCSI CDROM as a disk is beyond
422              * me and that should be investigated.
423              * For temporary workaround, ignore SCSI CDROM device.
424              */
425             if (!strncmp(names[i], "cd", 2))
426                 continue;
427
428             d = Open_Disk(names[i]);
429             if (!d) {
430                 msgDebug("Unable to open disk %s\n", names[i]);
431                 continue;
432             }
433
434             deviceRegister(names[i], names[i], d->name, DEVICE_TYPE_DISK, FALSE,
435                            dummyInit, dummyGet, dummyShutdown, d);
436             if (isDebug())
437                 msgDebug("Found a disk device named %s\n", names[i]);
438
439             /* Look for existing DOS partitions to register as "DOS media devices" */
440             for (c1 = d->chunks->part; c1; c1 = c1->next) {
441                 if (c1->type == fat || c1->type == efi || c1->type == extended) {
442                     Device *dev;
443                     char devname[80];
444
445                     /* Got one! */
446                     snprintf(devname, sizeof devname, "/dev/%s", c1->name);
447                     dev = deviceRegister(c1->name, c1->name, strdup(devname), DEVICE_TYPE_DOS, TRUE,
448                                          mediaInitDOS, mediaGetDOS, mediaShutdownDOS, NULL);
449                     dev->private = c1;
450                     if (isDebug())
451                         msgDebug("Found a DOS partition %s on drive %s\n", c1->name, d->name);
452                 }
453             }
454         }
455         free(names);
456     }
457     dialog_clear_norefresh();
458 }
459
460 /* Rescan all devices, after closing previous set - convenience function */
461 void
462 deviceRescan(void)
463 {
464     deviceReset();
465     deviceGetAll();
466 }
467
468 /*
469  * Find all devices that match the criteria, allowing "wildcarding" as well
470  * by allowing NULL or ANY values to match all.  The array returned is static
471  * and may be used until the next invocation of deviceFind().
472  */
473 Device **
474 deviceFind(char *name, DeviceType class)
475 {
476     static Device *found[DEV_MAX];
477     int i, j;
478
479     j = 0;
480     for (i = 0; i < numDevs; i++) {
481         if ((!name || !strcmp(Devices[i]->name, name))
482             && (class == DEVICE_TYPE_ANY || class == Devices[i]->type))
483             found[j++] = Devices[i];
484     }
485     found[j] = NULL;
486     return j ? found : NULL;
487 }
488
489 Device **
490 deviceFindDescr(char *name, char *desc, DeviceType class)
491 {
492     static Device *found[DEV_MAX];
493     int i, j;
494
495     j = 0;
496     for (i = 0; i < numDevs; i++) {
497         if ((!name || !strcmp(Devices[i]->name, name)) &&
498             (!desc || !strcmp(Devices[i]->description, desc)) &&
499             (class == DEVICE_TYPE_ANY || class == Devices[i]->type))
500             found[j++] = Devices[i];
501     }
502     found[j] = NULL;
503     return j ? found : NULL;
504 }
505
506 int
507 deviceCount(Device **devs)
508 {
509     int i;
510
511     if (!devs)
512         return 0;
513     for (i = 0; devs[i]; i++);
514     return i;
515 }
516
517 /*
518  * Create a menu listing all the devices of a certain type in the system.
519  * The passed-in menu is expected to be a "prototype" from which the new
520  * menu is cloned.
521  */
522 DMenu *
523 deviceCreateMenu(DMenu *menu, DeviceType type, int (*hook)(dialogMenuItem *d), int (*check)(dialogMenuItem *d))
524 {
525     Device **devs;
526     int numdevs;
527     DMenu *tmp = NULL;
528     int i, j;
529
530     devs = deviceFind(NULL, type);
531     numdevs = deviceCount(devs);
532     if (!numdevs)
533         return NULL;
534     tmp = (DMenu *)safe_malloc(sizeof(DMenu) + (sizeof(dialogMenuItem) * (numdevs + 1)));
535     bcopy(menu, tmp, sizeof(DMenu));
536     for (i = 0; devs[i]; i++) {
537         tmp->items[i].prompt = devs[i]->name;
538         for (j = 0; j < numDevs; j++) {
539             if (devs[i] == Devices[j]) {
540                 tmp->items[i].title = Devices[j]->description;
541                 break;
542             }
543         }
544         if (j == numDevs)
545             tmp->items[i].title = "<unknown device type>";
546         tmp->items[i].fire = hook;
547         tmp->items[i].checked = check;
548     }
549     tmp->items[i].title = NULL;
550     return tmp;
551 }