1*d449716aSchristos /* $NetBSD: temp.c,v 1.22 2010/01/12 14:45:31 christos Exp $ */
288b833a7Schristos
361f28255Scgd /*
42cb5542fSderaadt * Copyright (c) 1980, 1993
52cb5542fSderaadt * The Regents of the University of California. All rights reserved.
661f28255Scgd *
761f28255Scgd * Redistribution and use in source and binary forms, with or without
861f28255Scgd * modification, are permitted provided that the following conditions
961f28255Scgd * are met:
1061f28255Scgd * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd * notice, this list of conditions and the following disclaimer.
1261f28255Scgd * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd * notice, this list of conditions and the following disclaimer in the
1461f28255Scgd * documentation and/or other materials provided with the distribution.
1589aaa1bbSagc * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd * may be used to endorse or promote products derived from this software
1761f28255Scgd * without specific prior written permission.
1861f28255Scgd *
1961f28255Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd * SUCH DAMAGE.
3061f28255Scgd */
3161f28255Scgd
327c81c8f3Slukem #include <sys/cdefs.h>
3361f28255Scgd #ifndef lint
3488b833a7Schristos #if 0
3588b833a7Schristos static char sccsid[] = "@(#)temp.c 8.1 (Berkeley) 6/6/93";
3688b833a7Schristos #else
37*d449716aSchristos __RCSID("$NetBSD: temp.c,v 1.22 2010/01/12 14:45:31 christos Exp $");
3888b833a7Schristos #endif
3961f28255Scgd #endif /* not lint */
4061f28255Scgd
4161f28255Scgd #include "rcv.h"
428207b28aSchristos #include <util.h>
432cb5542fSderaadt #include "extern.h"
4461f28255Scgd
4561f28255Scgd /*
4661f28255Scgd * Mail -- a mail program
4761f28255Scgd *
4861f28255Scgd * Give names to all the temporary files that we will need.
4961f28255Scgd */
5061f28255Scgd
51f3098750Schristos PUBLIC void
tinit(void)52b127ccccSwiz tinit(void)
5361f28255Scgd {
54798fbc60Schristos char pathbuf[MAXPATHLEN];
5555ac0c2dSmycroft const char *cp;
564361c515Swiz char *p;
5761f28255Scgd
58798fbc60Schristos /*
59798fbc60Schristos * It's okay to call savestr in here because main will
60798fbc60Schristos * do a spreserve() after us.
61798fbc60Schristos */
62ece0fd5cSchristos if ((cp = getenv("TMPDIR")) == NULL || *cp == '\0')
63ece0fd5cSchristos cp = _PATH_TMP;
644361c515Swiz
65798fbc60Schristos tmpdir = savestr(cp);
664361c515Swiz
674361c515Swiz /* Remove trailing slashes. */
684361c515Swiz p = tmpdir + strlen(tmpdir) - 1;
694361c515Swiz while (p > tmpdir && *p == '/') {
704361c515Swiz *p = '\0';
714361c515Swiz p--;
722cb5542fSderaadt }
732cb5542fSderaadt
74ab850155Swiz if (myname != NULL) {
75c3df8941Swiz if (getuserid(myname) < 0)
76*d449716aSchristos errx(EXIT_FAILURE, "`%s' is not a user of this system", myname);
77f3098750Schristos }
78f3098750Schristos else {
79ab850155Swiz if ((cp = username()) == NULL) {
80ece0fd5cSchristos myname = savestr("nobody");
81f3098750Schristos if (mailmode == mm_receiving)
82f3098750Schristos errx(EXIT_FAILURE, "who am I receiving for?");
8361f28255Scgd } else
8461f28255Scgd myname = savestr(cp);
8561f28255Scgd }
86ab850155Swiz if ((cp = getenv("HOME")) == NULL)
8761f28255Scgd cp = ".";
8861f28255Scgd homedir = savestr(cp);
89798fbc60Schristos
90798fbc60Schristos if (getcwd(pathbuf, sizeof(pathbuf)) != NULL)
91798fbc60Schristos origdir = savestr(pathbuf);
92798fbc60Schristos else {
93798fbc60Schristos warn("getcwd");
94798fbc60Schristos origdir = savestr(".");
95798fbc60Schristos }
96798fbc60Schristos
9761f28255Scgd if (debug)
98798fbc60Schristos (void)printf("user = %s, homedir = %s, origdir = %s\n",
99798fbc60Schristos myname, homedir, origdir);
10061f28255Scgd }
101