Clean up some inconsistencies in themes.
[ntk.git] / test / fromdos.c
blob54dff5a9fb24ebc98bed5ae3c4059c60559e728a
1 /* fromdos.c : strip the stupid ^M characters without mistakes! */
3 /* this can do in-place conversion or be used as a pipe... */
5 #include <stdio.h>
6 #include <errno.h>
7 #include <unistd.h>
8 #include <string.h>
10 int main(int argc, char** argv) {
11 int f,c;
12 if (argc <= 1) {
13 if (isatty(0)) {
14 fprintf(stderr,"usage : %s <files>\nStrips ^M characters.\nCan do in-place conversion of many files or can be used in a pipe\n",argv[0]);
15 return 1;
17 for (;;) {
18 c = getchar();
19 while (c == '\r') {
20 c = getchar();
21 if (c != '\n') putchar(c);
23 if (c < 0) break;
24 putchar(c);
26 return 0;
28 for (f = 1; f < argc; f++) {
29 char* fname = argv[f];
30 char tempname[1024];
31 FILE* in = fopen(fname,"rb");
32 FILE* out;
33 int mod = 0;
34 if (!in) {
35 fprintf(stderr,"%s : %s\n", fname, strerror(errno));
36 return 1;
38 strcpy(tempname, fname);
39 strcat(tempname, ".temp");
40 out = fopen(tempname, "wb");
41 if (!out) {
42 fprintf(stderr,"%s : %s\n", fname, strerror(errno));
43 return 1;
45 for (;;) {
46 c = getc(in);
47 while (c == '\r') {
48 c = getc(in);
49 if (c == '\n') mod=1; else putc(c,out);
51 if (c < 0) break;
52 putc(c,out);
54 fclose(in);
55 fclose(out);
56 if (!mod) {
57 fprintf(stderr,"%s : no change\n", fname);
58 unlink(tempname);
59 } else if (rename(tempname, fname)) {
60 fprintf(stderr,"Can't mv %s %s : %s\n",tempname,fname,strerror(errno));
61 return 1;
64 return 0;