Fixed out-by-one error in previous commit.
[AROS.git] / workbench / c / HDTool / list.c
blob7e28bdc5c745273690ecf0470ca840421e622904
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <proto/dos.h>
5 #include <proto/exec.h>
6 #include <proto/partition.h>
7 #include <libraries/partition.h>
9 #include "list.h"
10 #include "deviceio.h"
12 char *listtemplate = "DEVICE/K/A,UNIT/K/N,PARTITION/K";
13 struct PartitionBase *PartitionBase;
15 /************************* list partition **********************************/
16 BOOL existsAttr(ULONG *list, ULONG attr) {
18 while (*list)
20 if (*list == attr)
21 return TRUE;
22 list++;
24 return FALSE;
27 void printPartitionInfo(struct PartitionHandle *ph) {
28 ULONG *pattr;
29 struct TagItem tags[2];
31 pattr = (void *)QueryPartitionAttrs(ph->root);
32 tags[1].ti_Tag = TAG_DONE;
33 /* get size */
35 struct DosEnvec de;
36 tags[0].ti_Tag = PT_DOSENVEC;
37 tags[0].ti_Data = (STACKIPTR)&de;
38 GetPartitionAttrs(ph, tags);
39 printf
41 "size: %lld\n",
42 (long long)(
43 (QUAD)(de.de_HighCyl-de.de_LowCyl+1)*de.de_Surfaces*
44 (QUAD)de.de_BlocksPerTrack*(de.de_SizeBlock<<2)
48 if (existsAttr(pattr, PTA_TYPE))
50 struct PartitionType type;
51 WORD i;
53 tags[0].ti_Tag = PT_TYPE;
54 tags[0].ti_Data = (STACKIPTR)&type;
55 GetPartitionAttrs(ph, tags);
56 printf("type: 0x");
57 for (i=0;i<type.id_len;i++)
58 printf("%02x", type.id[i]);
59 printf("\n");
61 if (existsAttr(pattr, PTA_POSITION))
63 ULONG pos;
65 tags[0].ti_Tag = PT_POSITION;
66 tags[0].ti_Data = (STACKIPTR)&pos;
67 GetPartitionAttrs(ph, tags);
68 printf("position: %lu\n", (unsigned long)pos);
70 if (existsAttr(pattr, PTA_ACTIVE))
72 ULONG active;
74 tags[0].ti_Tag = PT_ACTIVE;
75 tags[0].ti_Data = (STACKIPTR)&active;
76 GetPartitionAttrs(ph, tags);
77 printf("active: %lu\n", (unsigned long)active);
79 if (existsAttr(pattr, PTA_NAME))
81 UBYTE name[32];
83 tags[0].ti_Tag = PT_NAME;
84 tags[0].ti_Data = (STACKIPTR)name;
85 GetPartitionAttrs(ph, tags);
86 printf("name: %s\n", name);
88 if (existsAttr(pattr, PTA_BOOTABLE))
90 ULONG ba;
92 tags[0].ti_Tag = PT_ACTIVE;
93 tags[0].ti_Data = (STACKIPTR)&ba;
94 GetPartitionAttrs(ph, tags);
95 printf("bootable: %lu\n", (unsigned long)ba);
97 if (existsAttr(pattr, PTA_AUTOMOUNT))
99 ULONG am;
101 tags[0].ti_Tag = PT_ACTIVE;
102 tags[0].ti_Data = (STACKIPTR)&am;
103 GetPartitionAttrs(ph, tags);
104 printf("automount: %lu\n", (unsigned long)am);
108 LONG nextPartitionTable(struct PartitionHandle *part, STRPTR partition) {
109 LONG retval = RETURN_FAIL;
110 LONG pnum;
111 STRPTR newpos;
112 struct PartitionHandle *ph;
114 pnum=strtol(partition, (char **)&newpos, 0);
115 if (pnum>=0)
117 if (newpos != partition)
119 if (OpenPartitionTable(part)==0)
121 ph = (struct PartitionHandle *)part->table->list.lh_Head;
122 while (ph->ln.ln_Succ)
124 if (pnum == 0)
125 break;
126 pnum--;
127 ph = (struct PartitionHandle *)ph->ln.ln_Succ;
129 if (ph->ln.ln_Succ)
131 if (*newpos == ',')
132 newpos++;
133 if (*newpos == 0)
135 printPartitionInfo(ph);
136 retval = RETURN_OK;
138 else
140 retval=nextPartitionTable(ph, newpos);
143 else
144 printf("partition not found\n");
145 ClosePartitionTable(part);
147 else
148 printf("no partition table\n");
150 else
151 PrintFault(ERROR_BAD_NUMBER, NULL);
153 else
154 PrintFault(ERROR_BAD_NUMBER, NULL);
155 return retval;
158 LONG listPartition(STRPTR device, ULONG unit, STRPTR partition) {
159 LONG retval = RETURN_FAIL;
160 struct PartitionHandle *ph;
162 PartitionBase = (struct PartitionBase *)OpenLibrary("partition.library", 1);
163 if (PartitionBase)
165 ph = OpenRootPartition(device, unit);
166 if (ph)
168 retval = nextPartitionTable(ph, partition);
169 CloseRootPartition(ph);
171 else
172 printf("Could not open root partition on device %s unit %lu\n", device, (unsigned long)unit);
173 CloseLibrary((struct Library *)PartitionBase);
175 return retval;
178 /************************* list partitions *********************************/
179 void printTable(struct PartitionHandle *root, WORD depth) {
180 struct PartitionHandle *ph;
181 struct TagItem tags[2];
182 ULONG type;
183 WORD i,j;
185 tags[1].ti_Tag = TAG_DONE;
186 if (OpenPartitionTable(root) == 0)
188 tags[0].ti_Tag = PTT_TYPE;
189 tags[0].ti_Data = (STACKIPTR)&type;
190 GetPartitionTableAttrs(root, tags);
191 for (i=0;i<depth;i++) printf("\t");
192 printf("Partition table type = %lu\n", (unsigned long)type);
193 ph = (struct PartitionHandle *)root->table->list.lh_Head;
194 j = 0;
195 while (ph->ln.ln_Succ)
197 struct PartitionType ptype;
199 tags[0].ti_Tag = PT_TYPE;
200 tags[0].ti_Data = (IPTR)&ptype;
201 GetPartitionAttrs(ph, tags);
202 for (i=0;i<(depth+1);i++) printf("\t");
203 printf("Partition %d: type = 0x", j);
204 for (i=0;i<ptype.id_len;i++)
205 printf("%02x", ptype.id[i]);
206 printf("\n");
207 printTable(ph, depth+1);
208 j++;
209 ph = (struct PartitionHandle *)ph->ln.ln_Succ;
211 ClosePartitionTable(root);
215 BOOL listPartitions(STRPTR device, ULONG unit) {
216 BOOL retval = RETURN_FAIL;
217 struct PartitionHandle *ph;
219 PartitionBase = (struct PartitionBase *)OpenLibrary("partition.library", 1);
220 if (PartitionBase)
222 ph = OpenRootPartition(device, unit);
223 if (ph)
225 printTable(ph, 0);
226 retval = RETURN_OK;
227 CloseRootPartition(ph);
229 CloseLibrary((struct Library *)PartitionBase);
231 return retval;
234 /***************************** list HDs ************************************/
235 BOOL printHD(STRPTR device, ULONG unit) {
236 BOOL retval=FALSE;
237 char id[64];
238 struct DeviceIO dio;
240 if (openIO(&dio, device, unit))
242 if (iscorrectType(dio.iotd))
244 printf("\tUnit=%lu: ", (unsigned long)unit);
245 if (identify(dio.iotd, id))
246 printf("id\n");
247 else
248 printf("unknown\n");
250 closeIO(&dio);
251 retval = TRUE;
253 return retval;
256 LONG listHDs(STRPTR device) {
257 LONG retval = RETURN_OK;
258 WORD i,max;
260 if (strcmp(device, "ide.device") == 0)
261 max=4;
262 else if (strcmp(device, "scsi.devce") == 0)
263 max=6;
264 else
265 max=1;
266 printf("%s\n", device);
267 for (i=0;i<max;i++)
269 if (!printHD(device, i))
271 retval = RETURN_FAIL;
272 break;
275 return retval;
278 LONG list(char *name, STRPTR args) {
279 BOOL retval = RETURN_FAIL;
280 IPTR myargs[]={0,0,0,0};
281 struct RDArgs *rdargs;
282 struct RDArgs rda = {{args, strlen(args), 0}, 0, 0, 0, NULL, 0};
284 rda.RDA_Source.CS_Buffer[rda.RDA_Source.CS_Length]='\n';
285 rdargs = ReadArgs(listtemplate,myargs, &rda);
286 if (rdargs)
288 if (myargs[2])
290 retval = listPartition
292 (STRPTR)myargs[0],
293 *(LONG *)myargs[1],
294 (STRPTR)myargs[2]
297 else
299 if (myargs[1])
300 retval = listPartitions((STRPTR)myargs[0], *(LONG *)myargs[1]);
301 else
302 retval = listHDs((STRPTR)myargs[0]);
304 FreeArgs(rdargs);
306 else
307 PrintFault(IoErr(), name);
308 return retval;