make-config.in: complete path (leftover of [807f64e2], 2015-12-26!)
[s-mailx.git] / path.c
blobfbdffeca2d4795930910913a0acbd717a4cadc6c
1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Path and directory related operations.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2018 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
6 * SPDX-License-Identifier: BSD-3-Clause TODO ISC
7 */
8 /*
9 * Copyright (c) 1980, 1993
10 * The Regents of the University of California. All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
36 #undef n_FILE
37 #define n_FILE path
39 #ifndef HAVE_AMALGAMATION
40 # include "nail.h"
41 #endif
43 FL bool_t
44 n_is_dir(char const *name, bool_t check_access){
45 struct stat sbuf;
46 bool_t rv;
47 NYD2_ENTER;
49 if((rv = (stat(name, &sbuf) == 0))){
50 if((rv = (S_ISDIR(sbuf.st_mode) != 0)) && check_access){
51 int mode;
53 mode = R_OK | X_OK;
54 if(check_access != TRUM1)
55 mode |= W_OK;
56 rv = (access(name, mode) == 0);
59 NYD2_LEAVE;
60 return rv;
63 FL bool_t
64 n_path_mkdir(char const *name){
65 struct stat st;
66 bool_t rv;
67 NYD_ENTER;
69 jredo:
70 if(!mkdir(name, 0777))
71 rv = TRU1;
72 else{
73 int e = n_err_no;
75 /* Try it recursively */
76 if(e == n_ERR_NOENT){
77 char const *vp;
79 if((vp = strrchr(name, '/')) != NULL){ /* TODO magic dirsep */
80 while(vp > name && vp[-1] == '/')
81 --vp;
82 vp = savestrbuf(name, PTR2SIZE(vp - name));
84 if(n_path_mkdir(vp))
85 goto jredo;
89 rv = ((e == n_ERR_EXIST || e == n_ERR_NOSYS) && !stat(name, &st) &&
90 S_ISDIR(st.st_mode));
92 NYD_LEAVE;
93 return rv;
96 FL bool_t
97 n_path_rm(char const *name){
98 struct stat sb;
99 bool_t rv;
100 NYD2_ENTER;
102 if(stat(name, &sb) != 0)
103 rv = FAL0;
104 else if(!S_ISREG(sb.st_mode))
105 rv = TRUM1;
106 else
107 rv = (unlink(name) == 0);
108 NYD2_LEAVE;
109 return rv;
112 #ifdef HAVE_FCHDIR
113 FL enum okay
114 cwget(struct cw *cw)
116 enum okay rv = STOP;
117 NYD_ENTER;
119 if ((cw->cw_fd = open(".", O_RDONLY)) == -1)
120 goto jleave;
121 if (fchdir(cw->cw_fd) == -1) {
122 close(cw->cw_fd);
123 goto jleave;
125 rv = OKAY;
126 jleave:
127 NYD_LEAVE;
128 return rv;
131 FL enum okay
132 cwret(struct cw *cw)
134 enum okay rv = STOP;
135 NYD_ENTER;
137 if (!fchdir(cw->cw_fd))
138 rv = OKAY;
139 NYD_LEAVE;
140 return rv;
143 FL void
144 cwrelse(struct cw *cw)
146 NYD_ENTER;
147 close(cw->cw_fd);
148 NYD_LEAVE;
151 #else /* !HAVE_FCHDIR */
152 FL enum okay
153 cwget(struct cw *cw)
155 enum okay rv = STOP;
156 NYD_ENTER;
158 if (getcwd(cw->cw_wd, sizeof cw->cw_wd) != NULL && !chdir(cw->cw_wd))
159 rv = OKAY;
160 NYD_LEAVE;
161 return rv;
164 FL enum okay
165 cwret(struct cw *cw)
167 enum okay rv = STOP;
168 NYD_ENTER;
170 if (!chdir(cw->cw_wd))
171 rv = OKAY;
172 NYD_LEAVE;
173 return rv;
176 FL void
177 cwrelse(struct cw *cw)
179 NYD_ENTER;
180 n_UNUSED(cw);
181 NYD_LEAVE;
183 #endif /* !HAVE_FCHDIR */
185 /* s-it-mode */