nano-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Nano-devel] merged patch


From: Steven Kneizys
Subject: Re: [Nano-devel] merged patch
Date: Thu, 21 Feb 2002 21:40:44 -0800 (PST)

As a quickie thing, I changed the get_next_file to use only the buffer
given:
void get_next_filename(char *name, char *ret)
{
    int i = 0, max_int_digits = num_of_digits(INT_MAX);
    char cnum[max_int_digits + 2];
    struct stat fs;

    strcpy(ret, name);

    while(1) {

       if (stat(ret, &fs) == -1)
           break;
       if (i == INT_MAX)
           break;

       i++;
       sprintf(cnum, ".%d", i);
       strcpy(ret, name);
       strcat(ret, cnum);
    }

    if (!(i < INT_MAX))
       strcpy(ret,"");
}

I was thinking maybe pass it a static buffer initially instead of a
malloced one as well.  I was thinking it would be good to prevent an
error if the get_next_filename fails, so I changed the calls to be of
the form:

       get_next_filename(name, ret);
       if (ret[0] == '\0') i = -1; else i = write_file(ret, 1, 0, 0);

I earlier found and submitted this as part of a patch but didn't
mention it, in files.c there was a non-wrapped realloc call:
@@ -2465,7 +2453,7 @@
                char *saveanswer = NULL;
 
                saveanswer = mallocstrcpy(saveanswer, answer);
-               answer = realloc(answer, strlen(path) + strlen(saveanswer) + 2);
+               answer = nrealloc(answer, strlen(path) + strlen(saveanswer) + 
2);
                sprintf(answer, "%s/%s", path, saveanswer);
                free(saveanswer);
            }

I added a status bar change notification when changing buffers, it
looked odd to me when reading in a file, then switching buffers, that
the "read lines" message was still there.  This is is the routines that
switch to prev and next open file:

    load_open_file();
 
    statusbar(_("Switched to %s"),open_files->data);

in proto.h, this was declared twice, I deleted the second one:

void *nmalloc(size_t howmuch);

I also added the multi-buffer patch, the free_shortcutage patch, and my
prior patches where the multi buffers were switched instead of copied
and the memory was freed.  Also, memory was freed after do_browser code
for the path names and the files themselves, and an off-by-one in
free-ing of the array pointers in files.c:

@@ -2178,7 +2150,7 @@
 {
     int i;
 
-    for (i = 0; i < len - 1; i++)
+    for (i = 0; i < len; i++)
        free(array[i]);
     free(array);
 }

All of this is combined and merged into the attached patch of merged
patches ;-)

The memory leaks seem to be plugged on my version of things, but I
notice I am a few lines off from other folks in the 'diff' files so I
don't know what I am missing!  I'll fing out on the next tarball I am
sure.  Anyway, the main advantage of "thanks_for_the_memories" is that
right after I can run a check to make sure all memory is free, and I
haven't spotted any problems recently :)  I'll have to compile in more
options and see what happens.

thanks,

Steve...


--- Chris Allegretta <address@hidden> wrote:
> 
> On Thu, Feb 21, 2002 at 02:09:17PM -0800, Steven Kneizys wrote:
> > Hi,
> > 
> > I got most of the merged patch to go in easily using a lil fuzz and
> > installing the gnu patch 2.5.4 (of course the native patch program
> > didn't understand the patch!), except my native compiler on the
> Compaq
> > Alpha Unix 4.0d has a lil problem with some new stuff:
> > 
> > cc: Error: files.c, line 329: In the declaration of "cnum",
> > "max_int_digits" is not constant, but occurs in a context that
> requires
> > a constant expression.
> >     char cnum[max_int_digits + 2];
> > --------------^
> > cc: Error: files.c, line 330: In the declaration of "buf",
> > "strlen(...)" is not constant, but occurs in a context that
> requires a
> > constant expression.
> >     char buf[strlen(name) + max_int_digits + 2];
> > -------------^
> > cc: Error: files.c, line 330: In the declaration of "buf",
> > "max_int_digits" is not constant, but occurs in a context that
> requires
> > a constant expression.
> >     char buf[strlen(name) + max_int_digits + 2];
> > ----------------------------^
> 
> Ack, I'm surprised gcc lets this go through without a complaint.  I
> dont 
> think this type of declaration is portable.  I'll try and bang on
> this 
> patch this weekend, there just seems to be something about it...it
> seems 
> like we're doing an awful lot of memory allocations.  Considering the
> 
> program is (in theory) exiting with abnormal status I would tend to
> try 
> and keep the malloc()ing to a minimum.  Let me have a look.
> 
> Chris A
> -- 
> Chris Allegretta      http://www.asty.org
> 
> "Share and Enjoy" - Douglas Adams, 1952 - 2001
> 
> _______________________________________________
> Nano-devel mailing list
> address@hidden
> http://mail.gnu.org/mailman/listinfo/nano-devel


__________________________________________________
Do You Yahoo!?
Yahoo! Sports - Coverage of the 2002 Olympic Games
http://sports.yahoo.com
diff -u nano-1.1.6-cvs/files.c nano-1.1.6-cvs.spk/files.c
--- nano-1.1.6-cvs/files.c      Sun Feb 17 19:03:38 2002
+++ nano-1.1.6-cvs.spk/files.c  Thu Feb 21 22:27:09 2002
@@ -54,18 +54,10 @@
     current = fileage;
 
 #ifdef ENABLE_MULTIBUFFER
-    /* if quiet is zero, add a new entry to the open_files structure, and
-       do duplicate checking; otherwise, update the current entry and
-       don't do duplicate checking (the latter is needed in the case of
-       the alternate spell checker); if a duplicate entry was found,
-       reload the currently open file (it may have been changed during
-       duplicate handling) */
-    if (quiet != 0)
-       quiet = 1;
-    if (add_open_file(quiet, 1 - quiet) == 2) {
-       load_open_file();
-       statusbar(_("File already loaded"));
-    }
+    /* if quiet is zero, add a new entry to the open_files structure;
+       otherwise, update the current entry (the latter is needed in the
+       case of the alternate spell checker) */
+    add_open_file(quiet);
 #endif
 
     wmove(edit, current_y, current_x);
@@ -89,12 +81,11 @@
 
 #ifdef ENABLE_MULTIBUFFER
     /* if there aren't any entries in open_files, create the entry for
-       this new file, and, of course, don't bother checking for
-       duplicates; without this, if nano is started without a filename on
-       the command line, a new file will be created, but it will be given
-       no open_files entry, leading to problems later on */
+       this new file; without this, if nano is started without a filename
+       on the command line, a new file will be created, but it will be
+       given no open_files entry, leading to problems later on */
     if (!open_files) {
-       add_open_file(0, 0);
+       add_open_file(0);
        /* turn off view mode in this case; this is for consistency
           whether multibuffers are compiled in or not */
        UNSET(VIEW_MODE);
@@ -210,8 +201,9 @@
            buf[0] = 0;
            i = 0;
 #ifndef NANO_SMALL
-        } else if (!ISSET(NO_CONVERT) && input[0] < 32 
-                       && input[0] != '\r' && input[0] != '\n') 
+        } else if (!ISSET(NO_CONVERT) && input[0] >= 0 && input[0] <= 31 
+                       && input[0] != '\t' && input[0] != '\r'
+                       && input[0] != '\n') 
            /* If the file has binary chars in it, don't stupidly
                assume it's a DOS or Mac formatted file! */
            SET(NO_CONVERT);
@@ -295,9 +287,8 @@
     struct stat fileinfo;
 
     if (!strcmp(filename, "") || stat(filename, &fileinfo) == -1) {
-       if (insert) {
-           if (!quiet)
-               statusbar(_("\"%s\" not found"), filename);
+       if (insert && !quiet) {
+           statusbar(_("\"%s\" not found"), filename);
            return -1;
        } else {
            /* We have a new file */
@@ -332,6 +323,31 @@
     return 1;
 }
 
+void get_next_filename(char *name, char *ret)
+{
+    int i = 0, max_int_digits = num_of_digits(INT_MAX);
+    char cnum[max_int_digits + 2];
+    struct stat fs;
+
+    strcpy(ret, name);
+
+    while(1) {
+
+       if (stat(ret, &fs) == -1)
+           break;
+       if (i == INT_MAX)
+           break;
+
+       i++;
+       sprintf(cnum, ".%d", i);
+       strcpy(ret, name);
+       strcat(ret, cnum);
+    }
+
+    if (!(i < INT_MAX))
+       strcpy(ret,"");
+}
+
 int do_insertfile(int loading_file)
 {
     int i;
@@ -343,7 +359,17 @@
     currshortcut = insertfile_list;
 #endif
 
-    i = statusq(1, insertfile_list, "", _("File to insert [from ./] "));
+#ifndef DISABLE_OPERATINGDIR
+    if ((operating_dir) && (strcmp(operating_dir,"."))){
+       i = statusq(1, insertfile_list, "", _("File to insert [from %s] "),
+               operating_dir);
+    } else {
+#endif
+       i = statusq(1, insertfile_list, "", _("File to insert [from ./] "));
+#ifndef DISABLE_OPERATINGDIR
+    }
+#endif
+
     if (i != -1) {
 
 #ifdef DEBUG
@@ -365,19 +391,24 @@
 #endif
 
 #ifdef DISABLE_TABCOMP
+            if (realname != NULL) free(realname);
            realname = NULL;
 #endif
-           if  (tmp != NULL)
-               realname = mallocstrcpy(realname, tmp);
-           else
-               return do_insertfile(loading_file);
-       }
+           if (tmp != NULL) {
+               realname = mallocstrcpy(realname, tmp);
+               free(tmp);
+           } else {
+               if (realname != NULL) free(realname);
+               return do_insertfile(loading_file);
+           }
+       }
 #endif
 
 #ifndef DISABLE_OPERATINGDIR
        if (operating_dir) {
            if (check_operating_dir(realname, 0)) {
                statusbar(_("Can't insert file from outside of %s"), 
operating_dir);
+               if (realname != NULL) free(realname);
                return 0;
            }
        }
@@ -386,18 +417,15 @@
 #ifdef ENABLE_MULTIBUFFER
        if (loading_file) {
 
-           /* update the current entry in the open_files structure; we
-              don't need to check for duplicate entries (the conditions
-              that could create them are taken care of elsewhere) */
-           add_open_file(1, 0);
+           /* update the current entry in the open_files structure */
+           add_open_file(1);
 
-           free_filestruct(fileage);
            new_file();
            UNSET(MODIFIED);
        }
 #endif
 
-       i = open_file(realname, 1, 0);
+       i = open_file(realname, 1, loading_file);
 
 #ifdef ENABLE_MULTIBUFFER
        if (loading_file)
@@ -471,25 +499,15 @@
 /*
  * Add/update an entry to the open_files filestruct.  If update is
  * zero, a new entry is created; otherwise, the current entry is updated.
- * If dup_fix is zero, checking for and handling duplicate entries is not
- * done; otherwise, it is.  Return 0 on success, 1 on error, or 2 on
- * finding a duplicate entry.
+ * Return 0 on success or 1 on error.
  */
-int add_open_file(int update, int dup_fix)
+int add_open_file(int update)
 {
     filestruct *tmp;
 
     if (!fileage || !current || !filename)
        return 1;
 
-    /* first, if duplicate checking is allowed, do it */
-    if (dup_fix) {
-
-       /* if duplicates were found and handled, we're done */
-       if (open_file_dup_fix(update))
-           return 2;
-    }
-
     /* if no entries, make the first one */
     if (!open_files) {
        open_files = make_new_node(NULL);
@@ -497,6 +515,7 @@
        /* if open_files->file is NULL at the nrealloc() below, we get a
           segfault
        open_files->file = open_files; */
+       open_files->file = NULL;
     }
 
     else if (!update) {
@@ -515,14 +534,12 @@
        /* if open_files->file is NULL at the nrealloc() below, we get a
           segfault
        open_files->file = open_files; */
+       open_files->file = NULL;
     }
 
     /* save current filename */
     open_files->data = mallocstrcpy(open_files->data, filename);
 
-    /* save the full path location */
-    open_files->file_path = get_full_path(open_files->data);
-
     /* save current total number of lines */
     open_files->file_totlines = totlines;
 
@@ -547,8 +564,7 @@
     if (!(ISSET(VIEW_MODE) && !update)) {
        /* save current filestruct and restore full file position
           afterward */
-       open_files->file = nmalloc(sizeof(filestruct));
-       open_files->file = copy_filestruct(fileage);
+       open_files->file = fileage; 
        do_gotopos(open_files->lineno, open_files->file_current_x, 
open_files->file_current_y, open_files->file_placewewant);
     }
 
@@ -574,9 +590,6 @@
     /* save current filename */
     open_files->data = mallocstrcpy(open_files->data, filename);
 
-    /* save the full path location */
-    open_files->file_path = get_full_path(open_files->data);
-
     return 0;
 }
 
@@ -593,7 +606,7 @@
     /* set up the filename, the file buffer, the total number of lines in
        the file, and the total file size */
     filename = mallocstrcpy(filename, open_files->data);
-    fileage = copy_filestruct(open_files->file);
+    fileage = open_files->file;
     current = fileage;
     totlines = open_files->file_totlines;
     totsize = open_files->file_totsize;
@@ -630,95 +643,6 @@
 }
 
 /*
- * Search the open_files structure for an entry with the same value for
- * the file_path member as the current entry (i. e. a duplicate entry).
- * If one is found, return a pointer to it; otherwise, return NULL.
- *
- * Note: This should only be called inside open_file_dup_fix().
- */
-filestruct *open_file_dup_search(int update)
-{
-    filestruct *tmp;
-    char *path;
-
-    if (!open_files || !filename)
-       return NULL;
-
-    tmp = open_files;
-    path = get_full_path(filename);
-
-    /* if there's only one entry, handle it */
-    if (!tmp->prev && !tmp->next) {
-       if (!strcmp(tmp->file_path, path))
-           return tmp;
-    }
-
-    /* otherwise, go to the beginning */
-    while (tmp->prev)
-       tmp = tmp->prev;
-
-    /* and search the entries one by one */
-    while (tmp) {
-
-       if (!strcmp(tmp->file_path, path)) {
-
-           if (!update)
-               /* if we're making a new entry and there's an entry with
-                  the same full path, we've found a duplicate */
-               return tmp;
-           else {
-
-               /* if we're updating an existing entry and there's an
-                  entry with the same full path that isn't the current
-                  entry, we've found a duplicate */
-               if (tmp != open_files) 
-                   return tmp;
-           }
-       }
-
-       /* go to the next entry */
-       tmp = tmp->next;
-
-    }
-
-    return NULL;
-}
-
-/*
- * Search for duplicate entries in the open_files structure using
- * open_file_dup_search(), and, if one is found, handle it properly.
- * Return 0 if no duplicates were found, and 1 otherwise.
- */
-int open_file_dup_fix(int update)
-{
-    filestruct *tmp = open_file_dup_search(update);
-
-    if (!tmp)
-       return 0;
-
-    /* if there's only one entry, handle it */
-    if (!tmp->prev && !tmp->next)
-       return 1;
-
-    /* otherwise, if we're not updating, the user's trying to load a
-       duplicate; switch to the original instead */
-    if (!update) {
-       open_files = tmp;
-       return 1;
-    }
-
-    /* if we are updating, the filename's been changed via a save; it's
-       thus more recent than the original, so remove the original */
-    else {
-       unlink_node(tmp);
-       free_filestruct(tmp->file);
-       free(tmp->file_path);
-       delete_node(tmp);
-    }
-    return 0;
-}
-
-/*
  * Open the previous entry in the open_files structure.  If closing_file
  * is zero, update the current entry before switching from it.
  * Otherwise, we are about to close that entry, so don't bother doing so.
@@ -730,10 +654,9 @@
        return 1;
 
     /* if we're not about to close the current entry, update it before
-       doing anything; since we're only switching, we don't need to check
-       for duplicate entries */
-    if (!closing_file)
-       add_open_file(1, 0);
+       doing anything */
+    if (!closing_file) 
+       add_open_file(1);
 
     if (!open_files->prev && !open_files->next) {
 
@@ -766,6 +689,8 @@
 
     load_open_file();
 
+    statusbar(_("Switched to %s"),open_files->data);
+
 #ifdef DEBUG
     dump_buffer(current);
 #endif
@@ -792,10 +717,9 @@
        return 1;
 
     /* if we're not about to close the current entry, update it before
-       doing anything; since we're only switching, we don't need to check
-       for duplicate entries */
+       doing anything */
     if (!closing_file)
-       add_open_file(1, 0);
+       add_open_file(1);
 
     if (!open_files->prev && !open_files->next) {
 
@@ -828,6 +752,8 @@
 
     load_open_file();
 
+    statusbar(_("Switched to %s"),open_files->data);
+
 #ifdef DEBUG
     dump_buffer(current);
 #endif
@@ -862,7 +788,6 @@
 
     unlink_node(tmp);
     free_filestruct(tmp->file);
-    free(tmp->file_path);
     delete_node(tmp);
 
     shortcut_init(0);
@@ -871,7 +796,7 @@
 }
 #endif /* MULTIBUFFER */
 
-#if defined (ENABLE_MULTIBUFFER) || !defined (DISABLE_SPELLER) || !defined 
(DISABLE_OPERATINGDIR)
+#if !defined (DISABLE_SPELLER) || !defined (DISABLE_OPERATINGDIR)
 /*
  * When passed "[relative path]" or "[relative path][filename]" in
  * origpath, return "[full path]" or "[full path][filename]" on success,
@@ -988,8 +913,10 @@
                        strcat(d_there, "/");
                    }
                }
-               else
+               else {
+                   free(newpath);
                    return NULL;
+               }
            }
 
            /* finally, go back to where we were before, d_here (no error
@@ -1020,7 +947,7 @@
 
     return newpath;
 }
-#endif /* ENABLE_MULTIBUFFER || !DISABLE_SPELLER || !DISABLE_OPERATINGDIR */
+#endif /* !DISABLE_SPELLER || !DISABLE_OPERATINGDIR */
 
 #ifndef DISABLE_SPELLER
 /*
@@ -1156,10 +1083,10 @@
  */
 int check_operating_dir(char *currpath, int allow_tabcomp)
 {
-    /* this is static so that we only need to get it the first time this
-       function is called; also, a relative operating directory path will
+    /* The char *full_operating_dir is global for mem cleanup, and
+       therefore we only need to get it the first time this function
+       is called; also, a relative operating directory path will
        only be handled properly if this is done */
-    static char *full_operating_dir = NULL;
 
     char *fullpath, *whereami1, *whereami2 = NULL;
 
@@ -1206,17 +1133,22 @@
        whereami2 = strstr(full_operating_dir, fullpath);
 
     /* if both searches failed, we're outside the operating directory */
-    if (!whereami1 && !whereami2)
+    if (!whereami1 && !whereami2) {
+       free(fullpath);
        return 1;
+    }
 
     /* check the search results; if the full operating directory path is
        not at the beginning of the full current path (for normal usage)
        and vice versa (for tab completion, if we're allowing it), we're
        outside the operating directory */
-    if (whereami1 != fullpath && whereami2 != full_operating_dir)
+    if (whereami1 != fullpath && whereami2 != full_operating_dir) {
+       free(fullpath);
        return 1;
+    }
 
     /* otherwise, we're still inside it */
+    free(fullpath);
     return 0;
 }
 #endif
@@ -1238,11 +1170,11 @@
 int write_file(char *name, int tmp, int append, int nonamechange)
 {
     long size, lineswritten = 0;
-    static char *buf = NULL;
+    char *buf = NULL;
     filestruct *fileptr;
     int fd, mask = 0, realexists, anyexists;
     struct stat st, lst;
-    static char *realname = NULL;
+    char *realname = NULL;
 
     if (!strcmp(name, "")) {
        statusbar(_("Cancelled"));
@@ -1251,12 +1183,6 @@
     titlebar(NULL);
     fileptr = fileage;
 
-    if (realname != NULL)
-       free(realname);
-
-    if (buf != NULL)
-       free(buf);
-
 #ifndef DISABLE_TABCOMP
     realname = real_dir_from_tilde(name);
 #else
@@ -1269,7 +1195,7 @@
           operating directory, so skip the operating directory test */
        if (check_operating_dir(realname, 0)) {
            statusbar(_("Can't write outside of %s"), operating_dir);
-
+           free(realname);
            return -1;
        }
     }
@@ -1282,8 +1208,13 @@
     anyexists = lstat(realname, &lst);
 
     /* New case: if the file exists, just give up */
-    if (tmp && anyexists != -1)
+    if (tmp && anyexists != -1) {
+       if (realname != NULL) 
+           free(realname);
+       if (buf != NULL)
+           free(buf);
        return -1;
+    }
     /* NOTE: If you change this statement, you MUST CHANGE the if 
        statement below (that says:
                if (realexists == -1 || tmp || (!ISSET(FOLLOW_SYMLINKS) &&
@@ -1301,6 +1232,10 @@
 
        /* First, just give up if we couldn't even open the file */
        if (fd == -1) {
+           if (realname != NULL) 
+               free(realname);
+           if (buf != NULL)
+               free(buf);
            if (!tmp && ISSET(TEMP_OPT)) {
                UNSET(TEMP_OPT);
                return do_writeout(filename, 1, 0);
@@ -1317,6 +1252,10 @@
        strncpy(buf, realname, strlen(realname)+1);
        strcat(buf, ".XXXXXX");
        if ((fd = mkstemp(buf)) == -1) {
+           if (realname != NULL) 
+               free(realname);
+           if (buf != NULL)
+               free(buf);
            if (ISSET(TEMP_OPT)) {
                UNSET(TEMP_OPT);
                return do_writeout(filename, 1, 0);
@@ -1337,6 +1276,10 @@
        if (size == -1) {
            statusbar(_("Could not open file for writing: %s"),
                      strerror(errno));
+           if (realname != NULL) 
+               free(realname);
+           if (buf != NULL)
+               free(buf);
            return -1;
        } else {
 #ifdef DEBUG
@@ -1360,6 +1303,10 @@
        if (size == -1) {
            statusbar(_("Could not open file for writing: %s"),
                      strerror(errno));
+           if (realname != NULL) 
+               free(realname);
+           if (buf != NULL)
+               free(buf);
            return -1;
        } else if (size > 0) {
 #ifndef NANO_SMALL
@@ -1369,6 +1316,10 @@
                if (size == -1) {
                    statusbar(_("Could not open file for writing: %s"),
                          strerror(errno));
+                   if (realname != NULL) 
+                       free(realname);
+                   if (buf != NULL)
+                       free(buf);
                    return -1;
                }
            }
@@ -1381,6 +1332,10 @@
                if (size == -1) {
                    statusbar(_("Could not open file for writing: %s"),
                          strerror(errno));
+                   if (realname != NULL) 
+                       free(realname);
+                   if (buf != NULL)
+                       free(buf);
                    return -1;
                }
            }
@@ -1391,6 +1346,10 @@
     if (close(fd) == -1) {
        statusbar(_("Could not close %s: %s"), realname, strerror(errno));
        unlink(buf);
+       if (realname != NULL) 
+           free(realname);
+       if (buf != NULL)
+           free(buf);
        return -1;
     }
 
@@ -1415,6 +1374,10 @@
                statusbar(_("Could not open %s for writing: %s"),
                          realname, strerror(errno));
                unlink(buf);
+               if (realname != NULL) 
+                   free(realname);
+               if (buf != NULL)
+                   free(buf);
                return -1;
            }
        }
@@ -1424,11 +1387,19 @@
            statusbar(_("Could not open %s for writing: %s"),
                      name, strerror(errno));
            unlink(buf);
+           if (realname != NULL) 
+               free(realname);
+           if (buf != NULL)
+               free(buf);
            return -1;
        } else if (rename(buf, realname) == -1) {       /* Try a rename?? */
            statusbar(_("Could not open %s for writing: %s"),
                      realname, strerror(errno));
            unlink(buf);
+           if (realname != NULL) 
+               free(realname);
+           if (buf != NULL)
+               free(buf);
            return -1;
        }
     }
@@ -1444,6 +1415,10 @@
        UNSET(MODIFIED);
        titlebar(NULL);
     }
+    if (realname != NULL) 
+       free(realname);
+    if (buf != NULL)
+       free(buf);
     return 1;
 }
 
@@ -1602,18 +1577,16 @@
            if (!exiting) {
 
                /* first, if the filename was changed during the save,
-                  update the filename and full path stored in the
-                  current entry, and then update the current entry,
-                  checking for duplicate entries */
+                  update the filename stored in the current entry, and
+                  then update the current entry */
                if (strcmp(open_files->data, filename)) {
                    open_file_change_name();
-                   add_open_file(1, 1);
+                   add_open_file(1);
                }
                else {
 
-                   /* otherwise, just update the current entry without
-                      checking for duplicate entries */
-                   add_open_file(1, 0);
+                   /* otherwise, just update the current entry */
+                   add_open_file(1);
                }
            }
 #endif
@@ -2127,7 +2100,6 @@
 
 }
 
-
 /* Initialize the browser code, including the list of files in *path */
 char **browser_init(char *path, int *longest, int *numents)
 {
@@ -2178,7 +2150,7 @@
 {
     int i;
 
-    for (i = 0; i < len - 1; i++)
+    for (i = 0; i < len; i++)
        free(array[i]);
     free(array);
 }
@@ -2217,7 +2189,10 @@
     if (tmp != foo)
        *tmp = 0;
     else
+    { /* SPK may need to make a 'default' path here */
+        if (*tmp != '/') *(tmp) = '.';
        *(tmp+1) = 0;
+    }
 
     return;
 }
@@ -2411,6 +2386,17 @@
            }
 #endif
 
+           /* SPK for '.' path, get the current path via getcwd */
+           if (!strcmp(path, "./..")) {
+               free(path);
+               path = getcwd(NULL, 0);
+               striponedir(path);                  
+               align(&path);
+               free_charptrarray(filelist, numents);
+               free(foo);
+               return do_browser(path);
+           }
+
            st = filestat(path);
            if (S_ISDIR(st.st_mode)) {
                if ((test_dir = opendir(path)) == NULL) {
@@ -2431,6 +2417,8 @@
                }
 
                /* Start over again with the new path value */
+               free_charptrarray(filelist, numents);
+               free(foo);
                return do_browser(path);
            } else {
                retval = path;
@@ -2465,7 +2453,7 @@
                char *saveanswer = NULL;
 
                saveanswer = mallocstrcpy(saveanswer, answer);
-               answer = realloc(answer, strlen(path) + strlen(saveanswer) + 2);
+               answer = nrealloc(answer, strlen(path) + strlen(saveanswer) + 
2);
                sprintf(answer, "%s/%s", path, saveanswer);
                free(saveanswer);
            }
@@ -2592,6 +2580,7 @@
 {
     struct stat st;
     char *tmp = NULL;
+    char *ret;
 
     tmp = mallocstrcpy(tmp, inpath);
 
@@ -2603,23 +2592,27 @@
 #else
        char *from = getcwd(NULL, 0);
 #endif /* PATH_MAX */
-       return do_browser(from ? from : "./");
+        if (tmp != NULL) free(tmp); 
+       ret = do_browser(from ? from : "./");
+        if (from != NULL) free(from); 
+       return ret;
     }
 
     /* If the string is a directory, pass do_browser that */
     st = filestat(tmp);
-    if (S_ISDIR(st.st_mode))
-       return do_browser(tmp);
+    if (S_ISDIR(st.st_mode)){
+       ret = do_browser(tmp);
+       free(tmp);
+       return ret;
+    }
 
     /* Okay, there's a dir in there, but not at the end of the string... 
        try stripping it off */
     striponedir(tmp);
     align(&tmp);
-    return do_browser(tmp);
-
+    ret = do_browser(tmp);
+    free(tmp);
+    return ret;
 }
-
-
-
 #endif
 
diff -u nano-1.1.6-cvs/global.c nano-1.1.6-cvs.spk/global.c
--- nano-1.1.6-cvs/global.c     Sun Feb 17 19:03:51 2002
+++ nano-1.1.6-cvs.spk/global.c Thu Feb 21 22:46:25 2002
@@ -35,6 +35,7 @@
 /*
  * Global variables
  */
+
 int flags = 0;                 /* Our new flag containing many options */
 WINDOW *edit;                  /* The file portion of the editor  */
 WINDOW *topwin;                        /* Top line of screen */
@@ -76,8 +77,8 @@
 int mark_beginx;               /* X value in the string to start */
 
 #ifndef DISABLE_OPERATINGDIR
-char *operating_dir = NULL;    /* Operating directory, which we can't go
-                                  higher than */
+char *operating_dir = NULL;    /* Operating directory, which we can't */
+char *full_operating_dir = NULL;/* go higher than */
 #endif
 
 #ifndef DISABLE_SPELLER
@@ -87,7 +88,7 @@
 shortcut *main_list = NULL;
 shortcut *whereis_list = NULL;
 shortcut *replace_list = NULL;
-shortcut *replace_list_2;      /* 2nd half of replace dialog */
+shortcut *replace_list_2 = NULL;       /* 2nd half of replace dialog */
 shortcut *goto_list = NULL;
 shortcut *gotodir_list = NULL;
 shortcut *writefile_list = NULL;
@@ -271,6 +272,12 @@
     char *nano_openprev_msg = "", *nano_opennext_msg = "";
 #endif
 
+    int reinit_main_list = 0;
+    if (main_list) {
+       free_shortcutage(&main_list);
+       reinit_main_list = 1;
+    }
+ 
     nano_help_msg = _("Invoke the help menu");
     nano_writeout_msg = _("Write the current file to disk");
 
@@ -475,6 +482,9 @@
                    NANO_OPENNEXT_KEY, 0, 0, VIEW, open_nextfile_void);
 #endif
 
+    if (reinit_main_list)
+       return;
+ 
     sc_init_one(&whereis_list, NANO_HELP_KEY,
                _("Get Help"), nano_help_msg, 0, 0, 0, VIEW, do_help);
 
@@ -654,4 +664,109 @@
 #endif
 
     toggle_init();
+}
+
+/* delete the structure */
+int free_shortcutage(shortcut **shortcutage)
+{
+    shortcut *s,*ps;
+
+    s = *shortcutage;
+    if (s == NULL) {
+       return;
+    } else {
+       s = *shortcutage;
+       do {
+               ps = s;
+               s = s->next; 
+               free(ps);
+       } while (s->next != NULL);
+       free(s);
+       *shortcutage = NULL;
+    }
+}
+
+#ifndef NANO_SMALL
+/* clear the toggles */
+void free_toggles(void)
+{
+    toggle *u,*lu;
+
+    if (toggles == NULL) {
+       return;
+    } else {
+       lu = NULL;
+       for (u = toggles; u->next != NULL; u = u->next) {
+               if (lu != NULL) free(lu);
+               lu = u;
+       }
+       if (lu != NULL) free(lu);
+       if (u != NULL) free(u);
+    }
+}
+#endif
+
+/* added by SPK for memory cleanup, gracefully return our malloc()s */
+int thanks_for_the_memories(void) 
+{
+    if (operating_dir != NULL)
+       free(operating_dir);
+    if (full_operating_dir != NULL)
+       free(full_operating_dir);
+    if (last_search != NULL)
+       free(last_search);
+    if (last_replace != NULL)
+       free(last_replace);
+    if (hblank != NULL)
+       free(hblank);
+    if (alt_speller != NULL)
+       free(alt_speller);
+    if (help_text != NULL)
+       free(help_text);
+    if (filename != NULL)
+       free(filename);
+    if (answer != NULL)
+       free(answer);
+    if (cutbuffer != NULL)
+        free_filestruct(cutbuffer);
+
+    free_shortcutage(&main_list);
+    free_shortcutage(&whereis_list);
+    free_shortcutage(&replace_list);
+    free_shortcutage(&replace_list_2);
+    free_shortcutage(&help_list);
+    free_shortcutage(&writefile_list);
+    free_shortcutage(&insertfile_list);
+    free_shortcutage(&spell_list);
+#ifndef DISABLE_BROWSER
+    free_shortcutage(&browser_list);
+#endif
+    free_shortcutage(&gotodir_list);
+    free_shortcutage(&goto_list);
+
+#ifndef NANO_SMALL
+    free_toggles();
+#endif
+
+#ifdef ENABLE_MULTIBUFFER
+    if (open_files != NULL){
+        while (open_files->prev != NULL) 
+           open_files = open_files->prev;
+        while (open_files->next != NULL) {
+           if (open_files->file == fileage) fileage=NULL;
+           free_filestruct(open_files->file);
+           open_files = open_files->next;
+           free_filestruct(open_files->prev);
+        }
+        if (open_files->file == fileage) fileage=NULL;
+       free_filestruct(open_files->file);
+        free_filestruct(open_files);
+    }
+#endif
+
+    if (fileage != NULL)
+        free_filestruct(fileage);
+
+    /* that is all for now */
+
 }
diff -u nano-1.1.6-cvs/nano.1 nano-1.1.6-cvs.spk/nano.1
--- nano-1.1.6-cvs/nano.1       Fri Jan  4 20:45:44 2002
+++ nano-1.1.6-cvs.spk/nano.1   Thu Feb 21 19:43:10 2002
@@ -45,6 +45,9 @@
 .B \-M (\-\-mac)
 Write file in Mac format.
 .TP
+.B \-N (\-\-noconvert)
+Disable automatic conversion of files from DOS/Mac format.
+.TP
 .B \-R (\-\-regexp)
 Enable regular expression matching for search strings, as well as
 \\n subexpression replacement for replace strings, if available.
diff -u nano-1.1.6-cvs/nano.c nano-1.1.6-cvs.spk/nano.c
--- nano-1.1.6-cvs/nano.c       Sun Feb 17 19:04:07 2002
+++ nano-1.1.6-cvs.spk/nano.c   Thu Feb 21 21:00:19 2002
@@ -100,6 +100,8 @@
     /* Restore the old term settings */
     tcsetattr(0, TCSANOW, &oldterm);
 
+    thanks_for_the_memories();
+
     exit(sigage);
 }
 
@@ -158,27 +160,34 @@
 
 void die_save_file(char *die_filename)
 {
-    char *name;
-    int i;
+    char *name, *ret;
+    int i, int_max_digits = num_of_digits(INT_MAX);
 
     /* if we can't save we have REAL bad problems,
      * but we might as well TRY. */
     if (die_filename[0] == '\0') {
        name = "nano.save";
-       i = write_file(name, 1, 0, 0);
-    } else {
-
+       ret = charalloc(strlen(name) + int_max_digits + 2);
+       get_next_filename(name, ret);
+       if (ret[0] == '\0') i = -1; else i = write_file(ret, 1, 0, 0);
+       name = ret;
+    }
+    else {
        char *buf = charalloc(strlen(die_filename) + 6);
        strcpy(buf, die_filename);
        strcat(buf, ".save");
-       i = write_file(buf, 1, 0, 0);
-       name = buf;
+       ret = charalloc(strlen(buf) + int_max_digits + 2);
+       get_next_filename(buf, ret);
+       if (ret[0] == '\0') i = -1; else i = write_file(ret, 1, 0, 0);
+       name = ret;
     }
 
     if (i != -1)
        fprintf(stderr, _("\nBuffer written to %s\n"), name);
     else
-       fprintf(stderr, _("\nNo %s written (file exists?)\n"), name);
+       fprintf(stderr, _("\nNo %s written (too many backup files?)\n"), name);
+
+    free(ret);
 }
 
 /* Die with an error message that the screen was too small if, well, the
@@ -1744,9 +1753,8 @@
 #ifdef ENABLE_MULTIBUFFER
     /* update the current open_files entry before spell-checking, in case
        any problems occur; the case of there being no open_files entries
-       is handled elsewhere (before we reach this point); no duplicate
-       checking is needed here */
-    add_open_file(1, 0);
+       is handled elsewhere (before we reach this point) */
+    add_open_file(1);
 #endif
 
     if (alt_speller)
@@ -2454,12 +2462,11 @@
                "or --multibuffer command line flags, the Meta-F toggle or "
                "using a nanorc file, inserting a file will cause it to be "
                "loaded into a separate buffer (use Meta-< and > to switch "
-               "between file buffers).\n\n In multiple buffer mode, the "
-               "same file cannot be loaded twice, not even a \"New "
-               "Buffer.\" A workaround to load another blank buffer is to "
-               "load a nonexistent filename into a separate buffer.\n\n "
-               "The following function keys are available in Insert File "
-               "mode:\n\n");
+               "between file buffers).\n\n If you need another blank "
+               "buffer, just press Enter at the prompt without typing in a "
+               "filename, or type in a nonexistent filename at the prompt "
+               "and press Enter.\n\n The following function keys are "
+               "available in Insert File mode:\n\n");
     else if (currshortcut == writefile_list)
        ptr = _("Write File Help Text\n\n "
                "Type the name that you wish to save the current file "
@@ -3358,3 +3365,5 @@
     finish(0);
 
 }
+
+
diff -u nano-1.1.6-cvs/nano.h nano-1.1.6-cvs.spk/nano.h
--- nano-1.1.6-cvs/nano.h       Sun Feb 17 19:17:22 2002
+++ nano-1.1.6-cvs.spk/nano.h   Thu Feb 21 19:43:10 2002
@@ -79,7 +79,6 @@
     int file_current_x;                /* Current file's x-coordinate position 
*/
     int file_current_y;                /* Current file's y-coordinate position 
*/
     int file_modified;         /* Current file's modification status */
-    char *file_path;           /* Current file's full path location */
     int file_placewewant;      /* Current file's place we want */
     int file_totlines;         /* Current file's total number of lines */
     long file_totsize;         /* Current file's total size */
diff -u nano-1.1.6-cvs/proto.h nano-1.1.6-cvs.spk/proto.h
--- nano-1.1.6-cvs/proto.h      Sun Feb 17 19:17:30 2002
+++ nano-1.1.6-cvs.spk/proto.h  Thu Feb 21 22:19:30 2002
@@ -47,6 +47,7 @@
 extern char *last_replace;
 #ifndef DISABLE_OPERATINGDIR
 extern char *operating_dir;
+extern char *full_operating_dir;
 #endif
 #ifndef DISABLE_SPELLER
 extern  char *alt_speller;
@@ -91,6 +92,7 @@
 int search_init(int replacing);
 int renumber(filestruct * fileptr);
 int free_filestruct(filestruct * src);
+int free_shortcutage(shortcut **src);
 int xplustabs(void);
 int do_yesno(int all, int leavecursor, char *msg, ...);
 int actual_x(filestruct * fileptr, int xplus);
@@ -104,9 +106,10 @@
 int open_file(char *filename, int insert, int quiet);
 int do_insertfile(int loading_file);
 int length_of_list(shortcut *s);
+int num_of_digits(int n);
 
 #ifdef ENABLE_MULTIBUFFER
-int add_open_file(int update, int dup_fix);
+int add_open_file(int update);
 #endif
 
 #ifndef DISABLE_OPERATINGDIR
@@ -165,7 +168,6 @@
 void nano_small_msg(void);
 void nano_disable_msg(void);
 void do_early_abort(void);
-void *nmalloc(size_t howmuch);
 void *nrealloc(void *ptr, size_t howmuch);
 void die(char *msg, ...);
 void die_save_file(char *die_filename);
@@ -186,6 +188,7 @@
 void unlink_node(filestruct * fileptr);
 void cut_marked_segment(filestruct * top, int top_x, filestruct * bot,
                         int bot_x, int destructive);
+void get_next_filename(char *name, char *ret);
 
 #ifdef ENABLE_NANORC
 void do_rcfile(void);
@@ -212,7 +215,6 @@
 int keypad_on(WINDOW * win, int newval);
 
 #ifdef ENABLE_MULTIBUFFER
-int open_file_dup_fix(int update);
 int open_prevfile(int closing_file), open_nextfile(int closing_file);
 int open_prevfile_void(void), open_nextfile_void(void);
 #endif
@@ -250,12 +252,6 @@
 filestruct *findnextstr(int quiet, int bracket_mode, filestruct * begin,
                        int beginx, char *needle);
 
-#ifdef ENABLE_MULTIBUFFER
-filestruct *open_file_dup_search(int update);
-#endif
-
 #ifndef DISABLE_HELP
 void help_init(void);
 #endif
-
-
diff -u nano-1.1.6-cvs/rcfile.c nano-1.1.6-cvs.spk/rcfile.c
--- nano-1.1.6-cvs/rcfile.c     Sun Feb 17 19:04:15 2002
+++ nano-1.1.6-cvs.spk/rcfile.c Thu Feb 21 19:43:10 2002
@@ -432,7 +432,6 @@
 {
     char *unable = _("Unable to open ~/.nanorc file, %s");
     struct stat fileinfo;
-    int skip=0;
     FILE *rcstream;
 
     if (getenv("HOME") == NULL)
diff -u nano-1.1.6-cvs/search.c nano-1.1.6-cvs.spk/search.c
--- nano-1.1.6-cvs/search.c     Sun Feb 17 19:04:33 2002
+++ nano-1.1.6-cvs.spk/search.c Sun Feb 17 21:46:35 2002
@@ -151,6 +151,7 @@
        reset_cursor();
        free(backupstring);
        backupstring = NULL;
+       free(buf);
        return -1;
     } else 
     switch (i) {
@@ -191,22 +192,27 @@
                TOGGLE(t->flag);
 #endif
 
+       free(buf);
        return 1;
     case NANO_OTHERSEARCH_KEY:
        backupstring = mallocstrcpy(backupstring, answer);
+       free(buf);
        return -2;              /* Call the opposite search function */
     case NANO_FROMSEARCHTOGOTO_KEY:
        free(backupstring);
        backupstring = NULL;
        do_gotoline_void();
+       free(buf);
        return -3;
     default:
        do_early_abort();
        free(backupstring);
        backupstring = NULL;
+       free(buf);
        return -3;
     }
 
+    free(buf);
     return 0;
 }
 
@@ -711,7 +717,7 @@
     int i, numreplaced, beginx;
     filestruct *begin;
     char *prevanswer = NULL, *buf = NULL;
-
+    
     if (ISSET(VIEW_MODE)) {
        print_view_warning();
        replace_abort();
@@ -795,6 +801,8 @@
     edit_update(current, CENTER);
     print_replaced(numreplaced);
     replace_abort();
+    free(prevanswer);
+    if (buf != NULL) free(buf);
     return 1;
 }
 
diff -u nano-1.1.6-cvs/utils.c nano-1.1.6-cvs.spk/utils.c
--- nano-1.1.6-cvs/utils.c      Sun Feb 17 19:04:41 2002
+++ nano-1.1.6-cvs.spk/utils.c  Thu Feb 21 22:06:36 2002
@@ -36,6 +36,21 @@
 #define _(string) (string)
 #endif
 
+int num_of_digits(int n)
+{
+    int i = 1;
+
+    if (n < 0)
+       n = 0 - n;
+
+    while (n > 10) {
+       n /= 10;
+       i++;
+    }
+
+    return i;
+}
+
 /* Lower case a string - must be null terminated */
 void lowercase(char *src)
 {

reply via email to

[Prev in Thread] Current Thread [Next in Thread]