]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/write_entry.c
ncurses 6.0 - patch 20171028
[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.99 2017/04/09 23:35:01 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     static int call_count;
273     static time_t start_time;   /* time at start of writes */
274
275 #endif /* USE_HASHED_DB */
276
277     char name_list[MAX_TERMINFO_LENGTH];
278     char *first_name, *other_names;
279     char *ptr;
280     char *term_names = tp->term_names;
281     size_t name_size = strlen(term_names);
282
283     if (name_size == 0) {
284         _nc_syserr_abort("no terminal name found.");
285     } else if (name_size >= sizeof(name_list) - 1) {
286         _nc_syserr_abort("terminal name too long: %s", term_names);
287     }
288
289     _nc_STRCPY(name_list, term_names, sizeof(name_list));
290     DEBUG(7, ("Name list = '%s'", name_list));
291
292     first_name = name_list;
293
294     ptr = &name_list[name_size - 1];
295     other_names = ptr + 1;
296
297     while (ptr > name_list && *ptr != '|')
298         ptr--;
299
300     if (ptr != name_list) {
301         *ptr = '\0';
302
303         for (ptr = name_list; *ptr != '\0' && *ptr != '|'; ptr++)
304             continue;
305
306         if (*ptr == '\0')
307             other_names = ptr;
308         else {
309             *ptr = '\0';
310             other_names = ptr + 1;
311         }
312     }
313
314     DEBUG(7, ("First name = '%s'", first_name));
315     DEBUG(7, ("Other names = '%s'", other_names));
316
317     _nc_set_type(first_name);
318
319 #if USE_HASHED_DB
320     if (_nc_write_object(tp, buffer + 1, &offset, limit - 1) != ERR) {
321         DB *capdb = _nc_db_open(_nc_tic_dir(0), TRUE);
322         DBT key, data;
323
324         if (capdb != 0) {
325             buffer[0] = 0;
326
327             memset(&key, 0, sizeof(key));
328             key.data = term_names;
329             key.size = name_size;
330
331             memset(&data, 0, sizeof(data));
332             data.data = buffer;
333             data.size = offset + 1;
334
335             _nc_db_put(capdb, &key, &data);
336
337             buffer[0] = 2;
338
339             key.data = name_list;
340             key.size = strlen(name_list);
341
342             _nc_STRCPY(buffer + 1,
343                        term_names,
344                        sizeof(buffer) - 1);
345             data.size = name_size + 1;
346
347             total_size += data.size;
348             total_parts++;
349             _nc_db_put(capdb, &key, &data);
350
351             while (*other_names != '\0') {
352                 ptr = other_names++;
353                 assert(ptr < buffer + sizeof(buffer) - 1);
354                 while (*other_names != '|' && *other_names != '\0')
355                     other_names++;
356
357                 if (*other_names != '\0')
358                     *(other_names++) = '\0';
359
360                 key.data = ptr;
361                 key.size = strlen(ptr);
362
363                 total_size += data.size;
364                 total_parts++;
365                 _nc_db_put(capdb, &key, &data);
366             }
367         }
368     }
369 #else /* !USE_HASHED_DB */
370     if (call_count++ == 0) {
371         start_time = 0;
372     }
373
374     if (strlen(first_name) >= sizeof(filename) - (2 + LEAF_LEN))
375         _nc_warning("terminal name too long.");
376
377     _nc_SPRINTF(filename, _nc_SLIMIT(sizeof(filename))
378                 LEAF_FMT "/%s", first_name[0], first_name);
379
380     /*
381      * Has this primary name been written since the first call to
382      * write_entry()?  If so, the newer write will step on the older,
383      * so warn the user.
384      */
385     if (start_time > 0 &&
386         stat(filename, &statbuf) >= 0
387         && statbuf.st_mtime >= start_time) {
388 #if HAVE_LINK && !USE_SYMLINKS
389         /*
390          * If the file has more than one link, the reason for the previous
391          * write could be that the current primary name used to be an alias for
392          * the previous entry.  In that case, unlink the file so that we will
393          * not modify the previous entry as we write this one.
394          */
395         if (statbuf.st_nlink > 1) {
396             _nc_warning("name redefined.");
397             unlink(filename);
398         } else {
399             _nc_warning("name multiply defined.");
400         }
401 #else
402         _nc_warning("name multiply defined.");
403 #endif
404     }
405
406     check_writeable(first_name[0]);
407     write_file(filename, tp);
408
409     if (start_time == 0) {
410         if (stat(filename, &statbuf) < 0
411             || (start_time = statbuf.st_mtime) == 0) {
412             _nc_syserr_abort("error obtaining time from %s/%s",
413                              _nc_tic_dir(0), filename);
414         }
415     }
416     while (*other_names != '\0') {
417         ptr = other_names++;
418         while (*other_names != '|' && *other_names != '\0')
419             other_names++;
420
421         if (*other_names != '\0')
422             *(other_names++) = '\0';
423
424         if (strlen(ptr) > sizeof(linkname) - (2 + LEAF_LEN)) {
425             _nc_warning("terminal alias %s too long.", ptr);
426             continue;
427         }
428         if (strchr(ptr, '/') != 0) {
429             _nc_warning("cannot link alias %s.", ptr);
430             continue;
431         }
432
433         check_writeable(ptr[0]);
434         _nc_SPRINTF(linkname, _nc_SLIMIT(sizeof(linkname))
435                     LEAF_FMT "/%s", ptr[0], ptr);
436
437         if (strcmp(filename, linkname) == 0) {
438             _nc_warning("self-synonym ignored");
439         } else if (stat(linkname, &statbuf) >= 0 &&
440                    statbuf.st_mtime < start_time) {
441             _nc_warning("alias %s multiply defined.", ptr);
442         } else if (_nc_access(linkname, W_OK) == 0)
443 #if HAVE_LINK
444         {
445             int code;
446 #if USE_SYMLINKS
447 #define MY_SIZE sizeof(symlinkname) - 1
448             if (first_name[0] == linkname[0]) {
449                 _nc_STRNCPY(symlinkname, first_name, MY_SIZE);
450             } else {
451                 _nc_STRCPY(symlinkname, "../", sizeof(symlinkname));
452                 _nc_STRNCPY(symlinkname + 3, filename, MY_SIZE - 3);
453             }
454             symlinkname[MY_SIZE] = '\0';
455 #endif /* USE_SYMLINKS */
456 #if HAVE_REMOVE
457             code = remove(linkname);
458 #else
459             code = unlink(linkname);
460 #endif
461             if (code != 0 && errno == ENOENT)
462                 code = 0;
463 #if USE_SYMLINKS
464             if (symlink(symlinkname, linkname) < 0)
465 #else
466             if (link(filename, linkname) < 0)
467 #endif /* USE_SYMLINKS */
468             {
469                 /*
470                  * If there wasn't anything there, and we cannot
471                  * link to the target because it is the same as the
472                  * target, then the source must be on a filesystem
473                  * that uses caseless filenames, such as Win32, etc.
474                  */
475                 if (code == 0 && errno == EEXIST)
476                     _nc_warning("can't link %s to %s", filename, linkname);
477                 else if (code == 0 && (errno == EPERM || errno == ENOENT))
478                     write_file(linkname, tp);
479                 else {
480 #if MIXEDCASE_FILENAMES
481                     _nc_syserr_abort("can't link %s to %s", filename, linkname);
482 #else
483                     _nc_warning("can't link %s to %s (errno=%d)", filename,
484                                 linkname, errno);
485 #endif
486                 }
487             } else {
488                 DEBUG(1, ("Linked %s", linkname));
489             }
490         }
491 #else /* just make copies */
492             write_file(linkname, tp);
493 #endif /* HAVE_LINK */
494     }
495 #endif /* USE_HASHED_DB */
496 }
497
498 static size_t
499 fake_write(char *dst,
500            unsigned *offset,
501            size_t limit,
502            char *src,
503            size_t want,
504            size_t size)
505 {
506     size_t have = (limit - *offset);
507
508     want *= size;
509     if (have > 0) {
510         if (want > have)
511             want = have;
512         memcpy(dst + *offset, src, want);
513         *offset += (unsigned) want;
514     } else {
515         want = 0;
516     }
517     return (want / size);
518 }
519
520 #define Write(buf, size, count) fake_write(buffer, offset, (size_t) limit, (char *) buf, (size_t) count, (size_t) size)
521
522 #undef LITTLE_ENDIAN            /* BSD/OS defines this as a feature macro */
523 #define HI(x)                   ((x) / 256)
524 #define LO(x)                   ((x) % 256)
525 #define LITTLE_ENDIAN(p, x)     (p)[0] = (unsigned char)LO(x),  \
526                                 (p)[1] = (unsigned char)HI(x)
527
528 #define WRITE_STRING(str) (Write(str, sizeof(char), strlen(str) + 1) == strlen(str) + 1)
529
530 static int
531 compute_offsets(char **Strings, size_t strmax, short *offsets)
532 {
533     int nextfree = 0;
534     size_t i;
535
536     for (i = 0; i < strmax; i++) {
537         if (Strings[i] == ABSENT_STRING) {
538             offsets[i] = -1;
539         } else if (Strings[i] == CANCELLED_STRING) {
540             offsets[i] = -2;
541         } else {
542             offsets[i] = (short) nextfree;
543             nextfree += (int) strlen(Strings[i]) + 1;
544             TRACE_OUT(("put Strings[%d]=%s(%d)", (int) i,
545                        _nc_visbuf(Strings[i]), (int) nextfree));
546         }
547     }
548     return nextfree;
549 }
550
551 static void
552 convert_shorts(unsigned char *buf, short *Numbers, size_t count)
553 {
554     size_t i;
555     for (i = 0; i < count; i++) {
556         if (Numbers[i] == ABSENT_NUMERIC) {     /* HI/LO won't work */
557             buf[2 * i] = buf[2 * i + 1] = 0377;
558         } else if (Numbers[i] == CANCELLED_NUMERIC) {   /* HI/LO won't work */
559             buf[2 * i] = 0376;
560             buf[2 * i + 1] = 0377;
561         } else {
562             LITTLE_ENDIAN(buf + 2 * i, Numbers[i]);
563             TRACE_OUT(("put Numbers[%u]=%d", (unsigned) i, Numbers[i]));
564         }
565     }
566 }
567
568 #if NCURSES_EXT_NUMBERS
569 static void
570 convert_numbers(unsigned char *buf, NCURSES_INT2 *Numbers, size_t count)
571 {
572     size_t i;
573     for (i = 0; i < count; i++) {
574         if (Numbers[i] == ABSENT_NUMERIC) {     /* HI/LO won't work */
575             buf[2 * i] = buf[2 * i + 1] = 0377;
576         } else if (Numbers[i] == CANCELLED_NUMERIC) {   /* HI/LO won't work */
577             buf[2 * i] = 0376;
578             buf[2 * i + 1] = 0377;
579         } else {
580             LITTLE_ENDIAN(buf + 2 * i, Numbers[i]);
581             TRACE_OUT(("put Numbers[%u]=%d", (unsigned) i, Numbers[i]));
582         }
583     }
584 }
585
586 #else
587 #define convert_numbers(buf,vec,len) convert_shorts(buf,vec,len)
588 #endif
589
590 #define even_boundary(value) \
591             ((value) % 2 != 0 && Write(&zero, sizeof(char), 1) != 1)
592
593 #if NCURSES_XNAMES
594 static unsigned
595 extended_Booleans(TERMTYPE2 *tp)
596 {
597     unsigned result = 0;
598     unsigned i;
599
600     for (i = 0; i < tp->ext_Booleans; ++i) {
601         if (tp->Booleans[BOOLCOUNT + i] == TRUE)
602             result = (i + 1);
603     }
604     return result;
605 }
606
607 static unsigned
608 extended_Numbers(TERMTYPE2 *tp)
609 {
610     unsigned result = 0;
611     unsigned i;
612
613     for (i = 0; i < tp->ext_Numbers; ++i) {
614         if (tp->Numbers[NUMCOUNT + i] != ABSENT_NUMERIC)
615             result = (i + 1);
616     }
617     return result;
618 }
619
620 static unsigned
621 extended_Strings(TERMTYPE2 *tp)
622 {
623     unsigned short result = 0;
624     unsigned short i;
625
626     for (i = 0; i < tp->ext_Strings; ++i) {
627         if (tp->Strings[STRCOUNT + i] != ABSENT_STRING)
628             result = (unsigned short) (i + 1);
629     }
630     return result;
631 }
632
633 /*
634  * _nc_align_termtype() will extend entries that are referenced in a use=
635  * clause - discard the unneeded data.
636  */
637 static bool
638 extended_object(TERMTYPE2 *tp)
639 {
640     bool result = FALSE;
641
642     if (_nc_user_definable) {
643         result = ((extended_Booleans(tp)
644                    + extended_Numbers(tp)
645                    + extended_Strings(tp)) != 0);
646     }
647     return result;
648 }
649 #endif
650
651 NCURSES_EXPORT(int)
652 _nc_write_object(TERMTYPE2 *tp, char *buffer, unsigned *offset, unsigned limit)
653 {
654     char *namelist;
655     size_t namelen, boolmax, nummax, strmax;
656     char zero = '\0';
657     size_t i;
658     int nextfree;
659     short offsets[MAX_ENTRY_SIZE / 2];
660     unsigned char buf[MAX_ENTRY_SIZE];
661     unsigned last_bool = BOOLWRITE;
662     unsigned last_num = NUMWRITE;
663     unsigned last_str = STRWRITE;
664
665 #if NCURSES_XNAMES
666     /*
667      * Normally we limit the list of values to exclude the "obsolete"
668      * capabilities.  However, if we are accepting extended names, add
669      * these as well, since they are used for supporting translation
670      * to/from termcap.
671      */
672     if (_nc_user_definable) {
673         last_bool = BOOLCOUNT;
674         last_num = NUMCOUNT;
675         last_str = STRCOUNT;
676     }
677 #endif
678
679     namelist = tp->term_names;
680     namelen = strlen(namelist) + 1;
681
682     boolmax = 0;
683     for (i = 0; i < last_bool; i++) {
684         if (tp->Booleans[i] == TRUE)
685             boolmax = i + 1;
686     }
687
688     nummax = 0;
689     for (i = 0; i < last_num; i++) {
690         if (tp->Numbers[i] != ABSENT_NUMERIC)
691             nummax = i + 1;
692     }
693
694     strmax = 0;
695     for (i = 0; i < last_str; i++) {
696         if (tp->Strings[i] != ABSENT_STRING)
697             strmax = i + 1;
698     }
699
700     nextfree = compute_offsets(tp->Strings, strmax, offsets);
701
702     /* fill in the header */
703     LITTLE_ENDIAN(buf, MAGIC);
704     LITTLE_ENDIAN(buf + 2, min(namelen, MAX_NAME_SIZE + 1));
705     LITTLE_ENDIAN(buf + 4, boolmax);
706     LITTLE_ENDIAN(buf + 6, nummax);
707     LITTLE_ENDIAN(buf + 8, strmax);
708     LITTLE_ENDIAN(buf + 10, nextfree);
709
710     /* write out the header */
711     TRACE_OUT(("Header of %s @%d", namelist, *offset));
712     if (Write(buf, 12, 1) != 1
713         || Write(namelist, sizeof(char), namelen) != namelen)
714           return (ERR);
715
716     for (i = 0; i < boolmax; i++)
717         if (tp->Booleans[i] == TRUE)
718             buf[i] = TRUE;
719         else
720             buf[i] = FALSE;
721     if (Write(buf, sizeof(char), boolmax) != boolmax)
722           return (ERR);
723
724     if (even_boundary(namelen + boolmax))
725         return (ERR);
726
727     TRACE_OUT(("Numerics begin at %04x", *offset));
728
729     /* the numerics */
730     convert_numbers(buf, tp->Numbers, nummax);
731     if (Write(buf, 2, nummax) != nummax)
732         return (ERR);
733
734     TRACE_OUT(("String offsets begin at %04x", *offset));
735
736     /* the string offsets */
737     convert_shorts(buf, offsets, strmax);
738     if (Write(buf, 2, strmax) != strmax)
739         return (ERR);
740
741     TRACE_OUT(("String table begins at %04x", *offset));
742
743     /* the strings */
744     for (i = 0; i < strmax; i++)
745         if (VALID_STRING(tp->Strings[i]))
746             if (!WRITE_STRING(tp->Strings[i]))
747                 return (ERR);
748
749 #if NCURSES_XNAMES
750     if (extended_object(tp)) {
751         unsigned extcnt = (unsigned) NUM_EXT_NAMES(tp);
752
753         if (even_boundary(nextfree))
754             return (ERR);
755
756         nextfree = compute_offsets(tp->Strings + STRCOUNT,
757                                    (size_t) tp->ext_Strings,
758                                    offsets);
759         TRACE_OUT(("after extended string capabilities, nextfree=%d", nextfree));
760
761         if (tp->ext_Strings >= SIZEOF(offsets))
762             return (ERR);
763
764         nextfree += compute_offsets(tp->ext_Names,
765                                     (size_t) extcnt,
766                                     offsets + tp->ext_Strings);
767         TRACE_OUT(("after extended capnames, nextfree=%d", nextfree));
768         strmax = tp->ext_Strings + extcnt;
769
770         /*
771          * Write the extended header
772          */
773         LITTLE_ENDIAN(buf + 0, tp->ext_Booleans);
774         LITTLE_ENDIAN(buf + 2, tp->ext_Numbers);
775         LITTLE_ENDIAN(buf + 4, tp->ext_Strings);
776         LITTLE_ENDIAN(buf + 6, strmax);
777         LITTLE_ENDIAN(buf + 8, nextfree);
778         TRACE_OUT(("WRITE extended-header @%d", *offset));
779         if (Write(buf, 10, 1) != 1)
780             return (ERR);
781
782         TRACE_OUT(("WRITE %d booleans @%d", tp->ext_Booleans, *offset));
783         if (tp->ext_Booleans
784             && Write(tp->Booleans + BOOLCOUNT, sizeof(char),
785                      tp->ext_Booleans) != tp->ext_Booleans)
786               return (ERR);
787
788         if (even_boundary(tp->ext_Booleans))
789             return (ERR);
790
791         TRACE_OUT(("WRITE %d numbers @%d", tp->ext_Numbers, *offset));
792         if (tp->ext_Numbers) {
793             convert_numbers(buf, tp->Numbers + NUMCOUNT, (size_t) tp->ext_Numbers);
794             if (Write(buf, 2, tp->ext_Numbers) != tp->ext_Numbers)
795                 return (ERR);
796         }
797
798         /*
799          * Convert the offsets for the ext_Strings and ext_Names tables,
800          * in that order.
801          */
802         convert_shorts(buf, offsets, strmax);
803         TRACE_OUT(("WRITE offsets @%d", *offset));
804         if (Write(buf, 2, strmax) != strmax)
805             return (ERR);
806
807         /*
808          * Write the string table after the offset tables so we do not
809          * have to do anything about alignment.
810          */
811         for (i = 0; i < tp->ext_Strings; i++) {
812             if (VALID_STRING(tp->Strings[i + STRCOUNT])) {
813                 TRACE_OUT(("WRITE ext_Strings[%d]=%s", (int) i,
814                            _nc_visbuf(tp->Strings[i + STRCOUNT])));
815                 if (!WRITE_STRING(tp->Strings[i + STRCOUNT]))
816                     return (ERR);
817             }
818         }
819
820         /*
821          * Write the extended names
822          */
823         for (i = 0; i < extcnt; i++) {
824             TRACE_OUT(("WRITE ext_Names[%d]=%s", (int) i, tp->ext_Names[i]));
825             if (!WRITE_STRING(tp->ext_Names[i]))
826                 return (ERR);
827         }
828
829     }
830 #endif /* NCURSES_XNAMES */
831
832     total_written++;
833     total_parts++;
834     total_size = total_size + (int) (*offset + 1);
835     return (OK);
836 }
837
838 /*
839  * Returns the total number of entries written by this process
840  */
841 NCURSES_EXPORT(int)
842 _nc_tic_written(void)
843 {
844     TR(TRACE_DATABASE, ("_nc_tic_written %d entries, %d parts, %d size",
845                         total_written, total_parts, total_size));
846     return total_written;
847 }