home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Frozen Fish 1: Amiga
/
FrozenFish-Apr94.iso
/
bbs
/
alib
/
d1xx
/
d168
/
dillonstuff.lha
/
src
/
com
/
addcr.c
next >
Wrap
C/C++ Source or Header
|
1988-11-22
|
2KB
|
95 lines
/*
* ADDCR.C
*
* ADDCR [-l] file1 file2 ... filen
*
* -l = convert filenames to lower case on writeout.
*/
#include <stdio.h>
extern char *malloc();
main(ac,av)
char *av[];
{
register short i;
FILE *fi;
long len;
char *buf;
char *wbuf;
char clower = 0;
for (i = 1; i < ac; ++i) {
if (strcmp(av[i], "-l") == 0) {
clower = 1;
continue;
}
printf("%-20s ", av[i]);
fflush(stdout);
if ((fi = fopen(av[i], "r")) == NULL) {
perror(av[i]);
continue;
}
fseek(fi, 0, 2);
len = ftell(fi);
printf("%6ld ", len);
fflush(stdout);
if (len < 0 || (buf = malloc(len*3)) == NULL) {
puts("len < 0 or malloc failed");
fclose(fi);
continue;
}
fseek(fi, 0, 0);
if (fread(buf, len, 1, fi) != 1) {
puts("read failed");
goto done;
}
fclose(fi);
wbuf = buf + len;
len = addcr(buf, len);
if (clower) {
register char *ptr;
for (ptr = av[i]; *ptr; ++ptr) {
if (*ptr >= 'A' && *ptr <= 'Z')
*ptr |= 0x20;
}
}
if ((fi = fopen(av[i], "w")) == NULL) {
puts("Unable to open for write");
goto done;
}
printf("write ");
fflush(stdout);
if (fwrite(wbuf, len, 1, fi) != 1) {
puts("write failed");
goto done;
}
puts("ok");
done:
fclose(fi);
free(buf);
}
}
addcr(buf, len)
register char *buf;
register long len;
{
register char *ptr = buf + len;
register long i, j;
for (i = j = 0; i < len; ++i) {
if (buf[i] == 13)
continue;
if (buf[i] != 10) {
ptr[j++] = buf[i];
continue;
}
ptr[j++] = 13;
ptr[j++] = 10;
}
return(j);
}