- dir2 = opendir (buf);
- if (!dir2) {
- reply_with_perror ("opendir: %s", buf);
- free_stringslen (r, size);
- return NULL;
- }
- while ((d = readdir (dir2)) != NULL) {
- if (strncmp (d->d_name, devname, strlen (devname)) == 0) {
- snprintf (buf, sizeof buf, "/dev/%s", d->d_name);
-
- if (add_string (&r, &size, &alloc, buf) == -1) {
- closedir (dir2);
- closedir (dir);
- return NULL;
- }
- }
- }
+ DIR *dir = opendir (devdir);
+ if (!dir) {
+ reply_with_perror ("opendir: %s", devdir);
+ free_stringslen (*r, *size);
+ return -1;
+ }
+
+ /* Look in /sys/block/<device>/ for entries starting with <device>
+ * e.g. /sys/block/sda/sda1
+ */
+ errno = 0;
+ struct dirent *d;
+ while ((d = readdir (dir)) != NULL) {
+ if (STREQLEN (d->d_name, device, strlen (device))) {
+ char part[256];
+ snprintf (part, sizeof part, "/dev/%s", d->d_name);