- Timestamp:
- Oct 29, 2017, 7:02:04 PM (8 years ago)
- Location:
- trunk/src
- Files:
-
- 7 added
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/kmk/Makefile.am
r3067 r3114 58 58 ../lib/kDep.c \ 59 59 ../lib/kbuild_version.c \ 60 ../lib/dos2unix.c \ 60 61 ../lib/maybe_con_fwrite.c \ 61 62 \ -
trunk/src/kmk/kmkbuiltin/install.c
r3112 r3114 91 91 #include "kmkbuiltin.h" 92 92 #include "k/kDefs.h" /* for K_OS */ 93 #include "dos2unix.h" 93 94 94 95 … … 102 103 #ifndef MAXBSIZE 103 104 # define MAXBSIZE 0x20000 104 #endif105 106 /* Bootstrap aid - this doesn't exist in most older releases */107 #ifndef MAP_FAILED108 #define MAP_FAILED ((void *)-1) /* from <sys/mman.h> */109 105 #endif 110 106 … … 137 133 static int ignore_perm_errors; 138 134 static int hard_link_files_when_possible; 135 static int dos2unix; 139 136 140 137 static struct option long_options[] = … … 146 143 { "hard-link-files-when-possible", no_argument, 0, 265 }, 147 144 { "no-hard-link-files-when-possible", no_argument, 0, 266 }, 145 { "dos2unix", no_argument, 0, 267 }, 146 { "unix2dos", no_argument, 0, 268 }, 148 147 { 0, 0, 0, 0 }, 149 148 }; 150 149 151 150 152 static int copy(int, const char *, int , const char *, off_t);151 static int copy(int, const char *, int *, const char *); 153 152 static int compare(int, const char *, size_t, int, const char *, size_t); 154 153 static int create_newfile(const char *, int, struct stat *); … … 160 159 static int usage(FILE *); 161 160 static char *last_slash(const char *); 161 static KBOOL needs_dos2unix_conversion(const char *pszFilename); 162 static KBOOL needs_unix2dos_conversion(const char *pszFilename); 162 163 163 164 int … … 173 174 (void)envp; 174 175 175 176 177 178 179 180 176 /* reinitialize globals */ 177 mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH; 178 suffix = BACKUP_SUFFIX; 179 gid = 0; 180 uid = 0; 181 dobackup = docompare = dodir = dopreserve = dostrip = nommap = safecopy = verbose = mode_given = 0; 181 182 ignore_perm_errors = geteuid() != 0; 182 hard_link_files_when_possible = 0; 183 184 /* reset getopt and set progname. */ 185 g_progname = argv[0]; 186 opterr = 1; 187 optarg = NULL; 188 optopt = 0; 189 optind = 0; /* init */ 183 hard_link_files_when_possible = 0; 184 dos2unix = 0; 185 186 /* reset getopt and set progname. */ 187 g_progname = argv[0]; 188 opterr = 1; 189 optarg = NULL; 190 optopt = 0; 191 optind = 0; /* init */ 190 192 191 193 iflags = 0; … … 264 266 hard_link_files_when_possible = 0; 265 267 break; 268 case 267: 269 dos2unix = 1; 270 break; 271 case 268: 272 dos2unix = -1; 273 break; 266 274 case '?': 267 275 default: … … 280 288 if (argc == 0 || (argc == 1 && !dodir)) 281 289 return usage(stderr); 290 291 /* and unix2dos doesn't combine well with a couple of other options. */ 292 if (dos2unix != 0) { 293 if (docompare) { 294 warnx("-C/-p and --dos2unix/unix2dos may not be specified together"); 295 return usage(stderr); 296 } 297 if (dostrip) { 298 warnx("-s and --dos2unix/unix2dos may not be specified together"); 299 return usage(stderr); 300 } 301 } 282 302 283 303 /* need to make a temp copy so we can compare stripped version */ … … 478 498 } else if (gid != (gid_t)-1 && gid != from_sb.st_gid) { 479 499 why_not = "gid mismatch"; 500 } else if (dos2unix > 0 && needs_dos2unix_conversion(from_name)) { 501 why_not = "dos2unix"; 502 } else if (dos2unix < 0 && needs_unix2dos_conversion(from_name)) { 503 why_not = "unix2dos"; 480 504 } else { 481 505 int rcLink = link(from_name, to_name); … … 544 568 } 545 569 if (!devnull) { 546 rc = copy(from_fd, from_name, to_fd, 547 tempcopy ? tempfile : to_name, from_sb.st_size); 570 rc = copy(from_fd, from_name, &to_fd, tempcopy ? tempfile : to_name); 548 571 if (rc) 549 572 goto l_done; … … 880 903 881 904 /* 905 * Write error handler. 906 */ 907 static int write_error(int *ptr_to_fd, const char *to_name, int nw) 908 { 909 int serrno = errno; 910 (void)close(*ptr_to_fd); 911 *ptr_to_fd = -1; 912 (void)unlink(to_name); 913 errno = nw > 0 ? EIO : serrno; 914 return err(EX_OSERR, "%s", to_name); 915 } 916 917 /* 918 * Read error handler. 919 */ 920 static int read_error(const char *from_name, int *ptr_to_fd, const char *to_name) 921 { 922 int serrno = errno; 923 (void)close(*ptr_to_fd); 924 *ptr_to_fd = -1; 925 (void)unlink(to_name); 926 errno = serrno; 927 return err(EX_OSERR, "%s", from_name); 928 } 929 930 /* 882 931 * copy -- 883 932 * copy from one file to another 884 933 */ 885 934 static int 886 copy(int from_fd, const char *from_name, int to_fd, const char *to_name, 887 off_t size) 888 { 935 copy(int from_fd, const char *from_name, int *ptr_to_fd, const char *to_name) 936 { 937 KBOOL fPendingCr = K_FALSE; 938 KSIZE cchDst; 889 939 int nr, nw; 890 int serrno;891 940 char buf[MAXBSIZE]; 892 893 (void)size; 941 int to_fd = *ptr_to_fd; 894 942 895 943 /* Rewind file descriptors. */ … … 899 947 return err(EX_OSERR, "lseek: %s", to_name); 900 948 901 while ((nr = read(from_fd, buf, sizeof(buf))) > 0) 902 if ((nw = write(to_fd, buf, nr)) != nr) { 903 serrno = errno; 904 (void)unlink(to_name); 905 errno = nw > 0 ? EIO : serrno; 906 return err(EX_OSERR, "%s", to_name); 907 } 908 if (nr != 0) { 909 serrno = errno; 910 (void)unlink(to_name); 911 errno = serrno; 912 return err(EX_OSERR, "%s", from_name); 913 } 914 return EX_OK; 949 if (dos2unix == 0) { 950 /* 951 * Copy bytes, no conversion. 952 */ 953 while ((nr = read(from_fd, buf, sizeof(buf))) > 0) 954 if ((nw = write(to_fd, buf, nr)) != nr) 955 return write_error(ptr_to_fd, to_name, nw); 956 } else if (dos2unix > 0) { 957 /* 958 * CRLF -> LF is a reduction, so we can work with full buffers. 959 */ 960 while ((nr = read(from_fd, buf, sizeof(buf))) > 0) { 961 if ( fPendingCr 962 && buf[0] != '\n' 963 && (nw = write(to_fd, "\r", 1)) != 1) 964 return write_error(ptr_to_fd, to_name, nw); 965 966 fPendingCr = dos2unix_convert_to_unix(buf, nr, buf, &cchDst); 967 968 nw = write(to_fd, buf, cchDst); 969 if (nw != (int)cchDst) 970 return write_error(ptr_to_fd, to_name, nw); 971 } 972 } else { 973 /* 974 * LF -> CRLF is an expansion, so we work with half buffers, reading 975 * into the upper half of the buffer and expanding into the full buffer. 976 * The conversion will never expand to more than the double size. 977 * 978 * Note! We do not convert valid CRLF line endings. This gives us 979 * valid DOS text, but no round-trip conversion. 980 */ 981 char * const pchSrc = &buf[sizeof(buf) / 2]; 982 while ((nr = read(from_fd, pchSrc, sizeof(buf) / 2)) > 0) { 983 if ( fPendingCr 984 && pchSrc[0] != '\n' 985 && (nw = write(to_fd, "\r", 1))!= 1) 986 return write_error(ptr_to_fd, to_name, nw); 987 988 fPendingCr = dos2unix_convert_to_dos(pchSrc, nr, buf, &cchDst); 989 990 nw = write(to_fd, buf, cchDst); 991 if (nw != (int)cchDst) 992 return write_error(ptr_to_fd, to_name, nw); 993 } 994 } 995 996 /* Check for read error. */ 997 if (nr != 0) 998 return read_error(from_name, ptr_to_fd, to_name); 999 1000 /* When converting, we might have a pending final CR to write. */ 1001 if ( fPendingCr 1002 && (nw = write(to_fd, "\r", 1))!= 1) 1003 return write_error(ptr_to_fd, to_name, nw); 1004 1005 return EX_OK; 915 1006 } 916 1007 … … 1009 1100 fprintf(pf, 1010 1101 "usage: %s [-bCcpSsv] [--[no-]hard-link-files-when-possible]\n" 1011 " [--[no-]ignore-perm-errors] [-B suffix] [-f flags] \n"1012 " [- g group] [-m mode] [-o owner] file1 file2\n"1102 " [--[no-]ignore-perm-errors] [-B suffix] [-f flags] [-g group]\n" 1103 " [-m mode] [-o owner] [--dos2unix|--unix2dos] file1 file2\n" 1013 1104 " or: %s [-bCcpSsv] [--[no-]ignore-perm-errors] [-B suffix] [-f flags]\n" 1014 1105 " [-g group] [-m mode] [-o owner] file1 ... fileN directory\n" … … 1044 1135 } 1045 1136 1137 /** 1138 * Checks if @a pszFilename actually needs dos2unix conversion. 1139 * 1140 * @returns boolean. 1141 * @param pszFilename The name of the file to check. 1142 */ 1143 static KBOOL needs_dos2unix_conversion(const char *pszFilename) 1144 { 1145 KU32 fStyle = 0; 1146 int iErr = dos2unix_analyze_file(pszFilename, &fStyle, NULL, NULL); 1147 return iErr != 0 1148 || (fStyle & (DOS2UNIX_STYLE_MASK | DOS2UNIX_F_BINARY)) != DOS2UNIX_STYLE_UNIX; 1149 } 1150 1151 /** 1152 * Checks if @a pszFilename actually needs unix2dos conversion. 1153 * 1154 * @returns boolean. 1155 * @param pszFilename The name of the file to check. 1156 */ 1157 static KBOOL needs_unix2dos_conversion(const char *pszFilename) 1158 { 1159 KU32 fStyle = 0; 1160 int iErr = dos2unix_analyze_file(pszFilename, &fStyle, NULL, NULL); 1161 return iErr != 0 1162 || (fStyle & (DOS2UNIX_STYLE_MASK | DOS2UNIX_F_BINARY)) != DOS2UNIX_STYLE_DOS; 1163 } 1164 -
trunk/src/kmk/main.c
r3051 r3114 1414 1414 # endif /* !ELECTRIC_HEAP */ 1415 1415 1416 # ifdef KMK 1417 /* Clear the SEM_NOGPFAULTERRORBOX flag so WER will generate dumps when we run 1418 under cygwin. To void popups, set WER registry value DontShowUI to 1. */ 1419 if (getenv("KMK_NO_SET_ERROR_MODE") == NULL) 1420 SetErrorMode(GetErrorMode() & ~SEM_NOGPFAULTERRORBOX); 1421 # endif 1422 1416 1423 /* start off assuming we have no shell */ 1417 1424 unixy_shell = 0; 1418 1425 no_default_sh_exe = 1; 1419 1426 #endif 1420 # 1427 #ifdef CONFIG_WITH_FAST_IS_SPACE /* bird */ 1421 1428 memset (space_map, '\0', sizeof(space_map)); 1422 1429 set_space_map_entry (' '); … … 1426 1433 set_space_map_entry ('\t'); 1427 1434 set_space_map_entry ('\v'); 1428 # 1435 #endif /* CONFIG_WITH_FAST_IS_SPACE */ 1429 1436 1430 1437 #ifdef CONFIG_WITH_PRINT_TIME_SWITCH -
trunk/src/lib/Makefile.kmk
r3060 r3114 41 41 maybe_con_write.c \ 42 42 maybe_con_fwrite.c \ 43 dos2unix.c \ 43 44 kbuild_version.c 44 45 kUtil_SOURCES.win = \ -
trunk/src/lib/kDep.c
r3105 r3114 69 69 /* For the GNU/hurd weirdo. */ 70 70 #if !defined(PATH_MAX) && !defined(_MAX_PATH) 71 # define PATH_MAX 204871 # define PATH_MAX 4096 72 72 #endif 73 73
Note:
See TracChangeset
for help on using the changeset viewer.