*** empty log message ***
[platform/upstream/rpm.git] / lib / formats.c
1 #include "config.h"
2
3 #include <stdlib.h>
4 #include <string.h>
5 #include <sys/stat.h>
6
7 #include "header.h"
8 #include "rpmlib.h"
9
10 static char * permsFormat(int_32 type, const void * data, 
11                           char * formatPrefix, int padding, int element);
12 static char * depflagsFormat(int_32 type, const void * data, 
13                              char * formatPrefix, int padding, int element);
14 static char * fflagsFormat(int_32 type, const void * data, 
15                            char * formatPrefix, int padding, int element);
16 static char * permsString(int mode);
17
18 const struct headerSprintfExtension rpmHeaderFormats[] = {
19     { HEADER_EXT_FORMAT, "depflags", { depflagsFormat } },
20     { HEADER_EXT_FORMAT, "fflags", { fflagsFormat } },
21     { HEADER_EXT_FORMAT, "perms", { permsFormat } },
22     { HEADER_EXT_FORMAT, "permissions", { permsFormat } },
23     { HEADER_EXT_MORE, NULL, { (void *) headerDefaultFormats } }
24 } ;
25
26 static char * permsString(int mode) {
27     char * perms = malloc(11);
28
29     strcpy(perms, "-----------");
30    
31     if (mode & S_ISVTX) perms[10] = 't';
32
33     if (mode & S_IRUSR) perms[1] = 'r';
34     if (mode & S_IWUSR) perms[2] = 'w';
35     if (mode & S_IXUSR) perms[3] = 'x';
36  
37     if (mode & S_IRGRP) perms[4] = 'r';
38     if (mode & S_IWGRP) perms[5] = 'w';
39     if (mode & S_IXGRP) perms[6] = 'x';
40
41     if (mode & S_IROTH) perms[7] = 'r';
42     if (mode & S_IWOTH) perms[8] = 'w';
43     if (mode & S_IXOTH) perms[9] = 'x';
44
45     if (mode & S_ISUID) {
46         if (mode & S_IXUSR) 
47             perms[3] = 's'; 
48         else
49             perms[3] = 'S'; 
50     }
51
52     if (mode & S_ISGID) {
53         if (mode & S_IXGRP) 
54             perms[6] = 's'; 
55         else
56             perms[6] = 'S'; 
57     }
58
59     if (S_ISDIR(mode)) 
60         perms[0] = 'd';
61     else if (S_ISLNK(mode)) {
62         perms[0] = 'l';
63     }
64     else if (S_ISFIFO(mode)) 
65         perms[0] = 'p';
66     else if (S_ISSOCK(mode)) 
67         perms[0] = 'l';
68     else if (S_ISCHR(mode)) {
69         perms[0] = 'c';
70     } else if (S_ISBLK(mode)) {
71         perms[0] = 'b';
72     }
73
74     return perms;
75 }
76
77 static char * permsFormat(int_32 type, const void * data, 
78                          char * formatPrefix, int padding, int element) {
79     char * val;
80     char * buf;
81
82     if (type != RPM_INT32_TYPE) {
83         val = malloc(20);
84         strcpy(val, "(not a number)");
85     } else {
86         val = malloc(15 + padding);
87         strcat(formatPrefix, "s");
88         buf = permsString(*((int_32 *) data));
89         sprintf(val, formatPrefix, buf);
90         free(buf);
91     }
92
93     return val;
94 }
95
96 static char * fflagsFormat(int_32 type, const void * data, 
97                          char * formatPrefix, int padding, int element) {
98     char * val;
99     char buf[10];
100     int anint = *((int_32 *) data);
101
102     if (type != RPM_INT32_TYPE) {
103         val = malloc(20);
104         strcpy(val, "(not a number)");
105     } else {
106         buf[0] = '\0';
107         if (anint & RPMFILE_DOC)
108             strcat(buf, "d");
109         if (anint & RPMFILE_CONFIG)
110             strcat(buf, "c");
111
112         val = malloc(5 + padding);
113         strcat(formatPrefix, "s");
114         sprintf(val, formatPrefix, buf);
115     }
116
117     return val;
118 }
119
120 static char * depflagsFormat(int_32 type, const void * data, 
121                          char * formatPrefix, int padding, int element) {
122     char * val;
123     char buf[10];
124     int anint = *((int_32 *) data);
125
126     if (type != RPM_INT32_TYPE) {
127         val = malloc(20);
128         strcpy(val, "(not a number)");
129     } else {
130         *buf = '\0';
131
132         if (anint & RPMSENSE_LESS) 
133             strcat(buf, "<");
134         if (anint & RPMSENSE_GREATER)
135             strcat(buf, ">");
136         if (anint & RPMSENSE_EQUAL)
137             strcat(buf, "=");
138         if (anint & RPMSENSE_SERIAL)
139             strcat(buf, "S");
140
141         val = malloc(5 + padding);
142         strcat(formatPrefix, "s");
143         sprintf(val, formatPrefix, buf);
144     }
145
146     return val;
147 }