3
** Copyright (C) 2000,2001 by Bernhard Rosenkraenzer
5
** Contributions by A. Seigo and W. Bastian.
10
** This program is free software; you can redistribute it and/or modify
11
** it under the terms of the GNU General Public License as published by
12
** the Free Software Foundation; either version 2 of the License, or
13
** (at your option) any later version.
15
** This program is distributed in the hope that it will be useful,
16
** but WITHOUT ANY WARRANTY; without even the implied warranty of
17
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
** GNU General Public License for more details.
20
** You should have received a copy of the GNU General Public License
21
** along with this program in a file called COPYING; if not, write to
22
** the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
23
** MA 02110-1301, USA.
27
** Bug reports and questions can be sent to <kde-devel@kde.org>
38
StringList ptable::disklist()
40
/* The basics behind scanning for a disk are simple: If it can be *
41
* opened, it exists. *
42
* Using access() is not sufficient - that's successful if the device *
43
* node exists but there's no actual device. *
44
* Checking /proc/devices would work, but it wouldn't autoload modules*
45
* so chances are we'd be missing something. */
49
#define CHECK_DEV(d) if((fd=open(d, O_RDONLY))<0) { \
55
String dev="/dev/hd"; // IDE devices: /dev/hd[a-t]
56
for(char a='a'; a<='t'; a++) {
57
// We can't use finished for IDE drives - it's perfectly OK
58
// to have a /dev/hdc but no /dev/hdb.
62
dev="/dev/sd"; // SCSI disks 0-25
63
for(char a='a'; a<='z' && !finished; a++) {
66
for(char prefix='a'; prefix <='d' && !finished; prefix++) {
67
dev="/dev/sd" + prefix; // SCSI disks 26-127
68
for(char a='a'; (a<='x' || (a<='z' && prefix<'d')) && !finished; a++) {
73
dev="/dev/i2o/hd"; // I2O disks 0-25: /dev/i2o/hd[a-z]
74
for(char a='a'; a<='z' && !finished; a++) {
77
for(char prefix='a'; prefix <='d' && !finished; prefix++) {
78
dev="/dev/i2o/hd" + prefix; // I2O disks 26-127
79
for(char a='a'; (a<='x' || (a<='z' && prefix<'d')) && !finished; a++) {
84
dev="/dev/pd"; // Parallel port disks: /dev/pd[a-d]
85
for(char a='a'; a<='d' && !finished; a++) {
89
for(unsigned int i=0; i<=31 && !finished; i++) { // Software RAID
91
device.sprintf("/dev/md%u", i);
95
for(unsigned int i=0; i<=7; i++) { //Compaq Smart Array
96
for(unsigned int j=0; j<=15 && !finished; j++) {
98
device.sprintf("/dev/ida/c%ud%u", i, j);
103
for(unsigned int i=0; i<=7; i++) { // Mylex DAC960
104
for(unsigned int j=0; j<=31 && !finished; j++) {
106
device.sprintf("/dev/rd/c%ud%u", i, j);
111
dev="/dev/ed"; // MCA ESDI harddisks: /dev/ed[ab]
112
for(char a='a'; a<='b' && !finished; a++) {
116
dev="/dev/xd"; // XT (8-bit) harddisks: /dev/xd[ab]
117
for(char a='a'; a<='b' && !finished; a++) {
122
StringList ptable::partlist()
125
StringList d=disklist();
126
for(StringList::const_iterator it=d.begin(); it!=d.end(); it++) {
127
for(int i=1; i<32; i++) {
129
drive.sprintf("%s%u", (*it).cstr(), i);
130
int fd=open(drive, O_RDONLY);
133
if(read(fd, &test, 1)>0)
142
ptable::ptable(StringList const &disks)
147
for(StringList::const_iterator it=disks.begin(); it!=disks.end(); it++)
150
ptable::ptable(String const &disk)
157
void ptable::scandisk(String const &disk)
160
cmd.sprintf("fdisk -l %s 2>&1", (char*)disk);
161
FILE *fdisk=popen(cmd, "r");
162
char *buf=(char *) malloc(1024);
164
while(fgets(buf, 1024, fdisk)) {
165
if(strncmp("/dev/", buf, 5)==0) { // Partition entry
166
// We don't care about active vs. non-active partitions.
168
while(strchr(buf, '*')) *strchr(buf, '*')=' ';
169
// blanks are blanks...
170
while(strchr(buf, '\t')) *strchr(buf, '\t')=' ';
175
// And figure out where/if it's mounted
176
mountpt[dev]=mountpoint(dev);
177
// Lastly, get the partition type.
178
strcpy(buf, buf+strlen(buf)+1);
179
while(isspace(*buf)) strcpy(buf, buf+1);
180
strcpy(buf, strchr(buf, ' ')); // skip Start
181
while(isspace(*buf)) strcpy(buf, buf+1);
182
strcpy(buf, strchr(buf, ' ')); // skip End
183
while(isspace(*buf)) strcpy(buf, buf+1);
184
strcpy(buf, strchr(buf, ' ')); // skip Blocks
185
while(isspace(*buf)) strcpy(buf, buf+1);
186
id[dev]=strtol(buf, NULL, 16);
192
String ptable::mountpoint(String const &device, bool fstab_fallback)
194
char *buf=new char[1024];
195
FILE *f=fopen("/etc/mtab", "r");
197
while(fgets(buf, 1024, f)) {
201
*strchr(buf, '\t')=0;
202
if(device.cmp(buf)) {
203
strcpy(buf, buf+strlen(buf)+1);
209
*strchr(buf, '\t')=0;
211
mp=mp.simplifyWhiteSpace();
216
if(mp.empty() && fstab_fallback) {
217
f=fopen("/etc/fstab", "r");
218
while(fgets(buf, 1024, f)) {
222
*strchr(buf, '\t')=0;
223
if(device.cmp(buf)) {
224
strcpy(buf, buf+strlen(buf)+1);
230
*strchr(buf, '\t')=0;
232
mp=mp.simplifyWhiteSpace();
241
String ptable::device(String const &mountpt, bool fstab_fallback)
243
char *buf=new char[1024];
244
FILE *f=fopen("/etc/mtab", "r");
246
while(fgets(buf, 1024, f)) {
250
*strchr(buf, '\t')=0;
252
strcpy(buf, buf+strlen(buf)+1);
258
*strchr(buf, '\t')=0;
260
mp=mp.simplifyWhiteSpace();
268
if(dev.empty() && fstab_fallback) {
269
// The FS is not mounted - maybe it could be, though.
270
f=fopen("/etc/fstab", "r");
271
while(fgets(buf, 1024, f)) {
275
*strchr(buf, '\t')=0;
277
strcpy(buf, buf+strlen(buf)+1);
283
*strchr(buf, '\t')=0;
285
mp=mp.simplifyWhiteSpace();