(BWDIC!) Rewrite variable handling..
[s-mailx.git] / path.c
blob2fdadceda20852201b9acf17dd7974a6537575aa
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 - 2015 Steffen (Daode) Nurpmeso <sdaoden@users.sf.net>.
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 if (!mkdir(name, 0700))
67 rv = TRU1;
68 else {
69 int e = errno;
71 rv = ((e == EEXIST || e == ENOSYS) && !stat(name, &st) &&
72 S_ISDIR(st.st_mode));
74 NYD_LEAVE;
75 return rv;
78 FL bool_t
79 n_path_rm(char const *name)
81 struct stat sb;
82 bool_t rv;
83 NYD_ENTER;
85 if (stat(name, &sb) != 0)
86 rv = FAL0;
87 else if (!S_ISREG(sb.st_mode))
88 rv = TRUM1;
89 else
90 rv = (unlink(name) == 0);
91 NYD_LEAVE;
92 return rv;
95 #ifdef HAVE_FCHDIR
96 FL enum okay
97 cwget(struct cw *cw)
99 enum okay rv = STOP;
100 NYD_ENTER;
102 if ((cw->cw_fd = open(".", O_RDONLY)) == -1)
103 goto jleave;
104 if (fchdir(cw->cw_fd) == -1) {
105 close(cw->cw_fd);
106 goto jleave;
108 rv = OKAY;
109 jleave:
110 NYD_LEAVE;
111 return rv;
114 FL enum okay
115 cwret(struct cw *cw)
117 enum okay rv = STOP;
118 NYD_ENTER;
120 if (!fchdir(cw->cw_fd))
121 rv = OKAY;
122 NYD_LEAVE;
123 return rv;
126 FL void
127 cwrelse(struct cw *cw)
129 NYD_ENTER;
130 close(cw->cw_fd);
131 NYD_LEAVE;
134 #else /* !HAVE_FCHDIR */
135 FL enum okay
136 cwget(struct cw *cw)
138 enum okay rv = STOP;
139 NYD_ENTER;
141 if (getcwd(cw->cw_wd, sizeof cw->cw_wd) != NULL && !chdir(cw->cw_wd))
142 rv = OKAY;
143 NYD_LEAVE;
144 return rv;
147 FL enum okay
148 cwret(struct cw *cw)
150 enum okay rv = STOP;
151 NYD_ENTER;
153 if (!chdir(cw->cw_wd))
154 rv = OKAY;
155 NYD_LEAVE;
156 return rv;
159 FL void
160 cwrelse(struct cw *cw)
162 NYD_ENTER;
163 UNUSED(cw);
164 NYD_LEAVE;
166 #endif /* !HAVE_FCHDIR */
168 /* s-it-mode */