]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/write_entry.c
ncurses 5.7 - patch 20091128
[ncurses.git] / ncurses / tinfo / write_entry.c
1 /****************************************************************************
2  * Copyright (c) 1998-2008,2009 Free Software Foundation, Inc.              *
3  *                                                                          *
4  * Permission is hereby granted, free of charge, to any person obtaining a  *
5  * copy of this software and associated documentation files (the            *
6  * "Software"), to deal in the Software without restriction, including      *
7  * without limitation the rights to use, copy, modify, merge, publish,      *
8  * distribute, distribute with modifications, sublicense, and/or sell       *
9  * copies of the Software, and to permit persons to whom the Software is    *
10  * furnished to do so, subject to the following conditions:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22  *                                                                          *
23  * Except as contained in this notice, the name(s) of the above copyright   *
24  * holders shall not be used in advertising or otherwise to promote the     *
25  * sale, use or other dealings in this Software without prior written       *
26  * authorization.                                                           *
27  ****************************************************************************/
28
29 /****************************************************************************
30  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
31  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
32  *     and: Thomas E. Dickey                        1996-on                 *
33  ****************************************************************************/
34
35 /*
36  *      write_entry.c -- write a terminfo structure onto the file system
37  */
38
39 #include <curses.priv.h>
40 #include <hashed_db.h>
41
42 #include <sys/stat.h>
43
44 #include <tic.h>
45 #include <term_entry.h>
46
47 #ifndef S_ISDIR
48 #define S_ISDIR(mode) ((mode & S_IFMT) == S_IFDIR)
49 #endif
50
51 #if 1
52 #define TRACE_OUT(p) DEBUG(2, p)
53 #else
54 #define TRACE_OUT(p)            /*nothing */
55 #endif
56
57 MODULE_ID("$Id: write_entry.c,v 1.74 2009/09/19 20:30:48 Daniel.Jacobowitz Exp $")
58
59 static int total_written;
60
61 static int make_db_root(const char *);
62 static int write_object(TERMTYPE *, char *, unsigned *, unsigned);
63
64 #if !USE_HASHED_DB
65 static void
66 write_file(char *filename, TERMTYPE *tp)
67 {
68     char buffer[MAX_ENTRY_SIZE];
69     unsigned limit = sizeof(buffer);
70     unsigned offset = 0;
71
72     FILE *fp = (_nc_access(filename, W_OK) == 0) ? fopen(filename, "wb") : 0;
73     if (fp == 0) {
74         perror(filename);
75         _nc_syserr_abort("can't open %s/%s", _nc_tic_dir(0), filename);
76     }
77     DEBUG(1, ("Created %s", filename));
78
79     if (write_object(tp, buffer, &offset, limit) == ERR
80         || fwrite(buffer, sizeof(char), offset, fp) != offset) {
81         _nc_syserr_abort("error writing %s/%s", _nc_tic_dir(0), filename);
82     }
83
84     fclose(fp);
85 }
86
87 /*
88  * Check for access rights to destination directories
89  * Create any directories which don't exist.
90  *
91  * Note:  there's no reason to return the result of make_db_root(), since
92  * this function is called only in instances where that has to succeed.
93  */
94 static void
95 check_writeable(int code)
96 {
97     static const char dirnames[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
98     static bool verified[sizeof(dirnames)];
99
100     char dir[sizeof(LEAF_FMT)];
101     char *s = 0;
102
103     if (code == 0 || (s = strchr(dirnames, code)) == 0)
104         _nc_err_abort("Illegal terminfo subdirectory \"" LEAF_FMT "\"", code);
105
106     if (verified[s - dirnames])
107         return;
108
109     sprintf(dir, LEAF_FMT, code);
110     if (make_db_root(dir) < 0) {
111         _nc_err_abort("%s/%s: permission denied", _nc_tic_dir(0), dir);
112     }
113
114     verified[s - dirnames] = TRUE;
115 }
116 #endif /* !USE_HASHED_DB */
117
118 static int
119 make_db_path(char *dst, const char *src, unsigned limit)
120 {
121     int rc = -1;
122     const char *top = _nc_tic_dir(0);
123
124     if (src == top || _nc_is_abs_path(src)) {
125         if (strlen(src) + 1 <= limit) {
126             (void) strcpy(dst, src);
127             rc = 0;
128         }
129     } else {
130         if (strlen(top) + strlen(src) + 2 <= limit) {
131             (void) sprintf(dst, "%s/%s", top, src);
132             rc = 0;
133         }
134     }
135 #if USE_HASHED_DB
136     if (rc == 0) {
137         if (_nc_is_dir_path(dst)) {
138             rc = -1;
139         } else {
140             unsigned have = strlen(dst);
141             if (have > 3 && strcmp(dst + have - 3, DBM_SUFFIX)) {
142                 if (have + 3 <= limit)
143                     strcat(dst, DBM_SUFFIX);
144                 else
145                     rc = -1;
146             }
147         }
148     }
149 #endif
150     return rc;
151 }
152
153 /*
154  * Make a database-root if it doesn't exist.
155  */
156 static int
157 make_db_root(const char *path)
158 {
159     int rc;
160     char fullpath[PATH_MAX];
161
162     if ((rc = make_db_path(fullpath, path, sizeof(fullpath))) == 0) {
163 #if USE_HASHED_DB
164         DB *capdbp;
165
166         if ((capdbp = _nc_db_open(fullpath, TRUE)) == NULL)
167             rc = -1;
168         else if (_nc_db_close(capdbp) < 0)
169             rc = -1;
170 #else
171         struct stat statbuf;
172
173         if ((rc = stat(path, &statbuf)) < 0) {
174             rc = mkdir(path
175 #if !defined(__MINGW32__)
176                        ,0777
177 #endif
178                 );
179         } else if (_nc_access(path, R_OK | W_OK | X_OK) < 0) {
180             rc = -1;            /* permission denied */
181         } else if (!(S_ISDIR(statbuf.st_mode))) {
182             rc = -1;            /* not a directory */
183         }
184 #endif
185     }
186     return rc;
187 }
188
189 /*
190  * Set the write directory for compiled entries.
191  */
192 NCURSES_EXPORT(void)
193 _nc_set_writedir(char *dir)
194 {
195     const char *destination;
196     char actual[PATH_MAX];
197
198     if (dir == 0
199         && use_terminfo_vars())
200         dir = getenv("TERMINFO");
201
202     if (dir != 0)
203         (void) _nc_tic_dir(dir);
204
205     destination = _nc_tic_dir(0);
206     if (make_db_root(destination) < 0) {
207         char *home = _nc_home_terminfo();
208
209         if (home != 0) {
210             destination = home;
211             if (make_db_root(destination) < 0)
212                 _nc_err_abort("%s: permission denied (errno %d)",
213                               destination, errno);
214         }
215     }
216
217     /*
218      * Note: because of this code, this logic should be exercised
219      * *once only* per run.
220      */
221 #if USE_HASHED_DB
222     make_db_path(actual, destination, sizeof(actual));
223 #else
224     if (chdir(_nc_tic_dir(destination)) < 0
225         || getcwd(actual, sizeof(actual)) == 0)
226         _nc_err_abort("%s: not a directory", destination);
227 #endif
228     _nc_keep_tic_dir(strdup(actual));
229 }
230
231 /*
232  *      Save the compiled version of a description in the filesystem.
233  *
234  *      make a copy of the name-list
235  *      break it up into first-name and all-but-last-name
236  *      creat(first-name)
237  *      write object information to first-name
238  *      close(first-name)
239  *      for each name in all-but-last-name
240  *          link to first-name
241  *
242  *      Using 'time()' to obtain a reference for file timestamps is unreliable,
243  *      e.g., with NFS, because the filesystem may have a different time
244  *      reference.  We check for pre-existence of links by latching the first
245  *      timestamp from a file that we create.
246  *
247  *      The _nc_warning() calls will report a correct line number only if
248  *      _nc_curr_line is properly set before the write_entry() call.
249  */
250
251 NCURSES_EXPORT(void)
252 _nc_write_entry(TERMTYPE *const tp)
253 {
254 #if USE_HASHED_DB
255
256     char buffer[MAX_ENTRY_SIZE + 1];
257     unsigned limit = sizeof(buffer);
258     unsigned offset = 0;
259
260 #else /* !USE_HASHED_DB */
261
262     struct stat statbuf;
263     char filename[PATH_MAX];
264     char linkname[PATH_MAX];
265 #if USE_SYMLINKS
266     char symlinkname[PATH_MAX];
267 #if !HAVE_LINK
268 #undef HAVE_LINK
269 #define HAVE_LINK 1
270 #endif
271 #endif /* USE_SYMLINKS */
272
273     static int call_count;
274     static time_t start_time;   /* time at start of writes */
275
276 #endif /* USE_HASHED_DB */
277
278     char name_list[MAX_TERMINFO_LENGTH];
279     char *first_name, *other_names;
280     char *ptr;
281
282     assert(strlen(tp->term_names) != 0);
283     assert(strlen(tp->term_names) < sizeof(name_list));
284
285     (void) strcpy(name_list, tp->term_names);
286     DEBUG(7, ("Name list = '%s'", name_list));
287
288     first_name = name_list;
289
290     ptr = &name_list[strlen(name_list) - 1];
291     other_names = ptr + 1;
292
293     while (ptr > name_list && *ptr != '|')
294         ptr--;
295
296     if (ptr != name_list) {
297         *ptr = '\0';
298
299         for (ptr = name_list; *ptr != '\0' && *ptr != '|'; ptr++)
300             continue;
301
302         if (*ptr == '\0')
303             other_names = ptr;
304         else {
305             *ptr = '\0';
306             other_names = ptr + 1;
307         }
308     }
309
310     DEBUG(7, ("First name = '%s'", first_name));
311     DEBUG(7, ("Other names = '%s'", other_names));
312
313     _nc_set_type(first_name);
314
315 #if USE_HASHED_DB
316     if (write_object(tp, buffer + 1, &offset, limit - 1) != ERR) {
317         DB *capdb = _nc_db_open(_nc_tic_dir(0), TRUE);
318         DBT key, data;
319
320         if (capdb != 0) {
321             buffer[0] = 0;
322
323             memset(&key, 0, sizeof(key));
324             key.data = tp->term_names;
325             key.size = strlen(tp->term_names);
326
327             memset(&data, 0, sizeof(data));
328             data.data = buffer;
329             data.size = offset + 1;
330
331             _nc_db_put(capdb, &key, &data);
332
333             buffer[0] = 2;
334
335             key.data = name_list;
336             key.size = strlen(name_list);
337
338             strcpy(buffer + 1, tp->term_names);
339             data.size = strlen(tp->term_names) + 1;
340
341             _nc_db_put(capdb, &key, &data);
342
343             while (*other_names != '\0') {
344                 ptr = other_names++;
345                 assert(ptr < buffer + sizeof(buffer) - 1);
346                 while (*other_names != '|' && *other_names != '\0')
347                     other_names++;
348
349                 if (*other_names != '\0')
350                     *(other_names++) = '\0';
351
352                 key.data = ptr;
353                 key.size = strlen(ptr);
354
355                 _nc_db_put(capdb, &key, &data);
356             }
357             _nc_db_close(capdb);
358         }
359     }
360 #else /* !USE_HASHED_DB */
361     if (call_count++ == 0) {
362         start_time = 0;
363     }
364
365     if (strlen(first_name) >= sizeof(filename) - 3)
366         _nc_warning("terminal name too long.");
367
368     sprintf(filename, LEAF_FMT "/%s", first_name[0], first_name);
369
370     /*
371      * Has this primary name been written since the first call to
372      * write_entry()?  If so, the newer write will step on the older,
373      * so warn the user.
374      */
375     if (start_time > 0 &&
376         stat(filename, &statbuf) >= 0
377         && statbuf.st_mtime >= start_time) {
378         _nc_warning("name multiply defined.");
379     }
380
381     check_writeable(first_name[0]);
382     write_file(filename, tp);
383
384     if (start_time == 0) {
385         if (stat(filename, &statbuf) < 0
386             || (start_time = statbuf.st_mtime) == 0) {
387             _nc_syserr_abort("error obtaining time from %s/%s",
388                              _nc_tic_dir(0), filename);
389         }
390     }
391     while (*other_names != '\0') {
392         ptr = other_names++;
393         while (*other_names != '|' && *other_names != '\0')
394             other_names++;
395
396         if (*other_names != '\0')
397             *(other_names++) = '\0';
398
399         if (strlen(ptr) > sizeof(linkname) - 3) {
400             _nc_warning("terminal alias %s too long.", ptr);
401             continue;
402         }
403         if (strchr(ptr, '/') != 0) {
404             _nc_warning("cannot link alias %s.", ptr);
405             continue;
406         }
407
408         check_writeable(ptr[0]);
409         sprintf(linkname, LEAF_FMT "/%s", ptr[0], ptr);
410
411         if (strcmp(filename, linkname) == 0) {
412             _nc_warning("self-synonym ignored");
413         } else if (stat(linkname, &statbuf) >= 0 &&
414                    statbuf.st_mtime < start_time) {
415             _nc_warning("alias %s multiply defined.", ptr);
416         } else if (_nc_access(linkname, W_OK) == 0)
417 #if HAVE_LINK
418         {
419             int code;
420 #if USE_SYMLINKS
421             if (first_name[0] == linkname[0])
422                 strncpy(symlinkname, first_name, sizeof(symlinkname) - 1);
423             else {
424                 strcpy(symlinkname, "../");
425                 strncat(symlinkname, filename, sizeof(symlinkname) - 4);
426             }
427             symlinkname[sizeof(symlinkname) - 1] = '\0';
428 #endif /* USE_SYMLINKS */
429 #if HAVE_REMOVE
430             code = remove(linkname);
431 #else
432             code = unlink(linkname);
433 #endif
434             if (code != 0 && errno == ENOENT)
435                 code = 0;
436 #if USE_SYMLINKS
437             if (symlink(symlinkname, linkname) < 0)
438 #else
439             if (link(filename, linkname) < 0)
440 #endif /* USE_SYMLINKS */
441             {
442                 /*
443                  * If there wasn't anything there, and we cannot
444                  * link to the target because it is the same as the
445                  * target, then the source must be on a filesystem
446                  * that uses caseless filenames, such as Win32, etc.
447                  */
448                 if (code == 0 && errno == EEXIST)
449                     _nc_warning("can't link %s to %s", filename, linkname);
450                 else if (code == 0 && (errno == EPERM || errno == ENOENT))
451                     write_file(linkname, tp);
452                 else {
453 #if MIXEDCASE_FILENAMES
454                     _nc_syserr_abort("can't link %s to %s", filename, linkname);
455 #else
456                     _nc_warning("can't link %s to %s (errno=%d)", filename,
457                                 linkname, errno);
458 #endif
459                 }
460             } else {
461                 DEBUG(1, ("Linked %s", linkname));
462             }
463         }
464 #else /* just make copies */
465             write_file(linkname, tp);
466 #endif /* HAVE_LINK */
467     }
468 #endif /* USE_HASHED_DB */
469 }
470
471 static unsigned
472 fake_write(char *dst,
473            unsigned *offset,
474            unsigned limit,
475            char *src,
476            unsigned want,
477            unsigned size)
478 {
479     unsigned have = (limit - *offset);
480
481     want *= size;
482     if (have > 0) {
483         if (want > have)
484             want = have;
485         memcpy(dst + *offset, src, want);
486         *offset += want;
487     } else {
488         want = 0;
489     }
490     return (want / size);
491 }
492
493 #define Write(buf, size, count) fake_write(buffer, offset, limit, (char *) buf, count, size)
494
495 #undef LITTLE_ENDIAN            /* BSD/OS defines this as a feature macro */
496 #define HI(x)                   ((x) / 256)
497 #define LO(x)                   ((x) % 256)
498 #define LITTLE_ENDIAN(p, x)     (p)[0] = (unsigned char)LO(x),  \
499                                 (p)[1] = (unsigned char)HI(x)
500
501 #define WRITE_STRING(str) (Write(str, sizeof(char), strlen(str) + 1) == strlen(str) + 1)
502
503 static int
504 compute_offsets(char **Strings, unsigned strmax, short *offsets)
505 {
506     int nextfree = 0;
507     unsigned i;
508
509     for (i = 0; i < strmax; i++) {
510         if (Strings[i] == ABSENT_STRING) {
511             offsets[i] = -1;
512         } else if (Strings[i] == CANCELLED_STRING) {
513             offsets[i] = -2;
514         } else {
515             offsets[i] = (short) nextfree;
516             nextfree += (int) strlen(Strings[i]) + 1;
517             TRACE_OUT(("put Strings[%d]=%s(%d)", (int) i,
518                        _nc_visbuf(Strings[i]), (int) nextfree));
519         }
520     }
521     return nextfree;
522 }
523
524 static void
525 convert_shorts(unsigned char *buf, short *Numbers, unsigned count)
526 {
527     unsigned i;
528     for (i = 0; i < count; i++) {
529         if (Numbers[i] == ABSENT_NUMERIC) {     /* HI/LO won't work */
530             buf[2 * i] = buf[2 * i + 1] = 0377;
531         } else if (Numbers[i] == CANCELLED_NUMERIC) {   /* HI/LO won't work */
532             buf[2 * i] = 0376;
533             buf[2 * i + 1] = 0377;
534         } else {
535             LITTLE_ENDIAN(buf + 2 * i, Numbers[i]);
536             TRACE_OUT(("put Numbers[%d]=%d", i, Numbers[i]));
537         }
538     }
539 }
540
541 #define even_boundary(value) \
542             ((value) % 2 != 0 && Write(&zero, sizeof(char), 1) != 1)
543
544 #if NCURSES_XNAMES
545 static unsigned
546 extended_Booleans(TERMTYPE *tp)
547 {
548     unsigned result = 0;
549     unsigned i;
550
551     for (i = 0; i < tp->ext_Booleans; ++i) {
552         if (tp->Booleans[BOOLCOUNT + i] == TRUE)
553             result = (i + 1);
554     }
555     return result;
556 }
557
558 static unsigned
559 extended_Numbers(TERMTYPE *tp)
560 {
561     unsigned result = 0;
562     unsigned i;
563
564     for (i = 0; i < tp->ext_Numbers; ++i) {
565         if (tp->Numbers[NUMCOUNT + i] != ABSENT_NUMERIC)
566             result = (i + 1);
567     }
568     return result;
569 }
570
571 static unsigned
572 extended_Strings(TERMTYPE *tp)
573 {
574     unsigned short result = 0;
575     unsigned short i;
576
577     for (i = 0; i < tp->ext_Strings; ++i) {
578         if (tp->Strings[STRCOUNT + i] != ABSENT_STRING)
579             result = (i + 1);
580     }
581     return result;
582 }
583
584 /*
585  * _nc_align_termtype() will extend entries that are referenced in a use=
586  * clause - discard the unneeded data.
587  */
588 static bool
589 extended_object(TERMTYPE *tp)
590 {
591     bool result = FALSE;
592
593     if (_nc_user_definable) {
594         result = ((extended_Booleans(tp)
595                    + extended_Numbers(tp)
596                    + extended_Strings(tp)) != 0);
597     }
598     return result;
599 }
600 #endif
601
602 static int
603 write_object(TERMTYPE *tp, char *buffer, unsigned *offset, unsigned limit)
604 {
605     char *namelist;
606     size_t namelen, boolmax, nummax, strmax;
607     char zero = '\0';
608     size_t i;
609     int nextfree;
610     short offsets[MAX_ENTRY_SIZE / 2];
611     unsigned char buf[MAX_ENTRY_SIZE];
612     unsigned last_bool = BOOLWRITE;
613     unsigned last_num = NUMWRITE;
614     unsigned last_str = STRWRITE;
615
616 #if NCURSES_XNAMES
617     /*
618      * Normally we limit the list of values to exclude the "obsolete"
619      * capabilities.  However, if we are accepting extended names, add
620      * these as well, since they are used for supporting translation
621      * to/from termcap.
622      */
623     if (_nc_user_definable) {
624         last_bool = BOOLCOUNT;
625         last_num = NUMCOUNT;
626         last_str = STRCOUNT;
627     }
628 #endif
629
630     namelist = tp->term_names;
631     namelen = strlen(namelist) + 1;
632
633     boolmax = 0;
634     for (i = 0; i < last_bool; i++) {
635         if (tp->Booleans[i] == TRUE)
636             boolmax = i + 1;
637     }
638
639     nummax = 0;
640     for (i = 0; i < last_num; i++) {
641         if (tp->Numbers[i] != ABSENT_NUMERIC)
642             nummax = i + 1;
643     }
644
645     strmax = 0;
646     for (i = 0; i < last_str; i++) {
647         if (tp->Strings[i] != ABSENT_STRING)
648             strmax = i + 1;
649     }
650
651     nextfree = compute_offsets(tp->Strings, strmax, offsets);
652
653     /* fill in the header */
654     LITTLE_ENDIAN(buf, MAGIC);
655     LITTLE_ENDIAN(buf + 2, min(namelen, MAX_NAME_SIZE + 1));
656     LITTLE_ENDIAN(buf + 4, boolmax);
657     LITTLE_ENDIAN(buf + 6, nummax);
658     LITTLE_ENDIAN(buf + 8, strmax);
659     LITTLE_ENDIAN(buf + 10, nextfree);
660
661     /* write out the header */
662     TRACE_OUT(("Header of %s @%d", namelist, *offset));
663     if (Write(buf, 12, 1) != 1
664         || Write(namelist, sizeof(char), namelen) != namelen)
665           return (ERR);
666
667     for (i = 0; i < boolmax; i++)
668         if (tp->Booleans[i] == TRUE)
669             buf[i] = TRUE;
670         else
671             buf[i] = FALSE;
672     if (Write(buf, sizeof(char), boolmax) != boolmax)
673           return (ERR);
674
675     if (even_boundary(namelen + boolmax))
676         return (ERR);
677
678     TRACE_OUT(("Numerics begin at %04x", *offset));
679
680     /* the numerics */
681     convert_shorts(buf, tp->Numbers, nummax);
682     if (Write(buf, 2, nummax) != nummax)
683         return (ERR);
684
685     TRACE_OUT(("String offsets begin at %04x", *offset));
686
687     /* the string offsets */
688     convert_shorts(buf, offsets, strmax);
689     if (Write(buf, 2, strmax) != strmax)
690         return (ERR);
691
692     TRACE_OUT(("String table begins at %04x", *offset));
693
694     /* the strings */
695     for (i = 0; i < strmax; i++)
696         if (VALID_STRING(tp->Strings[i]))
697             if (!WRITE_STRING(tp->Strings[i]))
698                 return (ERR);
699
700 #if NCURSES_XNAMES
701     if (extended_object(tp)) {
702         unsigned extcnt = (unsigned) NUM_EXT_NAMES(tp);
703
704         if (even_boundary(nextfree))
705             return (ERR);
706
707         nextfree = compute_offsets(tp->Strings + STRCOUNT,
708                                    tp->ext_Strings,
709                                    offsets);
710         TRACE_OUT(("after extended string capabilities, nextfree=%d", nextfree));
711
712         if (tp->ext_Strings >= SIZEOF(offsets))
713             return (ERR);
714
715         nextfree += compute_offsets(tp->ext_Names,
716                                     extcnt,
717                                     offsets + tp->ext_Strings);
718         TRACE_OUT(("after extended capnames, nextfree=%d", nextfree));
719         strmax = tp->ext_Strings + extcnt;
720
721         /*
722          * Write the extended header
723          */
724         LITTLE_ENDIAN(buf + 0, tp->ext_Booleans);
725         LITTLE_ENDIAN(buf + 2, tp->ext_Numbers);
726         LITTLE_ENDIAN(buf + 4, tp->ext_Strings);
727         LITTLE_ENDIAN(buf + 6, strmax);
728         LITTLE_ENDIAN(buf + 8, nextfree);
729         TRACE_OUT(("WRITE extended-header @%d", *offset));
730         if (Write(buf, 10, 1) != 1)
731             return (ERR);
732
733         TRACE_OUT(("WRITE %d booleans @%d", tp->ext_Booleans, *offset));
734         if (tp->ext_Booleans
735             && Write(tp->Booleans + BOOLCOUNT, sizeof(char),
736                      tp->ext_Booleans) != tp->ext_Booleans)
737               return (ERR);
738
739         if (even_boundary(tp->ext_Booleans))
740             return (ERR);
741
742         TRACE_OUT(("WRITE %d numbers @%d", tp->ext_Numbers, *offset));
743         if (tp->ext_Numbers) {
744             convert_shorts(buf, tp->Numbers + NUMCOUNT, tp->ext_Numbers);
745             if (Write(buf, 2, tp->ext_Numbers) != tp->ext_Numbers)
746                 return (ERR);
747         }
748
749         /*
750          * Convert the offsets for the ext_Strings and ext_Names tables,
751          * in that order.
752          */
753         convert_shorts(buf, offsets, strmax);
754         TRACE_OUT(("WRITE offsets @%d", *offset));
755         if (Write(buf, 2, strmax) != strmax)
756             return (ERR);
757
758         /*
759          * Write the string table after the offset tables so we do not
760          * have to do anything about alignment.
761          */
762         for (i = 0; i < tp->ext_Strings; i++) {
763             if (VALID_STRING(tp->Strings[i + STRCOUNT])) {
764                 TRACE_OUT(("WRITE ext_Strings[%d]=%s", (int) i,
765                            _nc_visbuf(tp->Strings[i + STRCOUNT])));
766                 if (!WRITE_STRING(tp->Strings[i + STRCOUNT]))
767                     return (ERR);
768             }
769         }
770
771         /*
772          * Write the extended names
773          */
774         for (i = 0; i < extcnt; i++) {
775             TRACE_OUT(("WRITE ext_Names[%d]=%s", (int) i, tp->ext_Names[i]));
776             if (!WRITE_STRING(tp->ext_Names[i]))
777                 return (ERR);
778         }
779
780     }
781 #endif /* NCURSES_XNAMES */
782
783     total_written++;
784     return (OK);
785 }
786
787 /*
788  * Returns the total number of entries written by this process
789  */
790 NCURSES_EXPORT(int)
791 _nc_tic_written(void)
792 {
793     return total_written;
794 }