rpm  5.4.4
lib/misc.c
Go to the documentation of this file.
00001 
00005 #include "system.h"
00006 
00007 #include <rpmversion.h>
00008 #include <rpmiotypes.h>
00009 #include <rpmlog.h>
00010 #include <rpmurl.h>
00011 #include <rpmmacro.h>   /* XXX for rpmGetPath */
00012 #include <rpmtypes.h>
00013 #include "misc.h"
00014 #include "debug.h"
00015 
00016 /*@unchecked@*/ /*@observer@*/
00017 const char * RPMVERSION = VERSION;
00018 
00019 rpmRC rpmMkdirPath (const char * dpath, const char * dname)
00020 {
00021     struct stat st;
00022     int rc;
00023 
00024     if ((rc = Stat(dpath, &st)) < 0) {
00025         int ut = urlPath(dpath, NULL);
00026         switch (ut) {
00027         case URL_IS_PATH:
00028         case URL_IS_UNKNOWN:
00029             if (errno != ENOENT)
00030                 break;
00031             /*@fallthrough@*/
00032         case URL_IS_HTTPS:
00033         case URL_IS_HTTP:
00034         case URL_IS_FTP:
00035             rc = Mkdir(dpath, 0755);
00036             break;
00037         case URL_IS_DASH:
00038         case URL_IS_HKP:
00039         case URL_IS_MONGO:      /* XXX FIXME */
00040             break;
00041         }
00042         if (rc < 0) {
00043             rpmlog(RPMLOG_ERR, _("cannot create %%%s %s\n"), dname, dpath);
00044             return RPMRC_FAIL;
00045         }
00046     }
00047     return RPMRC_OK;
00048 }
00049 
00050 int doputenv(const char *str)
00051 {
00052     char * a;
00053 
00054     /* FIXME: this leaks memory! */
00055     a = xmalloc(strlen(str) + 1);
00056     strcpy(a, str);
00057     return putenv(a);
00058 }
00059 
00060 int dosetenv(const char * name, const char * value, int overwrite)
00061 {
00062     char * a;
00063 
00064     if (!overwrite && getenv(name)) return 0;
00065 
00066     /* FIXME: this leaks memory! */
00067     a = xmalloc(strlen(name) + strlen(value) + sizeof("="));
00068     (void) stpcpy( stpcpy( stpcpy( a, name), "="), value);
00069     return putenv(a);
00070 }
00071 
00072 char * currentDirectory(void)
00073 {
00074     int currDirLen = 0;
00075     char * currDir = NULL;
00076 
00077     do {
00078         currDirLen += 128;
00079         currDir = xrealloc(currDir, currDirLen);
00080         memset(currDir, 0, currDirLen);
00081     } while (getcwd(currDir, currDirLen) == NULL && errno == ERANGE);
00082 
00083     return currDir;
00084 }