dos2unix.c 2.8 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 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 ch;
  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. int fd;
  47. resolved_fn = xmalloc_follow_symlinks(fn);
  48. if (resolved_fn == NULL)
  49. bb_simple_perror_msg_and_die(fn);
  50. in = xfopen_for_read(resolved_fn);
  51. xfstat(fileno(in), &st, resolved_fn);
  52. temp_fn = xasprintf("%sXXXXXX", resolved_fn);
  53. fd = xmkstemp(temp_fn);
  54. if (fchmod(fd, st.st_mode) == -1)
  55. bb_simple_perror_msg_and_die(temp_fn);
  56. fchown(fd, st.st_uid, st.st_gid);
  57. out = xfdopen_for_write(fd);
  58. }
  59. while ((ch = fgetc(in)) != EOF) {
  60. if (ch == '\r')
  61. continue;
  62. if (ch == '\n')
  63. if (conv_type == CT_UNIX2DOS)
  64. fputc('\r', out);
  65. fputc(ch, out);
  66. }
  67. if (fn != NULL) {
  68. if (fclose(in) < 0 || fclose(out) < 0) {
  69. unlink(temp_fn);
  70. bb_perror_nomsg_and_die();
  71. }
  72. xrename(temp_fn, resolved_fn);
  73. free(temp_fn);
  74. free(resolved_fn);
  75. }
  76. }
  77. int dos2unix_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  78. int dos2unix_main(int argc UNUSED_PARAM, char **argv)
  79. {
  80. int o, conv_type;
  81. /* See if we are supposed to be doing dos2unix or unix2dos */
  82. conv_type = CT_UNIX2DOS;
  83. if (applet_name[0] == 'd') {
  84. conv_type = CT_DOS2UNIX;
  85. }
  86. /* -u convert to unix, -d convert to dos */
  87. opt_complementary = "u--d:d--u"; /* mutually exclusive */
  88. o = getopt32(argv, "du");
  89. /* Do the conversion requested by an argument else do the default
  90. * conversion depending on our name. */
  91. if (o)
  92. conv_type = o;
  93. argv += optind;
  94. do {
  95. /* might be convert(NULL) if there is no filename given */
  96. convert(*argv, conv_type);
  97. } while (*argv && *++argv);
  98. return 0;
  99. }