dos2unix.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 {
  16. CT_UNIX2DOS = 1,
  17. CT_DOS2UNIX
  18. };
  19. /* if fn is NULL then input is stdin and output is stdout */
  20. static int convert(char *fn, int conv_type)
  21. {
  22. FILE *in, *out;
  23. int i;
  24. #define name_buf bb_common_bufsiz1
  25. in = stdin;
  26. out = stdout;
  27. if (fn != NULL) {
  28. in = 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(name_buf, sizeof(name_buf), "%sXXXXXX", fn);
  35. i = mkstemp(&name_buf[0]);
  36. if (i == -1 || chmod(name_buf, 0600) == -1) {
  37. bb_perror_nomsg_and_die();
  38. }
  39. out = fdopen(i, "w+");
  40. if (!out) {
  41. close(i);
  42. remove(name_buf);
  43. return -2;
  44. }
  45. }
  46. while ((i = fgetc(in)) != EOF) {
  47. if (i == '\r')
  48. continue;
  49. if (i == '\n') {
  50. if (conv_type == CT_UNIX2DOS)
  51. fputc('\r', out);
  52. fputc('\n', out);
  53. continue;
  54. }
  55. fputc(i, out);
  56. }
  57. if (fn != NULL) {
  58. if (fclose(in) < 0 || fclose(out) < 0) {
  59. bb_perror_nomsg();
  60. remove(name_buf);
  61. return -2;
  62. }
  63. /* Assume they are both on the same filesystem (which
  64. * should be true since we put them into the same directory
  65. * so we _should_ be ok, but you never know... */
  66. if (rename(name_buf, fn) < 0) {
  67. bb_perror_msg("cannot rename '%s' as '%s'", name_buf, fn);
  68. return -1;
  69. }
  70. }
  71. return 0;
  72. }
  73. int dos2unix_main(int argc, char **argv);
  74. int dos2unix_main(int argc, char **argv)
  75. {
  76. int o, conv_type;
  77. /* See if we are supposed to be doing dos2unix or unix2dos */
  78. if (applet_name[0] == 'd') {
  79. conv_type = CT_DOS2UNIX; /* 2 */
  80. } else {
  81. conv_type = CT_UNIX2DOS; /* 1 */
  82. }
  83. /* -u convert to unix, -d convert to dos */
  84. opt_complementary = "u--d:d--u"; /* mutually exclusive */
  85. o = getopt32(argv, "du");
  86. /* Do the conversion requested by an argument else do the default
  87. * conversion depending on our name. */
  88. if (o)
  89. conv_type = o;
  90. do {
  91. /* might be convert(NULL) if there is no filename given */
  92. o = convert(argv[optind], conv_type);
  93. if (o < 0)
  94. break;
  95. optind++;
  96. } while (optind < argc);
  97. return o;
  98. }