rpm 5.3.12
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             break;
00040         }
00041         if (rc < 0) {
00042             rpmlog(RPMLOG_ERR, _("cannot create %%%s %s\n"), dname, dpath);
00043             return RPMRC_FAIL;
00044         }
00045     }
00046     return RPMRC_OK;
00047 }
00048 
00049 int doputenv(const char *str)
00050 {
00051     char * a;
00052 
00053     /* FIXME: this leaks memory! */
00054     a = xmalloc(strlen(str) + 1);
00055     strcpy(a, str);
00056     return putenv(a);
00057 }
00058 
00059 int dosetenv(const char * name, const char * value, int overwrite)
00060 {
00061     char * a;
00062 
00063     if (!overwrite && getenv(name)) return 0;
00064 
00065     /* FIXME: this leaks memory! */
00066     a = xmalloc(strlen(name) + strlen(value) + sizeof("="));
00067     (void) stpcpy( stpcpy( stpcpy( a, name), "="), value);
00068     return putenv(a);
00069 }
00070 
00071 char * currentDirectory(void)
00072 {
00073     int currDirLen = 0;
00074     char * currDir = NULL;
00075 
00076     do {
00077         currDirLen += 128;
00078         currDir = xrealloc(currDir, currDirLen);
00079         memset(currDir, 0, currDirLen);
00080     } while (getcwd(currDir, currDirLen) == NULL && errno == ERANGE);
00081 
00082     return currDir;
00083 }