<<< Date Index >>>     <<< Thread Index >>>

[PATCH] Clean up mx.c: init stat structs to zero



# HG changeset patch
# User Aron Griffis <agriffis@xxxxxxxxx>
# Date 1183988719 14400
# Branch HEAD
# Node ID 1d6b7eee167c081494f8c6962241035adc7dc03f
# Parent  8d68a4432571f5d47a19792f899fd5fa0d7cb58e
Clean up mx.c: init stat structs to zero

prev_sb.st_size was initialized to zero if USE_FCNTL but not if
USE_FLOCK.  Silence the gcc warning for both cases by zeroing the stat
structures on the stack.

Other minor cleanups:
- clean up some whitespace
- convert return (foo) to return foo
- return r at the bottom of the function instead of testing values and
  returning them explicitly
- add vim modeline

Signed-off-by: Aron Griffis <agriffis@xxxxxxxxx>

diff -r 8d68a4432571 -r 1d6b7eee167c mx.c
--- a/mx.c      Sun Jul 08 20:32:21 2007 -0400
+++ b/mx.c      Mon Jul 09 09:45:19 2007 -0400
@@ -149,7 +149,7 @@ int mx_lock_file (const char *path, int 
 #if defined (USE_FCNTL) || defined (USE_FLOCK)
   int count;
   int attempt;
-  struct stat prev_sb;
+  struct stat sb = { 0 }, prev_sb = { 0 }; /* silence gcc warnings */
 #endif
   int r = 0;
 
@@ -162,19 +162,17 @@ int mx_lock_file (const char *path, int 
 
   count = 0;
   attempt = 0;
-  prev_sb.st_size = 0; /* silence a GCC warning */
   while (fcntl (fd, F_SETLK, &lck) == -1)
   {
-    struct stat sb;
     dprint(1,(debugfile, "mx_lock_file(): fcntl errno %d.\n", errno));
     if (errno != EAGAIN && errno != EACCES)
     {
       mutt_perror ("fcntl");
-      return (-1);
+      return -1;
     }
 
     if (fstat (fd, &sb) != 0)
-     sb.st_size = 0;
+      sb.st_size = 0;
     
     if (count == 0)
       prev_sb = sb;
@@ -184,7 +182,7 @@ int mx_lock_file (const char *path, int 
     {
       if (timeout)
        mutt_error _("Timeout exceeded while attempting fcntl lock!");
-      return (-1);
+      return -1;
     }
 
     prev_sb = sb;
@@ -199,7 +197,6 @@ int mx_lock_file (const char *path, int 
   attempt = 0;
   while (flock (fd, (excl ? LOCK_EX : LOCK_SH) | LOCK_NB) == -1)
   {
-    struct stat sb;
     if (errno != EWOULDBLOCK)
     {
       mutt_perror ("flock");
@@ -207,11 +204,11 @@ int mx_lock_file (const char *path, int 
       break;
     }
 
-    if (fstat(fd,&sb) != 0 )
-     sb.st_size=0;
+    if (fstat(fd, &sb) != 0)
+      sb.st_size = 0;
     
     if (count == 0)
-      prev_sb=sb;
+      prev_sb = sb;
 
     /* only unlock file if it is unchanged */
     if (prev_sb.st_size == sb.st_size && ++count >= (timeout?MAXLOCKATTEMPT:0))
@@ -234,7 +231,7 @@ int mx_lock_file (const char *path, int 
     r = dotlock_file (path, fd, timeout);
 #endif /* USE_DOTLOCK */
 
-  if (r == -1)
+  if (r != 0)
   {
     /* release any other locks obtained in this routine */
 
@@ -246,11 +243,9 @@ int mx_lock_file (const char *path, int 
 #ifdef USE_FLOCK
     flock (fd, LOCK_UN);
 #endif /* USE_FLOCK */
-
-    return (-1);
-  }
-
-  return 0;
+  }
+
+  return r;
 }
 
 int mx_unlock_file (const char *path, int fd, int dot)
@@ -1640,3 +1635,5 @@ int mx_check_empty (const char *path)
   }
   /* not reached */
 }
+
+/* vim: set sw=2: */