source: MondoRescue/branches/3.3/mindi-busybox/coreutils/dos2unix.c@ 3865

Last change on this file since 3865 was 3621, checked in by Bruno Cornec, 10 years ago

New 3?3 banch for incorporation of latest busybox 1.25. Changing minor version to handle potential incompatibilities.

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