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