3 /* mallocを実行し、メモリを確保できない場合は */
5 void *malloc_chk(size_t size, char *tag)
9 if((p = malloc(size)) == NULL) {
10 fprintf(stderr, "%s: cannot allocate memory\n", tag);
23 bool addcerrlist(int newerrc, CERR newerrv[])
28 assert(newerrc > 0 && newerrv != NULL);
29 if(cerrlist == NULL) {
30 p = cerrlist = malloc_chk(sizeof(CERRLIST), "cerrlist");
32 for(p = cerrlist; p != NULL; p = p->next) {
35 p = q->next = malloc_chk(sizeof(CERRLIST), "cerrlist.next");
37 for(i = 0; i < newerrc; i++) {
38 p->cerr = &(newerrv[i]);
39 p->next = malloc_chk(sizeof(CERRLIST), "cerrlist.next");
48 void setcerr(int num, const char *str)
51 cerr->msg = malloc_chk(CERRMSGSIZE + 1, "cerr.msg");
52 if(str != NULL && strlen(str) <= CERRSTRSIZE) {
53 sprintf(cerr->msg, "%s: %s", str, getcerrmsg(cerr->num));
55 strcpy(cerr->msg, getcerrmsg(cerr->num));
59 /* エラーリストから、エラー番号に対応するメッセージを返す */
60 char *getcerrmsg(int num)
64 for(p = cerrlist; p != NULL; p = p->next) {
65 if(num == p->cerr->num) {
69 return "unkown error";
72 /* エラーリストと現在のエラーを解放する */
75 CERRLIST *p = cerrlist, *q;