3
0

dos2unix.c 2.6 KB

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