Changeset 2725 in MondoRescue for branches/2.2.9/mindi-busybox/coreutils/dos2unix.c
- Timestamp:
- Feb 25, 2011, 9:26:54 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2.2.9/mindi-busybox/coreutils/dos2unix.c
r1765 r2725 10 10 * dos2unix filters reading input from stdin and writing output to stdout. 11 11 * 12 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.12 * Licensed under GPLv2 or later, see file LICENSE in this source tree. 13 13 */ 14 14 15 15 #include "libbb.h" 16 17 /* This is a NOEXEC applet. Be very careful! */ 16 18 17 19 enum { … … 21 23 22 24 /* if fn is NULL then input is stdin and output is stdout */ 23 static intconvert(char *fn, int conv_type)25 static void convert(char *fn, int conv_type) 24 26 { 25 27 FILE *in, *out; 26 28 int i; 27 #define name_buf bb_common_bufsiz1 29 char *temp_fn = temp_fn; /* for compiler */ 30 char *resolved_fn = resolved_fn; 28 31 29 32 in = stdin; 30 33 out = stdout; 31 34 if (fn != NULL) { 32 in = xfopen(fn, "rw"); 33 /* 34 The file is then created with mode read/write and 35 permissions 0666 for glibc 2.0.6 and earlier or 36 0600 for glibc 2.0.7 and later. 37 */ 38 snprintf(name_buf, sizeof(name_buf), "%sXXXXXX", fn); 39 i = mkstemp(&name_buf[0]); 40 if (i == -1 || chmod(name_buf, 0600) == -1) { 41 bb_perror_nomsg_and_die(); 42 } 43 out = fdopen(i, "w+"); 44 if (!out) { 45 close(i); 46 remove(name_buf); 47 return -2; 48 } 35 struct stat st; 36 37 resolved_fn = xmalloc_follow_symlinks(fn); 38 if (resolved_fn == NULL) 39 bb_simple_perror_msg_and_die(fn); 40 in = xfopen_for_read(resolved_fn); 41 fstat(fileno(in), &st); 42 43 temp_fn = xasprintf("%sXXXXXX", resolved_fn); 44 i = xmkstemp(temp_fn); 45 if (fchmod(i, st.st_mode) == -1) 46 bb_simple_perror_msg_and_die(temp_fn); 47 48 out = xfdopen_for_write(i); 49 49 } 50 50 … … 52 52 if (i == '\r') 53 53 continue; 54 if (i == '\n') {54 if (i == '\n') 55 55 if (conv_type == CT_UNIX2DOS) 56 56 fputc('\r', out); 57 fputc('\n', out);58 continue;59 }60 57 fputc(i, out); 61 58 } … … 63 60 if (fn != NULL) { 64 61 if (fclose(in) < 0 || fclose(out) < 0) { 65 bb_perror_nomsg(); 66 remove(name_buf); 67 return -2; 62 unlink(temp_fn); 63 bb_perror_nomsg_and_die(); 68 64 } 69 /* Assume they are both on the same filesystem (which 70 * should be true since we put them into the same directory 71 * so we _should_ be ok, but you never know... */ 72 if (rename(name_buf, fn) < 0) { 73 bb_perror_msg("cannot rename '%s' as '%s'", name_buf, fn); 74 return -1; 75 } 65 xrename(temp_fn, resolved_fn); 66 free(temp_fn); 67 free(resolved_fn); 76 68 } 77 78 return 0;79 69 } 80 70 81 int dos2unix_main(int argc, char **argv) ;82 int dos2unix_main(int argc , char **argv)71 int dos2unix_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 72 int dos2unix_main(int argc UNUSED_PARAM, char **argv) 83 73 { 84 74 int o, conv_type; 85 75 86 76 /* See if we are supposed to be doing dos2unix or unix2dos */ 77 conv_type = CT_UNIX2DOS; 87 78 if (applet_name[0] == 'd') { 88 conv_type = CT_DOS2UNIX; /* 2 */ 89 } else { 90 conv_type = CT_UNIX2DOS; /* 1 */ 79 conv_type = CT_DOS2UNIX; 91 80 } 92 81 … … 100 89 conv_type = o; 101 90 91 argv += optind; 102 92 do { 103 93 /* might be convert(NULL) if there is no filename given */ 104 o = convert(argv[optind], conv_type); 105 if (o < 0) 106 break; 107 optind++; 108 } while (optind < argc); 94 convert(*argv, conv_type); 95 } while (*argv && *++argv); 109 96 110 return o;97 return 0; 111 98 }
Note:
See TracChangeset
for help on using the changeset viewer.