2 * Copyright (C) 2012 Sami Kerola <kerolasa@iki.fi>
10 #include <sys/resource.h>
13 #include "fileutils.h"
14 #include "pathnames.h"
17 /* Create open temporary file in safe way. Please notice that the
18 * file permissions are -rw------- by default. */
19 int xmkstemp(char **tmpname
, char *dir
)
26 /* Some use cases must be capable of being moved atomically
27 * with rename(2), which is the reason why dir is here. */
31 tmpenv
= getenv("TMPDIR");
34 xasprintf(&localtmp
, "%s/%s.XXXXXX", tmpenv
,
35 program_invocation_short_name
);
37 xasprintf(&localtmp
, "%s/%s.XXXXXX", _PATH_TMP
,
38 program_invocation_short_name
);
39 old_mode
= umask(077);
40 fd
= mkostemp(localtmp
, O_RDWR
|O_CREAT
|O_EXCL
|O_CLOEXEC
);
51 * portable getdtablesize()
53 int get_fd_tabsize(void)
57 #if defined(HAVE_GETDTABLESIZE)
59 #elif defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE)
62 getrlimit(RLIMIT_NOFILE
, &rl
);
64 #elif defined(HAVE_SYSCONF) && defined(_SC_OPEN_MAX)
65 m
= sysconf(_SC_OPEN_MAX
);
77 f
= xfmkstemp(&tmpname
, NULL
);
86 int mkdir_p(const char *path
, mode_t mode
)
94 dir
= p
= strdup(path
);
102 char *e
= strchr(p
, '/');
106 rc
= mkdir(dir
, mode
);
107 if (rc
&& errno
!= EEXIST
)
121 /* returns basename and keeps dirname in the @path, if @path is "/" (root)
122 * then returns empty string */
123 char *stripoff_last_component(char *path
)
125 char *p
= path
? strrchr(path
, '/') : NULL
;