* Implement a different way to delete a password from the cache.
[alpine.git] / pico / osdep / signals.c
blob9e0709381c3cb5d8933344ec161325daf187dcdf
1 /*
2 * ========================================================================
3 * Copyright 2006-2007 University of Washington
4 * Copyright 2013-2022 Eduardo Chappa
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * ========================================================================
15 #include <system.h>
16 #include <general.h>
18 #include "../headers.h"
20 #include "../../pith/charconv/filesys.h"
22 #include "tty.h"
24 #include "signals.h"
27 /* internal prototypes */
28 #ifndef _WINDOWS
29 RETSIGTYPE do_hup_signal(int);
30 #endif
34 * picosigs - Install any handlers for the signals we're interested
35 * in catching.
37 void
38 picosigs(void)
40 #ifndef _WINDOWS
41 signal(SIGHUP, do_hup_signal); /* deal with SIGHUP */
42 signal(SIGTERM, do_hup_signal); /* deal with SIGTERM */
43 #ifdef SIGTSTP
44 signal(SIGTSTP, SIG_DFL);
45 #endif
46 #if defined(SIGWINCH) && defined(TIOCGWINSZ)
47 signal(SIGWINCH, winch_handler); /* window size changes */
48 #endif
49 #endif /* !_WINDOWS */
55 #ifndef _WINDOWS
57 * do_hup_signal - jump back in the stack to where we can handle this
59 RETSIGTYPE
60 do_hup_signal(int sig)
62 signal(SIGHUP, SIG_IGN); /* ignore further SIGHUP's */
63 signal(SIGTERM, SIG_IGN); /* ignore further SIGTERM's */
64 if(Pmaster){
65 extern jmp_buf finstate;
67 longjmp(finstate, COMP_GOTHUP);
69 else{
71 * if we've been interrupted and the buffer is changed,
72 * save it...
74 if(anycb() == TRUE){ /* time to save */
75 if(curbp->b_fname[0] == '\0'){ /* name it */
76 strncpy(curbp->b_fname, "pico.save", sizeof(curbp->b_fname));
77 curbp->b_fname[sizeof(curbp->b_fname)-1] = '\0';
79 else{
80 strncat(curbp->b_fname, ".save", sizeof(curbp->b_fname)-strlen(curbp->b_fname)-1);
81 curbp->b_fname[sizeof(curbp->b_fname)-1] = '\0';
84 our_unlink(curbp->b_fname);
85 writeout(curbp->b_fname, TRUE);
88 vttidy();
89 exit(1);
92 #endif /* !_WINDOWS */
95 #if defined(SIGWINCH) && defined(TIOCGWINSZ)
97 * winch_handler - handle window change signal
99 RETSIGTYPE
100 winch_handler(int sig)
102 int i = 0;
103 signal(SIGWINCH, winch_handler);
104 if(wheadp != NULL)
105 ttresize();
106 else if (Pmaster){
107 i = Pmaster->arm_winch_cleanup;
108 Pmaster->arm_winch_cleanup = 1;
110 if(Pmaster && Pmaster->winch_cleanup && Pmaster->arm_winch_cleanup)
111 (*Pmaster->winch_cleanup)();
112 if(wheadp == NULL && Pmaster != NULL)
113 Pmaster->arm_winch_cleanup = i;
115 #endif /* SIGWINCH and friends */
119 #ifdef POSIX_SIGNALS
120 /*----------------------------------------------------------------------
121 Reset signals after critical imap code
122 ----*/
123 void
124 (*posix_signal(int sig_num, RETSIGTYPE (*action)(int)))(int)
126 struct sigaction new_action, old_action;
128 memset((void *)&new_action, 0, sizeof(struct sigaction));
129 sigemptyset (&new_action.sa_mask);
130 new_action.sa_handler = action;
131 #ifdef SA_RESTART
132 new_action.sa_flags = SA_RESTART;
133 #else
134 new_action.sa_flags = 0;
135 #endif
136 sigaction(sig_num, &new_action, &old_action);
137 return(old_action.sa_handler);
141 posix_sigunblock(int mask)
143 sigset_t sig_mask;
145 sigemptyset(&sig_mask);
146 sigaddset(&sig_mask, mask);
147 #if HAVE_PTHREAD
148 # define sigprocmask pthread_sigmask
149 #endif
150 return(sigprocmask(SIG_UNBLOCK, &sig_mask, NULL));
152 #endif /* POSIX_SIGNALS */
155 #if defined(sv3) || defined(ct)
156 /* Placed by rll to handle the rename function not found in AT&T */
158 rename(char *oldname, char *newname)
160 int rtn;
161 char b[NLINE];
163 strncpy(b, fname_to_locale(oldname), sizeof(b));
164 b[sizeof(b)-1] = '\0';
166 if ((rtn = link(b, fname_to_locale(newname))) != 0) {
167 perror("Was not able to rename file.");
168 return(rtn);
171 if ((rtn = our_unlink(b)) != 0)
172 perror("Was not able to unlink file.");
174 return(rtn);
176 #endif