Refactor error messages to avoid macros.
[m4/ericb.git] / src / output.c
blob478d3b28d4516d506083569dd22088fb0aafa7a9
1 /* GNU m4 -- A simple macro processor
3 Copyright (C) 1989, 1990, 1991, 1992, 1993, 1994, 2004, 2005, 2006,
4 2007 Free Software Foundation, Inc.
6 This file is part of GNU M4.
8 GNU M4 is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU M4 is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "m4.h"
24 #include <limits.h>
25 #include <sys/stat.h>
27 #include "gl_avltree_oset.h"
29 /* Size of initial in-memory buffer size for diversions. Small diversions
30 would usually fit in. */
31 #define INITIAL_BUFFER_SIZE 512
33 /* Maximum value for the total of all in-memory buffer sizes for
34 diversions. */
35 #define MAXIMUM_TOTAL_SIZE (512 * 1024)
37 /* Size of buffer size to use while copying files. */
38 #define COPY_BUFFER_SIZE (32 * 512)
40 /* Output functions. Most of the complexity is for handling cpp like
41 sync lines.
43 This code is fairly entangled with the code in input.c, and maybe it
44 belongs there? */
46 typedef struct temp_dir m4_temp_dir;
48 /* When part of diversion_table, each struct m4_diversion either
49 represents an open file (zero size, non-NULL u.file), an in-memory
50 buffer (non-zero size, non-NULL u.buffer), or an unused placeholder
51 diversion (zero size, u is NULL, non-zero used indicates that a
52 file has been created). When not part of diversion_table, u.next
53 is a pointer to the free_list chain. */
55 typedef struct m4_diversion m4_diversion;
57 struct m4_diversion
59 union
61 FILE *file; /* diversion file on disk */
62 char *buffer; /* in-memory diversion buffer */
63 m4_diversion *next; /* free-list pointer */
64 } u;
65 int divnum; /* which diversion this represents */
66 int size; /* usable size before reallocation */
67 int used; /* used length in characters */
70 /* Table of diversions 1 through INT_MAX. */
71 static gl_oset_t diversion_table;
73 /* Diversion 0 (not part of diversion_table). */
74 static m4_diversion div0;
76 /* Linked list of reclaimed diversion storage. */
77 static m4_diversion *free_list;
79 /* Obstack from which diversion storage is allocated. */
80 static struct obstack diversion_storage;
82 /* Total size of all in-memory buffer sizes. */
83 static int total_buffer_size;
85 /* The number of the currently active diversion. This variable is
86 maintained for the `divnum' builtin function. */
87 int current_diversion;
89 /* Current output diversion, NULL if output is being currently discarded. */
90 static m4_diversion *output_diversion;
92 /* Values of some output_diversion fields, cached out for speed. */
93 static FILE *output_file; /* current value of (file) */
94 static char *output_cursor; /* current value of (buffer + used) */
95 static int output_unused; /* current value of (size - used) */
97 /* Number of input line we are generating output for. */
98 int output_current_line;
100 /* Temporary directory holding all spilled diversion files. */
101 static m4_temp_dir *output_temp_dir;
105 /*------------------------.
106 | Output initialization. |
107 `------------------------*/
109 /* Callback for comparing list elements ELT1 and ELT2 for order in
110 diversion_table. */
111 static int
112 cmp_diversion_CB (const void *elt1, const void *elt2)
114 const m4_diversion *d1 = (const m4_diversion *) elt1;
115 const m4_diversion *d2 = (const m4_diversion *) elt2;
116 /* No need to worry about overflow, since we don't create diversions
117 with negative divnum. */
118 return d1->divnum - d2->divnum;
121 /* Callback for comparing list element ELT against THRESHOLD. */
122 static bool
123 threshold_diversion_CB (const void *elt, const void *threshold)
125 const m4_diversion *div = (const m4_diversion *) elt;
126 /* No need to worry about overflow, since we don't create diversions
127 with negative divnum. */
128 return div->divnum >= *(const int *) threshold;
131 void
132 output_init (void)
134 diversion_table = gl_oset_create_empty (GL_AVLTREE_OSET, cmp_diversion_CB,
135 NULL);
136 div0.u.file = stdout;
137 output_diversion = &div0;
138 output_file = stdout;
139 obstack_init (&diversion_storage);
142 void
143 output_exit (void)
145 /* Order is important, since we may have registered cleanup_tmpfile
146 as an atexit handler, and it must not traverse stale memory. */
147 gl_oset_t table = diversion_table;
148 diversion_table = NULL;
149 gl_oset_free (table);
150 obstack_free (&diversion_storage, NULL);
153 /* Clean up any temporary directory. Designed for use as an atexit
154 handler, where it is not safe to call exit() recursively; so this
155 calls _exit if a problem is encountered. */
156 static void
157 cleanup_tmpfile (void)
159 /* Close any open diversions. */
160 bool fail = false;
162 if (diversion_table)
164 const void *elt;
165 gl_oset_iterator_t iter = gl_oset_iterator (diversion_table);
166 while (gl_oset_iterator_next (&iter, &elt))
168 m4_diversion *diversion = (m4_diversion *) elt;
169 if (!diversion->size && diversion->u.file
170 && close_stream_temp (diversion->u.file) != 0)
172 m4_warn (errno, NULL,
173 _("cannot clean temporary file for diversion"));
174 fail = true;
177 gl_oset_iterator_free (&iter);
180 /* Clean up the temporary directory. */
181 if (cleanup_temp_dir (output_temp_dir) != 0)
182 fail = true;
183 if (fail)
184 _exit (exit_failure);
187 /* Convert DIVNUM into a temporary file name for use in m4_tmp*. */
188 static const char *
189 m4_tmpname (int divnum)
191 static char *buffer;
192 static char *tail;
193 if (buffer == NULL)
195 tail = xasprintf ("%s/m4-%d", output_temp_dir->dir_name, INT_MAX);
196 buffer = obstack_copy0 (&diversion_storage, tail, strlen (tail));
197 free (tail);
198 tail = strrchr (buffer, '-') + 1;
200 if (sprintf (tail, "%d", divnum) < 0)
201 m4_error (EXIT_FAILURE, errno, NULL,
202 _("cannot create temporary file for diversion"));
203 return buffer;
206 /* Create a temporary file for diversion DIVNUM open for reading and
207 writing in a secure temp directory. The file will be automatically
208 closed and deleted on a fatal signal. The file can be closed and
209 reopened with m4_tmpclose and m4_tmpopen; when finally done with
210 the file, close it with m4_tmpremove. Exits on failure, so the
211 return value is always an open file. */
212 static FILE *
213 m4_tmpfile (int divnum)
215 const char *name;
216 FILE *file;
218 if (output_temp_dir == NULL)
220 output_temp_dir = create_temp_dir ("m4-", NULL, true);
221 if (output_temp_dir == NULL)
222 m4_error (EXIT_FAILURE, errno, NULL,
223 _("cannot create temporary file for diversion"));
224 atexit (cleanup_tmpfile);
226 name = m4_tmpname (divnum);
227 register_temp_file (output_temp_dir, name);
228 file = fopen_temp (name, O_BINARY ? "wb+" : "w+");
229 if (file == NULL)
231 unregister_temp_file (output_temp_dir, name);
232 m4_error (EXIT_FAILURE, errno, NULL,
233 _("cannot create temporary file for diversion"));
235 else if (set_cloexec_flag (fileno (file), true) != 0)
236 m4_warn (errno, NULL, _("cannot protect diversion across forks"));
237 return file;
240 /* Reopen a temporary file for diversion DIVNUM for reading and
241 writing in a secure temp directory. Exits on failure, so the
242 return value is always an open file. */
243 static FILE *
244 m4_tmpopen (int divnum)
246 const char *name = m4_tmpname (divnum);
247 FILE *file;
249 file = fopen_temp (name, O_BINARY ? "ab+" : "a+");
250 if (file == NULL)
251 m4_error (EXIT_FAILURE, errno, NULL,
252 _("cannot create temporary file for diversion"));
253 else if (set_cloexec_flag (fileno (file), true) != 0)
254 m4_warn (errno, NULL, _("cannot protect diversion across forks"));
255 /* POSIX states that it is undefined whether an append stream starts
256 at offset 0 or at the end. We want the beginning. */
257 else if (fseeko (file, 0, SEEK_SET) != 0)
258 m4_error (EXIT_FAILURE, errno, NULL,
259 _("cannot seek to beginning of diversion"));
260 return file;
263 /* Close, but don't delete, a temporary FILE. */
264 static int
265 m4_tmpclose (FILE *file)
267 return close_stream_temp (file);
270 /* Delete a closed temporary FILE for diversion DIVNUM. */
271 static int
272 m4_tmpremove (int divnum)
274 return cleanup_temp_file (output_temp_dir, m4_tmpname (divnum));
277 /*-----------------------------------------------------------------------.
278 | Reorganize in-memory diversion buffers so the current diversion can |
279 | accomodate LENGTH more characters without further reorganization. The |
280 | current diversion buffer is made bigger if possible. But to make room |
281 | for a bigger buffer, one of the in-memory diversion buffers might have |
282 | to be flushed to a newly created temporary file. This flushed buffer |
283 | might well be the current one. |
284 `-----------------------------------------------------------------------*/
286 static void
287 make_room_for (int length)
289 int wanted_size;
290 m4_diversion *selected_diversion = NULL;
292 /* Compute needed size for in-memory buffer. Diversions in-memory
293 buffers start at 0 bytes, then 512, then keep doubling until it is
294 decided to flush them to disk. */
296 output_diversion->used = output_diversion->size - output_unused;
298 for (wanted_size = output_diversion->size;
299 wanted_size < output_diversion->used + length;
300 wanted_size = wanted_size == 0 ? INITIAL_BUFFER_SIZE : wanted_size * 2)
303 /* Check if we are exceeding the maximum amount of buffer memory. */
305 if (total_buffer_size - output_diversion->size + wanted_size
306 > MAXIMUM_TOTAL_SIZE)
308 int selected_used;
309 char *selected_buffer;
310 m4_diversion *diversion;
311 int count;
312 gl_oset_iterator_t iter;
313 const void *elt;
315 /* Find out the buffer having most data, in view of flushing it to
316 disk. Fake the current buffer as having already received the
317 projected data, while making the selection. So, if it is
318 selected indeed, we will flush it smaller, before it grows. */
320 selected_diversion = output_diversion;
321 selected_used = output_diversion->used + length;
323 iter = gl_oset_iterator (diversion_table);
324 while (gl_oset_iterator_next (&iter, &elt))
326 diversion = (m4_diversion *) elt;
327 if (diversion->used > selected_used)
329 selected_diversion = diversion;
330 selected_used = diversion->used;
333 gl_oset_iterator_free (&iter);
335 /* Create a temporary file, write the in-memory buffer of the
336 diversion to this file, then release the buffer. Zero the
337 diversion before doing anything that can exit () (including
338 m4_tmpfile), so that the atexit handler doesn't try to close
339 a garbage pointer as a file. */
341 selected_buffer = selected_diversion->u.buffer;
342 total_buffer_size -= selected_diversion->size;
343 selected_diversion->size = 0;
344 selected_diversion->u.file = NULL;
345 selected_diversion->u.file = m4_tmpfile (selected_diversion->divnum);
347 if (selected_diversion->used > 0)
349 count = fwrite (selected_buffer, (size_t) selected_diversion->used,
350 1, selected_diversion->u.file);
351 if (count != 1)
352 m4_error (EXIT_FAILURE, errno, NULL,
353 _("cannot flush diversion to temporary file"));
356 /* Reclaim the buffer space for other diversions. */
358 free (selected_buffer);
359 selected_diversion->used = 1;
362 /* Reload output_file, just in case the flushed diversion was current. */
364 if (output_diversion == selected_diversion)
366 /* The flushed diversion was current indeed. */
368 output_file = output_diversion->u.file;
369 output_cursor = NULL;
370 output_unused = 0;
372 else
374 /* Close any selected file since it is not the current diversion. */
375 if (selected_diversion)
377 FILE *file = selected_diversion->u.file;
378 selected_diversion->u.file = NULL;
379 if (m4_tmpclose (file) != 0)
380 m4_warn (errno, NULL,
381 _("cannot close temporary file for diversion"));
384 /* The current buffer may be safely reallocated. */
385 output_diversion->u.buffer
386 = xrealloc (output_diversion->u.buffer, (size_t) wanted_size);
388 total_buffer_size += wanted_size - output_diversion->size;
389 output_diversion->size = wanted_size;
391 output_cursor = output_diversion->u.buffer + output_diversion->used;
392 output_unused = wanted_size - output_diversion->used;
396 /*------------------------------------------------------------------------.
397 | Output one character CHAR, when it is known that it goes to a diversion |
398 | file or an in-memory diversion buffer. |
399 `------------------------------------------------------------------------*/
401 #define OUTPUT_CHARACTER(Char) \
402 if (output_file) \
403 putc ((Char), output_file); \
404 else if (output_unused == 0) \
405 output_character_helper ((Char)); \
406 else \
407 (output_unused--, *output_cursor++ = (Char))
409 static void
410 output_character_helper (int character)
412 make_room_for (1);
414 if (output_file)
415 putc (character, output_file);
416 else
418 *output_cursor++ = character;
419 output_unused--;
423 /*------------------------------------------------------------------------.
424 | Output one TEXT having LENGTH characters, when it is known that it goes |
425 | to a diversion file or an in-memory diversion buffer. |
426 `------------------------------------------------------------------------*/
428 void
429 output_text (const char *text, int length)
431 int count;
433 if (!output_diversion || !length)
434 return;
436 if (!output_file && length > output_unused)
437 make_room_for (length);
439 if (output_file)
441 count = fwrite (text, length, 1, output_file);
442 if (count != 1)
443 m4_error (EXIT_FAILURE, errno, NULL, _("error copying inserted file"));
445 else
447 memcpy (output_cursor, text, (size_t) length);
448 output_cursor += length;
449 output_unused -= length;
453 /*--------------------------------------------------------------------.
454 | Add some text into an obstack OBS, taken from TEXT, having LENGTH |
455 | characters. If OBS is NULL, output the text to an external file |
456 | or an in-memory diversion buffer instead. If OBS is NULL, and |
457 | there is no output file, the text is discarded. LINE is the line |
458 | where the token starts (not necessarily current_line, in the case |
459 | of multiline tokens). |
461 | If we are generating sync lines, the output has to be examined, |
462 | because we need to know how much output each input line generates. |
463 | In general, sync lines are output whenever a single input lines |
464 | generates several output lines, or when several input lines do not |
465 | generate any output. |
466 `--------------------------------------------------------------------*/
468 void
469 shipout_text (struct obstack *obs, const char *text, int length, int line)
471 static bool start_of_output_line = true;
472 const char *cursor;
474 /* If output goes to an obstack, merely add TEXT to it. */
476 if (obs != NULL)
478 obstack_grow (obs, text, length);
479 return;
482 /* Do nothing if TEXT should be discarded. */
484 if (output_diversion == NULL)
485 return;
487 /* Output TEXT to a file, or in-memory diversion buffer. */
489 if (!sync_output)
490 switch (length)
493 /* In-line short texts. */
495 case 8: OUTPUT_CHARACTER (*text); text++;
496 case 7: OUTPUT_CHARACTER (*text); text++;
497 case 6: OUTPUT_CHARACTER (*text); text++;
498 case 5: OUTPUT_CHARACTER (*text); text++;
499 case 4: OUTPUT_CHARACTER (*text); text++;
500 case 3: OUTPUT_CHARACTER (*text); text++;
501 case 2: OUTPUT_CHARACTER (*text); text++;
502 case 1: OUTPUT_CHARACTER (*text);
503 case 0:
504 return;
506 /* Optimize longer texts. */
508 default:
509 output_text (text, length);
511 else
513 /* Check for syncline only at the start of a token. Multiline
514 tokens, and tokens that are out of sync but in the middle of
515 the line, must wait until the next raw newline triggers a
516 syncline. */
517 if (start_of_output_line)
519 start_of_output_line = false;
520 output_current_line++;
521 #ifdef DEBUG_OUTPUT
522 xfprintf (stderr, "DEBUG: line %d, cur %d, cur out %d\n",
523 line, current_line, output_current_line);
524 #endif
526 /* Output a `#line NUM' synchronization directive if needed.
527 If output_current_line was previously given a negative
528 value (invalidated), output `#line NUM "FILE"' instead. */
530 if (output_current_line != line)
532 OUTPUT_CHARACTER ('#');
533 OUTPUT_CHARACTER ('l');
534 OUTPUT_CHARACTER ('i');
535 OUTPUT_CHARACTER ('n');
536 OUTPUT_CHARACTER ('e');
537 OUTPUT_CHARACTER (' ');
538 for (cursor = ntoa (line, 10); *cursor; cursor++)
539 OUTPUT_CHARACTER (*cursor);
540 if (output_current_line < 1 && current_file[0] != '\0')
542 OUTPUT_CHARACTER (' ');
543 OUTPUT_CHARACTER ('"');
544 for (cursor = current_file; *cursor; cursor++)
545 OUTPUT_CHARACTER (*cursor);
546 OUTPUT_CHARACTER ('"');
548 OUTPUT_CHARACTER ('\n');
549 output_current_line = line;
553 /* Output the token, and track embedded newlines. */
554 for (; length-- > 0; text++)
556 if (start_of_output_line)
558 start_of_output_line = false;
559 output_current_line++;
560 #ifdef DEBUG_OUTPUT
561 xfprintf (stderr, "DEBUG: line %d, cur %d, cur out %d\n",
562 line, current_line, output_current_line);
563 #endif
565 OUTPUT_CHARACTER (*text);
566 if (*text == '\n')
567 start_of_output_line = true;
572 /* Functions for use by diversions. */
574 /*--------------------------------------------------------------------------.
575 | Make a file for diversion DIVNUM, and install it in the diversion table. |
576 | Grow the size of the diversion table as needed. |
577 `--------------------------------------------------------------------------*/
579 /* The number of possible diversions is limited only by memory and
580 available file descriptors (each overflowing diversion uses one). */
582 void
583 make_diversion (int divnum)
585 m4_diversion *diversion = NULL;
587 if (current_diversion == divnum)
588 return;
590 if (output_diversion)
592 if (!output_diversion->size && !output_diversion->u.file)
594 if (!gl_oset_remove (diversion_table, output_diversion))
595 assert (false);
596 output_diversion->u.next = free_list;
597 output_diversion->used = 0;
598 free_list = output_diversion;
600 else if (output_diversion->size)
601 output_diversion->used = output_diversion->size - output_unused;
602 else if (output_diversion->used)
604 FILE *file = output_diversion->u.file;
605 output_diversion->u.file = NULL;
606 if (m4_tmpclose (file) != 0)
607 m4_warn (errno, NULL,
608 _("cannot close temporary file for diversion"));
610 output_diversion = NULL;
611 output_file = NULL;
612 output_cursor = NULL;
613 output_unused = 0;
616 current_diversion = divnum;
618 if (divnum < 0)
619 return;
621 if (divnum == 0)
622 diversion = &div0;
623 else
625 const void *elt;
626 if (gl_oset_search_atleast (diversion_table, threshold_diversion_CB,
627 &divnum, &elt))
629 m4_diversion *temp = (m4_diversion *) elt;
630 if (temp->divnum == divnum)
631 diversion = temp;
634 if (diversion == NULL)
636 /* First time visiting this diversion. */
637 if (free_list)
639 diversion = free_list;
640 free_list = diversion->u.next;
642 else
644 diversion = (m4_diversion *) obstack_alloc (&diversion_storage,
645 sizeof *diversion);
646 diversion->size = 0;
647 diversion->used = 0;
649 diversion->u.file = NULL;
650 diversion->divnum = divnum;
651 gl_oset_add (diversion_table, diversion);
654 output_diversion = diversion;
655 if (output_diversion->size)
657 output_cursor = output_diversion->u.buffer + output_diversion->used;
658 output_unused = output_diversion->size - output_diversion->used;
660 else
662 if (!output_diversion->u.file && output_diversion->used)
663 output_diversion->u.file = m4_tmpopen (output_diversion->divnum);
664 output_file = output_diversion->u.file;
666 output_current_line = -1;
669 /*-------------------------------------------------------------------.
670 | Insert a FILE into the current output file, in the same manner |
671 | diversions are handled. This allows files to be included, without |
672 | having them rescanned by m4. |
673 `-------------------------------------------------------------------*/
675 void
676 insert_file (FILE *file)
678 char buffer[COPY_BUFFER_SIZE];
679 size_t length;
681 /* Optimize out inserting into a sink. */
683 if (!output_diversion)
684 return;
686 /* Insert output by big chunks. */
688 for (;;)
690 length = fread (buffer, 1, COPY_BUFFER_SIZE, file);
691 if (ferror (file))
692 m4_error (EXIT_FAILURE, errno, NULL, _("error reading inserted file"));
693 if (length == 0)
694 break;
695 output_text (buffer, length);
699 /*-------------------------------------------------------------------.
700 | Insert DIVERSION (but not div0) into the current output file. The |
701 | diversion is NOT placed on the expansion obstack, because it must |
702 | not be rescanned. When the file is closed, it is deleted by the |
703 | system. |
704 `-------------------------------------------------------------------*/
706 static void
707 insert_diversion_helper (m4_diversion *diversion)
709 /* Effectively undivert only if an output stream is active. */
710 if (output_diversion)
712 if (diversion->size)
713 output_text (diversion->u.buffer, diversion->used);
714 else
716 if (!diversion->u.file)
717 diversion->u.file = m4_tmpopen (diversion->divnum);
718 insert_file (diversion->u.file);
721 output_current_line = -1;
724 /* Return all space used by the diversion. */
725 if (diversion->size)
727 free (diversion->u.buffer);
728 diversion->size = 0;
729 diversion->used = 0;
731 else
733 if (diversion->u.file)
735 FILE *file = diversion->u.file;
736 diversion->u.file = NULL;
737 diversion->used = 0;
738 if (m4_tmpclose (file) != 0)
739 m4_warn (errno, NULL,
740 _("cannot clean temporary file for diversion"));
742 if (m4_tmpremove (diversion->divnum) != 0)
743 m4_warn (errno, NULL, _("cannot clean temporary file for diversion"));
745 gl_oset_remove (diversion_table, diversion);
746 diversion->u.next = free_list;
747 free_list = diversion;
750 /*-------------------------------------------------------------------------.
751 | Insert diversion number DIVNUM into the current output file. The |
752 | diversion is NOT placed on the expansion obstack, because it must not be |
753 | rescanned. When the file is closed, it is deleted by the system. |
754 `-------------------------------------------------------------------------*/
756 void
757 insert_diversion (int divnum)
759 const void *elt;
761 /* Do not care about nonexistent diversions, and undiverting stdout
762 or self is a no-op. */
763 if (divnum <= 0 || current_diversion == divnum)
764 return;
765 if (gl_oset_search_atleast (diversion_table, threshold_diversion_CB,
766 &divnum, &elt))
768 m4_diversion *diversion = (m4_diversion *) elt;
769 if (diversion->divnum == divnum)
770 insert_diversion_helper (diversion);
774 /*-------------------------------------------------------------------------.
775 | Get back all diversions. This is done just before exiting from main (), |
776 | and from m4_undivert (), if called without arguments. |
777 `-------------------------------------------------------------------------*/
779 void
780 undivert_all (void)
782 const void *elt;
783 gl_oset_iterator_t iter = gl_oset_iterator (diversion_table);
784 while (gl_oset_iterator_next (&iter, &elt))
786 m4_diversion *diversion = (m4_diversion *) elt;
787 if (diversion->divnum != current_diversion)
788 insert_diversion_helper (diversion);
790 gl_oset_iterator_free (&iter);
793 /*-------------------------------------------------------------.
794 | Produce all diversion information in frozen format on FILE. |
795 `-------------------------------------------------------------*/
797 void
798 freeze_diversions (FILE *file)
800 int saved_number;
801 int last_inserted;
802 gl_oset_iterator_t iter;
803 const void *elt;
805 saved_number = current_diversion;
806 last_inserted = 0;
807 make_diversion (0);
808 output_file = file; /* kludge in the frozen file */
810 iter = gl_oset_iterator (diversion_table);
811 while (gl_oset_iterator_next (&iter, &elt))
813 m4_diversion *diversion = (m4_diversion *) elt;;
814 if (diversion->size || diversion->used)
816 if (diversion->size)
817 xfprintf (file, "D%d,%d\n", diversion->divnum, diversion->used);
818 else
820 struct stat file_stat;
821 diversion->u.file = m4_tmpopen (diversion->divnum);
822 if (fstat (fileno (diversion->u.file), &file_stat) < 0)
823 m4_error (EXIT_FAILURE, errno, NULL,
824 _("cannot stat diversion"));
825 if (file_stat.st_size < 0
826 || file_stat.st_size != (unsigned long int) file_stat.st_size)
827 m4_error (EXIT_FAILURE, 0, NULL, _("diversion too large"));
828 xfprintf (file, "D%d,%lu\n", diversion->divnum,
829 (unsigned long int) file_stat.st_size);
832 insert_diversion_helper (diversion);
833 putc ('\n', file);
835 last_inserted = diversion->divnum;
838 gl_oset_iterator_free (&iter);
840 /* Save the active diversion number, if not already. */
842 if (saved_number != last_inserted)
843 xfprintf (file, "D%d,0\n\n", saved_number);