DOS2UNIX.C 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // DOS2UNIX - a Win32 utility to convert single text files from MS-DOS to Unix 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. #include <io.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <sys/types.h>
  27. #include <sys/stat.h>
  28. #include <sys/utime.h>
  29. #ifndef TRUE
  30. # define TRUE (1)
  31. # define FALSE (0)
  32. #endif
  33. #define R_CNTRL "rb"
  34. #define W_CNTRL "wb"
  35. struct stat s_buf;
  36. int dos2u (path)
  37. char *path;
  38. {
  39. FILE *in, *out;
  40. int ch,
  41. rval = FALSE;
  42. char temppath [16];
  43. struct _utimbuf ut_buf;
  44. strcpy (temppath, "./clntmp");
  45. strcat (temppath, "XXXXXX");
  46. mktemp (temppath);
  47. if ((in=fopen (path, R_CNTRL)) == (FILE *) 0)
  48. return TRUE;
  49. if ((out=fopen (temppath, W_CNTRL)) == (FILE *) 0)
  50. {
  51. fclose (in);
  52. return TRUE;
  53. }
  54. while ((ch = getc (in)) != EOF)
  55. if ((ch != '\015' && ch != '\032') &&
  56. (putc (ch, out) == EOF))
  57. {
  58. rval = TRUE;
  59. break;
  60. }
  61. if (fclose (in) == EOF)
  62. {
  63. rval = TRUE;
  64. }
  65. if (fclose (out) == EOF)
  66. {
  67. rval = TRUE;
  68. }
  69. ut_buf.actime = s_buf.st_atime;
  70. ut_buf.modtime = s_buf.st_mtime;
  71. if (_utime (temppath, &ut_buf) == -1)
  72. rval = TRUE;
  73. if (unlink (path) == -1)
  74. rval = TRUE;
  75. if (rval)
  76. {
  77. unlink (temppath);
  78. return TRUE;
  79. }
  80. if (rename (temppath,path) == -1)
  81. {
  82. fprintf (stderr, "Dos2Unix: Problems renaming '%s' to '%s'.\n", temppath, path);
  83. fprintf (stderr, " However, file '%s' remains.\n", temppath);
  84. exit (1);
  85. }
  86. unlink (temppath);
  87. return FALSE;
  88. }
  89. void main (argc, argv)
  90. int argc;
  91. char **argv;
  92. {
  93. char *path;
  94. while (--argc>0)
  95. {
  96. if (stat (path=*++argv, &s_buf) != -1)
  97. {
  98. printf ("Dos2Unix: Processing file %s ...\n", path);
  99. if (dos2u (path))
  100. {
  101. fprintf (stderr, "Dos2Unix: Problems processing file %s.\n", path);
  102. exit (1);
  103. }
  104. }
  105. else
  106. {
  107. fprintf (stderr, "Dos2Unix: Can't stat '%s'.\n", path);
  108. exit (1);
  109. }
  110. }
  111. }