static int scrub(int bootable) { unsigned int part_size; /* int major, minor, blocks; int bytesread; char done; char *dev; char *line; char *ptr; */ char *buf; char *diskdevice; FILE *fd; char *cmd = "format </dev/null | nawk '/[0-9]+. c[0-9]/ { print $2\"p0\"}'"; int i; // printf("Command is: %s\n",cmd); part_size = 2048; buf = malloc(part_size); memset(buf,'\0',part_size); // debug("after buffer memset\n"); if ((fd = popen(cmd, "r")) != NULL) fgets(buf, part_size, fd); (void) pclose(fd); diskdevice = buf; for(i=0;i<part_size;i++) if(diskdevice[i]=='\n'){ diskdevice[i]='\0'; } fprintf(stderr,"Disk Device is %s\n",diskdevice); nuke_it(diskdevice, bootable); return(0); }
static int scrub(int bootable) { unsigned int part_size; int fd; int major, minor, blocks; int bytesread; char done; char *buf; char *dev; char *diskdevice; char *line; char *ptr; part_size = 2048; done = 0; while (!done) { if ((buf = (char *)malloc(part_size)) == NULL) { perror("scrub:malloc failed"); return(-1); } if ((fd = open("/proc/partitions", O_RDONLY)) < 0) { perror("scrub:open failed for /proc/partitions"); return(-1); } bytesread = read(fd, buf, part_size); if (bytesread < 0) { perror("scrub:read failed for /proc/partitions"); return(-1); } if (bytesread < part_size) { done = 1; } else { free(buf); part_size = part_size * 2; } close(fd); } diskdevice = NULL; /* * eat the first two lines * * there is a two line header on the output of * /proc/partitions -- toss those lines, then do * the work */ ptr = buf; line = strsep(&ptr, "\n"); line = strsep(&ptr, "\n"); while ((line = strsep(&ptr, "\n")) != NULL) { if (strcmp(line, "") != 0) { major = atoi(strtok(line, " ")); minor = atoi(strtok(NULL, " ")); blocks = atoi(strtok(NULL, " ")); dev = strtok(NULL, " "); if (diskdevice == NULL) { diskdevice = strdup(dev); nuke_it(diskdevice, bootable); } else { if (strncmp(dev, diskdevice, strlen(diskdevice)) != 0) { free(diskdevice); diskdevice = strdup(dev); nuke_it(diskdevice, bootable); } } } } if (diskdevice != NULL) { free(diskdevice); } free(buf); return(0); }