blob: 92a391d7e3374aaa1d1c98140d2ce9d9a7882521 [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>
Ethan Yonker95e80072017-08-24 21:45:50 -050012#include <sys/limits.h>
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050013
14#include "c.h"
15#include "fileutils.h"
16#include "pathnames.h"
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050017
Ethan Yonkerc798c9c2015-10-09 11:15:26 -050018#ifndef _PATH_TMP
19#define _PATH_TMP "/tmp/"
20#endif
21
Ethan Yonker95e80072017-08-24 21:45:50 -050022#ifndef OPEN_MAX
23#define OPEN_MAX 256
24#endif
25
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050026/* Create open temporary file in safe way. Please notice that the
27 * file permissions are -rw------- by default. */
28int xmkstemp(char **tmpname, char *dir)
29{
30 char *localtmp;
31 char *tmpenv;
32 mode_t old_mode;
bigbiff7b4c7a62015-01-01 19:44:14 -050033 int fd, rc;
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050034
35 /* Some use cases must be capable of being moved atomically
36 * with rename(2), which is the reason why dir is here. */
37 if (dir != NULL)
38 tmpenv = dir;
39 else
40 tmpenv = getenv("TMPDIR");
41
42 if (tmpenv)
bigbiff7b4c7a62015-01-01 19:44:14 -050043 rc = asprintf(&localtmp, "%s/%s.XXXXXX", tmpenv,
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050044 program_invocation_short_name);
45 else
bigbiff7b4c7a62015-01-01 19:44:14 -050046 rc = asprintf(&localtmp, "%s/%s.XXXXXX", _PATH_TMP,
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050047 program_invocation_short_name);
bigbiff7b4c7a62015-01-01 19:44:14 -050048
49 if (rc < 0)
50 return -1;
51
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050052 old_mode = umask(077);
53 fd = mkstemp(localtmp);
54 umask(old_mode);
55 if (fd == -1) {
56 free(localtmp);
57 localtmp = NULL;
58 }
59 *tmpname = localtmp;
60 return fd;
61}
62
63/*
64 * portable getdtablesize()
65 */
66int get_fd_tabsize(void)
67{
68 int m;
69
70#if defined(HAVE_GETDTABLESIZE)
71 m = getdtablesize();
72#elif defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE)
73 struct rlimit rl;
74
75 getrlimit(RLIMIT_NOFILE, &rl);
76 m = rl.rlim_cur;
77#elif defined(HAVE_SYSCONF) && defined(_SC_OPEN_MAX)
78 m = sysconf(_SC_OPEN_MAX);
79#else
80 m = OPEN_MAX;
81#endif
82 return m;
83}
84
85#ifdef TEST_PROGRAM
86int main(void)
87{
88 FILE *f;
89 char *tmpname;
90 f = xfmkstemp(&tmpname, NULL);
91 unlink(tmpname);
92 free(tmpname);
93 fclose(f);
94 return EXIT_FAILURE;
95}
96#endif
bigbiff7b4c7a62015-01-01 19:44:14 -050097
98
99int mkdir_p(const char *path, mode_t mode)
100{
101 char *p, *dir;
102 int rc = 0;
103
104 if (!path || !*path)
105 return -EINVAL;
106
107 dir = p = strdup(path);
108 if (!dir)
109 return -ENOMEM;
110
111 if (*p == '/')
112 p++;
113
114 while (p && *p) {
115 char *e = strchr(p, '/');
116 if (e)
117 *e = '\0';
118 if (*p) {
119 rc = mkdir(dir, mode);
120 if (rc && errno != EEXIST)
121 break;
122 rc = 0;
123 }
124 if (!e)
125 break;
126 *e = '/';
127 p = e + 1;
128 }
129
130 free(dir);
131 return rc;
132}
133
134/* returns basename and keeps dirname in the @path, if @path is "/" (root)
135 * then returns empty string */
136char *stripoff_last_component(char *path)
137{
138 char *p = path ? strrchr(path, '/') : NULL;
139
140 if (!p)
141 return NULL;
142 *p = '\0';
143 return p + 1;
144}