]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/write_entry.c
ncurses 5.7 - patch 20090314
[ncurses.git] / ncurses / tinfo / write_entry.c
1 /****************************************************************************
2  * Copyright (c) 1998-2007,2008 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.72 2008/08/03 19:24:00 tom 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, 0777);
175         } else if (_nc_access(path, R_OK | W_OK | X_OK) < 0) {
176             rc = -1;            /* permission denied */
177         } else if (!(S_ISDIR(statbuf.st_mode))) {
178             rc = -1;            /* not a directory */
179         }
180 #endif
181     }
182     return rc;
183 }
184
185 /*
186  * Set the write directory for compiled entries.
187  */
188 NCURSES_EXPORT(void)
189 _nc_set_writedir(char *dir)
190 {
191     const char *destination;
192     char actual[PATH_MAX];
193
194     if (dir == 0
195         && use_terminfo_vars())
196         dir = getenv("TERMINFO");
197
198     if (dir != 0)
199         (void) _nc_tic_dir(dir);
200
201     destination = _nc_tic_dir(0);
202     if (make_db_root(destination) < 0) {
203         char *home = _nc_home_terminfo();
204
205         if (home != 0) {
206             destination = home;
207             if (make_db_root(destination) < 0)
208                 _nc_err_abort("%s: permission denied (errno %d)",
209                               destination, errno);
210         }
211     }
212
213     /*
214      * Note: because of this code, this logic should be exercised
215      * *once only* per run.
216      */
217 #if USE_HASHED_DB
218     make_db_path(actual, destination, sizeof(actual));
219 #else
220     if (chdir(_nc_tic_dir(destination)) < 0
221         || getcwd(actual, sizeof(actual)) == 0)
222         _nc_err_abort("%s: not a directory", destination);
223 #endif
224     _nc_keep_tic_dir(strdup(actual));
225 }
226
227 /*
228  *      Save the compiled version of a description in the filesystem.
229  *
230  *      make a copy of the name-list
231  *      break it up into first-name and all-but-last-name
232  *      creat(first-name)
233  *      write object information to first-name
234  *      close(first-name)
235  *      for each name in all-but-last-name
236  *          link to first-name
237  *
238  *      Using 'time()' to obtain a reference for file timestamps is unreliable,
239  *      e.g., with NFS, because the filesystem may have a different time
240  *      reference.  We check for pre-existence of links by latching the first
241  *      timestamp from a file that we create.
242  *
243  *      The _nc_warning() calls will report a correct line number only if
244  *      _nc_curr_line is properly set before the write_entry() call.
245  */
246
247 NCURSES_EXPORT(void)
248 _nc_write_entry(TERMTYPE *const tp)
249 {
250 #if USE_HASHED_DB
251
252     char buffer[MAX_ENTRY_SIZE + 1];
253     unsigned limit = sizeof(buffer);
254     unsigned offset = 0;
255
256 #else /* !USE_HASHED_DB */
257
258     struct stat statbuf;
259     char filename[PATH_MAX];
260     char linkname[PATH_MAX];
261 #if USE_SYMLINKS
262     char symlinkname[PATH_MAX];
263 #if !HAVE_LINK
264 #undef HAVE_LINK
265 #define HAVE_LINK 1
266 #endif
267 #endif /* USE_SYMLINKS */
268
269     static int call_count;
270     static time_t start_time;   /* time at start of writes */
271
272 #endif /* USE_HASHED_DB */
273
274     char name_list[MAX_TERMINFO_LENGTH];
275     char *first_name, *other_names;
276     char *ptr;
277
278     assert(strlen(tp->term_names) != 0);
279     assert(strlen(tp->term_names) < sizeof(name_list));
280
281     (void) strcpy(name_list, tp->term_names);
282     DEBUG(7, ("Name list = '%s'", name_list));
283
284     first_name = name_list;
285
286     ptr = &name_list[strlen(name_list) - 1];
287     other_names = ptr + 1;
288
289     while (ptr > name_list && *ptr != '|')
290         ptr--;
291
292     if (ptr != name_list) {
293         *ptr = '\0';
294
295         for (ptr = name_list; *ptr != '\0' && *ptr != '|'; ptr++)
296             continue;
297
298         if (*ptr == '\0')
299             other_names = ptr;
300         else {
301             *ptr = '\0';
302             other_names = ptr + 1;
303         }
304     }
305
306     DEBUG(7, ("First name = '%s'", first_name));
307     DEBUG(7, ("Other names = '%s'", other_names));
308
309     _nc_set_type(first_name);
310
311 #if USE_HASHED_DB
312     if (write_object(tp, buffer + 1, &offset, limit - 1) != ERR) {
313         DB *capdb = _nc_db_open(_nc_tic_dir(0), TRUE);
314         DBT key, data;
315
316         if (capdb != 0) {
317             buffer[0] = 0;
318
319             memset(&key, 0, sizeof(key));
320             key.data = tp->term_names;
321             key.size = strlen(tp->term_names);
322
323             memset(&data, 0, sizeof(data));
324             data.data = buffer;
325             data.size = offset + 1;
326
327             _nc_db_put(capdb, &key, &data);
328
329             buffer[0] = 2;
330
331             key.data = name_list;
332             key.size = strlen(name_list);
333
334             strcpy(buffer + 1, tp->term_names);
335             data.size = strlen(tp->term_names) + 1;
336
337             _nc_db_put(capdb, &key, &data);
338
339             while (*other_names != '\0') {
340                 ptr = other_names++;
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) - 3)
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         assert(ptr < buffer + sizeof(buffer) - 1);
389         while (*other_names != '|' && *other_names != '\0')
390             other_names++;
391
392         if (*other_names != '\0')
393             *(other_names++) = '\0';
394
395         if (strlen(ptr) > sizeof(linkname) - 3) {
396             _nc_warning("terminal alias %s too long.", ptr);
397             continue;
398         }
399         if (strchr(ptr, '/') != 0) {
400             _nc_warning("cannot link alias %s.", ptr);
401             continue;
402         }
403
404         check_writeable(ptr[0]);
405         sprintf(linkname, LEAF_FMT "/%s", ptr[0], ptr);
406
407         if (strcmp(filename, linkname) == 0) {
408             _nc_warning("self-synonym ignored");
409         } else if (stat(linkname, &statbuf) >= 0 &&
410                    statbuf.st_mtime < start_time) {
411             _nc_warning("alias %s multiply defined.", ptr);
412         } else if (_nc_access(linkname, W_OK) == 0)
413 #if HAVE_LINK
414         {
415             int code;
416 #if USE_SYMLINKS
417             strcpy(symlinkname, "../");
418             strncat(symlinkname, filename, sizeof(symlinkname) - 4);
419             symlinkname[sizeof(symlinkname) - 1] = '\0';
420 #endif /* USE_SYMLINKS */
421 #if HAVE_REMOVE
422             code = remove(linkname);
423 #else
424             code = unlink(linkname);
425 #endif
426             if (code != 0 && errno == ENOENT)
427                 code = 0;
428 #if USE_SYMLINKS
429             if (symlink(symlinkname, linkname) < 0)
430 #else
431             if (link(filename, linkname) < 0)
432 #endif /* USE_SYMLINKS */
433             {
434                 /*
435                  * If there wasn't anything there, and we cannot
436                  * link to the target because it is the same as the
437                  * target, then the source must be on a filesystem
438                  * that uses caseless filenames, such as Win32, etc.
439                  */
440                 if (code == 0 && errno == EEXIST)
441                     _nc_warning("can't link %s to %s", filename, linkname);
442                 else if (code == 0 && (errno == EPERM || errno == ENOENT))
443                     write_file(linkname, tp);
444                 else {
445 #if MIXEDCASE_FILENAMES
446                     _nc_syserr_abort("can't link %s to %s", filename, linkname);
447 #else
448                     _nc_warning("can't link %s to %s (errno=%d)", filename,
449                                 linkname, errno);
450 #endif
451                 }
452             } else {
453                 DEBUG(1, ("Linked %s", linkname));
454             }
455         }
456 #else /* just make copies */
457             write_file(linkname, tp);
458 #endif /* HAVE_LINK */
459     }
460 #endif /* USE_HASHED_DB */
461 }
462
463 static unsigned
464 fake_write(char *dst,
465            unsigned *offset,
466            unsigned limit,
467            char *src,
468            unsigned want,
469            unsigned size)
470 {
471     int have = (limit - *offset);
472
473     want *= size;
474     if (have > 0) {
475         if ((int) want > have)
476             want = have;
477         memcpy(dst + *offset, src, want);
478         *offset += want;
479     } else {
480         want = 0;
481     }
482     return (int) (want / size);
483 }
484
485 #define Write(buf, size, count) fake_write(buffer, offset, limit, (char *) buf, count, size)
486
487 #undef LITTLE_ENDIAN            /* BSD/OS defines this as a feature macro */
488 #define HI(x)                   ((x) / 256)
489 #define LO(x)                   ((x) % 256)
490 #define LITTLE_ENDIAN(p, x)     (p)[0] = LO(x), (p)[1] = HI(x)
491
492 #define WRITE_STRING(str) (Write(str, sizeof(char), strlen(str) + 1) == strlen(str) + 1)
493
494 static int
495 compute_offsets(char **Strings, unsigned strmax, short *offsets)
496 {
497     size_t nextfree = 0;
498     unsigned i;
499
500     for (i = 0; i < strmax; i++) {
501         if (Strings[i] == ABSENT_STRING) {
502             offsets[i] = -1;
503         } else if (Strings[i] == CANCELLED_STRING) {
504             offsets[i] = -2;
505         } else {
506             offsets[i] = nextfree;
507             nextfree += strlen(Strings[i]) + 1;
508             TRACE_OUT(("put Strings[%d]=%s(%d)", (int) i,
509                        _nc_visbuf(Strings[i]), (int) nextfree));
510         }
511     }
512     return nextfree;
513 }
514
515 static void
516 convert_shorts(unsigned char *buf, short *Numbers, unsigned count)
517 {
518     unsigned i;
519     for (i = 0; i < count; i++) {
520         if (Numbers[i] == ABSENT_NUMERIC) {     /* HI/LO won't work */
521             buf[2 * i] = buf[2 * i + 1] = 0377;
522         } else if (Numbers[i] == CANCELLED_NUMERIC) {   /* HI/LO won't work */
523             buf[2 * i] = 0376;
524             buf[2 * i + 1] = 0377;
525         } else {
526             LITTLE_ENDIAN(buf + 2 * i, Numbers[i]);
527             TRACE_OUT(("put Numbers[%d]=%d", i, Numbers[i]));
528         }
529     }
530 }
531
532 #define even_boundary(value) \
533             ((value) % 2 != 0 && Write(&zero, sizeof(char), 1) != 1)
534
535 #if NCURSES_XNAMES
536 static unsigned
537 extended_Booleans(TERMTYPE *tp)
538 {
539     unsigned short result = 0;
540     unsigned short i;
541
542     for (i = 0; i < tp->ext_Booleans; ++i) {
543         if (tp->Booleans[BOOLCOUNT + i] == TRUE)
544             result = (i + 1);
545     }
546     return result;
547 }
548
549 static unsigned
550 extended_Numbers(TERMTYPE *tp)
551 {
552     unsigned short result = 0;
553     unsigned short i;
554
555     for (i = 0; i < tp->ext_Numbers; ++i) {
556         if (tp->Numbers[NUMCOUNT + i] != ABSENT_NUMERIC)
557             result = (i + 1);
558     }
559     return result;
560 }
561
562 static unsigned
563 extended_Strings(TERMTYPE *tp)
564 {
565     unsigned short result = 0;
566     unsigned short i;
567
568     for (i = 0; i < tp->ext_Strings; ++i) {
569         if (tp->Strings[STRCOUNT + i] != ABSENT_STRING)
570             result = (i + 1);
571     }
572     return result;
573 }
574
575 /*
576  * _nc_align_termtype() will extend entries that are referenced in a use=
577  * clause - discard the unneeded data.
578  */
579 static bool
580 extended_object(TERMTYPE *tp)
581 {
582     bool result = FALSE;
583
584     if (_nc_user_definable) {
585         result = ((extended_Booleans(tp)
586                    + extended_Numbers(tp)
587                    + extended_Strings(tp)) != 0);
588     }
589     return result;
590 }
591 #endif
592
593 static int
594 write_object(TERMTYPE *tp, char *buffer, unsigned *offset, unsigned limit)
595 {
596     char *namelist;
597     size_t namelen, boolmax, nummax, strmax;
598     char zero = '\0';
599     size_t i;
600     short nextfree;
601     short offsets[MAX_ENTRY_SIZE / 2];
602     unsigned char buf[MAX_ENTRY_SIZE];
603     unsigned last_bool = BOOLWRITE;
604     unsigned last_num = NUMWRITE;
605     unsigned last_str = STRWRITE;
606
607 #if NCURSES_XNAMES
608     /*
609      * Normally we limit the list of values to exclude the "obsolete"
610      * capabilities.  However, if we are accepting extended names, add
611      * these as well, since they are used for supporting translation
612      * to/from termcap.
613      */
614     if (_nc_user_definable) {
615         last_bool = BOOLCOUNT;
616         last_num = NUMCOUNT;
617         last_str = STRCOUNT;
618     }
619 #endif
620
621     namelist = tp->term_names;
622     namelen = strlen(namelist) + 1;
623
624     boolmax = 0;
625     for (i = 0; i < last_bool; i++) {
626         if (tp->Booleans[i] == TRUE)
627             boolmax = i + 1;
628     }
629
630     nummax = 0;
631     for (i = 0; i < last_num; i++) {
632         if (tp->Numbers[i] != ABSENT_NUMERIC)
633             nummax = i + 1;
634     }
635
636     strmax = 0;
637     for (i = 0; i < last_str; i++) {
638         if (tp->Strings[i] != ABSENT_STRING)
639             strmax = i + 1;
640     }
641
642     nextfree = compute_offsets(tp->Strings, strmax, offsets);
643
644     /* fill in the header */
645     LITTLE_ENDIAN(buf, MAGIC);
646     LITTLE_ENDIAN(buf + 2, min(namelen, MAX_NAME_SIZE + 1));
647     LITTLE_ENDIAN(buf + 4, boolmax);
648     LITTLE_ENDIAN(buf + 6, nummax);
649     LITTLE_ENDIAN(buf + 8, strmax);
650     LITTLE_ENDIAN(buf + 10, nextfree);
651
652     /* write out the header */
653     TRACE_OUT(("Header of %s @%d", namelist, *offset));
654     if (Write(buf, 12, 1) != 1
655         || Write(namelist, sizeof(char), namelen) != namelen)
656           return (ERR);
657
658     for (i = 0; i < boolmax; i++)
659         if (tp->Booleans[i] == TRUE)
660             buf[i] = TRUE;
661         else
662             buf[i] = FALSE;
663     if (Write(buf, sizeof(char), boolmax) != boolmax)
664           return (ERR);
665
666     if (even_boundary(namelen + boolmax))
667         return (ERR);
668
669     TRACE_OUT(("Numerics begin at %04x", *offset));
670
671     /* the numerics */
672     convert_shorts(buf, tp->Numbers, nummax);
673     if (Write(buf, 2, nummax) != nummax)
674         return (ERR);
675
676     TRACE_OUT(("String offsets begin at %04x", *offset));
677
678     /* the string offsets */
679     convert_shorts(buf, offsets, strmax);
680     if (Write(buf, 2, strmax) != strmax)
681         return (ERR);
682
683     TRACE_OUT(("String table begins at %04x", *offset));
684
685     /* the strings */
686     for (i = 0; i < strmax; i++)
687         if (VALID_STRING(tp->Strings[i]))
688             if (!WRITE_STRING(tp->Strings[i]))
689                 return (ERR);
690
691 #if NCURSES_XNAMES
692     if (extended_object(tp)) {
693         unsigned extcnt = NUM_EXT_NAMES(tp);
694
695         if (even_boundary(nextfree))
696             return (ERR);
697
698         nextfree = compute_offsets(tp->Strings + STRCOUNT,
699                                    tp->ext_Strings,
700                                    offsets);
701         TRACE_OUT(("after extended string capabilities, nextfree=%d", nextfree));
702
703         if (tp->ext_Strings >= SIZEOF(offsets))
704             return (ERR);
705
706         nextfree += compute_offsets(tp->ext_Names,
707                                     extcnt,
708                                     offsets + tp->ext_Strings);
709         TRACE_OUT(("after extended capnames, nextfree=%d", nextfree));
710         strmax = tp->ext_Strings + extcnt;
711
712         /*
713          * Write the extended header
714          */
715         LITTLE_ENDIAN(buf + 0, tp->ext_Booleans);
716         LITTLE_ENDIAN(buf + 2, tp->ext_Numbers);
717         LITTLE_ENDIAN(buf + 4, tp->ext_Strings);
718         LITTLE_ENDIAN(buf + 6, strmax);
719         LITTLE_ENDIAN(buf + 8, nextfree);
720         TRACE_OUT(("WRITE extended-header @%d", *offset));
721         if (Write(buf, 10, 1) != 1)
722             return (ERR);
723
724         TRACE_OUT(("WRITE %d booleans @%d", tp->ext_Booleans, *offset));
725         if (tp->ext_Booleans
726             && Write(tp->Booleans + BOOLCOUNT, sizeof(char),
727                      tp->ext_Booleans) != tp->ext_Booleans)
728               return (ERR);
729
730         if (even_boundary(tp->ext_Booleans))
731             return (ERR);
732
733         TRACE_OUT(("WRITE %d numbers @%d", tp->ext_Numbers, *offset));
734         if (tp->ext_Numbers) {
735             convert_shorts(buf, tp->Numbers + NUMCOUNT, tp->ext_Numbers);
736             if (Write(buf, 2, tp->ext_Numbers) != tp->ext_Numbers)
737                 return (ERR);
738         }
739
740         /*
741          * Convert the offsets for the ext_Strings and ext_Names tables,
742          * in that order.
743          */
744         convert_shorts(buf, offsets, strmax);
745         TRACE_OUT(("WRITE offsets @%d", *offset));
746         if (Write(buf, 2, strmax) != strmax)
747             return (ERR);
748
749         /*
750          * Write the string table after the offset tables so we do not
751          * have to do anything about alignment.
752          */
753         for (i = 0; i < tp->ext_Strings; i++) {
754             if (VALID_STRING(tp->Strings[i + STRCOUNT])) {
755                 TRACE_OUT(("WRITE ext_Strings[%d]=%s", (int) i,
756                            _nc_visbuf(tp->Strings[i + STRCOUNT])));
757                 if (!WRITE_STRING(tp->Strings[i + STRCOUNT]))
758                     return (ERR);
759             }
760         }
761
762         /*
763          * Write the extended names
764          */
765         for (i = 0; i < extcnt; i++) {
766             TRACE_OUT(("WRITE ext_Names[%d]=%s", (int) i, tp->ext_Names[i]));
767             if (!WRITE_STRING(tp->ext_Names[i]))
768                 return (ERR);
769         }
770
771     }
772 #endif /* NCURSES_XNAMES */
773
774     total_written++;
775     return (OK);
776 }
777
778 /*
779  * Returns the total number of entries written by this process
780  */
781 NCURSES_EXPORT(int)
782 _nc_tic_written(void)
783 {
784     return total_written;
785 }