rpm  5.4.15
misc.c
Go to the documentation of this file.
1 
5 #include "system.h"
6 
7 #include <rpmversion.h>
8 #include <rpmiotypes.h>
9 #include <rpmlog.h>
10 #include <rpmurl.h>
11 #include <rpmmacro.h> /* XXX for rpmGetPath */
12 #include <rpmtypes.h>
13 #include "misc.h"
14 #include "debug.h"
15 
16 /*@unchecked@*/ /*@observer@*/
17 const char * RPMVERSION = VERSION;
18 
19 rpmRC rpmMkdirPath (const char * dpath, const char * dname)
20 {
21  struct stat st;
22  int rc;
23 
24  if ((rc = Stat(dpath, &st)) < 0) {
25  int ut = urlPath(dpath, NULL);
26  switch (ut) {
27  case URL_IS_PATH:
28  case URL_IS_UNKNOWN:
29  if (errno != ENOENT)
30  break;
31  /*@fallthrough@*/
32  case URL_IS_HTTPS:
33  case URL_IS_HTTP:
34  case URL_IS_FTP:
35  rc = Mkdir(dpath, 0755);
36  break;
37  case URL_IS_DASH:
38  case URL_IS_HKP:
39  case URL_IS_MONGO: /* XXX FIXME */
40  break;
41  }
42  if (rc < 0) {
43  rpmlog(RPMLOG_ERR, _("cannot create %%%s %s\n"), dname, dpath);
44  return RPMRC_FAIL;
45  }
46  }
47  return RPMRC_OK;
48 }
49 
50 int doputenv(const char *str)
51 {
52  char * a;
53 
54  /* FIXME: this leaks memory! */
55  a = (char *) xmalloc(strlen(str) + 1);
56  strcpy(a, str);
57  return putenv(a);
58 }
59 
60 int dosetenv(const char * name, const char * value, int overwrite)
61 {
62  char * a;
63 
64  if (!overwrite && getenv(name)) return 0;
65 
66  /* FIXME: this leaks memory! */
67  a = (char *) xmalloc(strlen(name) + strlen(value) + sizeof("="));
68  (void) stpcpy( stpcpy( stpcpy( a, name), "="), value);
69  return putenv(a);
70 }
71 
72 char * currentDirectory(void)
73 {
74  int currDirLen = 0;
75  char * currDir = NULL;
76 
77  do {
78  currDirLen += 128;
79  currDir = (char *) xrealloc(currDir, currDirLen);
80  memset(currDir, 0, currDirLen);
81  } while (getcwd(currDir, currDirLen) == NULL && errno == ERANGE);
82 
83  return currDir;
84 }
char * getenv(const char *name)
const char * RPMVERSION
Definition: misc.c:17
#define VERSION
Definition: config.h:1310
int Stat(const char *path, struct stat *st)
stat(2) clone.
Definition: rpmrpc.c:1361
int errno
static void rpmlog(int code, const char *fmt,...)
Definition: rpmlog.h:299
int doputenv(const char *str)
Like the libc function, but malloc()'s the space needed.
Definition: misc.c:50
const char * str
Definition: bson.h:593
Yet Another syslog(3) API clone.
char * currentDirectory(void)
Return (malloc'd) current working directory.
Definition: misc.c:72
int Mkdir(const char *path, mode_t mode)
mkdir(2) clone.
Definition: rpmrpc.c:73
rpmRC rpmMkdirPath(const char *dpath, const char *dname)
Create directory if it does not exist, and make sure path is writable.
Definition: misc.c:19
enum rpmRC_e rpmRC
RPM return codes.
urltype urlPath(const char *url, const char **pathp)
Return path component of URL.
Definition: url.c:430
char * stpcpy(char *dest, const char *src)
static const char * name
#define _(Text)
Definition: system.h:29
#define xmalloc
Definition: system.h:32
int dosetenv(const char *name, const char *value, int overwrite)
Like the libc function, but malloc()'s the space needed.
Definition: misc.c:60
#define xrealloc
Definition: system.h:35