dos2unix.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 "libbb.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. i = mkstemp(&bb_common_bufsiz1[0]);
  38. if (i == -1 || 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. int dos2unix_main(int argc, char **argv)
  79. {
  80. int o;
  81. /* See if we are supposed to be doing dos2unix or unix2dos */
  82. if (applet_name[0] == 'd') {
  83. ConvType = CT_DOS2UNIX; /*2 */
  84. } else {
  85. ConvType = CT_UNIX2DOS; /*1 */
  86. }
  87. /* -u and -d are mutally exclusive */
  88. opt_complementary = "?:u--d:d--u";
  89. /* process parameters */
  90. /* -u convert to unix */
  91. /* -d convert to dos */
  92. o = getopt32(argc, argv, "du");
  93. /* Do the conversion requested by an argument else do the default
  94. * conversion depending on our name. */
  95. if (o)
  96. ConvType = o;
  97. if (optind < argc) {
  98. while (optind < argc)
  99. if ((o = convert(argv[optind++])) < 0)
  100. break;
  101. } else
  102. o = convert(NULL);
  103. return o;
  104. }