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