0.5.4
- add show_cursor config option (idea from Cheryl Homiak)
- autoconf/automake update
+ - replace abook_malloc, abook_realloc and my_free with new xmalloc routines
0.5.3
- add allcvs filters (Christoph Sobotka)
abook_SOURCES = abook.c database.c filter.c list.c misc.c \
options.c edit.c ldif.c ui.c getname.c \
getopt.c getopt1.c abook_rl.c mbswidth.c \
+ xmalloc.c \
abook.h database.h filter.h list.h misc.h help.h \
options.h edit.h ldif.h abook_curses.h ui.h getname.h \
- getopt.h abook_rl.h mbswidth.h
+ getopt.h abook_rl.h mbswidth.h \
+ xmallloc.h
EXTRA_DIST = ANNOUNCE BUGS FAQ abook.1 abookrc.5 sample.abookrc abook.spec \
contrib
am_abook_OBJECTS = abook.$(OBJEXT) database.$(OBJEXT) filter.$(OBJEXT) \
list.$(OBJEXT) misc.$(OBJEXT) options.$(OBJEXT) edit.$(OBJEXT) \
ldif.$(OBJEXT) ui.$(OBJEXT) getname.$(OBJEXT) getopt.$(OBJEXT) \
- getopt1.$(OBJEXT) abook_rl.$(OBJEXT) mbswidth.$(OBJEXT)
+ getopt1.$(OBJEXT) abook_rl.$(OBJEXT) mbswidth.$(OBJEXT) \
+ xmalloc.$(OBJEXT)
abook_OBJECTS = $(am_abook_OBJECTS)
abook_LDADD = $(LDADD)
DEFAULT_INCLUDES = -I. -I$(srcdir) -I.
abook_SOURCES = abook.c database.c filter.c list.c misc.c \
options.c edit.c ldif.c ui.c getname.c \
getopt.c getopt1.c abook_rl.c mbswidth.c \
+ xmalloc.c \
abook.h database.h filter.h list.h misc.h help.h \
options.h edit.h ldif.h abook_curses.h ui.h getname.h \
- getopt.h abook_rl.h mbswidth.h
+ getopt.h abook_rl.h mbswidth.h \
+ xmallloc.h
EXTRA_DIST = ANNOUNCE BUGS FAQ abook.1 abookrc.5 sample.abookrc abook.spec \
contrib
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/misc.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/options.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ui.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/xmalloc.Po@am__quote@
.c.o:
@am__fastdepCC_TRUE@ if $(COMPILE) -MT $@ -MD -MP -MF "$(DEPDIR)/$*.Tpo" -c -o $@ $<; \
* Copyright (C) Jaakko Heinonen
*/
+#include <errno.h>
+#include <fcntl.h>
+#include <signal.h>
#include <stdio.h>
-#include <unistd.h>
#include <stdlib.h>
#include <string.h>
+#include <unistd.h>
#include <sys/stat.h>
-#include <signal.h>
-#include <fcntl.h>
-#include <errno.h>
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#if defined(HAVE_LOCALE_H) && defined(HAVE_SETLOCALE)
# include <locale.h>
#endif
+#include <assert.h>
#include "abook.h"
#include "ui.h"
#include "database.h"
#include "options.h"
#include "getname.h"
#include "getopt.h"
-#include <assert.h>
+#include "xmalloc.h"
static void init_abook();
static void quit_abook_sig(int i);
free(dir);
}
+static void
+xmalloc_error_handler(int err)
+{
+ if(is_ui_initialized())
+ quit_abook(QUIT_SAVE);
+
+ fprintf(stderr, "Memory allocation failure: %s\n", strerror(err));
+ exit(EXIT_FAILURE);
+}
+
static void
init_abook()
{
#if defined(HAVE_SETLOCALE) && defined(HAVE_LOCALE_H)
setlocale(LC_ALL, "");
#endif
+ xmalloc_set_error_handler(xmalloc_error_handler);
parse_command_line(argc, argv);
static void
free_filenames()
{
- my_free(rcfile);
- my_free(datafile);
+ xfree(rcfile);
+ xfree(datafile);
}
ui_init_curses();
}
-void *
-abook_malloc(size_t size)
-{
- void *ptr;
-
- if ( (ptr = malloc(size)) == NULL ) {
- if( is_ui_initialized() )
- quit_abook(QUIT_SAVE);
- perror("malloc() failed");
- exit(1);
- }
-
- return ptr;
-}
-
-void *
-abook_realloc(void *ptr, size_t size)
-{
- ptr = realloc(ptr, size);
-
- if(size == 0)
- return NULL;
-
- if(ptr == NULL) {
- if(is_ui_initialized())
- quit_abook(QUIT_SAVE);
- perror("realloc() failed");
- exit(1);
- }
-
- return ptr;
-}
-
FILE *
abook_fopen (const char *path, const char *mode)
{
getname(line, &name, &email);
add_email_count += add_email_add_item(quiet,
name, email);
- my_free(name);
- my_free(email);
+ xfree(name);
+ xfree(email);
}
- my_free(line);
+ xfree(line);
} while( !feof(stdin) );
quit_add_email();
#include <stdio.h>
-void *abook_malloc(size_t size);
-void *abook_realloc(void *ptr, size_t size);
FILE *abook_fopen (const char *path, const char *mode);
void quit_abook(int save_db);
void launch_wwwbrowser(int item);
#define show_cursor() curs_set(1)
#define safe_atoi(X) ((X == NULL) ? 0 : atoi(X))
-#define my_free(X) do {free(X); X=NULL;} while(0)
#define safe_str(X) ((X == NULL) ? "" : X)
#define safe_strdup(X) ((X == NULL) ? NULL : strdup(X))
#include "misc.h"
#include "options.h"
#include "filter.h"
+#include "xmalloc.h"
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
int i;
for(i=0; i<ITEM_FIELDS; i++)
- my_free(item[i]);
+ xfree(item[i]);
}
void
else
return;
- database = (list_item *)abook_realloc(database,
- sizeof(list_item) * list_capacity);
- selected = (char *)abook_realloc(selected, list_capacity);
+ database = xrealloc(database, sizeof(list_item) * list_capacity);
+ selected = xrealloc(selected, list_capacity);
}
int
ret = e.item;
goto out;
}
- my_free(tmp);
+ xfree(tmp);
}
}
* check if we are overwriting statically allocated default
*/
if(strcasecmp(abook_fields[i].name, abook_fields[i].key))
- my_free(abook_fields[i].name);
+ xfree(abook_fields[i].name);
- s = abook_malloc(MAX_FIELDNAME_LEN + 1);
+ s = xmalloc_inc(MAX_FIELDNAME_LEN, 1);
snprintf(s, MAX_FIELDNAME_LEN, "%s", name);
abook_fields[i].name = s;
}
#include "list.h"
#include "edit.h"
#include "misc.h"
+#include "xmalloc.h"
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
char *header;
char email[MAX_EMAIL_LEN];
- if( (header = (char *)malloc(EDITW_COLS)) == NULL )
+ if( (header = xmalloc(EDITW_COLS)) == NULL )
return;
get_first_email(email, item);
if(*field) {
free(old);
if(!**field)
- my_free(*field);
+ xfree(*field);
} else {
*field = old;
ret = 1;
change_field("Name: ", field);
if( *field == NULL || ! **field ) {
- my_free(*field);
+ xfree(*field);
*field = strdup(tmp);
}
- my_free(tmp);
+ xfree(tmp);
}
static void
} else
*emails[email_num] = 0;
- my_free(database[item][EMAIL]);
+ xfree(database[item][EMAIL]);
for(i = 0; i < MAX_EMAILS; i++) {
if( *emails[i] ) {
case CLEAR_UNDO:
if(backup) {
free_list_item(backup[0]);
- my_free(backup);
+ xfree(backup);
}
break;
case BACKUP_ITEM:
if(backup) {
free_list_item(backup[0]);
- my_free(backup);
+ xfree(backup);
}
- backup = (list_item *)abook_malloc(sizeof(list_item));
+ backup = xmalloc(sizeof(list_item));
for(i = 0; i < ITEM_FIELDS; i++)
backup[0][i] = safe_strdup(database[item][i]);
break;
if(backup) {
free_list_item(database[item]);
itemcpy(database[item], backup[0]);
- my_free(backup);
+ xfree(backup);
}
break;
default:
#include "list.h"
#include "misc.h"
#include "options.h"
+#include "xmalloc.h"
#include <assert.h>
extern int items;
bail_out:
for(i=0; i < LDIF_ITEM_FIELDS; i++)
- my_free(ldif_item[i]);
+ xfree(ldif_item[i]);
}
if( safe_strcmp("person", value))
break;
if(item[i])
- my_free(item[i]);
+ xfree(item[i]);
item[i] = strdup(value);
}
}
continue;
if( -1 == ( str_parse_line(line, &type, &value, &vlen)) ) {
- my_free(line);
+ xfree(line);
continue; /* just skip the errors */
}
ldif_convert(item, type, value);
- my_free(line);
+ xfree(line);
} while ( !feof(handle) );
ldif_convert(item, "dn", "");
/* includes also the trailing zero */
alias_len = (size_t)(ptr - tmp + 1);
- if( (*alias = (char *)malloc(alias_len)) == NULL) {
+ if( (*alias = xmalloc(alias_len)) == NULL) {
free(line);
return 1;
}
free(item[EMAIL]);
item[EMAIL] = tmp;
} else {
- my_free(email);
+ xfree(email);
}
}
}
int i, len, last;
int pine_conv_table[]= {NICK, NAME, EMAIL, -1, NOTES};
- memset(&item, 0, sizeof(item) );
+ memset(&item, 0, sizeof(item));
for(i=0, last=0; !last ; i++) {
if( ! (end = strchr(start, '\t')) )
while(!feof(in)) {
for(i = 2;;i++) {
- buf = (char *) realloc(buf, i*LINESIZE);
+ buf = xrealloc(buf, i*LINESIZE);
if(i == 2)
strcpy(buf, line);
fgets(line, LINESIZE, in);
strcat(buf, ptr);
}
if( *buf == '#' ) {
- my_free(buf);
+ xfree(buf);
continue;
}
pine_fixbuf(buf);
pine_parse_buf(buf);
- my_free(buf);
+ xfree(buf);
}
return 0;
len = strlen(trimmed);
if(trimmed[len - 1] == '\"' && *trimmed == '\"') {
if(len < 3) {
- my_free(copy);
+ xfree(copy);
return NULL;
}
trimmed[len - 1] = 0;
return trimmed;
}
- my_free(copy);
+ xfree(copy);
return strdup(s);
}
&& csv_conv_table[field] >= 0) {
item[csv_conv_table[field]] = newstr;
} else {
- my_free(newstr);
+ xfree(newstr);
}
}
&& allcsv_conv_table[field] >= 0) {
item[allcsv_conv_table[field]] = newstr;
} else {
- my_free(newstr);
+ xfree(newstr);
}
}
if(line && *line && *line != CSV_COMMENT_CHAR)
csv_parse_line(line);
- my_free(line);
+ xfree(line);
}
return 0;
if(line && *line && *line != CSV_COMMENT_CHAR)
allcsv_parse_line(line);
- my_free(line);
+ xfree(line);
}
return 0;
alias,
database[e.item][NAME],
email);
- my_free(alias);
+ xfree(alias);
}
return 0;
alias,
database[e.item][NAME],
email);
- my_free(alias);
+ xfree(alias);
}
return 0;
* Copyright (C) Jaakko Heinonen
*/
-#define ABOOK_SRC 1
-/*#undef ABOOK_SRC*/
-
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
# include <mbswidth.h>
#endif
#include "misc.h"
-#ifdef ABOOK_SRC
-# include "abook.h"
-#endif
+#include "xmalloc.h"
#ifndef DEBUG
# define NDEBUG 1
assert(s != NULL);
- for(t = s; ISSPACE(*t); t++);
+ for(t = s; isspace(*t); t++);
memmove(s, t, strlen(t)+1);
for (tt = t = s; *t != '\0'; t++)
- if(!ISSPACE(*t))
+ if(!isspace(*t))
tt = t+1;
*tt = '\0';
{
MY_VA_LOCAL_DECL;
size_t size = 100;
- char *buffer =
-#ifdef ABOOK_SRC
- (char *) abook_malloc (size);
-#else
- (char *) xmalloc (size);
-#endif
+ char *buffer = xmalloc (size);
assert(format != NULL);
else
size *= 2;
- buffer =
-#ifdef ABOOK_SRC
- (char *) abook_realloc (buffer, size);
-#else
- (char *) xrealloc (buffer, size);
-#endif
+ buffer = xrealloc (buffer, size);
}
}
MY_VA_END;
concat = (char *)
-#ifdef ABOOK_SRC
- abook_malloc(l);
-#else
xmalloc(l);
-#endif
if(concat == NULL)
return NULL;
char *dir = NULL;
size_t size = 100;
- if( (dir = (char *)malloc(size)) == NULL)
+ if( (dir = xmalloc(size)) == NULL)
return NULL;
*dir = 0;
while( getcwd(dir, size) == NULL && errno == ERANGE )
- if( (dir = (char *)realloc(dir, size *=2)) == NULL)
+ if( (dir = xrealloc(dir, size *=2)) == NULL)
return NULL;
return dir;
}
#define INITIAL_SIZE 128
-#ifndef ABOOK_SRC
-# define abook_malloc(X) xmalloc(X)
-# define abook_realloc(X, XX) xrealloc(X, XX)
-#endif
char *
getaline(FILE *f)
len = 0;
size = thres;
- buf = (char *)abook_malloc(size);
+ buf = xmalloc(size);
while (fgets(buf+len, size-len, f) != NULL) {
len += strlen(buf+len);
break; /* the whole line has been read */
for (inc = size, p = NULL; inc > mininc; inc /= 2)
- if ((p = (char *)abook_realloc(buf, size + inc)) !=
+ if ((p = xrealloc(buf, size + inc)) !=
NULL)
break;
}
if (len == 0) {
- free(buf);
+ xfree(buf);
return NULL; /* nothing read (eof or error) */
}
buf[--len] = '\0';
if (size - len > mucho) { /* a plenitude of unused memory? */
- p = (char *)abook_realloc(buf, len+1);
+ p = xrealloc(buf, len+1);
if (p != NULL) {
buf = p;
size = len+1;
err += opt_parse_line(line, n, filename) ? 1:0;
}
- my_free(line);
+ free(line);
+ line = NULL;
}
free(line);
--- /dev/null
+/*
+ * $Id$
+ *
+ * Common xmalloc memory allocation routines
+ *
+ * written by Jaakko Heinonen <jheinonen@users.sourceforge.net>
+ */
+
+/*
+ * Copyright (c) 2005 Jaakko Heinonen
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer
+ * in this position and unchanged.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+static void
+xmalloc_default_error_handler(int err)
+{
+ fprintf(stderr, "Memory allocation failure: %s\n", strerror(err));
+ exit(EXIT_FAILURE);
+}
+
+static void (*xmalloc_handle_error)(int err) = xmalloc_default_error_handler;
+
+void
+xmalloc_set_error_handler(void (*func)(int err))
+{
+ if(func)
+ xmalloc_handle_error = func;
+ else
+ xmalloc_handle_error = xmalloc_default_error_handler;
+}
+
+void *
+xmalloc(size_t size)
+{
+ void *p;
+
+ if((p = malloc(size)) == NULL)
+ (*xmalloc_handle_error)(errno);
+
+ return p;
+}
+
+void *
+xmalloc0(size_t size)
+{
+ void *p;
+
+ p = xmalloc(size);
+ if(p)
+ memset(p, 0, size);
+
+ return p;
+}
+
+void *
+xrealloc(void *ptr, size_t size)
+{
+ if((ptr = realloc(ptr, size)) == NULL)
+ (*xmalloc_handle_error)(errno);
+
+ return ptr;
+}
+
+void
+xfree(void *ptr)
+{
+ free(ptr);
+ ptr = NULL;
+}
+
+static void *
+_xmalloc_inc(size_t size, size_t inc, int zero)
+{
+ size_t total_size = size + inc;
+
+ /*
+ * check if the calculation overflowed
+ */
+ if(total_size < size) {
+ (*xmalloc_handle_error)(EINVAL);
+ return NULL;
+ }
+
+ return zero ? xmalloc0(total_size) : xmalloc(total_size);
+}
+
+void *
+xmalloc_inc(size_t size, size_t inc)
+{
+ return _xmalloc_inc(size, inc, 0);
+}
+
+void *
+xmalloc0_inc(size_t size, size_t inc)
+{
+ return _xmalloc_inc(size, inc, 1);
+}
+
--- /dev/null
+#ifndef _XMALLOC_H
+#define _XMALLOC_H
+
+#include <stdlib.h> /* for size_t */
+
+void xmalloc_set_error_handler(void (*)(int));
+void * xmalloc(size_t);
+void * xmalloc0(size_t);
+void * xmalloc_inc(size_t, size_t);
+void * xrealloc(void *, size_t);
+void xfree(void *);
+
+#endif