1 /*
2  *
3  * alias.c -
4  *
5  * $Id: alias.c,v 1.15.8.5 2008-03-06 14:47:31 opengl2772 Exp $
6  *
7  * Copyright (C) 1997-1999 Satoru Takabayashi All rights reserved.
8  * Copyright (C) 2000-2008 Namazu Project All rights reserved.
9  * Copyright (C) 1999 NOKUBI Takatsugu All rights reserved.
10  * This is free software with ABSOLUTELY NO WARRANTY.
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25  * 02111-1307, USA
26  *
27  *
28  */
29 
30 #ifdef HAVE_CONFIG_H
31 #  include "config.h"
32 #endif
33 #ifdef HAVE_SUPPORT_H
34 #  include "support.h"
35 #endif
36 
37 #include <stdio.h>
38 #ifdef HAVE_STDLIB_H
39 #  include <stdlib.h>
40 #endif
41 
42 #ifdef HAVE_ERRNO_H
43 #  include <errno.h>
44 #endif
45 
46 #include <assert.h>
47 
48 #ifdef HAVE_STRING_H
49 #  include <string.h>
50 #else
51 #  include <strings.h>
52 #endif
53 
54 #include "alias.h"
55 #include "util.h"
56 #include "libnamazu.h"
57 #include "i18n.h"
58 
59 static struct nmz_alias  *aliases  = NULL;
60 
61 /*
62  *
63  * Public functions
64  *
65  */
66 
67 /*
68  * Add a new element to the end of `aliases' list.
69  */
70 enum nmz_stat
nmz_add_alias(const char * alias,const char * real)71 nmz_add_alias(const char *alias, const char *real)
72 {
73     struct nmz_alias *newp;
74 
75     newp = malloc(sizeof(struct nmz_alias));
76     if (newp == NULL) {
77 	 nmz_set_dyingmsg(nmz_msg("%s", strerror(errno)));
78 	 return FAILURE;
79     }
80     newp->alias = malloc(strlen(alias) + 1);
81     if (newp->alias == NULL) {
82 	 nmz_set_dyingmsg(nmz_msg("%s", strerror(errno)));
83          free(newp);
84 	 return FAILURE;
85     }
86 
87     newp->real = malloc(strlen(real) + 1);
88     if (newp->real == NULL) {
89 	 nmz_set_dyingmsg(nmz_msg("%s", strerror(errno)));
90          free(newp->alias);
91          free(newp);
92 	 return FAILURE;
93     }
94 
95     strcpy(newp->alias, alias);
96     strcpy(newp->real, real);
97 
98     newp->next = NULL;
99     if (aliases == NULL) {
100 	aliases = newp;
101     } else {
102 	struct nmz_alias *ptr = aliases;
103 
104 	while (ptr->next != NULL) {
105 	    ptr  = ptr->next;
106 	}
107 	assert(ptr->next == NULL);
108 	ptr->next = newp;
109     }
110 
111     return SUCCESS;
112 }
113 
114 void
nmz_free_aliases(void)115 nmz_free_aliases(void)
116 {
117     struct nmz_alias *list, *next;
118     list = aliases;
119 
120     while (list) {
121 	next = list->next;
122 	free(list->alias);
123 	free(list->real);
124 	free(list);
125 	list = next;
126     }
127 }
128 
129 /*
130  * It's very dangerous to use!
131  */
132 struct nmz_alias *
nmz_get_aliases(void)133 nmz_get_aliases(void)
134 {
135     return aliases;
136 }
137