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