- Main Page
- Related Pages
- Modules
- Data Structures
- Files

- File List
- Globals

# lib/misc.c

Go to the documentation of this file.

    00001
    00005 #include "system.h"
    00006
    00007 /* just to put a marker in librpm.a */
    00008 const char * RPMVERSION = VERSION;
    00009
    00010 #include <rpmio.h>
    00011 #include <rpmlog.h>
    00012 #include <rpmurl.h>
    00013 #include <rpmmacro.h>   /* XXX for rpmGetPath */
    00014 #include <rpmlib.h>
    00015 #include "misc.h"
    00016 #include "debug.h"
    00017
    00018 rpmRC rpmMkdirPath (const char * dpath, const char * dname)
    00019 {
    00020     struct stat st;
    00021     int rc;
    00022
    00023     if ((rc = Stat(dpath, &st)) < 0) {
    00024         int ut = urlPath(dpath, NULL);
    00025         switch (ut) {
    00026         case URL_IS_PATH:
    00027         case URL_IS_UNKNOWN:
    00028             if (errno != ENOENT)
    00029                 break;
    00030             /*@fallthrough@*/
    00031         case URL_IS_HTTPS:
    00032         case URL_IS_HTTP:
    00033         case URL_IS_FTP:
    00034             rc = Mkdir(dpath, 0755);
    00035             break;
    00036         case URL_IS_DASH:
    00037         case URL_IS_HKP:
    00038             break;
    00039         }
    00040         if (rc < 0) {
    00041             rpmlog(RPMLOG_ERR, _("cannot create %%%s %s\n"), dname, dpath);
    00042             return RPMRC_FAIL;
    00043         }
    00044     }
    00045     return RPMRC_OK;
    00046 }
    00047
    00048 char ** splitString(const char * str, int length, char sep)
    00049 {
    00050     const char * source;
    00051     char * s, * dest;
    00052     char ** list;
    00053     int i;
    00054     int fields;
    00055
    00056     s = xmalloc(length + 1);
    00057
    00058     fields = 1;
    00059     for (source = str, dest = s, i = 0; i < length; i++, source++, dest++) {
    00060         *dest = *source;
    00061         if (*dest == sep) fields++;
    00062     }
    00063
    00064     *dest = '\0';
    00065
    00066     list = xmalloc(sizeof(*list) * (fields + 1));
    00067
    00068     dest = s;
    00069     list[0] = dest;
    00070     i = 1;
    00071     while (i < fields) {
    00072         if (*dest == sep) {
    00073             list[i++] = dest + 1;
    00074             *dest = 0;
    00075         }
    00076         dest++;
    00077     }
    00078
    00079     list[i] = NULL;
    00080
    00081 /*@-nullret@*/ /* FIX: list[i] is NULL */
    00082     return list;
    00083 /*@=nullret@*/
    00084 }
    00085
    00086 void freeSplitString(char ** list)
    00087 {
    00088     /*@-unqualifiedtrans@*/
    00089     list[0] = _free(list[0]);
    00090     /*@=unqualifiedtrans@*/
    00091     list = _free(list);
    00092 }
    00093
    00094 int doputenv(const char *str)
    00095 {
    00096     char * a;
    00097
    00098     /* FIXME: this leaks memory! */
    00099     a = xmalloc(strlen(str) + 1);
    00100     strcpy(a, str);
    00101     return putenv(a);
    00102 }
    00103
    00104 int dosetenv(const char * name, const char * value, int overwrite)
    00105 {
    00106     char * a;
    00107
    00108     if (!overwrite && getenv(name)) return 0;
    00109
    00110     /* FIXME: this leaks memory! */
    00111     a = xmalloc(strlen(name) + strlen(value) + sizeof("="));
    00112     (void) stpcpy( stpcpy( stpcpy( a, name), "="), value);
    00113     return putenv(a);
    00114 }
    00115
    00116 char * currentDirectory(void)
    00117 {
    00118     int currDirLen = 0;
    00119     char * currDir = NULL;
    00120
    00121     do {
    00122         currDirLen += 128;
    00123         currDir = xrealloc(currDir, currDirLen);
    00124         memset(currDir, 0, currDirLen);
    00125     } while (getcwd(currDir, currDirLen) == NULL && errno == ERANGE);
    00126
    00127     return currDir;
    00128 }