use non-zero data
[tridge/junkcode.git] / drivenames.c
1
2 #include <stdio.h>
3 #include <sys/types.h>
4 #include <dirent.h>
5
6 /*
7   find a list of drive names on this machine
8   return the number of drives found, return the names as 
9   a string array in **names
10 */
11 static int find_drive_names(char ***names)
12 {
13         DIR *dir;
14         struct dirent *de;
15         int count = 0;
16
17         dir = opendir("/proc/ide");
18         if (!dir) {
19                 perror("/proc/ide");
20                 return 0;
21         }
22
23         (*names) = NULL;
24
25         while ((de = (readdir(dir)))) {
26                 /* /proc/ide contains more than just a list of drives. We need
27                    to select only those bits that match 'hd*' */
28                 if (strncmp(de->d_name, "hd", 2) == 0) {
29                         /* found one */
30                         (*names) = realloc(*names, sizeof(char *) * (count+2));
31                         if (! (*names)) {
32                                 fprintf(stderr,"Out of memory in find_drive_names\n");
33                                 return 0;
34                         }
35                         asprintf(&(*names)[count],"/dev/%s", de->d_name);
36                         count++;
37                 }
38         }
39
40         if (count != 0) {
41                 (*names)[count] = NULL;
42         }
43         
44         closedir(dir);
45         return count;
46 }
47
48 main()
49 {
50         char **names;
51         int n, i;
52
53         n = find_drive_names(&names);
54
55         for (i=0;i<n;i++) {
56                 printf("%s\n", names[i]);
57         }
58         
59 }