~ubuntu-branches/ubuntu/hardy/uim/hardy

« back to all changes in this revision

Viewing changes to sumika/common/skk.c

  • Committer: Bazaar Package Importer
  • Author(s): Masahito Omote
  • Date: 2005-12-04 13:10:42 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20051204131042-ktzc8b17zi7a3cw8
Tags: 1:0.4.9.1-1
* New upstream release
* libuim0-nox, libuim-nox-dev, and libuim0-dbg-nox is now obsolete.
  Because libuim0 does not depends on X11. They now become dummy package,
  therefore you can safely remove them.
* Add --enable-debug in configure again.
* debian/patches/08_fix_privilage_escalation_CVE_2005_3149: disabled.
* Fix Error on purge because update-uim-config is not found.
  (closes: Bug#339345)
* uim-qt: New package for Qt utilities for uim. qt-immodule does not
  contained yet because of Debian's Qt3 does not support immodule and
  because uim does not recognize libqt4-dev's headers properly. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 *  $Id:$
3
 
 *  Copyright (c) 2003,2004 Masahito Omote <omote@utyuuzin.net>
4
 
 *
5
 
 *  All rights reserved.
6
 
 *
7
 
 *  Redistribution and use in source and binary forms, with or without
8
 
 *  modification, are permitted provided that the following conditions
9
 
 *  are met:
10
 
 *
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.
19
 
 *
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
30
 
 *  SUCH DAMAGE.
31
 
 */
32
 
 
33
 
#include <stdio.h>
34
 
#include <string.h>
35
 
#include <stdlib.h>
36
 
#include <unistd.h>
37
 
#include <fcntl.h>
38
 
#include <pwd.h>
39
 
#include <errno.h>
40
 
#include <sys/types.h>
41
 
#include <sys/stat.h>
42
 
#include "skk.h"
43
 
 
44
 
struct _skkdic_info {
45
 
    int fd;
46
 
    char *data;
47
 
    size_t len;
48
 
    int first;
49
 
    int boarder;
50
 
/*    struct skk_line head; */
51
 
} *skk_dic;
52
 
 
53
 
static char *get_skkdicfullpath(void) {
54
 
    char *homedirectory, *skkdicfullpath;
55
 
    char skkdicname[] = ".skk-jisyo";
56
 
 
57
 
    homedirectory = getenv("HOME");
58
 
    if(!homedirectory) {
59
 
        struct passwd *pw;
60
 
        pw = getpwuid(getuid());
61
 
        homedirectory = strdup(pw->pw_dir);
62
 
        free(pw);
63
 
    }
64
 
 
65
 
    skkdicfullpath = malloc(strlen(homedirectory) + strlen(skkdicname) + 2);
66
 
    if(skkdicfullpath == NULL)
67
 
        return NULL;
68
 
 
69
 
    snprintf(skkdicfullpath,
70
 
             strlen(homedirectory) + strlen(skkdicname) + 2,
71
 
             "%s/%s", homedirectory, skkdicname);
72
 
 
73
 
    return skkdicfullpath;
74
 
}
75
 
 
76
 
int read_skk_dic(word **head) {
77
 
    FILE *fp;
78
 
    int fd;
79
 
    struct stat st;
80
 
    char *ptr;
81
 
    void *addr;
82
 
    int len, buflen;
83
 
    char *skkdicfullpath;
84
 
    char buf[256], desc[256], phon[256];
85
 
    int okuri = 0;
86
 
 
87
 
    skkdicfullpath = get_skkdicfullpath();
88
 
    if(skkdicfullpath == NULL) {
89
 
        *head = NULL;
90
 
        return -1;
91
 
    }
92
 
 
93
 
    if(lstat(skkdicfullpath, &st) == -1) {
94
 
        free(skkdicfullpath);
95
 
        return -1;
96
 
    }
97
 
 
98
 
    fp = fopen(skkdicfullpath, "r");
99
 
 
100
 
    free(skkdicfullpath);
101
 
 
102
 
    if(!fp) {
103
 
        *head = NULL;
104
 
        return -1;
105
 
    }
106
 
 
107
 
    *head = NULL;
108
 
    while(fgets(buf, sizeof(buf), fp)) {
109
 
        if(buf[0] != '#' && buf[0] != ';') {
110
 
            sscanf(buf, "%s %s", phon, desc); /* XXX */
111
 
 
112
 
            word_append(head, WORD_TYPE_SKK,
113
 
                        phon, desc, NULL, 0, okuri, NULL); /* XXX */
114
 
        } else {
115
 
            if(strstr(buf, "okuri-ari")) {
116
 
                okuri = 0;
117
 
            }
118
 
            if(strstr(buf, "okuri-nasi")) {
119
 
                okuri = 1;
120
 
            }
121
 
        }
122
 
    }
123
 
    fclose(fp);
124
 
    return 0;
125
 
 
126
 
}
127
 
 
128
 
int write_skk_dic(word *head) {
129
 
    char *skkdicfullpath;
130
 
    word *pos;
131
 
    FILE *fp;
132
 
    int fd;
133
 
    struct flock lock;
134
 
 
135
 
    skkdicfullpath = get_skkdicfullpath();
136
 
    if(skkdicfullpath == NULL) {
137
 
        return -1;
138
 
    }
139
 
 
140
 
    fp = fopen(skkdicfullpath, "w");
141
 
    free(skkdicfullpath);
142
 
    if(!fp) {
143
 
        return -1;
144
 
    }
145
 
 
146
 
    fd = fileno(fp);
147
 
    lock.l_type = F_WRLCK;
148
 
    lock.l_start = 0;
149
 
    lock.l_whence = SEEK_SET;
150
 
    lock.l_len = 0;
151
 
 
152
 
    if(fcntl(fd, F_SETLK, &lock) < 0) {
153
 
        fprintf(stderr, "Lock failed: %s\n", strerror(errno));
154
 
        fclose(fp);
155
 
        return -1;
156
 
    }
157
 
 
158
 
    fprintf(fp, ";; okuri-ari entries.\n");
159
 
    for(pos = head; pos != NULL; pos = pos->next) {
160
 
        if(pos->okuri == 0)
161
 
            fprintf(fp, "%s %s\n", pos->phon, pos->desc);
162
 
    }
163
 
    fprintf(fp, ";; okuri-nasi entries.\n");
164
 
    for(pos = head; pos != NULL; pos = pos->next) {
165
 
        if(pos->okuri == 1)
166
 
            fprintf(fp, "%s %s\n", pos->phon, pos->desc);
167
 
    }
168
 
 
169
 
    lock.l_type = F_UNLCK;
170
 
    if(fcntl(fd, F_SETLK, &lock) < 0) {
171
 
        fprintf(stderr, "Unlock failed: %s\n", strerror(errno));
172
 
        fclose(fp);
173
 
        return -1;
174
 
    }
175
 
 
176
 
    fclose(fp);
177
 
    return 0;
178
 
}
179
 
 
180
 
/*
181
 
int read_line(char *ptr, ptrlen, char *buf, int len) {
182
 
    int i = 0;
183
 
    for(i = 0; i < len; i++, ptr++) {
184
 
        if(i >= ptrlen) break;
185
 
        if(*ptr == '\0' || *ptr == '\n') {
186
 
            buf[i] = '\0';
187
 
            i++;
188
 
            break;
189
 
        }
190
 
        buf[i] = *ptr;
191
 
    }
192
 
    return i;
193
 
}
194
 
*/