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