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