/* -*-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.
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;
}
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);
vfprintf(stderr, f, ap);
va_end(ap);
putc('\n', stderr);
- exit(EXIT_FAILURE);
+ exit(EX_FAIL);
}
/*----- Memory allocation -------------------------------------------------*/
void *xmalloc(size_t sz)
{
void *p = malloc(sz);
- if (!p)
- die("not enough memory");
+ if (!p) die("not enough memory");
return (p);
}
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);
}
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;
}
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++;
}