blob: 4e884d39a65ec44c158f6bcc400c3dbfb9a7019e [file] [log] [blame]
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001/*
2 * Copyright (C) 2012 Sami Kerola <kerolasa@iki.fi>
3 */
4
5#include <stdio.h>
6#include <stdlib.h>
bigbiff7b4c7a62015-01-01 19:44:14 -05007#include <paths.h>
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05008#include <sys/stat.h>
9#include <unistd.h>
10#include <sys/time.h>
11#include <sys/resource.h>
12
13#include "c.h"
14#include "fileutils.h"
15#include "pathnames.h"
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050016
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050017/* Create open temporary file in safe way. Please notice that the
18 * file permissions are -rw------- by default. */
19int xmkstemp(char **tmpname, char *dir)
20{
21 char *localtmp;
22 char *tmpenv;
23 mode_t old_mode;
bigbiff7b4c7a62015-01-01 19:44:14 -050024 int fd, rc;
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050025
26 /* Some use cases must be capable of being moved atomically
27 * with rename(2), which is the reason why dir is here. */
28 if (dir != NULL)
29 tmpenv = dir;
30 else
31 tmpenv = getenv("TMPDIR");
32
33 if (tmpenv)
bigbiff7b4c7a62015-01-01 19:44:14 -050034 rc = asprintf(&localtmp, "%s/%s.XXXXXX", tmpenv,
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050035 program_invocation_short_name);
36 else
bigbiff7b4c7a62015-01-01 19:44:14 -050037 rc = asprintf(&localtmp, "%s/%s.XXXXXX", _PATH_TMP,
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050038 program_invocation_short_name);
bigbiff7b4c7a62015-01-01 19:44:14 -050039
40 if (rc < 0)
41 return -1;
42
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050043 old_mode = umask(077);
44 fd = mkstemp(localtmp);
45 umask(old_mode);
46 if (fd == -1) {
47 free(localtmp);
48 localtmp = NULL;
49 }
50 *tmpname = localtmp;
51 return fd;
52}
53
54/*
55 * portable getdtablesize()
56 */
57int get_fd_tabsize(void)
58{
59 int m;
60
61#if defined(HAVE_GETDTABLESIZE)
62 m = getdtablesize();
63#elif defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE)
64 struct rlimit rl;
65
66 getrlimit(RLIMIT_NOFILE, &rl);
67 m = rl.rlim_cur;
68#elif defined(HAVE_SYSCONF) && defined(_SC_OPEN_MAX)
69 m = sysconf(_SC_OPEN_MAX);
70#else
71 m = OPEN_MAX;
72#endif
73 return m;
74}
75
76#ifdef TEST_PROGRAM
77int main(void)
78{
79 FILE *f;
80 char *tmpname;
81 f = xfmkstemp(&tmpname, NULL);
82 unlink(tmpname);
83 free(tmpname);
84 fclose(f);
85 return EXIT_FAILURE;
86}
87#endif
bigbiff7b4c7a62015-01-01 19:44:14 -050088
89
90int mkdir_p(const char *path, mode_t mode)
91{
92 char *p, *dir;
93 int rc = 0;
94
95 if (!path || !*path)
96 return -EINVAL;
97
98 dir = p = strdup(path);
99 if (!dir)
100 return -ENOMEM;
101
102 if (*p == '/')
103 p++;
104
105 while (p && *p) {
106 char *e = strchr(p, '/');
107 if (e)
108 *e = '\0';
109 if (*p) {
110 rc = mkdir(dir, mode);
111 if (rc && errno != EEXIST)
112 break;
113 rc = 0;
114 }
115 if (!e)
116 break;
117 *e = '/';
118 p = e + 1;
119 }
120
121 free(dir);
122 return rc;
123}
124
125/* returns basename and keeps dirname in the @path, if @path is "/" (root)
126 * then returns empty string */
127char *stripoff_last_component(char *path)
128{
129 char *p = path ? strrchr(path, '/') : NULL;
130
131 if (!p)
132 return NULL;
133 *p = '\0';
134 return p + 1;
135}