3
0

dos2unix.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * 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. * dos2unix filters reading input from stdin and writing output to stdout.
  10. *
  11. * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
  12. */
  13. #include <string.h>
  14. #include <unistd.h>
  15. #include <stdint.h>
  16. #include <fcntl.h>
  17. #include <sys/time.h>
  18. #include "busybox.h"
  19. #define CT_UNIX2DOS 1
  20. #define CT_DOS2UNIX 2
  21. #define tempFn bb_common_bufsiz1
  22. /* if fn is NULL then input is stdin and output is stdout */
  23. static int convert(char *fn, int ConvType)
  24. {
  25. int c, fd;
  26. FILE *in, *out;
  27. if (fn != NULL) {
  28. in = bb_xfopen(fn, "rw");
  29. /*
  30. The file is then created with mode read/write and
  31. permissions 0666 for glibc 2.0.6 and earlier or
  32. 0600 for glibc 2.0.7 and later.
  33. */
  34. snprintf(tempFn, sizeof(tempFn), "%sXXXXXX", fn);
  35. /*
  36. sizeof tempFn is 4096, so it should be big enough to hold the full
  37. path. however if the output is truncated the subsequent call to
  38. mkstemp would fail.
  39. */
  40. if ((fd = mkstemp(&tempFn[0])) == -1 || chmod(tempFn, 0600) == -1) {
  41. bb_perror_nomsg_and_die();
  42. }
  43. out = fdopen(fd, "w+");
  44. if (!out) {
  45. close(fd);
  46. remove(tempFn);
  47. }
  48. } else {
  49. in = stdin;
  50. out = stdout;
  51. }
  52. while ((c = fgetc(in)) != EOF) {
  53. if (c == '\r')
  54. continue;
  55. if (c == '\n') {
  56. if (ConvType == CT_UNIX2DOS)
  57. fputc('\r', out);
  58. fputc('\n', out);
  59. continue;
  60. }
  61. fputc(c, out);
  62. }
  63. if (fn != NULL) {
  64. if (fclose(in) < 0 || fclose(out) < 0) {
  65. bb_perror_nomsg();
  66. remove(tempFn);
  67. return -2;
  68. }
  69. /* Assume they are both on the same filesystem (which
  70. * should be true since we put them into the same directory
  71. * so we _should_ be ok, but you never know... */
  72. if (rename(tempFn, fn) < 0) {
  73. bb_perror_msg("cannot rename '%s' as '%s'", tempFn, fn);
  74. return -1;
  75. }
  76. }
  77. return 0;
  78. }
  79. int dos2unix_main(int argc, char *argv[])
  80. {
  81. int ConvType;
  82. int o;
  83. /* See if we are supposed to be doing dos2unix or unix2dos */
  84. if (argv[0][0]=='d') {
  85. ConvType = CT_DOS2UNIX; /*2*/
  86. } else {
  87. ConvType = CT_UNIX2DOS; /*1*/
  88. }
  89. /* -u and -d are mutally exclusive */
  90. bb_opt_complementally = "?:u--d:d--u";
  91. /* process parameters */
  92. /* -u convert to unix */
  93. /* -d convert to dos */
  94. o = bb_getopt_ulflags(argc, argv, "du");
  95. /* Do the conversion requested by an argument else do the default
  96. * conversion depending on our name. */
  97. if (o)
  98. ConvType = o;
  99. if (optind < argc) {
  100. while(optind < argc)
  101. if ((o = convert(argv[optind++], ConvType)) < 0)
  102. break;
  103. }
  104. else
  105. o = convert(NULL, ConvType);
  106. return o;
  107. }