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