~ubuntu-branches/ubuntu/breezy/bsdmainutils/breezy

« back to all changes in this revision

Viewing changes to usr.bin/calendar/wcslib.c

  • Committer: Bazaar Package Importer
  • Author(s): Graham Wilson
  • Date: 2005-06-08 01:18:22 UTC
  • Revision ID: james.westby@ubuntu.com-20050608011822-0fx65jwmw9av2xec
Tags: 6.1.2
Fix a typo in calendar.1.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* $Id: wcslib.c 123 2004-11-01 17:00:19Z bob $ */
 
1
/* $Id: wcslib.c 125 2004-11-02 01:31:58Z bob $ */
2
2
 
3
3
/*
4
4
 * Copyright (c) 1987, 1993
45
45
#include <iconv.h>
46
46
#include <errno.h>
47
47
#include <locale.h>
 
48
#include "calendar.h"
48
49
 
49
50
int
50
51
wcscasecmp(s1, s2)
122
123
myfgetws(wchar_t *s, int size, FILE *stream)
123
124
{
124
125
        static int utf8mode = 0;
 
126
        static char *filename = NULL;
 
127
        static int line = 0;
 
128
 
125
129
        char buf[2048 + 1], *p;
126
 
        int ch;
 
130
        int ch, r;
127
131
 
128
132
        if (fgets(buf, sizeof(buf), stream) == NULL)
129
133
                return NULL;
130
134
 
 
135
        /* Keep a count of line numbers for printing errors. */
 
136
        line++;
 
137
 
131
138
        if ((p = strchr(buf, '\n')) != NULL)
132
139
                *p = '\0';
133
140
        else
134
141
                while ((ch = getchar()) != '\n' && ch != EOF);
135
142
 
136
143
        /* handle special directives */
137
 
        if (strncmp(buf, "# 1 \"", 5) == 0) {
138
 
                utf8mode = 0;
139
 
                (void) setlocale(LC_ALL, "");
 
144
        if (strncmp(buf, "# ", 2) == 0) {
 
145
                char *newfile, *end;
 
146
                int n;
 
147
 
 
148
                line = strtol(buf + 2, &end, 10) - 1;
 
149
 
 
150
                /* Skip whitespace and the opening double quote. */
 
151
                while (isspace(*end))
 
152
                        end++;
 
153
                end++;
 
154
 
 
155
                /* Copy the filename to a buffer for printing errors. */
 
156
                for (n = 0, p = end; *p && *p != '"'; p++)
 
157
                        n++;
 
158
                if ((newfile = (char *) malloc(n + 1)) == NULL)
 
159
                        err(1, NULL);
 
160
                strncpy(newfile, end, n);
 
161
                newfile[n] = 0;
 
162
 
 
163
                /* If this is a new file, clear file-specific state. */
 
164
                if (!filename || strcmp(newfile, filename) != 0) {
 
165
                        int i;
 
166
 
 
167
                        utf8mode = 0;
 
168
                        for (i = 0; i < NUMEV; i++)
 
169
                                if (spev[i].uname) {
 
170
                                        free(spev[i].uname);
 
171
                                        spev[i].uname = NULL;
 
172
                                }
 
173
                        (void) setlocale(LC_ALL, "");
 
174
                }
 
175
                if (filename)
 
176
                        free(filename);
 
177
                filename = newfile;
140
178
        } else if (strncmp(buf, "LANG=utf-8", 10) == 0) {
141
179
                utf8mode = 1;
142
180
                (void) setlocale(LC_ALL, "C");
147
185
        }
148
186
 
149
187
        /* convert the line */
150
 
        if (utf8mode) {
151
 
                if (utf8towcs(s, buf, size) == -1)
152
 
                        err(1, "while reading input");
153
 
        } else {
154
 
                if (mbstowcs(s, buf, size) == -1)
155
 
                        errx(1, "invalid multibyte sequence encountered in input file");
 
188
        if (utf8mode)
 
189
                r = utf8towcs(s, buf, size);
 
190
        else
 
191
                r = mbstowcs(s, buf, size);
 
192
 
 
193
        if (r == -1) {
 
194
                if (!filename || strcmp(filename, "<stdin>") == 0)
 
195
                        err(1, "%s:%d", calendarPath, line);
 
196
                else
 
197
                        err(1, "%s:%d", filename, line);
156
198
        }
157
199
 
158
200
        return s;