Dillo v3.1.1-46-g8a360e32
Loading...
Searching...
No Matches
dpi_socket_dir.c
Go to the documentation of this file.
1/*
2 Copyright (C) 2003 Ferdi Franceschini <ferdif@optusnet.com.au>
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
22#include <errno.h>
23#include <stdlib.h>
24#include "dpid_common.h"
25#include "dpi.h"
26#include "misc_new.h"
27#include "dpi_socket_dir.h" /* for function prototypes */
28
34int w_dpi_socket_dir(char *dirname, char *sockdir)
35{
36 FILE *dir;
37
38 if ((dir = fopen(dirname, "w")) == NULL) {
39 ERRMSG("w_dpi_socket_dir", "fopen", errno);
40 return (-1);
41 }
42 fprintf(dir, "%s", sockdir);
43 fclose(dir);
44 return (1);
45}
46
54int tst_dir(char *dir)
55{
56 char *dirtest;
57 int ret = 0;
58
59 /* test for a directory */
60 dirtest = dStrconcat(dir, "/", NULL);
61 if (access(dirtest, F_OK) == -1) {
62 ERRMSG("tst_dir", "access", errno);
63 MSG_ERR(" - %s\n", dirtest);
64 } else {
65 ret = 1;
66 }
67 dFree(dirtest);
68
69 return ret;
70}
71
77char *mk_sockdir(void)
78{
79 char *template, *logname;
80
81 logname = getenv("LOGNAME") ? getenv("LOGNAME") : "dillo";
82 template = dStrconcat("/tmp/", logname, "-", "XXXXXX", NULL);
83 if (a_Misc_mkdtemp(template) == NULL) {
84 ERRMSG("mk_sockdir", "a_Misc_mkdtemp", 0);
85 MSG_ERR(" - %s\n", template);
86 dFree(template);
87 return (NULL);
88 }
89 return template;
90}
91
98char *init_sockdir(char *dpi_socket_dir)
99{
100 char *sockdir = NULL;
101 int dir_ok = 0;
102
103 if ((sockdir = a_Dpi_rd_dpi_socket_dir(dpi_socket_dir)) == NULL) {
104 MSG_ERR("init_sockdir: The dpi_socket_dir file %s does not exist\n",
105 dpi_socket_dir);
106 } else {
107 if ((dir_ok = tst_dir(sockdir)) == 1) {
108 MSG_ERR("init_sockdir: The socket directory %s exists and is OK\n",
109 sockdir);
110 } else {
111 MSG_ERR("init_sockdir: The socket directory %s does not exist "
112 "or is not a directory\n", sockdir);
113 dFree(sockdir);
114 }
115 }
116 if (!dir_ok) {
117 sockdir = mk_sockdir();
118 if (sockdir == NULL) {
119 ERRMSG("init_sockdir", "mk_sockdir", 0);
120 MSG_ERR(" - Failed to create dpi socket directory\n");
121 } else if ((w_dpi_socket_dir(dpi_socket_dir, sockdir)) == -1) {
122 ERRMSG("init_sockdir", "w_dpi_socket_dir", 0);
123 MSG_ERR(" - failed to save %s\n", sockdir);
124 dFree(sockdir);
125 sockdir = NULL;
126 }
127 }
128 return (sockdir);
129}
char * dStrconcat(const char *s1,...)
Concatenate a NULL-terminated list of strings.
Definition dlib.c:102
void dFree(void *mem)
Definition dlib.c:68
char * init_sockdir(char *dpi_socket_dir)
char * mk_sockdir(void)
int tst_dir(char *dir)
int w_dpi_socket_dir(char *dirname, char *sockdir)
char * a_Dpi_rd_dpi_socket_dir(char *dirname)
Definition dpi.c:51
#define MSG_ERR(...)
Definition dpid_common.h:23
#define ERRMSG(CALLER, CALLED, ERR)
Definition dpid_common.h:29
char * a_Misc_mkdtemp(char *template)
Alternative to mkdtemp().
Definition misc_new.c:131