privsep.c: and just verify the box is also in CWD (wapiflapi)
[s-mailx.git] / folder.c
blob8e924202d82c16958b2a3c695d52f70f341a9bc9
1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Folder (mailbox) initialization, newmail announcement and related.
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 folder
38 #ifndef HAVE_AMALGAMATION
39 # include "nail.h"
40 #endif
42 #include <pwd.h>
44 /* Update mailname (if name != NULL) and displayname, return whether displayname
45 * was large enough to swallow mailname */
46 static bool_t _update_mailname(char const *name);
47 #ifdef HAVE_C90AMEND1 /* TODO unite __narrow_suffix() into one fun! */
48 SINLINE size_t __narrow_suffix(char const *cp, size_t cpl, size_t maxl);
49 #endif
51 #ifdef HAVE_C90AMEND1
52 SINLINE size_t
53 __narrow_suffix(char const *cp, size_t cpl, size_t maxl)
55 int err;
56 size_t i, ok;
57 NYD_ENTER;
59 for (err = ok = i = 0; cpl > maxl || err;) {
60 int ml = mblen(cp, cpl);
61 if (ml < 0) { /* XXX _narrow_suffix(): mblen() error; action? */
62 (void)mblen(NULL, 0);
63 err = 1;
64 ml = 1;
65 } else {
66 if (!err)
67 ok = i;
68 err = 0;
69 if (ml == 0)
70 break;
72 cp += ml;
73 i += ml;
74 cpl -= ml;
76 NYD_LEAVE;
77 return ok;
79 #endif /* HAVE_C90AMEND1 */
81 static bool_t
82 _update_mailname(char const *name)
84 char *mailp, *dispp;
85 size_t i, j;
86 bool_t rv = TRU1;
87 NYD_ENTER;
89 /* Don't realpath(3) if it's only an update request */
90 if (name != NULL) {
91 #ifdef HAVE_REALPATH
92 enum protocol p = which_protocol(name);
94 if (p == PROTO_FILE || p == PROTO_MAILDIR) {
95 if (realpath(name, mailname) == NULL && errno != ENOENT) {
96 n_err(_("Can't canonicalize %s\n"), n_shexp_quote_cp(name, FAL0));
97 rv = FAL0;
98 goto jdocopy;
100 } else
101 jdocopy:
102 #endif
103 n_strscpy(mailname, name, sizeof(mailname));
106 mailp = mailname;
107 dispp = displayname;
109 /* Don't display an absolute path but "+FOLDER" if under *folder* */
110 /* C99 */{
111 char const *folderp;
113 if(*(folderp = folder_query()) != '\0'){
114 i = strlen(folderp);
115 if(!strncmp(folderp, mailp, i)){
116 mailp += i;
117 *dispp++ = '+';
122 /* We want to see the name of the folder .. on the screen */
123 i = strlen(mailp);
124 if (i < sizeof(displayname) -1)
125 memcpy(dispp, mailp, i +1);
126 else {
127 rv = FAL0;
128 /* Avoid disrupting multibyte sequences (if possible) */
129 #ifndef HAVE_C90AMEND1
130 j = sizeof(displayname) / 3 - 1;
131 i -= sizeof(displayname) - (1/* + */ + 3) - j;
132 #else
133 j = field_detect_clip(sizeof(displayname) / 3, mailp, i);
134 i = j + __narrow_suffix(mailp + j, i - j,
135 sizeof(displayname) - (1/* + */ + 3 + 1) - j);
136 #endif
137 snprintf(dispp, sizeof(displayname), "%.*s...%s",
138 (int)j, mailp, mailp + i);
141 /* C99 */{
142 bool_t reset = !(pstate & PS_ROOT);
144 pstate |= PS_ROOT;
145 ok_vset(_mailbox_resolved, mailname);
146 ok_vset(_mailbox_display, displayname);
147 if(reset)
148 pstate &= ~PS_ROOT;
150 NYD_LEAVE;
151 return rv;
154 FL int
155 setfile(char const *name, enum fedit_mode fm) /* TODO oh my god */
157 static int shudclob;
159 struct stat stb;
160 size_t offset;
161 char const *who;
162 int rv, omsgCount = 0;
163 FILE *ibuf = NULL, *lckfp = NULL;
164 bool_t isdevnull = FAL0;
165 NYD_ENTER;
167 pstate &= ~PS_SETFILE_OPENED;
169 /* C99 */{
170 enum fexp_mode fexpm;
172 if((who = shortcut_expand(name)) != NULL){
173 fexpm = FEXP_NSHORTCUT | FEXP_NSHELL;
174 name = who;
175 }else
176 fexpm = FEXP_NSHELL;
178 if(name[0] == '%'){
179 fm |= FEDIT_SYSBOX; /* TODO fexpand() needs to tell is-valid-user! */
180 if(*(who = &name[1]) == ':')
181 ++who;
182 if(*who == '\0')
183 goto jlogname;
184 }else
185 jlogname:
186 who = ok_vlook(LOGNAME);
188 if ((name = fexpand(name, fexpm)) == NULL)
189 goto jem1;
192 /* For at least substdate() users */
193 time_current_update(&time_current, FAL0);
195 switch (which_protocol(name)) {
196 case PROTO_FILE:
197 if (temporary_protocol_ext != NULL)
198 name = savecat(name, temporary_protocol_ext);
199 isdevnull = ((options & OPT_BATCH_FLAG) && !strcmp(name, "/dev/null"));
200 #ifdef HAVE_REALPATH
201 do { /* TODO we need objects, goes away then */
202 # ifdef HAVE_REALPATH_NULL
203 char *cp;
205 if ((cp = realpath(name, NULL)) != NULL) {
206 name = savestr(cp);
207 (free)(cp);
209 # else
210 char cbuf[PATH_MAX];
212 if (realpath(name, cbuf) != NULL)
213 name = savestr(cbuf);
214 # endif
215 } while (0);
216 #endif
217 rv = 1;
218 break;
219 case PROTO_MAILDIR:
220 shudclob = 1;
221 rv = maildir_setfile(name, fm);
222 goto jleave;
223 #ifdef HAVE_POP3
224 case PROTO_POP3:
225 shudclob = 1;
226 rv = pop3_setfile(name, fm);
227 goto jleave;
228 #endif
229 default:
230 n_err(_("Cannot handle protocol: %s\n"), name);
231 goto jem1;
234 if ((ibuf = Zopen(name, "r")) == NULL) {
235 int e = errno;
237 if ((fm & FEDIT_SYSBOX) && e == ENOENT) {
238 if (strcmp(who, ok_vlook(LOGNAME)) && getpwnam(who) == NULL) {
239 n_err(_("%s is not a user of this system\n"),
240 n_shexp_quote_cp(who, FAL0));
241 goto jem2;
243 if (!(options & OPT_QUICKRUN_MASK) && ok_blook(bsdcompat))
244 n_err(_("No mail for %s\n"), who);
246 if (fm & FEDIT_NEWMAIL)
247 goto jleave;
249 mb.mb_type = MB_VOID;
250 if (ok_blook(emptystart)) {
251 if (!(options & OPT_QUICKRUN_MASK) && !ok_blook(bsdcompat))
252 n_perr(name, e);
253 /* We must avoid returning -1 and causing program exit */
254 rv = 1;
255 goto jleave;
257 n_perr(name, e);
258 goto jem1;
261 if (fstat(fileno(ibuf), &stb) == -1) {
262 if (fm & FEDIT_NEWMAIL)
263 goto jleave;
264 n_perr(_("fstat"), 0);
265 goto jem1;
268 if (S_ISREG(stb.st_mode) || isdevnull) {
269 /* EMPTY */
270 } else {
271 if (fm & FEDIT_NEWMAIL)
272 goto jleave;
273 errno = S_ISDIR(stb.st_mode) ? EISDIR : EINVAL;
274 n_perr(name, 0);
275 goto jem1;
278 if (shudclob && !(fm & FEDIT_NEWMAIL) && !quit(FAL0))
279 goto jem2;
281 hold_sigs();
283 #ifdef HAVE_SOCKETS
284 if (!(fm & FEDIT_NEWMAIL) && mb.mb_sock.s_fd >= 0)
285 sclose(&mb.mb_sock); /* TODO sorry? VMAILFS->close(), thank you */
286 #endif
288 /* TODO There is no intermediate VOID box we've switched to: name may
289 * TODO point to the same box that we just have written, so any updates
290 * TODO we won't see! Reopen again in this case. RACY! Goes with VOID! */
291 /* TODO In addition: in case of compressed/hook boxes we lock a temporary! */
292 /* TODO We may uselessly open twice but quit() doesn't say whether we were
293 * TODO modified so we can't tell: Mailbox::is_modified() :-(( */
294 if (/*shudclob && !(fm & FEDIT_NEWMAIL) &&*/ !strcmp(name, mailname)) {
295 name = mailname;
296 Fclose(ibuf);
298 if ((ibuf = Zopen(name, "r")) == NULL ||
299 fstat(fileno(ibuf), &stb) == -1 ||
300 (!S_ISREG(stb.st_mode) && !isdevnull)) {
301 n_perr(name, 0);
302 rele_sigs();
303 goto jem2;
307 /* Copy the messages into /tmp and set pointers */
308 if (!(fm & FEDIT_NEWMAIL)) {
309 if (isdevnull) {
310 mb.mb_type = MB_VOID;
311 mb.mb_perm = 0;
312 } else {
313 mb.mb_type = MB_FILE;
314 mb.mb_perm = (((options & OPT_R_FLAG) || (fm & FEDIT_RDONLY) ||
315 access(name, W_OK) < 0) ? 0 : MB_DELE | MB_EDIT);
316 if (shudclob) {
317 if (mb.mb_itf) {
318 fclose(mb.mb_itf);
319 mb.mb_itf = NULL;
321 if (mb.mb_otf) {
322 fclose(mb.mb_otf);
323 mb.mb_otf = NULL;
327 shudclob = 1;
328 if (fm & FEDIT_SYSBOX)
329 pstate &= ~PS_EDIT;
330 else
331 pstate |= PS_EDIT;
332 initbox(name);
333 offset = 0;
334 } else {
335 fseek(mb.mb_otf, 0L, SEEK_END);
336 /* TODO Doing this without holding a lock is.. And not err checking.. */
337 fseek(ibuf, mailsize, SEEK_SET);
338 offset = mailsize;
339 omsgCount = msgCount;
342 if (isdevnull)
343 lckfp = (FILE*)-1;
344 else if (!(pstate & PS_EDIT))
345 lckfp = n_dotlock(name, fileno(ibuf), FLT_READ, offset,0,
346 (fm & FEDIT_NEWMAIL ? 0 : UIZ_MAX));
347 else if (n_file_lock(fileno(ibuf), FLT_READ, offset,0,
348 (fm & FEDIT_NEWMAIL ? 0 : UIZ_MAX)))
349 lckfp = (FILE*)-1;
351 if (lckfp == NULL) {
352 if (!(fm & FEDIT_NEWMAIL)) {
353 char const *emsg = (pstate & PS_EDIT)
354 ? N_("Unable to lock mailbox, aborting operation")
355 : N_("Unable to (dot) lock mailbox, aborting operation");
356 n_perr(V_(emsg), 0);
358 rele_sigs();
359 if (!(fm & FEDIT_NEWMAIL))
360 goto jem1;
361 goto jleave;
364 mailsize = fsize(ibuf);
366 /* TODO This is too simple minded? We should regenerate an index file
367 * TODO to be able to truly tell whether *anything* has changed! */
368 if ((fm & FEDIT_NEWMAIL) && UICMP(z, mailsize, <=, offset)) {
369 rele_sigs();
370 goto jleave;
372 setptr(ibuf, offset);
373 setmsize(msgCount);
374 if ((fm & FEDIT_NEWMAIL) && mb.mb_sorted) {
375 mb.mb_threaded = 0;
376 c_sort((void*)-1);
379 Fclose(ibuf);
380 ibuf = NULL;
381 if (lckfp != NULL && lckfp != (FILE*)-1) {
382 Pclose(lckfp, FAL0);
383 /*lckfp = NULL;*/
386 if (!(fm & FEDIT_NEWMAIL)) {
387 pstate &= ~PS_SAW_COMMAND;
388 pstate |= PS_SETFILE_OPENED;
391 rele_sigs();
393 if ((options & OPT_EXISTONLY) && !(options & OPT_HEADERLIST)) {
394 rv = (msgCount == 0);
395 goto jleave;
398 if ((!(pstate & PS_EDIT) || (fm & FEDIT_NEWMAIL)) && msgCount == 0) {
399 if (!(fm & FEDIT_NEWMAIL)) {
400 if (!ok_blook(emptystart))
401 n_err(_("No mail for %s\n"), who);
403 goto jleave;
406 if (fm & FEDIT_NEWMAIL)
407 newmailinfo(omsgCount);
409 rv = 0;
410 jleave:
411 if (ibuf != NULL) {
412 Fclose(ibuf);
413 if (lckfp != NULL && lckfp != (FILE*)-1)
414 Pclose(lckfp, FAL0);
416 NYD_LEAVE;
417 return rv;
418 jem2:
419 mb.mb_type = MB_VOID;
420 jem1:
421 rv = -1;
422 goto jleave;
425 FL int
426 newmailinfo(int omsgCount)
428 int mdot, i;
429 NYD_ENTER;
431 for (i = 0; i < omsgCount; ++i)
432 message[i].m_flag &= ~MNEWEST;
434 if (msgCount > omsgCount) {
435 for (i = omsgCount; i < msgCount; ++i)
436 message[i].m_flag |= MNEWEST;
437 printf(_("New mail has arrived.\n"));
438 if ((i = msgCount - omsgCount) == 1)
439 printf(_("Loaded 1 new message.\n"));
440 else
441 printf(_("Loaded %d new messages.\n"), i);
442 } else
443 printf(_("Loaded %d messages.\n"), msgCount);
445 check_folder_hook(TRU1);
447 mdot = getmdot(1);
449 if (ok_blook(header))
450 print_headers(omsgCount + 1, msgCount, FAL0);
451 NYD_LEAVE;
452 return mdot;
455 FL void
456 setmsize(int sz)
458 NYD_ENTER;
459 if (n_msgvec != NULL)
460 free(n_msgvec);
461 n_msgvec = scalloc(sz + 1, sizeof *n_msgvec);
462 NYD_LEAVE;
465 FL void
466 print_header_summary(char const *Larg)
468 size_t bot, top, i, j;
469 NYD_ENTER;
471 if (Larg != NULL) {
472 /* Avoid any messages XXX add a make_mua_silent() and use it? */
473 if ((options & (OPT_VERB | OPT_EXISTONLY)) == OPT_EXISTONLY) {
474 freopen("/dev/null", "w", stdout);
475 freopen("/dev/null", "w", stderr);
477 assert(n_msgvec != NULL);
478 i = (getmsglist(/*TODO make const */n_UNCONST(Larg), n_msgvec, 0) <= 0);
479 if (options & OPT_EXISTONLY) {
480 exit_status = (int)i;
481 goto jleave;
483 if (i)
484 goto jleave;
485 for (bot = msgCount, top = 0, i = 0; (j = n_msgvec[i]) != 0; ++i) {
486 if (bot > j)
487 bot = j;
488 if (top < j)
489 top = j;
491 } else
492 bot = 1, top = msgCount;
493 print_headers(bot, top, (Larg != NULL)); /* TODO should take iterator!! */
494 jleave:
495 NYD_LEAVE;
498 FL void
499 announce(int printheaders)
501 int vec[2], mdot;
502 NYD_ENTER;
504 mdot = newfileinfo();
505 vec[0] = mdot;
506 vec[1] = 0;
507 dot = message + mdot - 1;
508 if (printheaders && msgCount > 0 && ok_blook(header)) {
509 print_header_group(vec); /* XXX errors? */
511 NYD_LEAVE;
514 FL int
515 newfileinfo(void)
517 struct message *mp;
518 int u, n, mdot, d, s, hidden, moved;
519 NYD_ENTER;
521 if (mb.mb_type == MB_VOID) {
522 mdot = 1;
523 goto jleave;
526 mdot = getmdot(0);
527 s = d = hidden = moved =0;
528 for (mp = message, n = 0, u = 0; PTRCMP(mp, <, message + msgCount); ++mp) {
529 if (mp->m_flag & MNEW)
530 ++n;
531 if ((mp->m_flag & MREAD) == 0)
532 ++u;
533 if ((mp->m_flag & (MDELETED | MSAVED)) == (MDELETED | MSAVED))
534 ++moved;
535 if ((mp->m_flag & (MDELETED | MSAVED)) == MDELETED)
536 ++d;
537 if ((mp->m_flag & (MDELETED | MSAVED)) == MSAVED)
538 ++s;
539 if (mp->m_flag & MHIDDEN)
540 ++hidden;
543 /* If displayname gets truncated the user effectively has no option to see
544 * the full pathname of the mailbox, so print it at least for '? fi' */
545 printf(_("%s: "), n_shexp_quote_cp(
546 (_update_mailname(NULL) ? displayname : mailname), FAL0));
547 if (msgCount == 1)
548 printf(_("1 message"));
549 else
550 printf(_("%d messages"), msgCount);
551 if (n > 0)
552 printf(_(" %d new"), n);
553 if (u-n > 0)
554 printf(_(" %d unread"), u);
555 if (d > 0)
556 printf(_(" %d deleted"), d);
557 if (s > 0)
558 printf(_(" %d saved"), s);
559 if (moved > 0)
560 printf(_(" %d moved"), moved);
561 if (hidden > 0)
562 printf(_(" %d hidden"), hidden);
563 else if (mb.mb_perm == 0)
564 printf(_(" [Read only]"));
565 printf("\n");
566 jleave:
567 NYD_LEAVE;
568 return mdot;
571 FL int
572 getmdot(int nmail)
574 struct message *mp;
575 char *cp;
576 int mdot;
577 enum mflag avoid = MHIDDEN | MDELETED;
578 NYD_ENTER;
580 if (!nmail) {
581 if (ok_blook(autothread)) {
582 OBSOLETE(_("please use *autosort=thread* instead of *autothread*"));
583 c_thread(NULL);
584 } else if ((cp = ok_vlook(autosort)) != NULL) {
585 if (mb.mb_sorted != NULL)
586 free(mb.mb_sorted);
587 mb.mb_sorted = sstrdup(cp);
588 c_sort(NULL);
591 if (mb.mb_type == MB_VOID) {
592 mdot = 1;
593 goto jleave;
596 if (nmail)
597 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
598 if ((mp->m_flag & (MNEWEST | avoid)) == MNEWEST)
599 break;
601 if (!nmail || PTRCMP(mp, >=, message + msgCount)) {
602 if (mb.mb_threaded) {
603 for (mp = threadroot; mp != NULL; mp = next_in_thread(mp))
604 if ((mp->m_flag & (MNEW | avoid)) == MNEW)
605 break;
606 } else {
607 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
608 if ((mp->m_flag & (MNEW | avoid)) == MNEW)
609 break;
613 if ((mb.mb_threaded ? (mp == NULL) : PTRCMP(mp, >=, message + msgCount))) {
614 if (mb.mb_threaded) {
615 for (mp = threadroot; mp != NULL; mp = next_in_thread(mp))
616 if (mp->m_flag & MFLAGGED)
617 break;
618 } else {
619 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
620 if (mp->m_flag & MFLAGGED)
621 break;
625 if ((mb.mb_threaded ? (mp == NULL) : PTRCMP(mp, >=, message + msgCount))) {
626 if (mb.mb_threaded) {
627 for (mp = threadroot; mp != NULL; mp = next_in_thread(mp))
628 if (!(mp->m_flag & (MREAD | avoid)))
629 break;
630 } else {
631 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
632 if (!(mp->m_flag & (MREAD | avoid)))
633 break;
637 if (nmail &&
638 (mb.mb_threaded ? (mp != NULL) : PTRCMP(mp, <, message + msgCount)))
639 mdot = (int)PTR2SIZE(mp - message + 1);
640 else if (ok_blook(showlast)) {
641 if (mb.mb_threaded) {
642 for (mp = this_in_thread(threadroot, -1); mp;
643 mp = prev_in_thread(mp))
644 if (!(mp->m_flag & avoid))
645 break;
646 mdot = (mp != NULL) ? (int)PTR2SIZE(mp - message + 1) : msgCount;
647 } else {
648 for (mp = message + msgCount - 1; mp >= message; --mp)
649 if (!(mp->m_flag & avoid))
650 break;
651 mdot = (mp >= message) ? (int)PTR2SIZE(mp - message + 1) : msgCount;
653 } else if (!nmail &&
654 (mb.mb_threaded ? (mp != NULL) : PTRCMP(mp, <, message + msgCount)))
655 mdot = (int)PTR2SIZE(mp - message + 1);
656 else if (mb.mb_threaded) {
657 for (mp = threadroot; mp; mp = next_in_thread(mp))
658 if (!(mp->m_flag & avoid))
659 break;
660 mdot = (mp != NULL) ? (int)PTR2SIZE(mp - message + 1) : 1;
661 } else {
662 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
663 if (!(mp->m_flag & avoid))
664 break;
665 mdot = PTRCMP(mp, <, message + msgCount)
666 ? (int)PTR2SIZE(mp - message + 1) : 1;
668 jleave:
669 NYD_LEAVE;
670 return mdot;
673 FL void
674 initbox(char const *name)
676 char *tempMesg;
677 NYD_ENTER;
679 if (mb.mb_type != MB_VOID)
680 n_strscpy(prevfile, mailname, PATH_MAX);
682 /* TODO name always NE mailname (but goes away for objects anyway)
683 * TODO Well, not true no more except that in parens */
684 _update_mailname((name != mailname) ? name : NULL);
686 if ((mb.mb_otf = Ftmp(&tempMesg, "tmpmbox", OF_WRONLY | OF_HOLDSIGS)) ==
687 NULL) {
688 n_perr(_("temporary mail message file"), 0);
689 exit(EXIT_ERR);
691 if ((mb.mb_itf = safe_fopen(tempMesg, "r", NULL)) == NULL) {
692 n_perr(_("temporary mail message file"), 0);
693 exit(EXIT_ERR);
695 Ftmp_release(&tempMesg);
697 message_reset();
698 mb.mb_threaded = 0;
699 if (mb.mb_sorted != NULL) {
700 free(mb.mb_sorted);
701 mb.mb_sorted = NULL;
703 dot = prevdot = threadroot = NULL;
704 pstate &= ~PS_DID_PRINT_DOT;
705 NYD_LEAVE;
708 FL char const *
709 folder_query(void){
710 struct n_string s, *sp = &s;
711 char *cp;
712 char const *rv;
713 bool_t err;
714 NYD_ENTER;
716 sp = n_string_creat_auto(sp);
718 /* *folder* is linked with *_folder_resolved*: we only use the latter */
719 for(err = FAL0;;){
720 if((rv = ok_vlook(_folder_resolved)) != NULL)
721 break;
723 /* POSIX says:
724 * If directory does not start with a <slash> ('/'), the contents
725 * of HOME shall be prefixed to it.
726 * And:
727 * If folder is unset or set to null, [.] filenames beginning with
728 * '+' shall refer to files in the current directory.
729 * We may have the result already */
730 rv = n_empty;
731 err = FAL0;
733 if((cp = ok_vlook(folder)) == NULL)
734 goto jset;
736 /* Expand the *folder*; skip %: prefix for simplicity of use */
737 if(cp[0] == '%' && cp[1] == ':')
738 cp += 2;
739 if((err = (cp = fexpand(cp, FEXP_NSPECIAL | FEXP_NFOLDER | FEXP_NSHELL)
740 ) == NULL) || *cp == '\0')
741 goto jset;
743 switch(which_protocol(cp)){
744 case PROTO_POP3:
745 n_err(_("*folder* can't be set to a flat, readonly POP3 account\n"));
746 err = TRU1;
747 goto jset;
748 default:
749 /* Further expansion desired */
750 break;
753 /* Prefix HOME as necessary */
754 if(*cp != '/'){ /* XXX path_is_absolute() */
755 size_t l1, l2;
756 char const *home;
758 home = ok_vlook(HOME);
759 l1 = strlen(home);
760 l2 = strlen(cp);
762 sp = n_string_reserve(sp, l1 + 1 + l2 +1);
763 sp = n_string_push_buf(sp, home, l1);
764 sp = n_string_push_c(sp, '/');
765 sp = n_string_push_buf(sp, cp, l2);
766 cp = n_string_cp(sp);
767 sp = n_string_drop_ownership(sp);
770 rv = cp;
772 /* TODO Since our visual mailname is resolved via realpath(3) if available
773 * TODO to avoid that we loose track of our currently open folder in case
774 * TODO we chdir away, but still checks the leading path portion against
775 * TODO folder_query() to be able to abbreviate to the +FOLDER syntax if
776 * TODO possible, we need to realpath(3) the folder, too */
777 #ifdef HAVE_REALPATH
778 assert(sp->s_len == 0 && sp->s_dat == NULL);
779 # ifndef HAVE_REALPATH_NULL
780 sp = n_string_reserve(sp, PATH_MAX +1);
781 # endif
783 if((sp->s_dat = realpath(cp, sp->s_dat)) != NULL){
784 # ifdef HAVE_REALPATH_NULL
785 n_string_cp(sp = n_string_assign_cp(sp, cp = sp->s_dat));
786 (free)(cp);
787 # endif
788 rv = sp->s_dat;
789 }else if(errno == ENOENT)
790 rv = cp;
791 else{
792 n_err(_("Can't canonicalize *folder*: %s\n"),
793 n_shexp_quote_cp(cp, FAL0));
794 err = TRU1;
795 rv = n_empty;
797 sp = n_string_drop_ownership(sp);
798 #endif /* HAVE_REALPATH */
800 /* Always append a solidus to our result path upon success */
801 if(!err){
802 size_t i;
804 if(rv[(i = strlen(rv)) - 1] != '/'){
805 sp = n_string_reserve(sp, i + 1 +1);
806 sp = n_string_push_buf(sp, rv, i);
807 sp = n_string_push_c(sp, '/');
808 rv = n_string_cp(sp);
809 sp = n_string_drop_ownership(sp);
813 jset:
814 /* C99 */{
815 bool_t reset = !(pstate & PS_ROOT);
817 pstate |= PS_ROOT;
818 ok_vset(_folder_resolved, rv);
819 if(reset)
820 pstate &= ~PS_ROOT;
824 if(err){
825 n_err(_("*folder* is not resolvable, using CWD\n"));
826 assert(rv != NULL && *rv == '\0');
828 NYD_LEAVE;
829 return rv;
832 /* s-it-mode */