summary refs log tree commit diff
path: root/Src/Modules
diff options
context:
space:
mode:
Diffstat (limited to 'Src/Modules')
-rw-r--r--Src/Modules/zftp.c60
1 files changed, 38 insertions, 22 deletions
diff --git a/Src/Modules/zftp.c b/Src/Modules/zftp.c
index ef85dd330..3252b9d90 100644
--- a/Src/Modules/zftp.c
+++ b/Src/Modules/zftp.c
@@ -3197,6 +3197,39 @@ bin_zftp(char *name, char **args, char *ops, int func)
     return ret;
 }
 
+static void
+zftp_cleanup(void)
+{
+    /*
+     * There are various parameters hanging around, but they're
+     * all non-special so are entirely non-life-threatening.
+     */
+    LinkNode nptr;
+    Zftp_session cursess = zfsess;
+    for (zfsessno = 0, nptr = firstnode(zfsessions); nptr;
+	 zfsessno++, incnode(nptr)) {
+	zfsess = (Zftp_session)nptr->dat;
+	zfclosedata();
+	/*
+	 * When closing the current session, do the usual unsetting,
+	 * otherwise don't.
+	 */
+	zfclose(zfsess != cursess);
+    }
+    zsfree(lastmsg);
+    zfunsetparam("ZFTP_SESSION");
+    freelinklist(zfsessions, (FreeFunc) freesession);
+    zfree(zfstatusp, sizeof(int)*zfsesscnt);
+    deletebuiltins("zftp", bintab, sizeof(bintab)/sizeof(*bintab));
+}
+
+static int
+zftpexithook(Hookdef d, void *dummy)
+{
+    zftp_cleanup();
+    return 0;
+}
+
 /* The load/unload routines required by the zsh library interface */
 
 /**/
@@ -3211,7 +3244,7 @@ int
 boot_(Module m)
 {
     int ret;
-    if ((ret = addbuiltins(m->nam, bintab,
+    if ((ret = addbuiltins("zftp", bintab,
 			   sizeof(bintab)/sizeof(*bintab))) == 1) {
 	/* if successful, set some default parameters */
 	off_t tmout_def = 60;
@@ -3223,6 +3256,8 @@ boot_(Module m)
     
 	zfsessions = znewlinklist();
 	newsession("default");
+
+	addhookfunc("exit", zftpexithook);
     }
 
     return !ret;
@@ -3232,27 +3267,8 @@ boot_(Module m)
 int
 cleanup_(Module m)
 {
-    /*
-     * There are various parameters hanging around, but they're
-     * all non-special so are entirely non-life-threatening.
-     */
-    LinkNode nptr;
-    Zftp_session cursess = zfsess;
-    for (zfsessno = 0, nptr = firstnode(zfsessions); nptr;
-	 zfsessno++, incnode(nptr)) {
-	zfsess = (Zftp_session)nptr->dat;
-	zfclosedata();
-	/*
-	 * When closing the current session, do the usual unsetting,
-	 * otherwise don't.
-	 */
-	zfclose(zfsess != cursess);
-    }
-    zsfree(lastmsg);
-    zfunsetparam("ZFTP_SESSION");
-    freelinklist(zfsessions, (FreeFunc) freesession);
-    zfree(zfstatusp, sizeof(int)*zfsesscnt);
-    deletebuiltins(m->nam, bintab, sizeof(bintab)/sizeof(*bintab));
+    deletehookfunc("exit", zftpexithook);
+    zftp_cleanup();
     return 0;
 }