Imported from ../bash-3.1.tar.gz.
[platform/upstream/bash.git] / mailcheck.c
index 3f8a1b8..4b7e207 100644 (file)
@@ -1,6 +1,6 @@
 /* mailcheck.c -- The check is in the mail... */
 
-/* Copyright (C) 1987,1989 Free Software Foundation, Inc.
+/* Copyright (C) 1987-2004 Free Software Foundation, Inc.
 
 This file is part of GNU Bash, the Bourne Again SHell.
 
@@ -31,6 +31,7 @@ Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
 #endif
 #include "posixtime.h"
 #include "bashansi.h"
+#include "bashintl.h"
 
 #include "shell.h"
 #include "execute_cmd.h"
@@ -75,7 +76,7 @@ time_to_check_mail ()
 {
   char *temp;
   time_t now;
-  long seconds;
+  intmax_t seconds;
 
   temp = get_string_value ("MAILCHECK");
 
@@ -121,6 +122,15 @@ find_mail_file (file)
     } \
   while (0)
 
+#define UPDATE_MAIL_FILE(i, finfo) \
+  do \
+    { \
+      mailfiles[i]->access_time = finfo.st_atime; \
+      mailfiles[i]->mod_time = finfo.st_mtime; \
+      mailfiles[i]->file_size = finfo.st_size; \
+    } \
+  while (0)
+
 static void
 update_mail_file (i)
      int i;
@@ -130,11 +140,7 @@ update_mail_file (i)
 
   file = mailfiles[i]->name;
   if (mailstat (file, &finfo) == 0)
-    {
-      mailfiles[i]->access_time = finfo.st_atime;
-      mailfiles[i]->mod_time = finfo.st_mtime;
-      mailfiles[i]->file_size = finfo.st_size;
-    }
+    UPDATE_MAIL_FILE (i, finfo);
   else
     RESET_MAIL_FILE (i);
 }
@@ -154,11 +160,8 @@ add_mail_file (file, msg)
   if (i >= 0)
     {
       if (mailstat (filename, &finfo) == 0)
-       {
-         mailfiles[i]->mod_time = finfo.st_mtime;
-         mailfiles[i]->access_time = finfo.st_atime;
-         mailfiles[i]->file_size = finfo.st_size;
-       }
+       UPDATE_MAIL_FILE (i, finfo);
+
       free (filename);
       return i;
     }
@@ -181,9 +184,7 @@ reset_mail_files ()
   register int i;
 
   for (i = 0; i < mailfiles_count; i++)
-    {
-      RESET_MAIL_FILE (i);
-    }
+    RESET_MAIL_FILE (i);
 }
 
 /* Free the information that we have about the remembered mail files. */
@@ -207,7 +208,8 @@ free_mail_files ()
 }
 
 /* Return non-zero if FILE's mod date has changed and it has not been
-   accessed since modified. */
+   accessed since modified.  If the size has dropped to zero, reset
+   the cached mail file info. */
 static int
 file_mod_date_changed (i)
      int i;
@@ -222,6 +224,9 @@ file_mod_date_changed (i)
   if ((mailstat (file, &finfo) == 0) && (finfo.st_size > 0))
     return (mtime != finfo.st_mtime);
 
+  if (finfo.st_size == 0 && mailfiles[i]->file_size > 0)
+    UPDATE_MAIL_FILE (i, finfo);
+
   return (0);
 }
 
@@ -289,6 +294,7 @@ parse_mailpath_spec (str)
 char *
 make_default_mailpath ()
 {
+#if defined (DEFAULT_MAIL_DIRECTORY)
   char *mp;
 
   get_current_user_info ();
@@ -297,6 +303,9 @@ make_default_mailpath ()
   mp[sizeof(DEFAULT_MAIL_DIRECTORY) - 1] = '/';
   strcpy (mp + sizeof (DEFAULT_MAIL_DIRECTORY), current_user.user_name);
   return (mp);
+#else
+  return ((char *)NULL);
+#endif
 }
 
 /* Remember the dates of the files specified by MAILPATH, or if there is
@@ -321,8 +330,11 @@ remember_mail_dates ()
   if (mailpaths == 0)
     {
       mailpaths = make_default_mailpath ();
-      add_mail_file (mailpaths, (char *)NULL);
-      free (mailpaths);
+      if (mailpaths)
+       {
+         add_mail_file (mailpaths, (char *)NULL);
+         free (mailpaths);
+       }
       return;
     }
 
@@ -371,9 +383,9 @@ check_mail ()
          int file_is_bigger;
 
          use_user_notification = mailfiles[i]->msg != (char *)NULL;
-         message = mailfiles[i]->msg ? mailfiles[i]->msg : "You have mail in $_";
+         message = mailfiles[i]->msg ? mailfiles[i]->msg : _("You have mail in $_");
 
-         bind_variable ("_", current_mail_file);
+         bind_variable ("_", current_mail_file, 0);
 
 #define atime mailfiles[i]->access_time
 #define mtime mailfiles[i]->mod_time
@@ -396,7 +408,7 @@ check_mail ()
          /* If the mod time is later than the access time and the file
             has grown, note the fact that this is *new* mail. */
          if (use_user_notification == 0 && (atime < mtime) && file_is_bigger)
-           message = "You have new mail in $_";
+           message = _("You have new mail in $_");
 #undef atime
 #undef mtime
 
@@ -412,13 +424,13 @@ check_mail ()
       if (mail_warning && file_access_date_changed (i))
        {
          update_mail_file (i);
-         printf ("The mail in %s has been read\n", current_mail_file);
+         printf (_("The mail in %s has been read\n"), current_mail_file);
        }
     }
 
   if (dollar_underscore)
     {
-      bind_variable ("_", dollar_underscore);
+      bind_variable ("_", dollar_underscore, 0);
       free (dollar_underscore);
     }
   else