| 1 | /*----------------------------------------------------------- |
|---|
| 2 | fxl0.c - regrava arquivo em registros de tamanho fixo |
|---|
| 3 | |
|---|
| 4 | uso: fxl0 <leng> <filin> <filout> [opc1] |
|---|
| 5 | |
|---|
| 6 | <leng>: numero de bytes de dados que os registros |
|---|
| 7 | terao em <filout> |
|---|
| 8 | |
|---|
| 9 | <filin>: arquivo de entrada |
|---|
| 10 | |
|---|
| 11 | <filout>: arquivo de saida contendo os registros li- |
|---|
| 12 | dos mas preenchidos com brancos 'a direita |
|---|
| 13 | de forma que todos os registros tenham uma |
|---|
| 14 | largura de <leng> bytes, alem do LF |
|---|
| 15 | |
|---|
| 16 | <opc1>: para gravar <leng> - numero de brancos finais |
|---|
| 17 | em cada registro de saida |
|---|
| 18 | |
|---|
| 19 | Autor: AOT/LK |
|---|
| 20 | FCC, 02/04/87 |
|---|
| 21 | |
|---|
| 22 | Alter: AOT, 21/03/89 |
|---|
| 23 | 1. parametro <opc1> |
|---|
| 24 | 2. writbsiz |
|---|
| 25 | |
|---|
| 26 | ----------------------------------------------------------- */ |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | #include <stdio.h> |
|---|
| 30 | #include <string.h> |
|---|
| 31 | |
|---|
| 32 | #include "cisis.h" /* CISIS Interface header file */ |
|---|
| 33 | #include "cirun.h" /* CISIS Interface runtime declarations */ |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | #define LF 012 /* line feed */ |
|---|
| 37 | #define SP 040 /* space */ |
|---|
| 38 | |
|---|
| 39 | #define NULX 0xFF |
|---|
| 40 | |
|---|
| 41 | #define MODOP 0644 /* dono: LG, grupo: L, outros: L */ |
|---|
| 42 | |
|---|
| 43 | #define MBUFSIZ 8192 |
|---|
| 44 | |
|---|
| 45 | unsigned char ibuf[MBUFSIZ+1]; |
|---|
| 46 | unsigned char *iptr; |
|---|
| 47 | |
|---|
| 48 | unsigned char bufout[MBUFSIZ]; /* writbsiz*/ |
|---|
| 49 | unsigned char obuf1[MBUFSIZ]; |
|---|
| 50 | unsigned char *obuf = obuf1; |
|---|
| 51 | int obufsize; |
|---|
| 52 | int f2=0,n2free,i2free=0; |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | main(argc,argv) |
|---|
| 56 | |
|---|
| 57 | int argc; |
|---|
| 58 | char *argv[]; |
|---|
| 59 | |
|---|
| 60 | { |
|---|
| 61 | int f1; |
|---|
| 62 | int j; /* indice em obuf */ |
|---|
| 63 | int n1; |
|---|
| 64 | |
|---|
| 65 | int parmleng; |
|---|
| 66 | |
|---|
| 67 | int i,n,parmopc1,parmopc2; |
|---|
| 68 | unsigned char *p; |
|---|
| 69 | |
|---|
| 70 | LONGX count; |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | if (argc < 4) { |
|---|
| 74 | printf("%s",cicopyr("Utility AOT/FXL0")); |
|---|
| 75 | printf("\n"); |
|---|
| 76 | printf("fxl0 <leng> <filin> <filout> [opc1] \n"); |
|---|
| 77 | printf("\n"); |
|---|
| 78 | exit(1); |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | if(sscanf(argv[1],"%d",&parmleng) != 1) |
|---|
| 82 | erro("parametro <leng> invalido: ",argv[1]); |
|---|
| 83 | |
|---|
| 84 | if ((f1=open(argv[2],O_RDONLY)) == -1) |
|---|
| 85 | erro("impossivel abrir o arquivo ",argv[2]); |
|---|
| 86 | |
|---|
| 87 | if ((f2=creat(argv[3],MODOP)) == -1) |
|---|
| 88 | erro("impossivel criar o arquivo ",argv[3]); |
|---|
| 89 | |
|---|
| 90 | parmopc1=parmopc2=0; |
|---|
| 91 | |
|---|
| 92 | for (i=4; i < argc; i++) { |
|---|
| 93 | if (!strcmp(argv[i],"opc1")) { |
|---|
| 94 | parmopc1=1; |
|---|
| 95 | printf("+++opc1 on\n"); |
|---|
| 96 | continue; |
|---|
| 97 | } |
|---|
| 98 | if (!strcmp(argv[i],"opc2")) { |
|---|
| 99 | parmopc2=1; |
|---|
| 100 | printf("+++opc2 on\n"); |
|---|
| 101 | continue; |
|---|
| 102 | } |
|---|
| 103 | erro("parametro invalido: ",argv[i]); |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | n1=read(f1,ibuf,MBUFSIZ); |
|---|
| 107 | ibuf[n1]=NULX; |
|---|
| 108 | iptr=ibuf; |
|---|
| 109 | j=0; |
|---|
| 110 | count=0; |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | /* |
|---|
| 114 | inicializa writbsiz |
|---|
| 115 | */ |
|---|
| 116 | i2free=0; n2free=MBUFSIZ; |
|---|
| 117 | obufsize=parmleng+1; /* obufsize = entrada + SP's + LF */ |
|---|
| 118 | |
|---|
| 119 | if (parmopc1) |
|---|
| 120 | obufsize+=(1+4); |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | /* |
|---|
| 124 | loop principal |
|---|
| 125 | */ |
|---|
| 126 | while (n1 > 0) { /* enquanto nao for fim de arquivo */ |
|---|
| 127 | |
|---|
| 128 | while (*iptr != NULX && *iptr != LF && j < parmleng) |
|---|
| 129 | obuf[j++] = *iptr++; |
|---|
| 130 | |
|---|
| 131 | if (*iptr == NULX) { /* rele buffer e continua o "move" */ |
|---|
| 132 | n1=read(f1,ibuf,MBUFSIZ); |
|---|
| 133 | ibuf[n1]=NULX; |
|---|
| 134 | iptr=ibuf; |
|---|
| 135 | } |
|---|
| 136 | else { |
|---|
| 137 | if (parmopc2) |
|---|
| 138 | ; |
|---|
| 139 | else |
|---|
| 140 | while (n1 > 0 && *iptr != LF) { |
|---|
| 141 | while (*iptr != NULX && *iptr != LF) |
|---|
| 142 | iptr++; |
|---|
| 143 | if (*iptr == NULX) { |
|---|
| 144 | n1=read(f1,ibuf,MBUFSIZ); |
|---|
| 145 | ibuf[n1]=NULX; |
|---|
| 146 | iptr=ibuf; |
|---|
| 147 | } |
|---|
| 148 | } |
|---|
| 149 | if (*iptr == LF) { |
|---|
| 150 | iptr++; |
|---|
| 151 | while (j < parmleng) |
|---|
| 152 | obuf[j++]=SP; |
|---|
| 153 | if (parmopc1) { |
|---|
| 154 | for (p=obuf+parmleng, n=parmleng; n; n--) |
|---|
| 155 | if (*--p != SP) |
|---|
| 156 | break; |
|---|
| 157 | sprintf(obuf+parmleng,"|%04d",n); |
|---|
| 158 | } |
|---|
| 159 | obuf[obufsize-1]=LF; |
|---|
| 160 | writbsiz(); |
|---|
| 161 | count++; |
|---|
| 162 | if (!(count % 1000)) |
|---|
| 163 | fprintf(stderr, |
|---|
| 164 | "+++ %"_LD_" (%d bytes)\n",count,parmleng); |
|---|
| 165 | j=0; |
|---|
| 166 | } |
|---|
| 167 | } |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | if (j != 0) { |
|---|
| 171 | fprintf(stderr,"count:%"_LD_"\n",count); |
|---|
| 172 | erro("ultimo registro nao tem LF em ",argv[2]); |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | breakout(); |
|---|
| 176 | |
|---|
| 177 | fprintf(stderr, |
|---|
| 178 | "fxl0: %"_LD_" lines fixed\n",count); |
|---|
| 179 | |
|---|
| 180 | exit(0); |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | |
|---|
| 184 | erro(s1,s2) /* imprime mensagem de erro e morre */ |
|---|
| 185 | char *s1,*s2; |
|---|
| 186 | { |
|---|
| 187 | fprintf(stderr, |
|---|
| 188 | "fxl0: %s%s\n",s1,s2); |
|---|
| 189 | breakout(); |
|---|
| 190 | exit(1); |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | |
|---|
| 194 | |
|---|
| 195 | writbsiz() /* |
|---|
| 196 | --------- */ |
|---|
| 197 | |
|---|
| 198 | { |
|---|
| 199 | int n2; |
|---|
| 200 | unsigned char *p,*q; |
|---|
| 201 | int loop; |
|---|
| 202 | |
|---|
| 203 | /*+++ |
|---|
| 204 | write(f2,obuf,obufsize); |
|---|
| 205 | return; |
|---|
| 206 | +++*/ |
|---|
| 207 | |
|---|
| 208 | if (n2free >= obufsize) { |
|---|
| 209 | /*0 strncpy(&bufout[i2free],obuf,obufsize); */ |
|---|
| 210 | for (p=bufout+i2free, q=obuf, loop=obufsize; loop--; ) *p++ = *q++; |
|---|
| 211 | n2free-=obufsize; |
|---|
| 212 | i2free+=obufsize; |
|---|
| 213 | } |
|---|
| 214 | else { |
|---|
| 215 | /*0 strncpy(&bufout[i2free],obuf,n2free); */ |
|---|
| 216 | for (p=bufout+i2free, q=obuf, loop=n2free; loop--; ) *p++ = *q++; |
|---|
| 217 | n2=write(f2,bufout,MBUFSIZ); |
|---|
| 218 | if (n2 < MBUFSIZ) |
|---|
| 219 | erro("writbsiz - ","erro na gravacao"); |
|---|
| 220 | i2free=obufsize-n2free; |
|---|
| 221 | /*0 strncpy(bufout,&obuf[n2free],i2free); */ |
|---|
| 222 | for (p=bufout, q=obuf+n2free, loop=i2free; loop--; ) *p++ = *q++; |
|---|
| 223 | n2free=MBUFSIZ-i2free; |
|---|
| 224 | } |
|---|
| 225 | } |
|---|
| 226 | |
|---|
| 227 | |
|---|
| 228 | breakout() /* |
|---|
| 229 | --------- */ |
|---|
| 230 | |
|---|
| 231 | { |
|---|
| 232 | int n2; |
|---|
| 233 | |
|---|
| 234 | /*+++ |
|---|
| 235 | return; |
|---|
| 236 | +++*/ |
|---|
| 237 | |
|---|
| 238 | if (i2free) if (f2) { |
|---|
| 239 | n2=write(f2,bufout,i2free); |
|---|
| 240 | if (n2 < i2free) { |
|---|
| 241 | fprintf(stderr,"breakout - erro na gravacao\n"); |
|---|
| 242 | exit(1); |
|---|
| 243 | } |
|---|
| 244 | } |
|---|
| 245 | } |
|---|