d2c29f699ac7e33d5e1bb1879ec43e7f17e648b5
[infodrom/dtaus] / dtaus.c
1 /*
2     dtaus.c - Belegloser Datenträgeraustausch mit einer Bank
3     Copyright (c) 1996,8,2001,2,3  Martin Schulze <joey@infodrom.org>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
18
19     $Id$
20  */
21
22 #include "dtaus.h"
23 #include "bigint.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <ctype.h>
28 #include <time.h>
29 #include <malloc.h>
30
31 #define DEFAULT_EURO
32 #ifndef DEFAULT_EURO
33 int use_euro = 0;
34 #else
35 int use_euro = 1;
36 #endif
37
38 /*
39  *  First: Some control structures
40  */
41
42 typedef struct
43 {
44   char *name;
45   unsigned short int pos;
46   unsigned short int len;
47   int type;
48 } dtaus_record;
49
50 #define REC_A   1
51 #define REC_C   2
52 #define REC_E   3
53
54 #define REQ     1
55 #define OPT     2
56 #define IGN     3
57
58 dtaus_record recA[] = {
59   {"Art", 5, 2, REQ},
60   {"Name", 23, 27, REQ},
61   {"Konto", 60, 10, REQ},
62   {"BLZ", 7, 8, REQ},
63   {"Referenz", 70, 10, OPT},
64   {"Datum", 50, 6, IGN},
65   {"Ausfuehrung", 95, 8, OPT},
66   {"Currency", 127, 1, OPT},
67   {"Euro", 127, 1, OPT},
68   {"DM", 127, 1, OPT},
69   {NULL, 0, 0}
70 };
71
72 #define A_TRANS 0
73 #define A_NAME  1
74 #define A_KTO   2
75 #define A_BLZ   3
76 #define A_REF   4
77 #define A_DATE  5
78 #define A_TODO  6
79 #define A_CURR  7
80 #define A_EURO  8
81 #define A_DM    9
82 #define A_LEN   10
83 #define A_LOOP  7
84
85 dtaus_record recC[] = {
86   {"Name", 93, 27, REQ},
87   {"Konto", 21, 10, REQ},
88   {"BLZ", 13, 8, REQ},
89   {"Transaktion", 44, 5, REQ},
90 #ifndef DEFAULT_EURO
91   {"Betrag", 50, 11, REQ},
92 #else
93   {"Betrag", 79, 11, REQ},
94 #endif
95   {"Zweck", 155, 27, REQ},
96   {"myName", 128, 27, OPT},
97   {"myKonto", 69, 10, OPT},
98   {"myBLZ", 61, 8, OPT},
99   {"Text", 187, 29, OPT},
100   {"Extension", 216, 29, OPT},
101   {"Currency", 182, 1, IGN},
102 #ifndef DEFAULT_EURO
103   {"Betrag-Euro", 79, 11, IGN},
104 #else
105   {"Betrag-DM", 50, 11, IGN},
106 #endif
107   {NULL, 0, 0}
108 };
109
110 #define C_NAME  0
111 #define C_KTO   1
112 #define C_BLZ   2
113 #define C_TRANS 3
114 #define C_VAL   4
115 #define C_ZWECK 5
116 #define C_MYNAM 6
117 #define C_MYKTO 7
118 #define C_MYBLZ 8
119 #define C_TEXT  9
120 #define C_EXT  10
121 #define C_EURO 11
122 #ifndef DEFAULT_EURO
123 #define C_EUR  12
124 #else
125 #define C_DM   12
126 #endif
127 #define C_LEN  13
128 #define C_LOOP 11
129
130 #define MAX_TEXT 14
131
132 dtaus_record recE[] = {
133   {"Anzahl", 10, 7, IGN},
134 #ifndef DEFAULT_EURO
135   {"Summe", 17, 13, IGN},
136 #else
137   {"Summe", 64, 13, IGN},
138 #endif
139   {"Kontos", 30, 17, IGN},
140   {"BLZs", 47, 17, IGN},
141 #ifndef DEFAULT_EURO
142   {"Summe-Euro", 64, 13, IGN},
143 #else
144   {"Summe-DM", 17, 13, IGN},
145 #endif
146   {NULL, 0, 0}
147 };
148
149 #define E_COUNT 0
150 #define E_VAL   1
151 #define E_KTO   2
152 #define E_BLZ   3
153 #ifndef DEFAULT_EURO
154 #define E_EUR   4
155 #else
156 #define E_DM    4
157 #endif
158 #define E_LEN   5
159
160 /*
161  *  Second: Some low level routines
162  */
163
164 size_t dtaus_nextrec (void **buf, FILE *f)
165 {
166   memset (buf, 0, 128);
167   return fread(buf, 128, 1, f);
168 }
169
170 char *upcase(char *s)
171 {
172   static char x[100];
173   static char *xp;
174   char *cp;
175
176   for (cp=s,xp=x; *cp; cp++,xp++) {
177     if (strchr(" 0123456789.,&-/+*$%ABCDEFGHIJKLMNOPQRSTUVWXYZ",*cp)) {
178         *xp = *cp;
179     } else if (strchr("abcdefghijklmnopqrstuvwxyz",*cp)) {
180       *xp = toupper(*cp);
181     } else if (strchr ("üÜäÄöÖß", *cp)) {
182       switch (*cp) {
183       case 'ü': *(xp++) = 'U'; *xp='E'; break;
184       case 'Ü': *(xp++) = 'U'; *xp='E'; break;
185       case 'ä': *(xp++) = 'A'; *xp='E'; break;
186       case 'Ä': *(xp++) = 'A'; *xp='E'; break;
187       case 'ö': *(xp++) = 'O'; *xp='E'; break;
188       case 'Ö': *(xp++) = 'O'; *xp='E'; break;
189       case 'ß': *(xp++) = 'S'; *xp='S'; break;
190       }
191     } else
192       /*
193        * Filter out all other characters since credit institutes won't
194        * accept the file otherwise.
195        */
196       *xp = ' ';
197   }
198   *xp = '\0';
199
200   return x;
201 }
202
203 char *downcase(char *s)
204 {
205   static char x[100];
206   char *cp;
207
208   memset (x, 0, sizeof (x));
209   strncpy (x, s, 99);
210   
211   for (cp=x;*cp;cp++)
212     if (isupper(*cp))
213       *cp = tolower(*cp);
214   return x;
215 }
216
217 char *strip_spaces (char *s)
218 {
219   int i;
220   char *p;
221
222   for (i=strlen(s);(s[i-1] == ' '||s[i-1] == '\t')&&i>0; i--)
223     s[i-1] = '\0';
224   for (p=s; *p==' '; p++);
225   return p;
226 }
227
228 char *strip_zeros (char *s)
229 {
230   char *p;
231
232   for (p=s; *p=='0'; p++);
233   return p;
234 }
235
236 char *strip_nondigits (char *s)
237 {
238   char *p;
239   char *x;
240
241   for (x=s,p=s;*x;x++)
242     if (isdigit (*x))
243       *(p++) = *x;
244   *(p++) = '\0';
245
246   return s;
247 }
248
249 char dtaus_char (void *buf, unsigned int pos)
250 {
251   static char res;
252   char *bufp = (char *)buf;
253
254   bufp+=pos;
255   memcpy(&res, bufp, 1);
256   return res;
257 }
258
259 char *string2real(char *s)
260 {
261   static char res[20];
262   char *cp = s;
263
264   cp+=strlen(s)-2;
265   strncpy(res, s, strlen(s)-2);
266   res[strlen(s)-2] = '.';
267   res[strlen(s)-1] = s[strlen(s)-2];
268   res[strlen(s)] = s[strlen(s)-1];
269   return res;
270 }
271
272 char *real2string(char *s)
273 {
274   static char res[20];
275   char *cp;
276
277   strncpy(res, s, sizeof(res)-1);
278   res[sizeof(res)-1] = 0;
279   for (cp=res; *cp&&!(*cp == ',')&&!(*cp == '.');cp++);
280
281   if ((cp-res) >= (sizeof(res)-3)) { 
282     /* Bail out, since the number is too large, shouldn't be possible though. */
283     fprintf (stderr, "Value %s too large.\n", res);
284     exit (1);
285   }
286
287   if (*cp == '.' || *cp == ',') {
288     if (*(cp+1)) {
289       /* 1st decimal place */
290       if (isdigit(*(cp+1)))
291         *(cp++) = *(cp+1);
292       else
293         *(cp++) = '0';
294       /* 2nd decimal place */
295       if (*(cp+1) && isdigit(*(cp+1)))
296         *(cp++) = *(cp+1);
297       else
298         *(cp++) = '0';
299     } else {
300       *(cp++) = '0';
301       *(cp++) = '0';
302     }
303   } else {
304     *(cp++) = '0';
305     *(cp++) = '0';
306   }
307   *cp = '\0';
308   return res;
309 }
310
311 /*
312  * Return the last digit of the year as character
313  */
314 char get_date_lastdigit()
315 {
316   time_t timer;
317   struct tm *loctime;
318
319   timer = time ( NULL );
320   loctime = localtime(&timer);
321   return loctime->tm_year % 10 + '0';
322 }
323
324 char *string2trans (char *s)
325 {
326   static char res[30];
327
328   res[0] = '\0';
329   if (!strcmp(s, "04000"))
330     sprintf (res, "Abbuchung");
331   else if (!strcmp(s, "05000"))
332     sprintf (res, "Einzug");
333   else if (!strcmp(s, "05005"))
334     sprintf (res, "E-Cash");
335   else if (!strcmp(s, "05006"))
336     sprintf (res, "E-Cash-A");
337   else if (!strcmp(s, "51000"))
338     sprintf (res, "Gutschrift");
339   else if (!strcmp(s, "53000"))
340     sprintf (res, "Lohn");
341   else if (!strncmp(s, "5400", 4))
342     sprintf (res, "Vermögen %c",s[4]);
343   /*  else if (!strcmp(s, "56000"))
344     sprintf (res, ""); / * Überweisung öffentlicher Kassen */
345   return res;
346 }
347
348 char *trans2string (char *s)
349 {
350   static char res[30];
351   char *cp;
352
353   res[0] = '\0';
354   if (!strcmp(s, "Abbuchung"))
355     sprintf (res, "04000");
356   else if (!strcmp(s, "Einzug"))
357     sprintf (res, "05000");
358   else if (!strcmp(s, "E-Cash"))
359     sprintf (res, "05005");
360   else if (!strcmp(s, "E-Cash-A"))
361     sprintf (res, "05006");
362   else if (!strcmp(s, "Gutschrift"))
363     sprintf (res, "51000");
364   else if (!strcmp(s, "Lohn"))
365     sprintf (res, "53000");
366   else {
367     cp = NULL;
368     if (!strncmp(s, "Vermögen", 8))
369       cp = s+8;
370     if (!strncmp(s, "Vermoegen", 9))
371       cp = s+9;
372
373     if (!cp) {
374       fprintf (stderr, "Unbekannte Transaktion `%s'\n", res);
375       exit (1);
376     }
377
378     /*
379       Vermögen --> 5400<heutiges Jahr>
380       Vermögen 8 -> 5400<8>
381       Vermögen 2003 -> 5400<3>
382      */
383
384     if (*cp) while (!isspace(*cp)) cp++;
385     while (isspace(*cp)) cp++;
386
387     if (!*cp || !isdigit(*cp))
388       sprintf (res, "5400%c",get_date_lastdigit());
389     else {
390       while (isdigit(*cp)) cp++;
391       sprintf (res, "5400%c",*(cp-1));
392     }
393   }
394   /*  else if (!strcmp(s, ""))
395     sprintf (res, "56000"); / * Überweisung öffentlicher Kassen */
396   return res;
397 }
398
399 char *string2ext (char *s)
400 {
401   static char res[30];
402
403   res[0] = '\0';
404   if (!strcmp(s, "01"))
405     sprintf (res, "Kunde");
406   else if (!strcmp(s, "02"))
407     sprintf (res, "Text");
408   else if (!strcmp(s, "03"))
409     sprintf (res, "Auftraggeber");
410   return res;
411 }
412
413 char *ext2string (char *s)
414 {
415   static char res[3];
416
417   res[0] = '\0';
418   if (!strcmp(s, "Kunde"))
419     sprintf (res, "01");
420   else if (!strcmp(s, "Text"))
421     sprintf (res, "02");
422   else if (!strcmp(s, "Auftraggeber"))
423     sprintf (res, "03");
424   return res;
425 }
426     
427 unsigned long int dtaus_int(void *buf, unsigned int pos, unsigned int len)
428 {
429   char tmp[30];
430   char *bufp = (char *)buf;
431   static unsigned long int res;
432
433   bufp+=pos;
434   memcpy(tmp, bufp, len);
435   tmp[len] = '\0';
436   sscanf(tmp, "%lu", &res);
437   return res;
438 }
439
440 /*
441  * returns the first word in this line, returns it and shortens the
442  * line.
443  */
444 char *extract_ident (char *line)
445 {
446   char *c, *x, *y;
447   static char word[30];
448
449   if (strlen(line) > 0) {
450     x = strchr (line, ' ');
451     y = strchr (line, '\t');
452
453     if (!x && !y) {
454         strncpy(word, line, 29);
455         line[0] = '\0';
456         return word;
457     }
458
459     /* Check which index returns the lower value, and check if the
460        value is non-NULL */
461     if ((c = (x && x<y)?x:(y?y:x))) { 
462       strncpy(word, line, c - line);
463       word[c-line] = '\0';
464       for (;*c == '\t' || *c == ' '; c++);
465       for (x=line; *c; c++,x++)
466         *x = *c;
467       *x = '\0';
468       strcpy(word, downcase(word));
469       return word;
470     }
471     return NULL;
472   }
473   return line;
474 }
475
476 /*
477  * Pads a string with zero's on the left side.
478  */
479 char *padzeroclip (char *s, int len)
480 {
481   char *p, *q;
482
483   if (strlen(s) == len) return s;
484   if (strlen(s) > len) {
485       q=s+len;
486       *(q--) = '\0';
487       return s;
488   }
489
490   q=s+len;
491   *(q--) = '\0';
492   for (p=s+strlen(s)-1;p>=s;p--)
493     *(q--)=*p;
494   for (;q>=s;) *(q--)='0';
495   return s;
496 }
497
498 int rec_index(char *ident, int type)
499 {
500   int i;
501   dtaus_record *rec = NULL;
502
503   switch (type) {
504   case REC_A:   rec = recA; break;
505   case REC_C:   rec = recC; break;
506   case REC_E:   rec = recE; break;
507   }
508
509   for (i=0; (rec[i].name); i++) {
510     if (!strcmp(ident, downcase(rec[i].name)))
511       return i;
512   }
513
514   return -1;
515 }
516
517 size_t control_nextline (void **buf, int len, FILE *f)
518 {
519   char line[100];
520   char tmp[100];
521   char *cp;
522   int i;
523
524   memset (line, 0, sizeof(line));
525   memset (buf, 0, len);
526   cp = line;
527
528   while (!strlen(line) && (cp = fgets(line, 100, f))) {
529     if (strlen(line)) {
530       if (line[0] != '#') {
531         if (line[strlen(line)-1] != '\n') { 
532           strcpy(tmp, line);
533           while (tmp[strlen(tmp)-1] != '\n' && (cp = fgets(tmp, 100, f)));
534         } else
535           line[strlen(line)-1] = '\0';
536         if (line[strlen(line)-1] == '\r')
537           line[strlen(line)-1] = '\0';
538         for (i=strlen(line);(line[i-1] == ' '||line[i-1] == '\t')&&i>0; i--)
539           line[i-1] = '\0';
540         if (line[0] == '#')
541           line[0] = '\0';
542       } else
543         line[0] = '\0';
544     }
545   }
546   for (cp=line; *cp==' '; cp++);
547
548   if (strlen(cp)) {
549     memcpy(buf, cp, strlen(cp));
550     return 1;
551   } else
552     return 0;
553 }
554
555 /*
556  * Return the current date nicely formatted
557  */
558 char *get_date()
559 {
560   static char res[10];
561   time_t timer;
562   struct tm *loctime;
563
564   timer = time ( NULL );
565   loctime = localtime(&timer);
566   sprintf(res, "%02d.%02d.%02d", loctime->tm_mday, loctime->tm_mon+1, loctime->tm_year % 100);
567   return res;
568 }
569
570 /*
571  *  Prepare a record A according to the specs.
572  *  See dtaus.txt for explanation
573  */
574 void dtaus_prepareA (char *buf)
575 {
576   int i;
577   time_t timer;
578   struct tm *loctime;
579   char tmp[10];
580
581   memset (buf, 0, 129);
582   timer = time ( NULL );
583   loctime = localtime(&timer);
584
585   buf[0] = '0';
586   buf[1] = '1';
587   buf[2] = '2';
588   buf[3] = '8';
589   buf[4] = 'A';
590   for (i=15;i<15+8; i++) buf[i] = '0';          /* A5 */
591   sprintf(tmp, "%02d%02d%02d", loctime->tm_mday, loctime->tm_mon+1, loctime->tm_year % 100);
592   for (i=0; i<6; i++) buf[50+i] = tmp[i];       /* A7 (Date) */
593   for (i=56;i<56+4; i++) buf[i] = ' ';          /* A8 */
594   for (i=70;i<70+10; i++) buf[i] = '0';         /* A10 */
595   for (i=80;i<80+48; i++) buf[i] = ' ';         /* A11 */
596   if (use_euro)
597     buf[recA[A_CURR].pos] = '1';                                /* A12 (Currency) */
598   else
599     buf[recA[A_CURR].pos] = ' ';                                /* A12 (Currency) */
600 }
601
602 /*
603  *  Prepare a record C according to the specs.
604  *  See dtaus.txt for explanation
605  */
606 void dtaus_prepareC (char *buf, int normaltext, int maxtext)
607 {
608   int i;
609   int appendix = 0;
610
611   memset (buf, 0, 257);
612
613   if (normaltext)
614     appendix = 1;
615   appendix += maxtext;
616   i = 187 + (appendix * 29);
617
618   /* Bail out if the number is too large, shouldn't be possible though */
619   if (i >= 1000)
620     exit (1);
621
622   buf[0] = (i/1000)+48;i-=(i/1000)*100;
623   buf[1] = (i/100)+48;i-=(i/100)*100;
624   buf[2] = (i/10)+48;i-=(i/10)*10;
625   buf[3] = i+48;
626   buf[4] = 'C';
627
628   for (i=31;i<31+13; i++) buf[i] = '0';         /* C6 */
629   buf[49] = ' ';                                /* C8 */
630   for (i=50;i<50+11; i++) buf[i] = '0';         /* C9 (Betrag) */
631   for (i=79;i<79+11; i++) buf[i] = '0';         /* C12 (Betrag Euro) */
632   for (i=90;i<90+3; i++) buf[i] = ' ';          /* C13 */
633   for (i=93;i<90+27; i++) buf[i] = ' ';         /* C14a (Kunde) */
634   for (i=120;i<120+8; i++) buf[i] = ' ';        /* C14b */
635   if (use_euro)
636     buf[recC[C_EURO].pos] = '1';                                /* C17a (Currency) */
637   else
638     buf[recC[C_EURO].pos] = ' ';                                /* C17a (Currency) */
639   for (i=183;i<183+2; i++) buf[i] = ' ';        /* C17b */
640   for (i=187;i<187+(29*2); i++) buf[i] = ' ';   /* C19-C22 (misc text) */
641   for (i=245;i<245+11; i++) buf[i] = ' ';       /* C23 */
642
643   buf[185+0] = (appendix/10)+48;appendix-=(appendix/10)*10;
644   buf[185+1] = appendix+48;
645 }
646
647 /*
648  *  Prepare a record E according to the specs.
649  *  See dtaus.txt for explanation
650  */
651 void dtaus_prepareE (char *buf)
652 {
653   int i;
654
655   memset (buf, 0, 129);
656   buf[0] = '0';
657   buf[1] = '1';
658   buf[2] = '2';
659   buf[3] = '8';
660   buf[4] = 'E';
661   for (i=5;i<5+5; i++) buf[i] = ' ';    /* E3 */
662   for (i=17;i<17+13; i++) buf[i] = '0'; /* E8 (Check Betrag) */
663   for (i=64;i<64+13; i++) buf[i] = '0'; /* E8 (Check Euro) */
664   for (i=77;i<77+51; i++) buf[i] = ' '; /* E9 */
665 }
666
667 int dtaus_writeA(FILE *f, char **values)
668 {
669   char buf[129];
670   char tmp[30];
671   int i;
672   
673   for (i=0; (recA[i].name); i++)
674     if ((recA[i].type == REQ) && !values[i]) {
675       fprintf (stderr, "Anfangsdatensatz ist nicht vollständig, kein %s.\n", recA[i].name);
676       return 0;
677     }
678   if (!(((values[A_TRANS][0] == 'L')||(values[A_TRANS][0] == 'G'))
679         &&((values[A_TRANS][1] == 'B')||(values[A_TRANS][1] == 'K')))) {
680     fprintf (stderr, "Ungültiger Typ, nur LK, GK, LB oder GB erlaubt.\n");
681     return 0;
682   }
683
684   i=A_NAME;if (values[i] && strlen(values[i]) > recA[i].len)
685     values[i][recA[i].len] = '\0';
686
687   dtaus_prepareA(buf);
688   buf[5] = values[A_TRANS][0];
689   buf[6] = values[A_TRANS][1];
690   sprintf (tmp, "%s", padzeroclip (strip_nondigits (values[A_BLZ]),recA[A_BLZ].len));
691   for (i=0; i<recA[A_BLZ].len; i++) buf[recA[A_BLZ].pos+i] = tmp[i];
692   sprintf (tmp, "%-27.27s", upcase(values[A_NAME]));
693   for (i=0; i<27; i++) buf[recA[A_NAME].pos+i] = tmp[i];
694   if (values[A_TODO]) {
695     sprintf (tmp, "%s", padzeroclip (strip_nondigits (values[A_TODO]),recA[A_TODO].len));
696     for (i=0; i<recA[A_TODO].len; i++) buf[recA[A_TODO].pos+i] = tmp[i];
697   }
698   sprintf (tmp, "%s", padzeroclip (strip_nondigits (values[A_KTO]),recA[A_KTO].len));
699   for (i=0; i<recA[A_KTO].len; i++) buf[recA[A_KTO].pos+i] = tmp[i];
700
701   fputs(buf, f);
702   return 1;
703 }
704
705 int dtaus_writeC(FILE *f, char **valuesA, char **values, char **text)
706 {
707   char buf[257];
708   char appendix[129];
709   char tmp[30];
710   int i, k;
711   int maxtext = 0;
712   int fieldnr;
713
714   /* Just count */
715   if (text) for (maxtext=0;text[maxtext];maxtext++);
716
717 #if DEBUG
718   for (i=0; (recC[i].name); i++)
719     if (values[i])
720       printf ("%s: %s\n", recC[i].name, values[i]);
721 #endif
722
723   for (i=0; (recC[i].name); i++)
724     if ((recC[i].type == REQ) && !values[i]) {
725       fprintf (stderr, "Datensatz ist nicht vollständig, kein %s.\n", recC[i].name);
726       return 0;
727     }
728   sprintf (tmp, "%s", trans2string(values[C_TRANS]));
729   if (!strlen(tmp)) {
730     fprintf (stderr, "Ungültige Transaktion, nur Abbuchung, Einzug, E-Cash, E-Cash-A, Gutschrift und Lohn erlaubt.\n");
731     return 0;
732   }
733
734   i=C_TEXT;if (values[i] && strlen(values[i]) > recC[i].len)
735     values[i][recC[i].len-2] = '\0';
736   i=C_ZWECK;if (values[i] && strlen(values[i]) > recC[i].len)
737     values[i][recC[i].len] = '\0';
738   i=C_MYNAM;if (values[i] && strlen(values[i]) > recC[i].len)
739     values[i][recC[i].len] = '\0';
740   i=C_TEXT;if (values[i] && strlen(values[i]) > recC[i].len)
741     values[i][recC[i].len] = '\0';
742
743   dtaus_prepareC (buf, values[C_TEXT] != NULL, maxtext);
744   for (i=0; i<5; i++) buf[recC[C_TRANS].pos+i] = tmp[i];
745   if (values[C_MYBLZ])
746     sprintf (tmp, "%s", padzeroclip (strip_nondigits (values[C_MYBLZ]),8));
747   else
748     sprintf (tmp, "%s", padzeroclip (strip_nondigits (valuesA[A_BLZ]),8));
749   for (i=0; i<recC[C_MYBLZ].len; i++) buf[5+i] = tmp[i];
750   for (i=0; i<recC[C_MYBLZ].len; i++) buf[recC[C_MYBLZ].pos+i] = tmp[i];
751   sprintf (tmp, "%s", padzeroclip (strip_nondigits (values[C_BLZ]),8));
752   for (i=0; i<recC[C_BLZ].len; i++) buf[recC[C_BLZ].pos+i] = tmp[i];
753   sprintf (tmp, "%s", padzeroclip (strip_nondigits (values[C_KTO]),10));
754   for (i=0; i<recC[C_KTO].len; i++) buf[recC[C_KTO].pos+i] = tmp[i];
755   sprintf (tmp, "%s", padzeroclip (real2string(values[C_VAL]),11));
756 #ifndef DEFAULT_EURO
757   if (!use_euro)
758     for (i=0; i<recC[C_VAL].len; i++) buf[recC[C_VAL].pos+i] = tmp[i];
759   else
760     for (i=0; i<recC[C_EUR].len; i++) buf[recC[C_EUR].pos+i] = tmp[i];
761 #else
762   if (use_euro)
763     for (i=0; i<recC[C_VAL].len; i++) buf[recC[C_VAL].pos+i] = tmp[i];
764   else
765     for (i=0; i<recC[C_DM].len; i++) buf[recC[C_DM].pos+i] = tmp[i];
766 #endif
767   if (values[C_MYKTO])
768     sprintf (tmp, "%s", padzeroclip (strip_nondigits (values[C_MYKTO]),10));
769   else
770     sprintf (tmp, "%s", padzeroclip (strip_nondigits (valuesA[A_KTO]),10));
771   for (i=0; i<recC[C_MYKTO].len; i++) buf[recC[C_MYKTO].pos+i] = tmp[i];
772   sprintf (tmp, "%-27.27s", upcase(values[C_NAME]));
773   for (i=0; i<recC[C_NAME].len; i++) buf[recC[C_NAME].pos+i] = tmp[i];
774   if (values[C_MYNAM])
775     sprintf (tmp, "%-27.27s", upcase(values[C_MYNAM]));
776   else
777     sprintf (tmp, "%-27.27s", upcase(valuesA[A_NAME]));
778   for (i=0; i<recC[C_MYNAM].len; i++) buf[recC[C_MYNAM].pos+i] = tmp[i];
779   sprintf (tmp, "%-27.27s", upcase(values[C_ZWECK]));
780   for (i=0; i<recC[C_ZWECK].len; i++) buf[recC[C_ZWECK].pos+i] = tmp[i];
781
782   if (values[C_TEXT]) {
783     buf[recC[C_TEXT].pos+0] = '0';
784     buf[recC[C_TEXT].pos+1] = '2';
785     sprintf (tmp, "%-27.27s", upcase(values[C_TEXT]));
786     for (i=0; i<recC[C_TEXT].len-2; i++) buf[recC[C_TEXT].pos+2+i] = tmp[i];
787   }
788
789   if (text) {
790     buf[recC[C_EXT].pos+0] = '0';
791     buf[recC[C_EXT].pos+1] = '2';
792     sprintf (tmp, "%-27.27s", upcase(text[0]));
793     for (i=0; i<recC[C_EXT].len-2; i++) buf[recC[C_EXT].pos+2+i] = tmp[i];
794   }
795
796   fputs(buf, f);
797
798   if (text && maxtext > 1) {
799     fieldnr=1;
800     while (fieldnr<maxtext) {
801       memset (appendix, ' ', 128);
802       appendix[128] = '\0';
803       for (k=0; k<4 && (fieldnr+k)<maxtext; k++) {
804         sprintf (tmp, "%-27.27s", upcase(text[fieldnr+k]));
805         appendix[k*29] = '0';
806         appendix[(k*29)+1] = '2';
807         for (i=0; i<recC[C_TEXT].len-2; i++) appendix[(k*29)+2+i] = tmp[i];
808       }
809       fputs(appendix, f);
810       fieldnr += k;
811     }
812   }
813
814   return 1;
815 }
816
817 int dtaus_writeE(FILE *f, int count, bigint sum, bigint blz, bigint kto)
818 {
819   char buf[129];
820   char tmp[30];
821   int i;
822   
823   dtaus_prepareE(buf);
824
825   sprintf (tmp, "%07d", count);
826   for (i=0; i<recE[E_COUNT].len; i++) buf[recE[E_COUNT].pos+i] = tmp[i];
827   bigint_sprintf (tmp, "%s", sum);
828   padzeroclip (tmp,13);
829 #ifndef DEFAULT_EURO
830   if (!use_euro)
831     for (i=0; i<recE[E_VAL].len; i++) buf[recE[E_VAL].pos+i] = tmp[i];
832   else
833     for (i=0; i<recE[E_EUR].len; i++) buf[recE[E_EUR].pos+i] = tmp[i];
834 #else
835   if (use_euro)
836     for (i=0; i<recE[E_VAL].len; i++) buf[recE[E_VAL].pos+i] = tmp[i];
837   else
838     for (i=0; i<recE[E_DM].len; i++) buf[recE[E_DM].pos+i] = tmp[i];
839 #endif
840   bigint_sprintf (tmp, "%s", kto);
841   padzeroclip (tmp,17);
842   for (i=0; i<recE[E_KTO].len; i++) buf[recE[E_KTO].pos+i] = tmp[i];
843   bigint_sprintf (tmp, "%s", blz);
844   padzeroclip (tmp,17);
845   for (i=0; i<recE[E_BLZ].len; i++) buf[recE[E_BLZ].pos+i] = tmp[i];
846
847   fputs(buf, f);
848   return 1;
849 }
850
851 void printctln(FILE *f, char *field, char *value)
852 {
853   if (strlen(field) && strlen(value))
854     fprintf(f, "  %s\t%s\n", field, value);
855 }
856
857 /*
858  * one date line, format it properly
859  */
860 void printctlndate(FILE *f, char *field, char *value)
861 {
862   char mydate[11];
863   int i;
864
865   if (!strlen(field) || !strlen(value))
866     return;
867
868   for (i=0;isspace (value[i]) && i<= strlen (value); i++);
869   if (i == strlen (value))
870     return;
871
872   memset (mydate, 0, sizeof (mydate));
873   if (strlen (value) == 6) {
874     mydate[0] = value[0];
875     mydate[1] = value[1];
876     mydate[2] = '.';
877     mydate[3] = value[2];
878     mydate[4] = value[3];
879     mydate[5] = '.';
880     mydate[6] = value[4];
881     mydate[7] = value[5];
882     fprintf(f, "  %s\t%s\n", field, mydate);
883   } else if (strlen (value) == 8) {
884     mydate[0] = value[0];
885     mydate[1] = value[1];
886     mydate[2] = '.';
887     mydate[3] = value[2];
888     mydate[4] = value[3];
889     mydate[5] = '.';
890     mydate[6] = value[4];
891     mydate[7] = value[5];
892     mydate[8] = value[6];
893     mydate[9] = value[7];
894     fprintf(f, "  %s\t%s\n", field, mydate);
895   } else {
896     fprintf (stderr, "Broken date field: %s\n", value);
897     fprintf(f, "  %s\t%s\n", field, value);
898   }
899 }
900
901
902 /*
903  *  Third: Some high level routines
904  */
905
906 void dtaus2control (char *cdtaus, char *ccontrol)
907 {
908   FILE *fdtaus, *fcontrol;
909   char *buf;
910   char *bufp;
911   char tmp[30];
912   char x[30];
913   int index;
914   int extC;
915   div_t res;
916
917   if (!cdtaus) {
918     if (!(fdtaus = fopen("DTAUS0.TXT", "r")))
919       if (!(fdtaus = fopen("dtaus0.txt", "r")))
920         return;
921   } else {
922     if (!strcmp (cdtaus, "-"))
923       fdtaus = stdin;
924     else
925       if (!(fdtaus = fopen(cdtaus, "r")))
926         return;
927   }
928   if (!ccontrol) 
929     fcontrol = stdout;
930   else
931     if (!strcmp (ccontrol, "-"))
932       fcontrol = stdout;
933     else
934       if (!(fcontrol = fopen(ccontrol, "w")))
935         return;
936   if (!(buf = (char *)malloc (512)))
937     return;
938
939   /* 
940    * Record A lesen
941    */
942   if (dtaus_nextrec((void *)buf, fdtaus) == 1) {
943     if (dtaus_char(buf,4) == 'A') {
944       fprintf(fcontrol, "BEGIN {\n");
945       bufp = buf;
946
947       for (index=A_TRANS; index < A_LOOP; index++) {
948         bufp = buf + recA[index].pos;
949         memcpy(tmp, bufp, recA[index].len); tmp[recA[index].len] = '\0';
950         if (index == A_DATE || index == A_TODO)
951           printctlndate(fcontrol, recA[index].name, tmp);
952         else
953           printctln(fcontrol, recA[index].name, strip_zeros(strip_spaces(tmp)));
954       }
955
956       bufp = buf + recA[A_CURR].pos;
957       if (*bufp == '1') {
958         use_euro = 1;
959         fprintf(fcontrol, "  Euro\n");
960       } else {
961         use_euro = 0;
962         fprintf(fcontrol, "  DM\n");
963       }
964
965       fprintf(fcontrol, "}\n\n");
966     } else {
967       fprintf (stderr, "Datei fängt nicht mit dem Anfangsdatensatz an.\n");
968       return;
969     }
970   } else {
971     fprintf (stderr, "Der Anfangsdatensatz ist kaputt.\n");
972     return;
973   }
974
975   /*
976    * Record C lesen
977    */
978   if (dtaus_nextrec((void *)buf, fdtaus) == 1) {
979     while (dtaus_char(buf,4) == 'C') {
980       bufp = buf + 128;
981       if (dtaus_nextrec((void *)bufp, fdtaus) != 1) {
982         fprintf (stderr, "Der zweite Teil der Transaktion ist kaputt.\n");
983         return;
984       } else {
985         fprintf(fcontrol, "{\n");
986
987         for (index=C_NAME; index < C_LOOP; index++) {
988 #ifndef DEFAULT_EURO
989           if (use_euro && index == C_VAL)
990             index = C_EUR;
991 #else
992           if (!use_euro && index == C_VAL)
993             index = C_DM;
994 #endif
995           bufp = buf + recC[index].pos;
996           memcpy(tmp, bufp, recC[index].len); tmp[recC[index].len] = '\0';
997
998           /*
999            * C_EUR and C_DM are outside of the loop, can only be
1000            * selected for the non-default currency, but the value
1001            * should be stored in the normal record field.
1002            */
1003 #ifndef DEFAULT_EURO
1004           if (index == C_EUR)
1005 #else
1006           if (index == C_DM)
1007 #endif
1008             printctln(fcontrol, recC[C_VAL].name, strip_zeros(string2real(tmp)));
1009           else if (index == C_VAL)
1010             printctln(fcontrol, recC[index].name, strip_zeros(string2real(tmp)));
1011           else if (index == C_TRANS)
1012             printctln(fcontrol, recC[index].name, strip_zeros(string2trans(tmp)));
1013           else
1014             printctln(fcontrol, recC[index].name, strip_zeros(strip_spaces(tmp)));
1015 #ifndef DEFAULT_EURO
1016           if (use_euro && index == C_EUR)
1017             index = C_VAL;
1018 #else
1019           if (!use_euro && index == C_DM)
1020             index = C_VAL;
1021 #endif
1022         }
1023
1024         for (index=C_TEXT; index <= C_EXT; index++) {
1025           if (!(dtaus_char(buf,recC[index].pos) == ' ')) {
1026             bufp = buf + recC[index].pos;
1027             memcpy(x, bufp, 2); tmp[2] = '\0'; bufp+=2;
1028             memcpy(tmp, bufp, recC[index].len-2); tmp[recC[index].len-2] = '\0';
1029             printctln(fcontrol, string2ext(x), strip_spaces(tmp));
1030           }
1031         }
1032
1033         /* Number of extension records for this C record */
1034         extC = dtaus_int(buf, 185, 2);
1035         extC -= 2;
1036         if (extC > 0) {
1037           res = div (extC, 4);
1038           extC = res.quot;
1039           if (res.rem) extC++;
1040         }
1041       }
1042       if (dtaus_nextrec((void *)buf, fdtaus) != 1)
1043         memset (buf, 0, sizeof(buf));
1044
1045       /*
1046        * Are there extension records that we have to check?
1047        *
1048        * 2nd half of the AND is wrong, but since dtaus < 0.5 wrote 01
1049        *     instead of 00 we should let it in so it can read its own
1050        *     old files...  *sigh*
1051        */
1052       while (extC > 0 && dtaus_char(buf,4) != 'C' && dtaus_char(buf,4) != 'E') {
1053         for (index=0; index < 4; index++) {
1054           if ((dtaus_char(buf,index*29) != ' ')) {
1055             bufp = buf + index*29;
1056             memcpy(x, bufp, 2); tmp[2] = '\0'; bufp+=2;
1057             memcpy(tmp, bufp, recC[C_TEXT].len-2); tmp[recC[C_TEXT].len-2] = '\0';
1058             printctln(fcontrol, string2ext(x), strip_spaces(tmp));
1059           }
1060         }
1061         if (dtaus_nextrec((void *)buf, fdtaus) != 1)
1062           memset (buf, 0, sizeof(buf));
1063         extC--;
1064       }
1065       fprintf(fcontrol, "}\n");
1066     }
1067   }
1068
1069   /*
1070    * Record E lesen
1071    *   (gelesen ist er eigentlich schon...)
1072    */
1073   if (dtaus_char(buf,4) == 'E') {
1074     if (dtaus_char(buf,4) == 'E') {
1075       fprintf(fcontrol, "\nEND {\n");
1076
1077       for (index=E_COUNT; index <= E_BLZ; index++) {
1078 #ifndef DEFAULT_EURO
1079         if (use_euro && index == E_VAL)
1080           index = E_EUR;
1081 #else
1082         if (!use_euro && index == E_VAL)
1083           index = E_DM;
1084 #endif
1085
1086         bufp = buf + recE[index].pos;
1087         memcpy(tmp, bufp, recE[index].len); tmp[recE[index].len] = '\0';
1088
1089 #ifndef DEFAULT_EURO
1090         if (index == E_VAL || index == E_EUR)
1091 #else
1092         if (index == E_VAL || index == E_DM)
1093 #endif
1094           printctln(fcontrol, recE[E_VAL].name, strip_zeros(string2real(tmp)));
1095         else
1096           printctln(fcontrol, recE[index].name, strip_zeros(tmp));
1097 #ifndef DEFAULT_EURO
1098           if (use_euro && index == E_EUR)
1099             index = E_VAL;
1100 #else
1101           if (!use_euro && index == E_DM)
1102             index = E_VAL;
1103 #endif
1104       }
1105
1106       fprintf(fcontrol, "}\n");
1107     } else {
1108       fprintf (stderr, "Das ist kein Abschlußdatensatz.\n");
1109       return;
1110     }
1111   } else {
1112     fprintf (stderr, "Der Abschlußdatensatz ist leer oder kaputt.\n");
1113     return;
1114   }
1115   fclose(fcontrol);
1116   fclose(fdtaus);
1117 }
1118
1119 int control2dtaus (char *ccontrol, char *cdtaus, char *cbeleg, char *ccheck)
1120 {
1121   FILE *fdtaus, *fcontrol, *fbeleg, *fcheck;
1122   void *buf;
1123   char *ident;
1124   int  recindex;
1125   char tmp[30];
1126   char line[100];
1127   char *valA[A_LEN], *valC[C_LEN];
1128   int count;
1129   bigint sum_val, sum_blz, sum_kto, bi;
1130   char **text = NULL;
1131   char *cp;
1132   int textindex = 0;
1133   int len, i;
1134
1135   if (!cdtaus) {
1136     if (!(fdtaus = fopen("dtaus0.txt", "w")))
1137       return 0;
1138   } else {
1139     if (!strcmp (cdtaus, "-"))
1140       fdtaus = stdout;
1141     else
1142       if (!(fdtaus = fopen(cdtaus, "w")))
1143         return 0;
1144   }
1145   if (!ccontrol) {
1146     if (!(fcontrol = fopen("dtaus0.ctl", "r")))
1147       if (!(fcontrol = fopen("DTAUS0.CTL", "r")))
1148         return 0;
1149   } else {
1150     if (!strcmp (ccontrol, "-"))
1151       fcontrol = stdin;
1152     else
1153       if (!(fcontrol = fopen(ccontrol, "r")))
1154         return 0;
1155   }
1156   if (!cbeleg) {
1157     if (!(fbeleg = fopen("dtaus0.doc", "w")))
1158       return 0;
1159   } else {
1160     if (!(fbeleg = fopen(cbeleg, "w")))
1161       return 0;
1162   }
1163   if (!ccheck)
1164     fcheck = stdout;
1165   else
1166     if (!(fcheck = fopen(ccheck, "w")))
1167       return 0;
1168
1169
1170   if (!(buf = (char *)malloc (512)))
1171     return 0;
1172
1173   /* 
1174    * Record A lesen
1175    */
1176   memset (valA, 0, sizeof(valA));
1177   control_nextline ((void *)line, 100, fcontrol);
1178   ident = extract_ident(line);
1179   if (!strcmp(ident, "begin") && (line[0] == '{')) {
1180     control_nextline ((void *)line, 100, fcontrol);
1181     while (strlen(line) && line[0] != '}') {
1182       ident = extract_ident(line);
1183       if ((recindex = rec_index(ident, REC_A)) != -1)
1184         {
1185         if (recA[recindex].type != IGN)
1186           if ((valA[recindex] = (char *)malloc (strlen(line)+1)))
1187             strcpy(valA[recindex], line);
1188         } else {
1189           if (! strcasecmp (ident, "euro"))
1190             use_euro = 1;
1191           if (! strcasecmp (ident, "dm"))
1192             use_euro = 0;
1193         }
1194       control_nextline ((void *)line, 100, fcontrol);
1195     }
1196     if (((recindex = rec_index("art", REC_A)) != -1) && valA[recindex] && strlen(valA[recindex])) {
1197       fprintf(fbeleg, "\n\n");
1198       fprintf(fbeleg, "\n    Begleitzettel\n\n");
1199       fprintf(fbeleg, "\n    Belegloser Datentraegeraustausch\n\n");
1200       if (valA[recindex][0] == 'L')
1201         fprintf(fbeleg, "\n    Sammeleinziehungsauftrag\n\n");
1202       else if (valA[recindex][0] == 'G')
1203         fprintf(fbeleg, "\n    Sammelueberweisungsauftrag\n\n");
1204       else
1205         fprintf(fbeleg, "\n    Sammelauftrag\n\n");
1206       fprintf(fbeleg, "\n    VOL ........................:\n");
1207       fprintf(fbeleg, "\n    Erstellungsdatum ...........: %s\n", get_date());
1208       if (valA[A_TODO]) {
1209         fprintf(fbeleg, "\n    Ausfuehrugsdatum ...........: %s\n", valA[A_TODO]);
1210       }
1211       if (use_euro)
1212         fprintf(fbeleg, "\n    Waehrung ...................: Euro\n");
1213       else
1214         fprintf(fbeleg, "\n    Waehrung ...................: DM\n");
1215     }
1216     if (!dtaus_writeA(fdtaus, valA)) {
1217       fprintf (stderr, "Konnte den Anfangsdatensatz nicht schreiben.\n");
1218       return 0;
1219     }
1220
1221     fprintf (fcheck, "\n\n\n");
1222     if (valA[recindex][0] == 'L')
1223       fprintf (fcheck, "    Sammeleinziehungsauftrag\n\n");
1224     else if (valA[recindex][0] == 'G')
1225       fprintf (fcheck, "    Sammelueberweisungsauftrag\n\n");
1226     else
1227       fprintf (fcheck, "    Sammelauftrag\n\n");
1228     fprintf (fcheck, "    Erstellungsdatum : %s\n\n", get_date());
1229     if (use_euro)
1230       fprintf (fcheck, "    Waehrung         : Euro\n\n\n");
1231     else
1232       fprintf (fcheck, "    Waehrung         : DM\n\n\n");
1233     fprintf (fcheck, "     %-10s  %-8s  %-30s   %12s\n", "Kontonr.", "BLZ", "Name", "Betrag");
1234     fprintf (fcheck, "    --------------------------------------------------------------------\n");
1235   } else {
1236     fprintf (stderr, "Datei fängt nicht mit dem Anfangsdatensatz (BEGIN) an.\n");
1237     return 0;
1238   }
1239
1240   /* 
1241    * Record C lesen
1242    */
1243   count = 0;
1244   sum_val = bigint_int(0);
1245   sum_blz = bigint_int(0);
1246   sum_kto = bigint_int(0);
1247   memset (valC, 0, sizeof(valC));
1248   control_nextline ((void *)line, 100, fcontrol);
1249   if (line[0] == '{') {
1250     while (strlen(line) && line[0] == '{') {
1251       control_nextline ((void *)line, 100, fcontrol);
1252       if (text) {
1253         for (textindex=0; textindex < MAX_TEXT && text[textindex]; textindex++)
1254           free (text[textindex]);
1255         free (text);
1256         text = NULL;
1257       }
1258       while (strlen(line) && line[0] != '}') {
1259         ident = extract_ident(line);
1260         if ((recindex = rec_index(ident, REC_C)) != -1)
1261           if (recC[recindex].type != IGN) {
1262             /*
1263              * Special exception to support multiple Text fields
1264              */
1265             if (recindex == C_TEXT && valC[recindex]) {
1266               if (!text) {
1267                 if ((text = (char **)malloc ((MAX_TEXT+1) * sizeof (char *))) == NULL)
1268                   return 0;
1269                 else {
1270                   textindex = 0;
1271                   memset (text, 0, (MAX_TEXT+1) * sizeof (char *));
1272                 }
1273               }
1274               if (textindex < MAX_TEXT) {
1275                 if ((cp = (char *)malloc (strlen (line) + 1))) {
1276                   strcpy (cp, line);
1277                   cp[strlen (line)] = '\0';
1278                   text[textindex++] = cp;
1279                 }
1280               }
1281             } else {
1282               len = strlen(line);
1283               if (recindex == C_VAL) {
1284                 /* Convert commast to dots for later processing */
1285                 for (i=0; line[i]; i++) if (line[i] == ',') line[i] = '.';
1286
1287                 if ((cp = index (line, '.')) == NULL) {
1288                   if (!(valC[recindex] = (char *)malloc (strlen(line)+4)))
1289                     return 0;
1290                   sprintf (valC[recindex], "%s.00", line);
1291                 } else if ( ((len = cp - line + 3)) < strlen (line)) {
1292                   if (!(valC[recindex] = (char *)malloc (len+1)))
1293                     return 0;
1294                   strncpy (valC[recindex], line, len);
1295                   valC[recindex][len] = '\0';
1296                 } else {
1297                   if (!(valC[recindex] = (char *)malloc (strlen(line)+1)))
1298                     return 0;
1299                   strcpy(valC[recindex], line);
1300                 }
1301               } else {
1302                 if ((valC[recindex] = (char *)malloc (strlen(line)+1)))
1303                   strcpy(valC[recindex], line);
1304                 else
1305                   return 0;
1306               }
1307             }
1308           }
1309         control_nextline ((void *)line, 100, fcontrol);
1310       }
1311       if (!dtaus_writeC(fdtaus, valA, valC, text)) {
1312         fprintf (stderr, "Konnte den regulären Datensatz nicht schreiben.\n");
1313         return 0;
1314       }
1315       count++;
1316       bi = bigint_string(real2string(valC[C_VAL])); sum_val = bigint_add(sum_val, bi);
1317       bi = bigint_string(valC[C_BLZ]); sum_blz = bigint_add(sum_blz, bi);
1318       bi = bigint_string(valC[C_KTO]); sum_kto = bigint_add(sum_kto, bi);
1319
1320       fprintf (fcheck, "     %10s  %8s  %-30s   %12s\n", valC[C_KTO], valC[C_BLZ], valC[C_NAME], valC[C_VAL]);
1321       for (recindex=0; recindex<C_LEN; recindex++)
1322         if (valC[recindex])
1323           free(valC[recindex]);
1324       memset (valC, 0, sizeof(valC));
1325       control_nextline ((void *)line, 100, fcontrol);
1326     }
1327   } else {
1328     fprintf (stderr, "Kein regulärer Datensatz?\n");
1329     return 0;
1330   }
1331
1332   /* 
1333    * Record E lesen
1334    */
1335   dtaus_writeE(fdtaus, count, sum_val, sum_blz, sum_kto);
1336   fprintf (fcheck, "    --------------------------------------------------------------------\n");
1337   bigint_sprintf (tmp, "%s", sum_val);
1338   fprintf (fbeleg, "\n    Anzahl .....................: %d\n", count);
1339   recindex=strlen(tmp);
1340   tmp[recindex+1] = '\0';
1341   tmp[recindex] = tmp[recindex-1];
1342   tmp[recindex-1] = tmp[recindex-2];
1343   tmp[recindex-2] = '.';
1344   fprintf (fcheck, "     %-52s %14s\n", "Summe", tmp);
1345   fprintf (fbeleg, "\n    Summe ......................: %s\n", tmp);
1346   bigint_sprintf (tmp, "%s", sum_kto);
1347   fprintf (fbeleg, "\n    Kontrollsumme Kontonummern .: %s\n", tmp);
1348   bigint_sprintf (tmp, "%s", sum_blz);
1349   fprintf (fbeleg, "\n    Kontrollsumme Bankleitzahlen: %s\n", tmp);
1350   fprintf (fbeleg, "\n    Unsere Kontonummer .........: %s\n", valA[A_KTO]);
1351   fprintf (fbeleg, "\n    Unsere Bankleitzahl ........: %s\n", valA[A_BLZ]);
1352   fprintf (fbeleg, "\n\n\n\n\n    __________________________________________________\n");
1353   fprintf (fbeleg, "    Ort, Datum                     Unterschrift\n");
1354   for (recindex=0; recindex<A_LEN; recindex++)
1355     if (valA[recindex])
1356       free(valA[recindex]);
1357   fclose(fdtaus);
1358   fclose(fcontrol);
1359   fclose(fbeleg);
1360   if (ccheck)
1361     fclose(fcheck);
1362   return count;
1363 }