-/* エラーメッセージ */
-int cerrno = 0;
-char *cerrmsg;
-
-/* エラー番号とエラーメッセージを設定する */
-void setcerr(int num, const char *val)
-{
- cerrno = num;
- cerrmsg = malloc(MSGSIZE + 1);
- if(val != NULL) {
- strcpy(cerrmsg, val);
- strcat(cerrmsg, ": ");
- strcat(cerrmsg, getcerrmsg(cerrno));
+/* mallocを実行し、メモリを確保できない場合は */
+/* エラーを出力して終了 */
+void *malloc_chk(size_t size, char *tag)
+{
+ void *p;
+
+ if((p = malloc(size)) == NULL) {
+ fprintf(stderr, "%s: cannot allocate memory\n", tag);
+ exit(-1);
+ }
+ return p;
+}
+
+/* callocを実行し、メモリを確保できない場合は */
+/* エラーを出力して終了 */
+void *calloc_chk(size_t nmemb, size_t size, char *tag)
+{
+ void *p;
+
+ if((p = calloc(nmemb, size)) == NULL) {
+ fprintf(stderr, "%s: cannot allocate memory\n", tag);
+ exit(-1);
+ }
+ return p;
+}
+
+/* エラーの初期化 */
+void cerr_init()
+{
+ cerr = malloc_chk(sizeof(CERR), "cerr");
+ cerr->num = 0;
+}
+
+/* malloc_chkを実行してメモリを確保してから、 */
+/* コピーした文字列を返す */
+char *strdup_chk(const char *s, char *tag)
+{
+ assert(s != NULL);
+ char *t;
+
+ t = malloc_chk(strlen(s) + 1, tag);
+ strcpy(t, s);
+ return t;
+}
+
+/* 現在のエラー */
+CERR *cerr;
+
+/* エラーリスト */
+CERRLIST *cerrlist;
+
+/* エラーリストを作成・追加する */
+bool addcerrlist(int newerrc, CERR newerrv[])
+{
+ int i;
+ CERRLIST *p, *q;
+
+ assert(newerrc > 0 && newerrv != NULL);
+ if(cerrlist == NULL) {
+ p = cerrlist = malloc_chk(sizeof(CERRLIST), "cerrlist");