*.c: General spring-clean of the coding style.
[anag] / util.c
diff --git a/util.c b/util.c
index d35f4af..223fcf4 100644 (file)
--- a/util.c
+++ b/util.c
@@ -1,13 +1,11 @@
 /* -*-c-*-
  *
- * $Id: util.c,v 1.2 2004/04/08 01:36:19 mdw Exp $
- *
  * Various useful utilities, stolen from mLib
  *
  * (c) 2001 Mark Wooding
  */
 
-/*----- Licensing notice --------------------------------------------------* 
+/*----- Licensing notice --------------------------------------------------*
  *
  * This file is part of Anag: a simple wordgame helper.
  *
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
- * 
+ *
  * Anag is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
- * 
+ *
  * You should have received a copy of the GNU General Public License
  * along with Anag; if not, write to the Free Software Foundation,
  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
@@ -58,12 +56,9 @@ static const char *quis = "<unset>";
 void ego(const char *p)
 {
   const char *q = p;
-  while (*q) {
-    if (*q++ == PATHSEP)
-      p = q;
-  }
-  if (*p == '-')
-    p++;
+  while (*q)
+    if (*q++ == PATHSEP) p = q;
+  if (*p == '-') p++;
   quis = p;
 }
 
@@ -89,20 +84,16 @@ int pquis(FILE *fp, const char *p)
   while (*p) {
     sz = strcspn(p, "$");
     if (sz) {
-      if (fwrite(p, 1, sz, fp) < sz)
-       return (EOF);
+      if (fwrite(p, 1, sz, fp) < sz) return (EOF);
       p += sz;
     }
     if (*p == '$') {
       p++;
       if (*p == '$') {
-       if (fputc('$', fp) == EOF)
-         return (EOF);
+       if (fputc('$', fp) == EOF) return (EOF);
        p++;
-      } else {
-       if (fputs(quis, fp) == EOF)
-         return (EOF);
-      }
+      } else if (fputs(quis, fp) == EOF)
+       return (EOF);
     }
   }
   return (0);
@@ -126,7 +117,7 @@ void die(const char *f, ...)
   vfprintf(stderr, f, ap);
   va_end(ap);
   putc('\n', stderr);
-  exit(EXIT_FAILURE);
+  exit(EX_FAIL);
 }
 
 /*----- Memory allocation -------------------------------------------------*/
@@ -144,8 +135,7 @@ void die(const char *f, ...)
 void *xmalloc(size_t sz)
 {
   void *p = malloc(sz);
-  if (!p)
-    die("not enough memory");
+  if (!p) die("not enough memory");
   return (p);
 }
 
@@ -163,8 +153,7 @@ void *xmalloc(size_t sz)
 void *xrealloc(void *p, size_t sz)
 {
   p = realloc(p, sz);
-  if (!p)
-    die("not enough memory");
+  if (!p) die("not enough memory");
   return (p);
 }
 
@@ -210,23 +199,15 @@ void dstr_ensure(dstr *d, size_t sz)
   size_t rq = d->len + sz;
   size_t nsz;
 
-  /* --- If we have enough space, just leave it --- */
-
-  if (rq <= d->sz)
-    return;
-
-  /* --- Grow the buffer --- */
+  /* If we have enough space, just leave it. */
+  if (rq <= d->sz) return;
 
+  /* Grow the buffer. */
   nsz = d->sz;
-
-  if (nsz == 0)
-    nsz = (DSTR_INITSZ >> 1);
+  if (nsz == 0) nsz = (DSTR_INITSZ >> 1);
   do nsz <<= 1; while (nsz < rq);
-
-  if (d->buf)
-    d->buf = xrealloc(d->buf, nsz);
-  else
-    d->buf = xmalloc(nsz);
+  if (d->buf) d->buf = xrealloc(d->buf, nsz);
+  else d->buf = xmalloc(nsz);
   d->sz = nsz;
 }
 
@@ -252,33 +233,27 @@ int dstr_putline(dstr *d, FILE *fp)
 
   for (;;) {
 
-    /* --- Read the next byte --- */
-
+    /* Read the next byte. */
     ch = getc(fp);
 
-    /* --- End-of-file when no characters read is special --- */
-
-    if (ch == EOF && !rd)
-      return (EOF);
-
-    /* --- Make sure there's some buffer space --- */
+    /* End-of-file when no characters read is special. */
+    if (ch == EOF && !rd) return (EOF);
 
+    /* Make sure there's some buffer space. */
     if (!left) {
       d->len = off;
       dstr_ensure(d, 1);
       left = d->sz - off;
     }
 
-    /* --- End-of-file or newline ends the loop --- */
-
+    /* End-of-file or newline ends the loop. */
     if (ch == EOF || ch == '\n') {
       d->buf[off] = 0;
       d->len = off;
       return rd;
     }
 
-    /* --- Append the character and continue --- */
-
+    /* Append the character and continue. */
     d->buf[off++] = ch;
     left--; rd++;
   }