Upload Tizen:Base source
[framework/base/util-linux-ng.git] / fdisk / partitiontype.c
1 /*
2  * partitiontype.c, aeb, 2001-09-10
3  *
4  * call: partitiontype device
5  *
6  * either exit(1), or exit(0) with a single line of output
7  * DOS: sector 0 has a DOS signature.
8  */
9 #include <stdio.h>
10 #include <fcntl.h>
11 #include <stdlib.h>
12
13 struct aix_label {
14         unsigned int   magic;
15         /* more ... */
16 };
17
18 #define AIX_LABEL_MAGIC         0xc9c2d4c1
19 #define AIX_LABEL_MAGIC_SWAPPED 0xc1d4c2c9
20
21 struct bsd_label {
22         unsigned int   magic;
23         unsigned char  stuff[128];
24         unsigned int   magic2;
25         /* more ... */
26 };
27
28 #define BSD_LABEL_MAGIC         0x82564557
29
30 struct sgi_label {
31         unsigned int   magic;
32         /* more ... */
33 };
34
35 #define SGI_LABEL_MAGIC         0x0be5a941
36 #define SGI_LABEL_MAGIC_SWAPPED 0x41a9e50b
37
38 struct sun_label {
39         unsigned char stuff[508];
40         unsigned short magic;      /* Magic number */
41         unsigned short csum;       /* Label xor'd checksum */
42 };
43
44 #define SUN_LABEL_MAGIC          0xDABE
45 #define SUN_LABEL_MAGIC_SWAPPED  0xBEDA
46
47 int
48 main(int argc, char **argv) {
49         int fd, n;
50         unsigned char buf[1024];
51         struct aix_label *paix;
52         struct bsd_label *pbsd;
53         struct sgi_label *psgi;
54         struct sun_label *psun;
55
56         if (argc != 2) {
57                 fprintf(stderr, "call: %s device\n", argv[0]);
58                 exit(1);
59         }
60         fd = open(argv[1], O_RDONLY);
61         if (fd == -1) {
62                 perror(argv[1]);
63                 fprintf(stderr, "%s: cannot open device %s\n",
64                         argv[0], argv[1]);
65                 exit(1);
66         }
67         n = read(fd, buf, sizeof(buf));
68         if (n != sizeof(buf)) {
69                 if (n == -1)
70                         perror(argv[1]);
71                 fprintf(stderr, "%s: cannot read device %s\n",
72                         argv[0], argv[1]);
73                 exit(1);
74         }
75
76         psun = (struct sun_label *)(&buf);
77         if (psun->magic == SUN_LABEL_MAGIC ||
78             psun->magic == SUN_LABEL_MAGIC_SWAPPED) {
79                 unsigned short csum = 0, *p;
80                 int i;
81
82                 for (p = (unsigned short *)(&buf);
83                      p < (unsigned short *)(&buf[512]); p++)
84                         csum ^= *p;
85
86                 if (csum == 0) {
87                         printf("SUN\n");
88                         exit(0);
89                 }
90         }
91
92         pbsd = (struct bsd_label *)(&buf[512]);
93         if (pbsd->magic == BSD_LABEL_MAGIC &&
94             pbsd->magic2 == BSD_LABEL_MAGIC) {
95                 printf("BSD\n");
96                 exit(0);
97         }
98
99         pbsd = (struct bsd_label *)(&buf[64]);
100         if (pbsd->magic == BSD_LABEL_MAGIC &&
101             pbsd->magic2 == BSD_LABEL_MAGIC) {
102                 printf("BSD\n");
103                 exit(0);
104         }
105
106         paix = (struct aix_label *)(&buf);
107         if (paix->magic == AIX_LABEL_MAGIC ||
108             paix->magic == AIX_LABEL_MAGIC_SWAPPED) {
109                 printf("AIX\n");
110                 exit(0);
111         }
112
113         psgi = (struct sgi_label *)(&buf);
114         if (psgi->magic == SGI_LABEL_MAGIC ||
115             psgi->magic == SGI_LABEL_MAGIC_SWAPPED) {
116                 printf("SGI\n");
117                 exit(0);
118         }
119
120         if (buf[510] == 0x55 && buf[511] == 0xaa) {
121                 printf("DOS\n");
122                 exit(0);
123         }
124 #if 0
125         fprintf(stderr, "%s: do not recognize any label on %s\n",
126                 argv[0], argv[1]);
127 #endif
128         exit(1);                /* unknown */
129 }
130