summaryrefslogtreecommitdiff
path: root/test/fromdos.c
diff options
context:
space:
mode:
authorMichael R Sweet <michael.r.sweet@gmail.com>1998-10-06 18:21:25 +0000
committerMichael R Sweet <michael.r.sweet@gmail.com>1998-10-06 18:21:25 +0000
commitf9039b2ae21988783feae9b362818e7923e82d14 (patch)
tree6d6fe3679d73448758f9794e7d4d4f6b22a4adad /test/fromdos.c
parent67e89232f9ba067825a158734a09e0fa21aacbe3 (diff)
Initial revision
git-svn-id: file:///fltk/svn/fltk/trunk@2 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
Diffstat (limited to 'test/fromdos.c')
-rw-r--r--test/fromdos.c65
1 files changed, 65 insertions, 0 deletions
diff --git a/test/fromdos.c b/test/fromdos.c
new file mode 100644
index 000000000..54dff5a9f
--- /dev/null
+++ b/test/fromdos.c
@@ -0,0 +1,65 @@
+/* fromdos.c : strip the stupid ^M characters without mistakes! */
+
+/* this can do in-place conversion or be used as a pipe... */
+
+#include <stdio.h>
+#include <errno.h>
+#include <unistd.h>
+#include <string.h>
+
+int main(int argc, char** argv) {
+ int f,c;
+ if (argc <= 1) {
+ if (isatty(0)) {
+ 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]);
+ return 1;
+ }
+ for (;;) {
+ c = getchar();
+ while (c == '\r') {
+ c = getchar();
+ if (c != '\n') putchar(c);
+ }
+ if (c < 0) break;
+ putchar(c);
+ }
+ return 0;
+ }
+ for (f = 1; f < argc; f++) {
+ char* fname = argv[f];
+ char tempname[1024];
+ FILE* in = fopen(fname,"rb");
+ FILE* out;
+ int mod = 0;
+ if (!in) {
+ fprintf(stderr,"%s : %s\n", fname, strerror(errno));
+ return 1;
+ }
+ strcpy(tempname, fname);
+ strcat(tempname, ".temp");
+ out = fopen(tempname, "wb");
+ if (!out) {
+ fprintf(stderr,"%s : %s\n", fname, strerror(errno));
+ return 1;
+ }
+ for (;;) {
+ c = getc(in);
+ while (c == '\r') {
+ c = getc(in);
+ if (c == '\n') mod=1; else putc(c,out);
+ }
+ if (c < 0) break;
+ putc(c,out);
+ }
+ fclose(in);
+ fclose(out);
+ if (!mod) {
+ fprintf(stderr,"%s : no change\n", fname);
+ unlink(tempname);
+ } else if (rename(tempname, fname)) {
+ fprintf(stderr,"Can't mv %s %s : %s\n",tempname,fname,strerror(errno));
+ return 1;
+ }
+ }
+ return 0;
+}