3
* Copyright (c) 2003,2004 Masahito Omote <omote@utyuuzin.net>
7
* Redistribution and use in source and binary forms, with or without
8
* modification, are permitted provided that the following conditions
11
* 1. Redistributions of source code must retain the above copyright
12
* notice, this list of conditions and the following disclaimer.
13
* 2. Redistributions in binary form must reproduce the above copyright
14
* notice, this list of conditions and the following disclaimer in the
15
* documentation and/or other materials provided with the distribution.
16
* 3. Neither the name of authors nor the names of its contributors
17
* may be used to endorse or promote products derived from this software
18
* without specific prior written permission.
20
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40
#include <sys/types.h>
50
/* struct skk_line head; */
53
static char *get_skkdicfullpath(void) {
54
char *homedirectory, *skkdicfullpath;
55
char skkdicname[] = ".skk-jisyo";
57
homedirectory = getenv("HOME");
60
pw = getpwuid(getuid());
61
homedirectory = strdup(pw->pw_dir);
65
skkdicfullpath = malloc(strlen(homedirectory) + strlen(skkdicname) + 2);
66
if(skkdicfullpath == NULL)
69
snprintf(skkdicfullpath,
70
strlen(homedirectory) + strlen(skkdicname) + 2,
71
"%s/%s", homedirectory, skkdicname);
73
return skkdicfullpath;
76
int read_skk_dic(word **head) {
84
char buf[256], desc[256], phon[256];
87
skkdicfullpath = get_skkdicfullpath();
88
if(skkdicfullpath == NULL) {
93
if(lstat(skkdicfullpath, &st) == -1) {
98
fp = fopen(skkdicfullpath, "r");
100
free(skkdicfullpath);
108
while(fgets(buf, sizeof(buf), fp)) {
109
if(buf[0] != '#' && buf[0] != ';') {
110
sscanf(buf, "%s %s", phon, desc); /* XXX */
112
word_append(head, WORD_TYPE_SKK,
113
phon, desc, NULL, 0, okuri, NULL); /* XXX */
115
if(strstr(buf, "okuri-ari")) {
118
if(strstr(buf, "okuri-nasi")) {
128
int write_skk_dic(word *head) {
129
char *skkdicfullpath;
135
skkdicfullpath = get_skkdicfullpath();
136
if(skkdicfullpath == NULL) {
140
fp = fopen(skkdicfullpath, "w");
141
free(skkdicfullpath);
147
lock.l_type = F_WRLCK;
149
lock.l_whence = SEEK_SET;
152
if(fcntl(fd, F_SETLK, &lock) < 0) {
153
fprintf(stderr, "Lock failed: %s\n", strerror(errno));
158
fprintf(fp, ";; okuri-ari entries.\n");
159
for(pos = head; pos != NULL; pos = pos->next) {
161
fprintf(fp, "%s %s\n", pos->phon, pos->desc);
163
fprintf(fp, ";; okuri-nasi entries.\n");
164
for(pos = head; pos != NULL; pos = pos->next) {
166
fprintf(fp, "%s %s\n", pos->phon, pos->desc);
169
lock.l_type = F_UNLCK;
170
if(fcntl(fd, F_SETLK, &lock) < 0) {
171
fprintf(stderr, "Unlock failed: %s\n", strerror(errno));
181
int read_line(char *ptr, ptrlen, char *buf, int len) {
183
for(i = 0; i < len; i++, ptr++) {
184
if(i >= ptrlen) break;
185
if(*ptr == '\0' || *ptr == '\n') {