From f7567c55126775c1c40ca77f77e7109de522c709 Mon Sep 17 00:00:00 2001
From: Christoph <ckubu@oopen.de>
Date: Sun, 14 Dec 2025 11:48:10 +0100
Subject: [PATCH 1/5] Fix utime() usage for modern glibc

---
 src/osdep/unix/mbx.c    | 12 ++++++------
 src/osdep/unix/mmdf.c   | 10 +++++-----
 src/osdep/unix/mtx.c    | 16 ++++++++--------
 src/osdep/unix/os_slx.h | 20 ++++++++++++++++++++
 src/osdep/unix/tenex.c  | 16 ++++++++--------
 src/osdep/unix/unix.c   | 11 ++++++-----
 6 files changed, 53 insertions(+), 32 deletions(-)

diff --git a/src/osdep/unix/mbx.c b/src/osdep/unix/mbx.c
index 1ece5d8..251ccfb 100644
--- a/src/osdep/unix/mbx.c
+++ b/src/osdep/unix/mbx.c
@@ -300,7 +300,7 @@ int mbx_isvalid (MAILSTREAM **stream,char *name,char *tmp,int *ld,char *lock,
     if (sbuf.st_ctime > sbuf.st_atime) {
       tp[0] = sbuf.st_atime;	/* preserve atime and mtime */
       tp[1] = sbuf.st_mtime;
-      utime (tmp,tp);		/* set the times */
+      set_utime_from_tp (tmp,tp);		/* set the times */
     }
   }
 				/* in case INBOX but not mbx format */
@@ -774,7 +774,7 @@ void mbx_flag (MAILSTREAM *stream,char *sequence,char *flag,long flags)
 	 stream->user_flags[LOCAL->ffuserflag]) || (oldpid != LOCAL->lastpid))
       mbx_update_header (stream);
     tp[0] = time (0);		/* make sure read comes after all that */
-    utime (stream->mailbox,tp);
+    set_utime_from_tp(stream->mailbox,tp);
   }
   if (LOCAL->ld >= 0) {		/* unlock now */
     unlockfd (LOCAL->ld,LOCAL->lock);
@@ -1073,7 +1073,7 @@ long mbx_copy (MAILSTREAM *stream,char *sequence,char *mailbox,long options)
 				/* else preserve \Marked status */
   else tp[0] = (sbuf.st_ctime > sbuf.st_atime) ? sbuf.st_atime : time(0);
   tp[1] = sbuf.st_mtime;	/* preserve mtime */
-  utime (file,tp);		/* set the times */
+  set_utime_from_tp(file,tp);		/* set the times */
   close (fd);			/* close the file */
   MM_NOCRITICAL (stream);	/* release critical */
   unlockfd (ld,lock);		/* release exclusive parse/append permission */
@@ -1211,7 +1211,7 @@ long mbx_append (MAILSTREAM *stream,char *mailbox,append_t af,void *data)
 				/* else preserve \Marked status */
     else tp[0] = (sbuf.st_ctime > sbuf.st_atime) ? sbuf.st_atime : time(0);
     tp[1] = sbuf.st_mtime;	/* preserve mtime */
-    utime (file,tp);		/* set the times */
+    set_utime_from_tp(file,tp);		/* set the times */
     fclose (df);		/* close the file */
     MM_NOCRITICAL (dstream);	/* release critical */
   }
@@ -1444,7 +1444,7 @@ long mbx_parse (MAILSTREAM *stream)
     time_t tp[2];
     tp[0] = time (0);
     tp[1] = LOCAL->filetime;
-    utime (stream->mailbox,tp);
+    set_utime_from_tp(stream->mailbox,tp);
   }
   stream->silent = silent;	/* can pass up events now */
   mail_exists (stream,nmsgs);	/* notify upper level of new mailbox size */
@@ -1812,7 +1812,7 @@ unsigned long mbx_rewrite (MAILSTREAM *stream,unsigned long *reclaimed,
   fstat (LOCAL->fd,&sbuf);	/* get new write time */
   tp[1] = LOCAL->filetime = sbuf.st_mtime;
   tp[0] = time (0);		/* reset atime to now */
-  utime (stream->mailbox,tp);
+  set_utime_from_tp(stream->mailbox,tp);
   unlockfd (ld,lock);		/* release exclusive parse/append permission */
 				/* notify upper level of new mailbox size */
   mail_exists (stream,stream->nmsgs);
diff --git a/src/osdep/unix/mmdf.c b/src/osdep/unix/mmdf.c
index e962434..968e306 100644
--- a/src/osdep/unix/mmdf.c
+++ b/src/osdep/unix/mmdf.c
@@ -379,7 +379,7 @@ long mmdf_isvalid (char *name,char *tmp)
       if ((sbuf.st_ctime > sbuf.st_atime) || (sbuf.st_mtime > sbuf.st_atime)) {
 	tp[0] = sbuf.st_atime;	/* preserve atime and mtime */
 	tp[1] = sbuf.st_mtime;
-	utime (file,tp);	/* set the times */
+	set_utime_from_tp(file,tp);	/* set the times */
       }
     }
   }
@@ -1131,7 +1131,7 @@ long mmdf_copy (MAILSTREAM *stream,char *sequence,char *mailbox,long options)
   else tp[0] =			/* else preserve \Marked status */
 	 ((sbuf.st_ctime > sbuf.st_atime) || (sbuf.st_mtime > sbuf.st_atime)) ?
 	 sbuf.st_atime : tp[1];
-  utime (file,tp);		/* set the times */
+  set_utime_from_tp(file,tp);		/* set the times */
   mmdf_unlock (fd,NIL,&lock);	/* unlock and close mailbox */
   if (tstream) {		/* update last UID if we can */
     MMDFLOCAL *local = (MMDFLOCAL *) tstream->local;
@@ -1292,7 +1292,7 @@ long mmdf_append (MAILSTREAM *stream,char *mailbox,append_t af,void *data)
     ret = NIL;			/* return error */
   }
   else tp[0] = tp[1] - 1;	/* set atime to now-1 if successful copy */
-  utime (file,tp);		/* set the times */
+  set_utime_from_tp(file,tp);		/* set the times */
   fclose (sf);			/* done with scratch file */
 				/* force UIDVALIDITY assignment now */
   if (tstream && !tstream->uid_validity) tstream->uid_validity = time (0);
@@ -1550,7 +1550,7 @@ void mmdf_unlock (int fd,MAILSTREAM *stream,DOTLOCK *lock)
     }
     else now = 0;		/* no time change needed */
 				/* set the times, note change */
-    if (now && !utime (stream->mailbox,tp)) LOCAL->filetime = tp[1];
+    if (now && !set_utime_from_tp(stream->mailbox,tp)) LOCAL->filetime = tp[1];
   }
   flock (fd,LOCK_UN);		/* release flock'ers */
   if (!stream) close (fd);	/* close the file if no stream */
@@ -2393,7 +2393,7 @@ long mmdf_rewrite (MAILSTREAM *stream,unsigned long *nexp,DOTLOCK *lock,
 				/* set atime to now, mtime a second earlier */
     tp[1] = (tp[0] = time (0)) - 1;
 				/* set the times, note change */
-    if (!utime (stream->mailbox,tp)) LOCAL->filetime = tp[1];
+    if (!set_utime_from_tp(stream->mailbox,tp)) LOCAL->filetime = tp[1];
     close (LOCAL->fd);		/* close and reopen file */
     if ((LOCAL->fd = open (stream->mailbox,O_RDWR,
 			   (long) mail_parameters (NIL,GET_MBXPROTECTION,NIL)))
diff --git a/src/osdep/unix/mtx.c b/src/osdep/unix/mtx.c
index 8e6f76e..b00cebf 100644
--- a/src/osdep/unix/mtx.c
+++ b/src/osdep/unix/mtx.c
@@ -196,7 +196,7 @@ int mtx_isvalid (char *name,char *tmp)
       if (sbuf.st_ctime > sbuf.st_atime) {
 	tp[0] = sbuf.st_atime;	/* preserve atime and mtime */
 	tp[1] = sbuf.st_mtime;
-	utime (file,tp);	/* set the times */
+	set_utime_from_tp(file,tp);	/* set the times */
       }
     }
   }
@@ -565,7 +565,7 @@ void mtx_flag (MAILSTREAM *stream,char *sequence,char *flag,long flags)
     fstat (LOCAL->fd,&sbuf);	/* get current write time */
     tp[1] = LOCAL->filetime = sbuf.st_mtime;
     tp[0] = time (0);		/* make sure read comes after all that */
-    utime (stream->mailbox,tp);
+    set_utime_from_tp(stream->mailbox,tp);
   }
 }
 
@@ -834,7 +834,7 @@ long mtx_expunge (MAILSTREAM *stream,char *sequence,long options)
       fstat (LOCAL->fd,&sbuf);	/* get new write time */
       tp[1] = LOCAL->filetime = sbuf.st_mtime;
       tp[0] = time (0);		/* reset atime to now */
-      utime (stream->mailbox,tp);
+      set_utime_from_tp(stream->mailbox,tp);
       MM_NOCRITICAL (stream);	/* release critical */
 				/* notify upper level of new mailbox size */
       mail_exists (stream,stream->nmsgs);
@@ -929,7 +929,7 @@ long mtx_copy (MAILSTREAM *stream,char *sequence,char *mailbox,long options)
 				/* else preserve \Marked status */
   else tp[0] = (sbuf.st_ctime > sbuf.st_atime) ? sbuf.st_atime : time(0);
   tp[1] = sbuf.st_mtime;	/* preserve mtime */
-  utime (file,tp);		/* set the times */
+  set_utime_from_tp(file,tp);		/* set the times */
   close (fd);			/* close the file */
   unlockfd (ld,lock);		/* release exclusive parse/append permission */
   MM_NOCRITICAL (stream);	/* release critical */
@@ -946,7 +946,7 @@ long mtx_copy (MAILSTREAM *stream,char *sequence,char *mailbox,long options)
       fstat (LOCAL->fd,&sbuf);	/* get current write time */
       tp[1] = LOCAL->filetime = sbuf.st_mtime;
       tp[0] = time (0);		/* make sure atime remains greater */
-      utime (stream->mailbox,tp);
+      set_utime_from_tp(stream->mailbox,tp);
     }
   }
   if (ret && mail_parameters (NIL,GET_COPYUID,NIL))
@@ -1062,7 +1062,7 @@ long mtx_append (MAILSTREAM *stream,char *mailbox,append_t af,void *data)
 				/* else preserve \Marked status */
   else tp[0] = (sbuf.st_ctime > sbuf.st_atime) ? sbuf.st_atime : time(0);
   tp[1] = sbuf.st_mtime;	/* preserve mtime */
-  utime (file,tp);		/* set the times */
+  set_utime_from_tp(file,tp);		/* set the times */
   fclose (df);			/* close the file */
   unlockfd (ld,lock);		/* release exclusive parse/append permission */
   MM_NOCRITICAL (stream);	/* release critical */
@@ -1212,7 +1212,7 @@ long mtx_parse (MAILSTREAM *stream)
     time_t tp[2];
     tp[0] = time (0);
     tp[1] = LOCAL->filetime;
-    utime (stream->mailbox,tp);
+    set_utime_from_tp(stream->mailbox,tp);
   }
   stream->silent = silent;	/* can pass up events now */
   mail_exists (stream,nmsgs);	/* notify upper level of new mailbox size */
@@ -1312,7 +1312,7 @@ void mtx_update_status (MAILSTREAM *stream,unsigned long msgno,long syncflag)
       fstat (LOCAL->fd,&sbuf);	/* get new write time */
       tp[1] = LOCAL->filetime = sbuf.st_mtime;
       tp[0] = time (0);		/* make sure read is later */
-      utime (stream->mailbox,tp);
+      set_utime_from_tp(stream->mailbox,tp);
     }
   }
 }
diff --git a/src/osdep/unix/os_slx.h b/src/osdep/unix/os_slx.h
index b5f39ff..a316317 100644
--- a/src/osdep/unix/os_slx.h
+++ b/src/osdep/unix/os_slx.h
@@ -50,6 +50,26 @@
 #include <syslog.h>
 #include <sys/file.h>
 
+#include <utime.h>
+
+static inline int set_utime_from_tp (const char *path, time_t *tp)
+{
+  struct utimbuf ub;
+  ub.actime  = tp[0];
+  ub.modtime = tp[1];
+  return utime (path, &ub);
+}
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+int safe_flock (int fd, int operation);
+
+#ifdef __cplusplus
+}
+#endif
+
 
 /* Linux gets this wrong */
 
diff --git a/src/osdep/unix/tenex.c b/src/osdep/unix/tenex.c
index eee61fb..fd03c23 100644
--- a/src/osdep/unix/tenex.c
+++ b/src/osdep/unix/tenex.c
@@ -203,7 +203,7 @@ int tenex_isvalid (char *name,char *tmp)
       if (sbuf.st_ctime > sbuf.st_atime) {
 	tp[0] = sbuf.st_atime;	/* preserve atime and mtime */
 	tp[1] = sbuf.st_mtime;
-	utime (file,tp);	/* set the times */
+	set_utime_from_tp(file,tp);	/* set the times */
       }
     }
   }
@@ -654,7 +654,7 @@ void tenex_flag (MAILSTREAM *stream,char *sequence,char *flag,long flags)
     fstat (LOCAL->fd,&sbuf);	/* get current write time */
     tp[1] = LOCAL->filetime = sbuf.st_mtime;
     tp[0] = time (0);		/* make sure read comes after all that */
-    utime (stream->mailbox,tp);
+    set_utime_from_tp(stream->mailbox,tp);
   }
 }
 
@@ -924,7 +924,7 @@ long tenex_expunge (MAILSTREAM *stream,char *sequence,long options)
       fstat (LOCAL->fd,&sbuf);	/* get new write time */
       tp[1] = LOCAL->filetime = sbuf.st_mtime;
       tp[0] = time (0);		/* reset atime to now */
-      utime (stream->mailbox,tp);
+      set_utime_from_tp(stream->mailbox,tp);
       MM_NOCRITICAL (stream);	/* release critical */
 				/* notify upper level of new mailbox size */
       mail_exists (stream,stream->nmsgs);
@@ -1019,7 +1019,7 @@ long tenex_copy (MAILSTREAM *stream,char *sequence,char *mailbox,long options)
 				/* else preserve \Marked status */
   else tp[0] = (sbuf.st_ctime > sbuf.st_atime) ? sbuf.st_atime : time(0);
   tp[1] = sbuf.st_mtime;	/* preserve mtime */
-  utime (file,tp);		/* set the times */
+  set_utime_from_tp(file,tp);		/* set the times */
   close (fd);			/* close the file */
   unlockfd (ld,lock);		/* release exclusive parse/append permission */
   MM_NOCRITICAL (stream);	/* release critical */
@@ -1036,7 +1036,7 @@ long tenex_copy (MAILSTREAM *stream,char *sequence,char *mailbox,long options)
       fstat (LOCAL->fd,&sbuf);	/* get current write time */
       tp[1] = LOCAL->filetime = sbuf.st_mtime;
       tp[0] = time (0);		/* make sure atime remains greater */
-      utime (stream->mailbox,tp);
+      set_utime_from_tp(stream->mailbox,tp);
     }
   }
   if (ret && mail_parameters (NIL,GET_COPYUID,NIL))
@@ -1159,7 +1159,7 @@ long tenex_append (MAILSTREAM *stream,char *mailbox,append_t af,void *data)
 				/* else preserve \Marked status */
   else tp[0] = (sbuf.st_ctime > sbuf.st_atime) ? sbuf.st_atime : time(0);
   tp[1] = sbuf.st_mtime;	/* preserve mtime */
-  utime (file,tp);		/* set the times */
+  set_utime_from_tp(file,tp);		/* set the times */
   fclose (df);			/* close the file */
   unlockfd (ld,lock);		/* release exclusive parse/append permission */
   MM_NOCRITICAL (stream);	/* release critical */
@@ -1324,7 +1324,7 @@ long tenex_parse (MAILSTREAM *stream)
     time_t tp[2];
     tp[0] = time (0);
     tp[1] = LOCAL->filetime;
-    utime (stream->mailbox,tp);
+    set_utime_from_tp(stream->mailbox,tp);
   }
   stream->silent = silent;	/* can pass up events now */
   mail_exists (stream,nmsgs);	/* notify upper level of new mailbox size */
@@ -1424,7 +1424,7 @@ void tenex_update_status (MAILSTREAM *stream,unsigned long msgno,long syncflag)
       fstat (LOCAL->fd,&sbuf);	/* get new write time */
       tp[1] = LOCAL->filetime = sbuf.st_mtime;
       tp[0] = time (0);		/* make sure read is later */
-      utime (stream->mailbox,tp);
+      set_utime_from_tp(stream->mailbox,tp);
     }
   }
 }
diff --git a/src/osdep/unix/unix.c b/src/osdep/unix/unix.c
index be3c437..58638d6 100644
--- a/src/osdep/unix/unix.c
+++ b/src/osdep/unix/unix.c
@@ -45,6 +45,7 @@ extern int errno;		/* just in case */
 #include "mail.h"
 #include "osdep.h"
 #include <time.h>
+#include <utime.h>
 #include <sys/stat.h>
 #include "unix.h"
 #include "pseudo.h"
@@ -228,7 +229,7 @@ DRIVER *unix_valid (char *name)
       if ((sbuf.st_ctime > sbuf.st_atime) || (sbuf.st_mtime > sbuf.st_atime)) {
 	tp[0] = sbuf.st_atime;	/* yes, preserve atime and mtime */
 	tp[1] = sbuf.st_mtime;
-	utime (file,tp);	/* set the times */
+	set_utime_from_tp (file,tp);	/* set the times */
       }
     }
   }
@@ -995,7 +996,7 @@ long unix_copy (MAILSTREAM *stream,char *sequence,char *mailbox,long options)
   else tp[0] =			/* else preserve \Marked status */
 	 ((sbuf.st_ctime > sbuf.st_atime) || (sbuf.st_mtime > sbuf.st_atime)) ?
 	 sbuf.st_atime : tp[1];
-  utime (file,tp);		/* set the times */
+  set_utime_from_tp (file,tp);		/* set the times */
   unix_unlock (fd,NIL,&lock);	/* unlock and close mailbox */
   if (tstream) {		/* update last UID if we can */
     UNIXLOCAL *local = (UNIXLOCAL *) tstream->local;
@@ -1156,7 +1157,7 @@ long unix_append (MAILSTREAM *stream,char *mailbox,append_t af,void *data)
     ret = NIL;			/* return error */
   }
   else tp[0] = tp[1] - 1;	/* set atime to now-1 if successful copy */
-  utime (file,tp);		/* set the times */
+  set_utime_from_tp (file,tp);		/* set the times */
   fclose (sf);			/* done with scratch file */
 				/* force UIDVALIDITY assignment now */
   if (tstream && !tstream->uid_validity) tstream->uid_validity = time (0);
@@ -1421,7 +1422,7 @@ void unix_unlock (int fd,MAILSTREAM *stream,DOTLOCK *lock)
     }
     else now = 0;		/* no time change needed */
 				/* set the times, note change */
-    if (now && !utime (stream->mailbox,tp)) LOCAL->filetime = tp[1];
+    if (now && !set_utime_from_tp (stream->mailbox,tp)) LOCAL->filetime = tp[1];
   }
   flock (fd,LOCK_UN);		/* release flock'ers */
   if (!stream) close (fd);	/* close the file if no stream */
@@ -2247,7 +2248,7 @@ long unix_rewrite (MAILSTREAM *stream,unsigned long *nexp,DOTLOCK *lock,
 				/* set atime to now, mtime a second earlier */
     tp[1] = (tp[0] = time (0)) - 1;
 				/* set the times, note change */
-    if (!utime (stream->mailbox,tp)) LOCAL->filetime = tp[1];
+    if (!set_utime_from_tp (stream->mailbox,tp)) LOCAL->filetime = tp[1];
     close (LOCAL->fd);		/* close and reopen file */
     if ((LOCAL->fd = open (stream->mailbox,O_RDWR,
 			   (long) mail_parameters (NIL,GET_MBXPROTECTION,NIL)))
-- 
2.47.3

