chiark / gitweb /
regress: Handle negative usec deltas
[adns.git] / regress / hplayback.c.m4
index dd98f0ec0b8bf1df4172286ebecfd07232318614..7d0660e459d9f4d1b0f162b30d29db49af1cd020 100644 (file)
@@ -170,19 +170,25 @@ static int Perrno(const char *stuff) {
 
 static void P_updatetime(void) {
   int chars;
-  unsigned long sec, usec;
+  unsigned long sec;
+  long usec;
 
   if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
   fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
   chars= -1;
-  sscanf(vb2.buf," +%lu.%lu%n",&sec,&usec,&chars);
+  sscanf(vb2.buf," +%lu.%ld%n",&sec,&usec,&chars);
   if (chars==-1) Psyntax("update time invalid");
   currenttime.tv_sec+= sec;
-  currenttime.tv_usec+= usec;
-  if (currenttime.tv_usec > 1000000) {
+  usec = (long)currenttime.tv_usec + usec;
+  while (usec < 0) {
+    currenttime.tv_sec--;
+    usec += 1000000;
+  }
+  while (usec > 1000000) {
     currenttime.tv_sec++;
-    currenttime.tv_usec -= 1000000;
+    usec -= 1000000;
   }
+  currenttime.tv_usec = usec;
   if (vb2.buf[chars] != hm_squote\nhm_squote) Psyntax("not newline after update time");
 }