Magellan Linux

Annotation of /trunk/mkinitrd-magellan/busybox/coreutils/dos2unix.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 816 - (hide annotations) (download)
Fri Apr 24 18:33:46 2009 UTC (15 years ago) by niro
File MIME type: text/plain
File size: 2169 byte(s)
-updated to busybox-1.13.4
1 niro 532 /* 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    
15 niro 816 #include "libbb.h"
16 niro 532
17 niro 816 enum {
18 niro 532 CT_UNIX2DOS = 1,
19     CT_DOS2UNIX
20 niro 816 };
21 niro 532
22     /* if fn is NULL then input is stdin and output is stdout */
23 niro 816 static void convert(char *fn, int conv_type)
24 niro 532 {
25     FILE *in, *out;
26     int i;
27 niro 816 char *temp_fn = temp_fn; /* for compiler */
28     char *resolved_fn = resolved_fn;
29 niro 532
30 niro 816 in = stdin;
31     out = stdout;
32 niro 532 if (fn != NULL) {
33 niro 816 struct stat st;
34    
35     resolved_fn = xmalloc_follow_symlinks(fn);
36     if (resolved_fn == NULL)
37     bb_simple_perror_msg_and_die(fn);
38     in = xfopen_for_read(resolved_fn);
39     fstat(fileno(in), &st);
40    
41     temp_fn = xasprintf("%sXXXXXX", resolved_fn);
42     i = mkstemp(temp_fn);
43     if (i == -1
44     || fchmod(i, st.st_mode) == -1
45     || !(out = fdopen(i, "w+"))
46     ) {
47     bb_simple_perror_msg_and_die(temp_fn);
48 niro 532 }
49     }
50    
51     while ((i = fgetc(in)) != EOF) {
52     if (i == '\r')
53     continue;
54 niro 816 if (i == '\n')
55     if (conv_type == CT_UNIX2DOS)
56 niro 532 fputc('\r', out);
57     fputc(i, out);
58     }
59    
60     if (fn != NULL) {
61     if (fclose(in) < 0 || fclose(out) < 0) {
62 niro 816 unlink(temp_fn);
63     bb_perror_nomsg_and_die();
64 niro 532 }
65 niro 816 xrename(temp_fn, resolved_fn);
66     free(temp_fn);
67     free(resolved_fn);
68 niro 532 }
69     }
70    
71 niro 816 int dos2unix_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
72     int dos2unix_main(int argc, char **argv)
73 niro 532 {
74 niro 816 int o, conv_type;
75 niro 532
76     /* See if we are supposed to be doing dos2unix or unix2dos */
77 niro 816 conv_type = CT_UNIX2DOS;
78 niro 532 if (applet_name[0] == 'd') {
79 niro 816 conv_type = CT_DOS2UNIX;
80 niro 532 }
81    
82 niro 816 /* -u convert to unix, -d convert to dos */
83     opt_complementary = "u--d:d--u"; /* mutually exclusive */
84     o = getopt32(argv, "du");
85    
86 niro 532 /* Do the conversion requested by an argument else do the default
87     * conversion depending on our name. */
88     if (o)
89 niro 816 conv_type = o;
90 niro 532
91 niro 816 do {
92     /* might be convert(NULL) if there is no filename given */
93     convert(argv[optind], conv_type);
94     optind++;
95     } while (optind < argc);
96 niro 532
97 niro 816 return 0;
98 niro 532 }