Changeset 1770 in MondoRescue for branches/stable/mindi-busybox/coreutils/dos2unix.c
- Timestamp:
- Nov 6, 2007, 11:01:53 AM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/stable/mindi-busybox/coreutils/dos2unix.c
r821 r1770 13 13 */ 14 14 15 #include <string.h> 16 #include <unistd.h> 17 #include <stdint.h> 18 #include <fcntl.h> 19 #include "busybox.h" 15 #include "libbb.h" 20 16 21 enum ConvType{17 enum { 22 18 CT_UNIX2DOS = 1, 23 19 CT_DOS2UNIX 24 } ConvType;20 }; 25 21 26 22 /* if fn is NULL then input is stdin and output is stdout */ 27 static int convert(char *fn )23 static int convert(char *fn, int conv_type) 28 24 { 29 25 FILE *in, *out; 30 26 int i; 27 #define name_buf bb_common_bufsiz1 31 28 29 in = stdin; 30 out = stdout; 32 31 if (fn != NULL) { 33 in = bb_xfopen(fn, "rw");32 in = xfopen(fn, "rw"); 34 33 /* 35 34 The file is then created with mode read/write and 36 35 permissions 0666 for glibc 2.0.6 and earlier or 37 0600 for glibc 2.0.7 andlater.36 0600 for glibc 2.0.7 and later. 38 37 */ 39 snprintf(bb_common_bufsiz1, sizeof(bb_common_bufsiz1), "%sXXXXXX", fn); 40 /* 41 sizeof bb_common_bufsiz1 is 4096, so it should be big enough to 42 hold the full path. However if the output is truncated the 43 subsequent call to mkstemp would fail. 44 */ 45 if ((i = mkstemp(&bb_common_bufsiz1[0])) == -1 46 || chmod(bb_common_bufsiz1, 0600) == -1) { 38 snprintf(name_buf, sizeof(name_buf), "%sXXXXXX", fn); 39 i = mkstemp(&name_buf[0]); 40 if (i == -1 || chmod(name_buf, 0600) == -1) { 47 41 bb_perror_nomsg_and_die(); 48 42 } … … 50 44 if (!out) { 51 45 close(i); 52 remove(bb_common_bufsiz1); 46 remove(name_buf); 47 return -2; 53 48 } 54 } else {55 in = stdin;56 out = stdout;57 49 } 58 50 … … 61 53 continue; 62 54 if (i == '\n') { 63 if ( ConvType == CT_UNIX2DOS)55 if (conv_type == CT_UNIX2DOS) 64 56 fputc('\r', out); 65 57 fputc('\n', out); … … 72 64 if (fclose(in) < 0 || fclose(out) < 0) { 73 65 bb_perror_nomsg(); 74 remove( bb_common_bufsiz1);66 remove(name_buf); 75 67 return -2; 76 68 } … … 78 70 * should be true since we put them into the same directory 79 71 * so we _should_ be ok, but you never know... */ 80 if (rename( bb_common_bufsiz1, fn) < 0) {81 bb_perror_msg("cannot rename '%s' as '%s'", bb_common_bufsiz1, fn);72 if (rename(name_buf, fn) < 0) { 73 bb_perror_msg("cannot rename '%s' as '%s'", name_buf, fn); 82 74 return -1; 83 75 } … … 87 79 } 88 80 89 int dos2unix_main(int argc, char *argv[]) 81 int dos2unix_main(int argc, char **argv); 82 int dos2unix_main(int argc, char **argv) 90 83 { 91 int o ;84 int o, conv_type; 92 85 93 86 /* See if we are supposed to be doing dos2unix or unix2dos */ 94 if ( bb_applet_name[0] == 'd') {95 ConvType = CT_DOS2UNIX; /*2 */87 if (applet_name[0] == 'd') { 88 conv_type = CT_DOS2UNIX; /* 2 */ 96 89 } else { 97 ConvType = CT_UNIX2DOS; /*1 */90 conv_type = CT_UNIX2DOS; /* 1 */ 98 91 } 99 /* -u and -d are mutally exclusive */ 100 bb_opt_complementally = "?:u--d:d--u"; 101 /* process parameters */ 102 /* -u convert to unix */ 103 /* -d convert to dos */ 104 o = bb_getopt_ulflags(argc, argv, "du"); 92 93 /* -u convert to unix, -d convert to dos */ 94 opt_complementary = "u--d:d--u"; /* mutually exclusive */ 95 o = getopt32(argv, "du"); 105 96 106 97 /* Do the conversion requested by an argument else do the default 107 98 * conversion depending on our name. */ 108 99 if (o) 109 ConvType = o;100 conv_type = o; 110 101 111 if (optind < argc) { 112 while (optind < argc) 113 if ((o = convert(argv[optind++])) < 0) 114 break; 115 } else 116 o = convert(NULL); 102 do { 103 /* 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); 117 109 118 110 return o;
Note:
See TracChangeset
for help on using the changeset viewer.