nail.1: last fixes
[s-mailx.git] / path.c
blob9852426c345874e80c95d226534b7f9cb9540bbb
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 - 2017 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
6 */
7 /*
8 * Copyright (c) 1980, 1993
9 * The Regents of the University of California. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
35 #undef n_FILE
36 #define n_FILE path
38 #ifndef HAVE_AMALGAMATION
39 # include "nail.h"
40 #endif
42 FL bool_t
43 n_is_dir(char const *name, bool_t check_access){
44 struct stat sbuf;
45 bool_t rv;
46 NYD2_ENTER;
48 if((rv = (stat(name, &sbuf) == 0))){
49 if((rv = (S_ISDIR(sbuf.st_mode) != 0)) && check_access)
50 rv = (access(name, R_OK | W_OK | X_OK) == 0);
52 NYD2_LEAVE;
53 return rv;
56 FL bool_t
57 n_path_mkdir(char const *name){
58 struct stat st;
59 bool_t rv;
60 NYD_ENTER;
62 jredo:
63 if(!mkdir(name, 0777))
64 rv = TRU1;
65 else{
66 int e = n_err_no;
68 /* Try it recursively */
69 if(e == n_ERR_NOENT){
70 char const *vp;
72 if((vp = strrchr(name, '/')) != NULL){ /* TODO magic dirsep */
73 while(vp > name && vp[-1] == '/')
74 --vp;
75 vp = savestrbuf(name, PTR2SIZE(vp - name));
77 if(n_path_mkdir(vp))
78 goto jredo;
82 rv = ((e == n_ERR_EXIST || e == n_ERR_NOSYS) && !stat(name, &st) &&
83 S_ISDIR(st.st_mode));
85 NYD_LEAVE;
86 return rv;
89 FL bool_t
90 n_path_rm(char const *name){
91 struct stat sb;
92 bool_t rv;
93 NYD2_ENTER;
95 if(stat(name, &sb) != 0)
96 rv = FAL0;
97 else if(!S_ISREG(sb.st_mode))
98 rv = TRUM1;
99 else
100 rv = (unlink(name) == 0);
101 NYD2_LEAVE;
102 return rv;
105 #ifdef HAVE_FCHDIR
106 FL enum okay
107 cwget(struct cw *cw)
109 enum okay rv = STOP;
110 NYD_ENTER;
112 if ((cw->cw_fd = open(".", O_RDONLY)) == -1)
113 goto jleave;
114 if (fchdir(cw->cw_fd) == -1) {
115 close(cw->cw_fd);
116 goto jleave;
118 rv = OKAY;
119 jleave:
120 NYD_LEAVE;
121 return rv;
124 FL enum okay
125 cwret(struct cw *cw)
127 enum okay rv = STOP;
128 NYD_ENTER;
130 if (!fchdir(cw->cw_fd))
131 rv = OKAY;
132 NYD_LEAVE;
133 return rv;
136 FL void
137 cwrelse(struct cw *cw)
139 NYD_ENTER;
140 close(cw->cw_fd);
141 NYD_LEAVE;
144 #else /* !HAVE_FCHDIR */
145 FL enum okay
146 cwget(struct cw *cw)
148 enum okay rv = STOP;
149 NYD_ENTER;
151 if (getcwd(cw->cw_wd, sizeof cw->cw_wd) != NULL && !chdir(cw->cw_wd))
152 rv = OKAY;
153 NYD_LEAVE;
154 return rv;
157 FL enum okay
158 cwret(struct cw *cw)
160 enum okay rv = STOP;
161 NYD_ENTER;
163 if (!chdir(cw->cw_wd))
164 rv = OKAY;
165 NYD_LEAVE;
166 return rv;
169 FL void
170 cwrelse(struct cw *cw)
172 NYD_ENTER;
173 n_UNUSED(cw);
174 NYD_LEAVE;
176 #endif /* !HAVE_FCHDIR */
178 /* s-it-mode */