Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
1.8 KiB

  1. /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::: Include files */
  2. #include "windows.h"
  3. #include "stdio.h"
  4. #include "stdlib.h"
  5. int convert(char *buffer, char *filename);
  6. /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */
  7. __cdecl main(int argc, char *argv[])
  8. {
  9. char *buffer = malloc(1000*1024);
  10. int index;
  11. /* Validate input parameters */
  12. if(argc < 2)
  13. {
  14. printf("Invalid usage : CONVERT <filenames>\n");
  15. return(1);
  16. }
  17. for(index = 1; index < argc; index++)
  18. convert(buffer, argv[index]);
  19. return(0);
  20. }
  21. /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::: Convert file */
  22. int convert(char *buffer, char *filename)
  23. {
  24. FILE *FH;
  25. char *ptr;
  26. int chr;
  27. int last_chr = -1;
  28. /*.............................................. Open file to convert */
  29. if((FH = fopen(filename,"r+b")) == NULL)
  30. {
  31. printf("Failed to open file %s\n", filename);
  32. return(1);
  33. }
  34. /*........................................... Read in and convert file */
  35. ptr = buffer;
  36. while((chr = fgetc(FH)) != EOF)
  37. {
  38. switch(chr)
  39. {
  40. case 0xa :
  41. if(last_chr == 0xd) break;
  42. /* Fall throught and insert CR/LF in output buffer */
  43. case 0xd :
  44. *ptr++ = 0xd;
  45. *ptr++ = 0xa;
  46. break;
  47. default:
  48. *ptr++ = chr;
  49. break;
  50. }
  51. last_chr = chr;
  52. }
  53. /* Remove Control Z from end of file */
  54. if(*(ptr-1) == 0x1a)
  55. *(ptr-1) = 0;
  56. else
  57. *ptr = 0; /* terminate output buffer */
  58. /* Write out converted file */
  59. fseek(FH, 0, 0); /* Reset file pointer */
  60. fputs(buffer, FH);
  61. fclose(FH);
  62. }