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