conditionally include <machine/types.h>
[platform/upstream/rpm.git] / lib / rpmlead.c
1 #ifdef HAVE_MACHINE_TYPES_H
2 # include <machine/types.h>
3 #endif
4
5 #include <netinet/in.h>
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <unistd.h>
9 #include <string.h>
10 #include <errno.h>
11
12 #include "rpmerr.h"
13 #include "rpmlead.h"
14
15 /* The lead needs to be 8 byte aligned */
16
17 int writeLead(int fd, struct rpmlead *lead)
18 {
19     struct rpmlead l;
20
21     memcpy(&l, lead, sizeof(*lead));
22     
23     l.magic[0] = RPMLEAD_MAGIC0;
24     l.magic[1] = RPMLEAD_MAGIC1;
25     l.magic[2] = RPMLEAD_MAGIC2;
26     l.magic[3] = RPMLEAD_MAGIC3;
27
28     l.type = htons(l.type);
29     l.archnum = htons(l.archnum);
30     l.osnum = htons(l.osnum);
31     l.signature_type = htons(l.signature_type);
32         
33     write(fd, &l, sizeof(l));
34
35     return 0;
36 }
37
38 int readLead(int fd, struct rpmlead *lead)
39 {
40     if (read(fd, lead, sizeof(*lead)) != sizeof(*lead)) {
41         error(RPMERR_READERROR, "read failed: %s (%d)", strerror(errno), 
42               errno);
43         return 1;
44     }
45
46     lead->type = ntohs(lead->type);
47     lead->archnum = ntohs(lead->archnum);
48     lead->osnum = ntohs(lead->osnum);
49
50     if (lead->major >= 2)
51         lead->signature_type = ntohs(lead->signature_type);
52
53     return 0;
54 }
55