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