dos2unix.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 GPLv2 or later, see file LICENSE in this source tree.
  13. */
  14. //usage:#define dos2unix_trivial_usage
  15. //usage: "[-ud] [FILE]"
  16. //usage:#define dos2unix_full_usage "\n\n"
  17. //usage: "Convert FILE in-place from DOS to Unix format.\n"
  18. //usage: "When no file is given, use stdin/stdout.\n"
  19. //usage: "\n -u dos2unix"
  20. //usage: "\n -d unix2dos"
  21. //usage:
  22. //usage:#define unix2dos_trivial_usage
  23. //usage: "[-ud] [FILE]"
  24. //usage:#define unix2dos_full_usage "\n\n"
  25. //usage: "Convert FILE in-place from Unix to DOS format.\n"
  26. //usage: "When no file is given, use stdin/stdout.\n"
  27. //usage: "\n -u dos2unix"
  28. //usage: "\n -d unix2dos"
  29. #include "libbb.h"
  30. /* This is a NOEXEC applet. Be very careful! */
  31. enum {
  32. CT_UNIX2DOS = 1,
  33. CT_DOS2UNIX
  34. };
  35. /* if fn is NULL then input is stdin and output is stdout */
  36. static void convert(char *fn, int conv_type)
  37. {
  38. FILE *in, *out;
  39. int i;
  40. char *temp_fn = temp_fn; /* for compiler */
  41. char *resolved_fn = resolved_fn;
  42. in = stdin;
  43. out = stdout;
  44. if (fn != NULL) {
  45. struct stat st;
  46. resolved_fn = xmalloc_follow_symlinks(fn);
  47. if (resolved_fn == NULL)
  48. bb_simple_perror_msg_and_die(fn);
  49. in = xfopen_for_read(resolved_fn);
  50. fstat(fileno(in), &st);
  51. temp_fn = xasprintf("%sXXXXXX", resolved_fn);
  52. i = xmkstemp(temp_fn);
  53. if (fchmod(i, st.st_mode) == -1)
  54. bb_simple_perror_msg_and_die(temp_fn);
  55. out = xfdopen_for_write(i);
  56. }
  57. while ((i = fgetc(in)) != EOF) {
  58. if (i == '\r')
  59. continue;
  60. if (i == '\n')
  61. if (conv_type == CT_UNIX2DOS)
  62. fputc('\r', out);
  63. fputc(i, out);
  64. }
  65. if (fn != NULL) {
  66. if (fclose(in) < 0 || fclose(out) < 0) {
  67. unlink(temp_fn);
  68. bb_perror_nomsg_and_die();
  69. }
  70. xrename(temp_fn, resolved_fn);
  71. free(temp_fn);
  72. free(resolved_fn);
  73. }
  74. }
  75. int dos2unix_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  76. int dos2unix_main(int argc UNUSED_PARAM, char **argv)
  77. {
  78. int o, conv_type;
  79. /* See if we are supposed to be doing dos2unix or unix2dos */
  80. conv_type = CT_UNIX2DOS;
  81. if (applet_name[0] == 'd') {
  82. conv_type = CT_DOS2UNIX;
  83. }
  84. /* -u convert to unix, -d convert to dos */
  85. opt_complementary = "u--d:d--u"; /* mutually exclusive */
  86. o = getopt32(argv, "du");
  87. /* Do the conversion requested by an argument else do the default
  88. * conversion depending on our name. */
  89. if (o)
  90. conv_type = o;
  91. argv += optind;
  92. do {
  93. /* might be convert(NULL) if there is no filename given */
  94. convert(*argv, conv_type);
  95. } while (*argv && *++argv);
  96. return 0;
  97. }