New developer version 0.6.8; added select () function; added demonstrating example...
[ZeXOS.git] / kernel / utils / fs / fdisk.c
blob174cf2bf42a957b4696287d7ee4c0ee2efb5f2d5
1 /*
2 * ZeX/OS
3 * Copyright (C) 2007 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
4 * Copyright (C) 2009 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include <system.h>
22 #include <string.h>
23 #include <dev.h>
24 #include <partition.h>
26 extern dev_t dev_list;
27 extern partition_t partition_list;
28 extern char *argparse (char *cmd);
30 int fdisk (char *parm, unsigned len)
32 dev_t *dev;
34 if (len) {
35 char str[64];
37 if (len > 63)
38 return 0;
40 memcpy (str, parm, len);
41 str[len] = '\0';
43 char *a1 = argparse (str);
44 char *a2 = argparse (a1);
45 char *a3 = argparse (a2);
47 unsigned i;
48 for (i = 0; i < len; i ++) {
49 if (str[i] == ' ')
50 str[i] = '\0';
53 dev = dev_find (str);
55 if (!dev) {
56 printf ("fdisk: Device '%s' is not found\n", parm);
57 return 0;
60 int id = atoi (a1);
62 if (id < 0 || id > 3) {
63 printf ("fdisk: Wrong partition ID, values 0-3 are correct\n");
64 return 0;
67 int mbytes = atoi (a3);
69 if (!mbytes) {
70 printf ("fdisk: Wrong partition size, please set size in MBytes\n");
71 return 0;
74 ptable_t ntable[4];
76 /*ntable[0] = (ptable_t *) kmalloc (sizeof (ptable_t));
77 ntable[1] = (ptable_t *) kmalloc (sizeof (ptable_t));
78 ntable[2] = (ptable_t *) kmalloc (sizeof (ptable_t));
79 ntable[3] = (ptable_t *) kmalloc (sizeof (ptable_t));
81 if (!ntable[0] || !ntable[1] || !ntable[2] || !ntable[3])
82 return 0;*/
84 memset (&ntable[0], 0, sizeof (ptable_t));
85 memset (&ntable[1], 0, sizeof (ptable_t));
86 memset (&ntable[2], 0, sizeof (ptable_t));
87 memset (&ntable[3], 0, sizeof (ptable_t));
89 ntable[id].type = strtol (a2, 0, 16);
91 printf ("Partition %s%d (type: 0x%x; size: %dMBytes)\nDo you want to continue ? (y/N)\n", dev->devname, id, ntable[id].type, mbytes);
93 while (1) {
94 char c = getkey ();
96 if (c == 'y' || c == 'Y')
97 break;
98 if (c == 'n' || c == 'N')
99 return 0;
101 schedule ();
104 ntable[id].blocks = (mbytes * 1024 * 1024) / 512;
105 ntable[id].lba = 63;
107 partition_table_new (dev, (ptable_t *) &ntable);
109 printf ("fdisk: Partition %s%d (type: 0x%x; size: %dMBytes) was created\n", dev->devname, id, ntable[id].type, mbytes);
111 return 1;
114 for (dev = dev_list.next; dev != &dev_list; dev = dev->next) {
115 if (dev->attrib == DEV_ATTR_BLOCK) {
116 printf ("%s: %s\n", dev->devname, dev->desc);
118 unsigned l = strlen (dev->devname);
120 partition_t *p;
121 for (p = partition_list.next; p != &partition_list; p = p->next) {
122 if (!strncmp (p->name, dev->devname, l))
123 printf (" %c %s : %s\n", 192, p->name, p->fs->name);
126 printf ("\n");
130 return 1;