home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 February / CHIP_2_98.iso / misc / src / rpm / lib / formats.c < prev    next >
C/C++ Source or Header  |  1997-09-17  |  5KB  |  201 lines

  1. #include "config.h"
  2. #include "miscfn.h"
  3.  
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <sys/stat.h>
  7.  
  8. #include "header.h"
  9. #include "rpmlib.h"
  10.  
  11. static char * permsFormat(int_32 type, const void * data, 
  12.                   char * formatPrefix, int padding, int element);
  13. static char * depflagsFormat(int_32 type, const void * data, 
  14.                      char * formatPrefix, int padding, int element);
  15. static char * fflagsFormat(int_32 type, const void * data, 
  16.                    char * formatPrefix, int padding, int element);
  17. static int fsnamesTag(Header h, int_32 * type, void ** data, int_32 * count,
  18.               int * freeData);
  19. static int fssizesTag(Header h, int_32 * type, void ** data, int_32 * count,
  20.               int * freeData);
  21. static char * permsString(int mode);
  22.  
  23. const struct headerSprintfExtension rpmHeaderFormats[] = {
  24.     { HEADER_EXT_TAG, "RPMTAG_FSSIZES", { fssizesTag } },
  25.     { HEADER_EXT_TAG, "RPMTAG_FSNAMES", { fsnamesTag } },
  26.     { HEADER_EXT_FORMAT, "depflags", { depflagsFormat } },
  27.     { HEADER_EXT_FORMAT, "fflags", { fflagsFormat } },
  28.     { HEADER_EXT_FORMAT, "perms", { permsFormat } },
  29.     { HEADER_EXT_FORMAT, "permissions", { permsFormat } },
  30.     { HEADER_EXT_MORE, NULL, { (void *) headerDefaultFormats } }
  31. } ;
  32.  
  33. static char * permsString(int mode) {
  34.     char * perms = malloc(11);
  35.  
  36.     strcpy(perms, "-----------");
  37.    
  38.     if (mode & S_ISVTX) perms[10] = 't';
  39.  
  40.     if (mode & S_IRUSR) perms[1] = 'r';
  41.     if (mode & S_IWUSR) perms[2] = 'w';
  42.     if (mode & S_IXUSR) perms[3] = 'x';
  43.  
  44.     if (mode & S_IRGRP) perms[4] = 'r';
  45.     if (mode & S_IWGRP) perms[5] = 'w';
  46.     if (mode & S_IXGRP) perms[6] = 'x';
  47.  
  48.     if (mode & S_IROTH) perms[7] = 'r';
  49.     if (mode & S_IWOTH) perms[8] = 'w';
  50.     if (mode & S_IXOTH) perms[9] = 'x';
  51.  
  52.     if (mode & S_ISUID) {
  53.     if (mode & S_IXUSR) 
  54.         perms[3] = 's'; 
  55.     else
  56.         perms[3] = 'S'; 
  57.     }
  58.  
  59.     if (mode & S_ISGID) {
  60.     if (mode & S_IXGRP) 
  61.         perms[6] = 's'; 
  62.     else
  63.         perms[6] = 'S'; 
  64.     }
  65.  
  66.     if (S_ISDIR(mode)) 
  67.     perms[0] = 'd';
  68.     else if (S_ISLNK(mode)) {
  69.     perms[0] = 'l';
  70.     }
  71.     else if (S_ISFIFO(mode)) 
  72.     perms[0] = 'p';
  73.     else if (S_ISSOCK(mode)) 
  74.     perms[0] = 'l';
  75.     else if (S_ISCHR(mode)) {
  76.     perms[0] = 'c';
  77.     } else if (S_ISBLK(mode)) {
  78.     perms[0] = 'b';
  79.     }
  80.  
  81.     return perms;
  82. }
  83.  
  84. static char * permsFormat(int_32 type, const void * data, 
  85.                  char * formatPrefix, int padding, int element) {
  86.     char * val;
  87.     char * buf;
  88.  
  89.     if (type != RPM_INT32_TYPE) {
  90.     val = malloc(20);
  91.     strcpy(val, "(not a number)");
  92.     } else {
  93.     val = malloc(15 + padding);
  94.     strcat(formatPrefix, "s");
  95.     buf = permsString(*((int_32 *) data));
  96.     sprintf(val, formatPrefix, buf);
  97.     free(buf);
  98.     }
  99.  
  100.     return val;
  101. }
  102.  
  103. static char * fflagsFormat(int_32 type, const void * data, 
  104.                  char * formatPrefix, int padding, int element) {
  105.     char * val;
  106.     char buf[15];
  107.     int anint = *((int_32 *) data);
  108.  
  109.     if (type != RPM_INT32_TYPE) {
  110.     val = malloc(20);
  111.     strcpy(val, "(not a number)");
  112.     } else {
  113.     buf[0] = '\0';
  114.     if (anint & RPMFILE_DOC)
  115.         strcat(buf, "d");
  116.     if (anint & RPMFILE_CONFIG)
  117.         strcat(buf, "c");
  118.     if (anint & RPMFILE_SPECFILE)
  119.         strcat(buf, "s");
  120.     if (anint & RPMFILE_MISSINGOK)
  121.         strcat(buf, "m");
  122.     if (anint & RPMFILE_NOREPLACE)
  123.         strcat(buf, "n");
  124.  
  125.     val = malloc(5 + padding);
  126.     strcat(formatPrefix, "s");
  127.     sprintf(val, formatPrefix, buf);
  128.     }
  129.  
  130.     return val;
  131. }
  132.  
  133. static char * depflagsFormat(int_32 type, const void * data, 
  134.                  char * formatPrefix, int padding, int element) {
  135.     char * val;
  136.     char buf[10];
  137.     int anint = *((int_32 *) data);
  138.  
  139.     if (type != RPM_INT32_TYPE) {
  140.     val = malloc(20);
  141.     strcpy(val, "(not a number)");
  142.     } else {
  143.     *buf = '\0';
  144.  
  145.     if (anint & RPMSENSE_LESS) 
  146.         strcat(buf, "<");
  147.     if (anint & RPMSENSE_GREATER)
  148.         strcat(buf, ">");
  149.     if (anint & RPMSENSE_EQUAL)
  150.         strcat(buf, "=");
  151.     if (anint & RPMSENSE_SERIAL)
  152.         strcat(buf, "S");
  153.  
  154.     val = malloc(5 + padding);
  155.     strcat(formatPrefix, "s");
  156.     sprintf(val, formatPrefix, buf);
  157.     }
  158.  
  159.     return val;
  160. }
  161.  
  162. static int fsnamesTag(Header h, int_32 * type, void ** data, int_32 * count,
  163.               int * freeData) {
  164.     char ** list;
  165.  
  166.     if (rpmGetFilesystemList(&list, count)) {
  167.     return 1;
  168.     }
  169.  
  170.     *type = RPM_STRING_ARRAY_TYPE;
  171.     *((char ***) data) = list;
  172.  
  173.     *freeData = 0;
  174.  
  175.     return 0; 
  176. }
  177.  
  178. static int fssizesTag(Header h, int_32 * type, void ** data, int_32 * count,
  179.               int * freeData) {
  180.     char ** filenames;
  181.     int_32 * filesizes;
  182.     uint_32 * usages;
  183.     int numFiles;
  184.  
  185.     headerGetEntry(h, RPMTAG_FILENAMES, NULL, (void **) &filenames, NULL);
  186.     headerGetEntry(h, RPMTAG_FILESIZES, NULL, (void **) &filesizes, &numFiles);
  187.  
  188.     if (rpmGetFilesystemList(NULL, count)) {
  189.     return 1;
  190.     }
  191.  
  192.     if (rpmGetFilesystemUsage(filenames, filesizes, numFiles, &usages, 0))    
  193.     return 1;
  194.  
  195.     *type = RPM_INT32_TYPE;
  196.     *freeData = 1;
  197.     *data = usages;
  198.  
  199.     return 0;
  200. }
  201.