UNIX2DOS.C 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // UNIX2DOS - a Win32 utility to convert single text files from Unix to MS-DOS format.
  2. // Copyright (C) 1998 Clem Dye
  3. // This program is free software; you can redistribute it and/or
  4. // modify it under the terms of the GNU General Public License
  5. // as published by the Free Software Foundation; either version 2
  6. // of the License, or (at your option) any later version.
  7. // This program is distributed in the hope that it will be useful,
  8. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. // GNU General Public License for more details.
  11. // You should have received a copy of the GNU General Public License
  12. // along with this program; if not, write to the Free Software Foundation, Inc.,
  13. // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. // Additional notes.
  15. //
  16. // The author of the original source code is unknown.
  17. //
  18. // Modified by Clem Dye ([email protected]), December 1998, to compile cleanly
  19. // under Microsoft Visual C/C++ v4.0 (or later) for use on Windows NT. Added
  20. // exit(1) statements in main() to improve error reporting when the program is
  21. // used in batch scripts.
  22. // Decoding loop modified 08/08/00 by Johannes Herzig ([email protected]),
  23. // to add 'Carriage Return' only once in the situation that 'UNIX2DOS' finds 'CRLF'
  24. // combination in files with a mix of DOS and UNIX text file formats.
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <io.h>
  29. #include <sys/types.h>
  30. #include <sys/stat.h>
  31. #include <sys/utime.h>
  32. #ifndef TRUE
  33. # define TRUE (1)
  34. # define FALSE (0)
  35. #endif
  36. #define R_CNTRL "rb"
  37. #define W_CNTRL "wb"
  38. struct stat s_buf;
  39. int u2dos (path)
  40. char *path;
  41. {
  42. FILE *in, *out;
  43. int ch,
  44. prev_ch= 0,
  45. rval = FALSE;
  46. char temppath [16];
  47. struct _utimbuf ut_buf;
  48. strcpy (temppath, "./clntmp");
  49. strcat (temppath, "XXXXXX");
  50. mktemp (temppath);
  51. if ((in=fopen (path, R_CNTRL)) == (FILE *) 0)
  52. return TRUE;
  53. if ((out=fopen (temppath, W_CNTRL)) == (FILE *) 0)
  54. {
  55. fclose (in);
  56. return TRUE;
  57. }
  58. #define LF 0x0A
  59. #define CR 0x0D
  60. while ((ch = getc (in)) != EOF)
  61. {
  62. if ( ( ch == LF)
  63. && ( prev_ch != CR)
  64. && ( putc( CR, out) == EOF)
  65. || ( putc( ch, out) == EOF)
  66. )
  67. {
  68. rval = TRUE;
  69. break;
  70. }
  71. prev_ch= ch ;
  72. }
  73. if (fclose (in) == EOF)
  74. {
  75. rval = TRUE;
  76. }
  77. if (fclose (out) == EOF)
  78. {
  79. rval = TRUE;
  80. }
  81. ut_buf.actime = s_buf.st_atime;
  82. ut_buf.modtime = s_buf.st_mtime;
  83. if (_utime (temppath, &ut_buf) == -1)
  84. rval = TRUE;
  85. if (unlink (path) == -1)
  86. rval = TRUE;
  87. if (rval)
  88. {
  89. unlink (temppath);
  90. return TRUE;
  91. }
  92. if (rename (temppath,path) == -1)
  93. {
  94. fprintf (stderr, "Unix2Dos: Problems renaming '%s' to '%s'.\n", temppath, path);
  95. fprintf (stderr, " However, file '%s' remains.\n", temppath);
  96. exit (1);
  97. }
  98. unlink (temppath);
  99. return FALSE;
  100. }
  101. void main (argc, argv)
  102. int argc;
  103. char **argv;
  104. {
  105. char *path;
  106. while (--argc>0)
  107. {
  108. if (stat (path=*++argv, &s_buf) != -1)
  109. {
  110. printf ("Unix2Dos: Processing file %s ...\n", path);
  111. if (u2dos (path))
  112. {
  113. fprintf (stderr, "Unix2Dos: Problems processing file %s.\n", path);
  114. exit (1);
  115. }
  116. }
  117. else
  118. {
  119. fprintf (stderr, "Unix2Dos: Can't stat '%s'.\n", path);
  120. exit (1);
  121. }
  122. }
  123. }