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

  1. #include "config.h"
  2. #include "miscfn.h"
  3.  
  4. #if HAVE_ALLOCA_H
  5. # include <alloca.h>
  6. #endif
  7.  
  8. #include <errno.h>
  9. #include <ctype.h>
  10. #include <fcntl.h>
  11. #include <grp.h>
  12. #include <pwd.h>
  13. #include <stdlib.h>
  14. #include <sys/time.h>
  15. #include <sys/types.h>
  16. #include <sys/stat.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <unistd.h>
  20.  
  21. #include "intl.h"
  22. #include "misc.h"
  23. #include "rpmlib.h"
  24. #include "messages.h"
  25.  
  26. char * RPMVERSION = VERSION;
  27.  
  28. char ** splitString(char * str, int length, char sep) {
  29.     char * s, * source, * dest;
  30.     char ** list;
  31.     int i;
  32.     int fields;
  33.    
  34.     s = malloc(length + 1);
  35.     
  36.     fields = 1;
  37.     for (source = str, dest = s, i = 0; i < length; i++, source++, dest++) {
  38.     *dest = *source;
  39.     if (*dest == sep) fields++;
  40.     }
  41.  
  42.     *dest = '\0';
  43.  
  44.     list = malloc(sizeof(char *) * (fields + 1));
  45.  
  46.     dest = s;
  47.     list[0] = dest;
  48.     i = 1;
  49.     while (i < fields) {
  50.     if (*dest == sep) {
  51.         list[i++] = dest + 1;
  52.         *dest = 0;
  53.     }
  54.     dest++;
  55.     }
  56.  
  57.     list[i] = NULL;
  58.  
  59.     return list;
  60. }
  61.  
  62. void freeSplitString(char ** list) {
  63.     free(list[0]);
  64.     free(list);
  65. }
  66.  
  67. int exists(char * filespec) {
  68.     struct stat buf;
  69.  
  70.     if (stat(filespec, &buf)) {
  71.     switch(errno) {
  72.        case ENOENT:
  73.        case EINVAL:
  74.         return 0;
  75.     }
  76.     }
  77.  
  78.     return 1;
  79. }
  80.  
  81. int rpmvercmp(char * one, char * two) {
  82.     int num1, num2;
  83.     char oldch1, oldch2;
  84.     char * str1, * str2;
  85.     int rc;
  86.     int isnum;
  87.     
  88.     if (!strcmp(one, two)) return 0;
  89.  
  90.     str1 = alloca(strlen(one) + 1);
  91.     str2 = alloca(strlen(two) + 1);
  92.  
  93.     strcpy(str1, one);
  94.     strcpy(str2, two);
  95.  
  96.     one = str1;
  97.     two = str2;
  98.  
  99.     while (*one && *two) {
  100.     while (*one && !isalnum(*one)) one++;
  101.     while (*two && !isalnum(*two)) two++;
  102.  
  103.     str1 = one;
  104.     str2 = two;
  105.  
  106.     if (isdigit(*str1)) {
  107.         while (*str1 && isdigit(*str1)) str1++;
  108.         while (*str2 && isdigit(*str2)) str2++;
  109.         isnum = 1;
  110.     } else {
  111.         while (*str1 && isalpha(*str1)) str1++;
  112.         while (*str2 && isalpha(*str2)) str2++;
  113.         isnum = 0;
  114.     }
  115.         
  116.     oldch1 = *str1;
  117.     *str1 = '\0';
  118.     oldch2 = *str2;
  119.     *str2 = '\0';
  120.  
  121.     if (one == str1) return -1;    /* arbitrary */
  122.     if (two == str2) return -1;
  123.  
  124.     if (isnum) {
  125.         num1 = atoi(one);
  126.         num2 = atoi(two);
  127.  
  128.         if (num1 < num2) 
  129.         return -1;
  130.         else if (num1 > num2)
  131.         return 1;
  132.     } else {
  133.         rc = strcmp(one, two);
  134.         if (rc) return rc;
  135.     }
  136.     
  137.     *str1 = oldch1;
  138.     one = str1;
  139.     *str2 = oldch2;
  140.     two = str2;
  141.     }
  142.  
  143.     if ((!*one) && (!*two)) return 0;
  144.  
  145.     if (!*one) return -1; else return 1;
  146. }
  147.  
  148. void stripTrailingSlashes(char * str) {
  149.     char * chptr;
  150.  
  151.     chptr = str + strlen(str) - 1;
  152.     while (*chptr == '/' && chptr >= str) {
  153.     *chptr = '\0';
  154.     chptr--;
  155.     }
  156. }
  157.  
  158. int doputenv(const char *str) {
  159.     char * a;
  160.     
  161.     /* FIXME: this leaks memory! */
  162.  
  163.     a = malloc(strlen(str) + 1);
  164.     strcpy(a, str);
  165.  
  166.     return putenv(a);
  167. }
  168.  
  169. int dosetenv(const char *name, const char *value, int overwrite) {
  170.     int i;
  171.     char * a;
  172.  
  173.     /* FIXME: this leaks memory! */
  174.  
  175.     if (!overwrite && getenv(name)) return 0;
  176.  
  177.     i = strlen(name) + strlen(value) + 2;
  178.     a = malloc(i);
  179.     if (!a) return 1;
  180.     
  181.     strcpy(a, name);
  182.     strcat(a, "=");
  183.     strcat(a, value);
  184.     
  185.     return putenv(a);
  186. }
  187.  
  188. /* unameToUid(), uidTouname() and the group variants are really poorly
  189.    implemented. They really ought to use hash tables. I just made the
  190.    guess that most files would be owned by root or the same person/group
  191.    who owned the last file. Those two values are cached, everything else
  192.    is looked up via getpw() and getgr() functions.  If this performs
  193.    too poorly I'll have to implement it properly :-( */
  194.  
  195. int unameToUid(char * thisUname, uid_t * uid) {
  196.     static char * lastUname = NULL;
  197.     static int lastUnameLen = 0;
  198.     static int lastUnameAlloced;
  199.     static uid_t lastUid;
  200.     struct passwd * pwent;
  201.     int thisUnameLen;
  202.  
  203.     if (!thisUname) {
  204.     lastUnameLen = 0;
  205.     return -1;
  206.     } else if (!strcmp(thisUname, "root")) {
  207.     *uid = 0;
  208.     return 0;
  209.     }
  210.  
  211.     thisUnameLen = strlen(thisUname);
  212.     if (!lastUname || thisUnameLen != lastUnameLen || 
  213.     strcmp(thisUname, lastUname)) {
  214.     if (lastUnameAlloced < thisUnameLen + 1) {
  215.         lastUnameAlloced = thisUnameLen + 10;
  216.         lastUname = realloc(lastUname, lastUnameAlloced);
  217.     }
  218.     strcpy(lastUname, thisUname);
  219.  
  220.     pwent = getpwnam(thisUname);
  221.     if (!pwent) {
  222.         endpwent();
  223.         pwent = getpwnam(thisUname);
  224.         if (!pwent) return -1;
  225.     }
  226.  
  227.     lastUid = pwent->pw_uid;
  228.     }
  229.  
  230.     *uid = lastUid;
  231.  
  232.     return 0;
  233. }
  234.  
  235. int gnameToGid(char * thisGname, gid_t * gid) {
  236.     static char * lastGname = NULL;
  237.     static int lastGnameLen = 0;
  238.     static int lastGnameAlloced;
  239.     static uid_t lastGid;
  240.     int thisGnameLen;
  241.     struct group * grent;
  242.  
  243.     if (!thisGname) {
  244.     lastGnameLen = 0;
  245.     return -1;
  246.     } else if (!strcmp(thisGname, "root")) {
  247.     *gid = 0;
  248.     return 0;
  249.     }
  250.    
  251.     thisGnameLen = strlen(thisGname);
  252.     if (!lastGname || thisGnameLen != lastGnameLen || 
  253.     strcmp(thisGname, lastGname)) {
  254.     if (lastGnameAlloced < thisGnameLen + 1) {
  255.         lastGnameAlloced = thisGnameLen + 10;
  256.         lastGname = realloc(lastGname, lastGnameAlloced);
  257.     }
  258.     strcpy(lastGname, thisGname);
  259.  
  260.     grent = getgrnam(thisGname);
  261.     if (!grent) {
  262.         endgrent();
  263.         grent = getgrnam(thisGname);
  264.         if (!grent) return -1;
  265.     }
  266.     lastGid = grent->gr_gid;
  267.     }
  268.  
  269.     *gid = lastGid;
  270.  
  271.     return 0;
  272. }
  273.  
  274. char * uidToUname(uid_t uid) {
  275.     static int lastUid = -1;
  276.     static char * lastUname = NULL;
  277.     static int lastUnameLen = 0;
  278.     struct passwd * pwent;
  279.     int len;
  280.  
  281.     if (uid == (uid_t) -1) {
  282.     lastUid = -1;
  283.     return NULL;
  284.     } else if (!uid) {
  285.     return "root";
  286.     } else if (uid == lastUid) {
  287.     return lastUname;
  288.     } else {
  289.     pwent = getpwuid(uid);
  290.     if (!pwent) return NULL;
  291.  
  292.     lastUid = uid;
  293.     len = strlen(pwent->pw_name);
  294.     if (lastUnameLen < len + 1) {
  295.         lastUnameLen = len + 20;
  296.         lastUname = realloc(lastUname, lastUnameLen);
  297.     }
  298.     strcpy(lastUname, pwent->pw_name);
  299.  
  300.     return lastUname;
  301.     }
  302. }
  303.  
  304. char * gidToGname(gid_t gid) {
  305.     static int lastGid = -1;
  306.     static char * lastGname = NULL;
  307.     static int lastGnameLen = 0;
  308.     struct group * grent;
  309.     int len;
  310.  
  311.     if (gid == (gid_t) -1) {
  312.     lastGid = -1;
  313.     return NULL;
  314.     } else if (!gid) {
  315.     return "root";
  316.     } else if (gid == lastGid) {
  317.     return lastGname;
  318.     } else {
  319.     grent = getgrgid(gid);
  320.     if (!grent) return NULL;
  321.  
  322.     lastGid = gid;
  323.     len = strlen(grent->gr_name);
  324.     if (lastGnameLen < len + 1) {
  325.         lastGnameLen = len + 20;
  326.         lastGname = realloc(lastGname, lastGnameLen);
  327.     }
  328.     strcpy(lastGname, grent->gr_name);
  329.  
  330.     return lastGname;
  331.     }
  332. }
  333.  
  334. int makeTempFile(char * prefix, char ** fnptr, int * fdptr) {
  335.     char * fn;
  336.     int fd;
  337.     int ran;
  338.     char * tmpdir = rpmGetVar(RPMVAR_TMPPATH);
  339.     struct stat sb;
  340.  
  341.     if (!prefix) prefix = "";
  342.  
  343.     fn = malloc(strlen(prefix) + 25 + strlen(tmpdir));
  344.  
  345.     srandom(time(NULL));
  346.     ran = random() % 100000;
  347.     do {
  348.     sprintf(fn, "%s%s/rpm-tmp.%d", prefix, tmpdir, ran++);
  349.     } while (!access(fn, X_OK));
  350.     
  351.     fd = open(fn, O_CREAT | O_RDWR | O_EXCL, 0700);
  352.  
  353.     if (fd < 0) {
  354.     rpmError(RPMERR_SCRIPT, _("error creating temporary file %s"), fn);
  355.     return 1;
  356.     }
  357.  
  358.     if (!stat(fn, &sb) && S_ISLNK(sb.st_mode)) {
  359.     rpmError(RPMERR_SCRIPT, _("error creating temporary file %s"), fn);
  360.     return 1;
  361.     }
  362.  
  363.     if (fnptr) *fnptr = fn;
  364.     *fdptr = fd;
  365.  
  366.     return 0;
  367. }
  368.