int diocntl(dev_t device, int request, struct part_geom *entry) /* Get or set the geometry of a device. */ { char *name; int r, f, err; name= finddev(device); if ((f= open(name, O_RDONLY)) < 0) return -1; r= ioctl(f, request == DSETP ? DIOCSETP : DIOCGETP, (void *) entry); err= errno; (void) close(f); errno= err; return r; }
/* * Get device. * Prompt the user to enter either a device name or an equipment ordinal. * Validate the entry. */ int getdev( char *name) /* Device name or ordinal */ { Mvprintw(LINES - 1, 0, catgets(catfd, SET, 873, "Device name:")); (void) Clrtoeol(); cbreak(); echo(); getstr(name); noecho(); if (*name != '\0') DisEq = finddev(name); return (DisEq); }
static struct usb_device *finddev(struct usb_device *dev, int devnr) { unsigned int i; struct usb_device *d2; if (!dev) return NULL; if (dev->devnum == devnr) return dev; for (i = 0; i < dev->maxchild; i++) { if (!dev->children[i]) continue; if ((d2 = finddev(dev->children[i], devnr))) return d2; } return NULL; }
/* * Get robot. * Prompt the user to enter either a device name or an equipment ordinal. * Validate the entry as a robot. */ int getrobot(char *name) { dev_ent_t *dev; /* Device entry */ int n; Mvprintw(LINES - 1, 0, catgets(catfd, SET, 2139, "Robot name:")); (void) Clrtoeol(); cbreak(); echo(); getstr(name); noecho(); if (*name == '\0') return (DisRb); n = finddev(name); dev = (dev_ent_t *)SHM_ADDR(master_shm, Dev_Tbl->d_ent[n]); if ((dev->type & DT_CLASS_MASK) != DT_ROBOT) { Error(catgets(catfd, SET, 874, "Device not a robot (%s)."), name); } DisRb = (equ_t)n; return (n); }
static struct usb_device *usbdevfs_finddevice(struct usb_bus *bus, int devnr) { return finddev(bus->root_hub, devnr); }
int main(int argc, char **argv) { struct stat hdst; struct part_geom whole, entry; struct part_entry table[4], *pe; int drive, par = 0, device, incr; int partf; char *table_file; int hd_major, hd_minor; int needsort; int shrink; /* True if partitions are shrinked to fit. */ unsigned long base, size, limit; if ((arg0= strrchr(argv[0], '/')) == nil) arg0= argv[0]; else arg0++; if (argc < 2 || argc > 3) { fprintf(stderr, "Usage: %s device [partition-file]\n", arg0); exit(1); } dev_file= argv[1]; table_file= argv[argc - 1]; shrink= (argc == 2); if (stat(dev_file, &hdst) < 0) fatal(dev_file); /* Geometry (to print nice numbers.) */ if (diocntl(hdst.st_rdev, DGETP, &geometry) < 0) fatal(dev_file); if (!S_ISBLK(hdst.st_mode)) { fprintf(stderr, "%s: %s is not a device\n", arg0, dev_file); exit(1); } hd_major= major(hdst.st_rdev); hd_minor= minor(hdst.st_rdev); if (hd_minor >= MINOR_d0p0s0) { errno= EINVAL; fatal(dev_file); } if (hd_major == major(DEV_FD0)) { /* HD is actually a floppy. */ if (hd_minor >= 4) { errno= EINVAL; fatal(dev_file); } device= hd_minor + (28 << 2); incr= 4; needsort= 0; } else if (hd_minor % (1 + NR_PARTITIONS) == 0) { /* Partitioning hd0, hd5, ... */ device= hd_minor + 1; incr= 1; needsort= 1; } else { /* Subpartitioning hd[1-4], hd[6-9], ... */ drive= hd_minor / (1 + NR_PARTITIONS); par= hd_minor % (1 + NR_PARTITIONS) - 1; device= MINOR_d0p0s0 + (drive * NR_PARTITIONS + par) * NR_PARTITIONS; if (device + NR_PARTITIONS - 1 > BYTE) { errno= EINVAL; fatal(dev_file); } incr= 1; needsort= 0; } /* Device is now the first of the minor devices to be repartitioned. */ /* Read the partition table from the boot block. */ if ((partf= open(table_file, O_RDONLY)) < 0 || lseek(partf, (off_t) PART_TABLE_OFF, SEEK_SET) == -1 || (par= read(partf, (char *) table, (int) sizeof(table))) < 0 ) fatal(table_file); if (par < sizeof(table)) { fprintf(stderr, "%s: %s does not contain a partition table\n", arg0, table_file); exit(1); } if (needsort) partsort(table); /* Show the geometry of the affected drive or partition. */ if (diocntl(hdst.st_rdev, DGETP, &whole) < 0) fatal(dev_file); /* Use sector numbers. */ base = whole.base / SECTOR_SIZE; size = whole.size / SECTOR_SIZE; limit = base + size; show_part(dev_file, base, size); /* Send the partition table entries to the device driver. */ for (par= 0; par < NR_PARTITIONS; par++, device+= incr) { pe = &table[par]; if (shrink && pe->size != 0) { /* Shrink the partition entry to fit within the * enclosing device just like the driver does. */ unsigned long part_limit= pe->lowsec + pe->size; if (part_limit < pe->lowsec) part_limit= limit; if (part_limit > limit) part_limit= limit; if (pe->lowsec < base) pe->lowsec= base; if (part_limit < pe->lowsec) part_limit= pe->lowsec; pe->size= part_limit - pe->lowsec; } entry.base= pe->lowsec * SECTOR_SIZE; entry.size= pe->size * SECTOR_SIZE; if (diocntl(makedev(hd_major, device), DSETP, &entry) < 0) fatal(dev_file); show_part(finddev(makedev(hd_major, device)), pe->lowsec, pe->size); } exit(0); }