Generalize and straigthen handling of the soft-exit-status *!*
[s-mailx.git] / path.c
blob3e5cc6d72cc7e6e36adf852ff1a2c4601047253b
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 is_dir(char const *name)
45 struct stat sbuf;
46 bool_t rv;
47 NYD_ENTER;
49 for (rv = FAL0;;)
50 if (!stat(name, &sbuf)) {
51 rv = (S_ISDIR(sbuf.st_mode) != 0);
52 break;
53 } else if (errno != EINTR)
54 break;
55 NYD_LEAVE;
56 return rv;
59 FL bool_t
60 n_path_mkdir(char const *name)
62 struct stat st;
63 bool_t rv;
64 NYD_ENTER;
66 jredo:
67 if(!mkdir(name, 0777))
68 rv = TRU1;
69 else{
70 int e = errno;
72 /* Try it recursively */
73 if(e == ENOENT){
74 char const *vp;
77 if((vp = strrchr(name, '/')) != NULL){ /* TODO magic dirsep */
78 while(vp > name && vp[-1] == '/')
79 --vp;
80 vp = savestrbuf(name, PTR2SIZE(vp - name));
82 if(n_path_mkdir(vp))
83 goto jredo;
87 rv = ((e == EEXIST || e == ENOSYS) && !stat(name, &st) &&
88 S_ISDIR(st.st_mode));
90 NYD_LEAVE;
91 return rv;
94 FL bool_t
95 n_path_rm(char const *name)
97 struct stat sb;
98 bool_t rv;
99 NYD_ENTER;
101 if (stat(name, &sb) != 0)
102 rv = FAL0;
103 else if (!S_ISREG(sb.st_mode))
104 rv = TRUM1;
105 else
106 rv = (unlink(name) == 0);
107 NYD_LEAVE;
108 return rv;
111 #ifdef HAVE_FCHDIR
112 FL enum okay
113 cwget(struct cw *cw)
115 enum okay rv = STOP;
116 NYD_ENTER;
118 if ((cw->cw_fd = open(".", O_RDONLY)) == -1)
119 goto jleave;
120 if (fchdir(cw->cw_fd) == -1) {
121 close(cw->cw_fd);
122 goto jleave;
124 rv = OKAY;
125 jleave:
126 NYD_LEAVE;
127 return rv;
130 FL enum okay
131 cwret(struct cw *cw)
133 enum okay rv = STOP;
134 NYD_ENTER;
136 if (!fchdir(cw->cw_fd))
137 rv = OKAY;
138 NYD_LEAVE;
139 return rv;
142 FL void
143 cwrelse(struct cw *cw)
145 NYD_ENTER;
146 close(cw->cw_fd);
147 NYD_LEAVE;
150 #else /* !HAVE_FCHDIR */
151 FL enum okay
152 cwget(struct cw *cw)
154 enum okay rv = STOP;
155 NYD_ENTER;
157 if (getcwd(cw->cw_wd, sizeof cw->cw_wd) != NULL && !chdir(cw->cw_wd))
158 rv = OKAY;
159 NYD_LEAVE;
160 return rv;
163 FL enum okay
164 cwret(struct cw *cw)
166 enum okay rv = STOP;
167 NYD_ENTER;
169 if (!chdir(cw->cw_wd))
170 rv = OKAY;
171 NYD_LEAVE;
172 return rv;
175 FL void
176 cwrelse(struct cw *cw)
178 NYD_ENTER;
179 n_UNUSED(cw);
180 NYD_LEAVE;
182 #endif /* !HAVE_FCHDIR */
184 /* s-it-mode */