diff options
| author | bug1 <bug1@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2001-04-12 00:42:53 +0000 |
|---|---|---|
| committer | bug1 <bug1@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2001-04-12 00:42:53 +0000 |
| commit | 010fef493be367c520e6c47d5fffea0cd476505e (patch) | |
| tree | 4979643e9a5bf6989d06e69593c9c7b60f3c5b7c | |
| parent | 02426207066ffd7e4ecf2a68b03a8d893f5d3b9a (diff) | |
| download | busybox-w32-010fef493be367c520e6c47d5fffea0cd476505e.tar.gz busybox-w32-010fef493be367c520e6c47d5fffea0cd476505e.tar.bz2 busybox-w32-010fef493be367c520e6c47d5fffea0cd476505e.zip | |
Moved from dos2unix
git-svn-id: svn://busybox.net/trunk/busybox@2323 69ca8d6d-28ef-0310-b511-8ec308f3f277
| -rw-r--r-- | libbb/convert.c | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/libbb/convert.c b/libbb/convert.c new file mode 100644 index 000000000..1fb3f32c0 --- /dev/null +++ b/libbb/convert.c | |||
| @@ -0,0 +1,106 @@ | |||
| 1 | /* | ||
| 2 | * Taken from dos2unix for BusyBox | ||
| 3 | * | ||
| 4 | * dos2unix '\n' convertor 0.5.0 | ||
| 5 | * based on Unix2Dos 0.9.0 by Peter Hanecak (made 19.2.1997) | ||
| 6 | * Copyright 1997,.. by Peter Hanecak <hanecak@megaloman.sk>. | ||
| 7 | * All rights reserved. | ||
| 8 | * | ||
| 9 | * This program is free software; you can redistribute it and/or modify | ||
| 10 | * it under the terms of the GNU General Public License as published by | ||
| 11 | * the Free Software Foundation; either version 2 of the License, or | ||
| 12 | * (at your option) any later version. | ||
| 13 | * | ||
| 14 | * This program is distributed in the hope that it will be useful, | ||
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 17 | * GNU Library General Public License for more details. | ||
| 18 | * | ||
| 19 | * You should have received a copy of the GNU General Public License | ||
| 20 | * along with this program; if not, write to the Free Software | ||
| 21 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | ||
| 22 | */ | ||
| 23 | |||
| 24 | #include "libbb.h" | ||
| 25 | |||
| 26 | // if fn is NULL then input is stdin and output is stdout | ||
| 27 | extern int convert(char *fn, int ConvType) { | ||
| 28 | char c; | ||
| 29 | char *tempFn = NULL; | ||
| 30 | FILE *in = stdin, *out = stdout; | ||
| 31 | |||
| 32 | if (fn != NULL) { | ||
| 33 | if ((in = fopen(fn, "r")) == NULL) { | ||
| 34 | perror_msg(fn); | ||
| 35 | return -1; | ||
| 36 | } | ||
| 37 | tempFn = tmpnam(NULL); | ||
| 38 | if (tempFn == NULL || (out = fopen(tempFn, "w")) == NULL) { | ||
| 39 | perror_msg(NULL); | ||
| 40 | return -2; | ||
| 41 | } | ||
| 42 | } | ||
| 43 | |||
| 44 | while ((c = fgetc(in)) != EOF) { | ||
| 45 | if (c == '\r') { | ||
| 46 | if ((ConvType == CT_UNIX2DOS) && (fn != NULL)) { | ||
| 47 | // file is alredy in DOS format so it is not necessery to touch it | ||
| 48 | if (fclose(in) < 0 || fclose(out) < 0 || remove(tempFn) < 0) { | ||
| 49 | perror_msg(NULL); | ||
| 50 | return -2; | ||
| 51 | } | ||
| 52 | return 0; | ||
| 53 | } | ||
| 54 | if (!ConvType) | ||
| 55 | ConvType = CT_DOS2UNIX; | ||
| 56 | break; | ||
| 57 | } | ||
| 58 | if (c == '\n') { | ||
| 59 | if ((ConvType == CT_DOS2UNIX) && (fn != NULL)) { | ||
| 60 | // file is alredy in UNIX format so it is not necessery to touch it | ||
| 61 | if (fclose(in) < 0 || fclose(out) < 0 || remove(tempFn) < 0) { | ||
| 62 | perror_msg(NULL); | ||
| 63 | return -2; | ||
| 64 | } | ||
| 65 | return 0; | ||
| 66 | } | ||
| 67 | if (!ConvType) | ||
| 68 | ConvType = CT_UNIX2DOS; | ||
| 69 | if (ConvType == CT_UNIX2DOS) | ||
| 70 | fputc('\r', out); | ||
| 71 | fputc('\n', out); | ||
| 72 | break; | ||
| 73 | } | ||
| 74 | fputc(c, out); | ||
| 75 | } | ||
| 76 | if (c != EOF) | ||
| 77 | while ((c = fgetc(in)) != EOF) { | ||
| 78 | if (c == '\r') | ||
| 79 | continue; | ||
| 80 | if (c == '\n') { | ||
| 81 | if (ConvType == CT_UNIX2DOS) | ||
| 82 | fputc('\r', out); | ||
| 83 | fputc('\n', out); | ||
| 84 | continue; | ||
| 85 | } | ||
| 86 | fputc(c, out); | ||
| 87 | } | ||
| 88 | |||
| 89 | if (fn != NULL) { | ||
| 90 | if (fclose(in) < 0 || fclose(out) < 0 || | ||
| 91 | (in = fopen(tempFn, "r")) == NULL || (out = fopen(fn, "w")) == NULL) { | ||
| 92 | perror_msg(NULL); | ||
| 93 | return -2; | ||
| 94 | } | ||
| 95 | |||
| 96 | while ((c = fgetc(in)) != EOF) | ||
| 97 | fputc(c, out); | ||
| 98 | |||
| 99 | if (fclose(in) < 0 || fclose(out) < 0 || remove(tempFn) < 0) { | ||
| 100 | perror_msg(NULL); | ||
| 101 | return -2; | ||
| 102 | } | ||
| 103 | } | ||
| 104 | |||
| 105 | return 0; | ||
| 106 | } | ||
