From: "Juan Manuel Guerrero" Organization: Darmstadt University of Technology To: Eli Zaretskii Date: Mon, 19 Mar 2001 23:47:59 +0200 MIME-Version: 1.0 Content-type: text/plain; charset=US-ASCII Content-transfer-encoding: 7BIT Subject: Re: 2 suggestions for djtar CC: djgpp-workers AT delorie DOT com References: <5081AB53B41 AT HRZ1 DOT hrz DOT tu-darmstadt DOT de> In-reply-to: X-mailer: Pegasus Mail for Windows (v2.54DE) Message-ID: <5167B200FDB@HRZ1.hrz.tu-darmstadt.de> Reply-To: djgpp-workers AT delorie DOT com On Mon, 19 Mar 2001 13:22:41 +0200 (IST), Eli Zaretskii wrote: > It is okay to add such a feature, but the implementation is not right, > IMHO: if a .bat file already has CR-LF style EOLs (most of them do), > djtar might corrupt it. > > To be useful, this option should only convert batch files that have > Unix-style LF-only EOLs. > > (Yes, I know that djtar examines the first block of the file to decide > whether it has Unix-style EOLs. However, for a feature that is > automatically turned on and cannot be turned off, this is not safe > enough, because the block size is small. If this feature is made > optional, then I think it will be okay to use the implementation you > suggest.) okay, here is the second attempt. _ Only batch files will be processed. .btm files will not be modified. _ Every single EOL will be checked. If EOL == LF then it will be converted to CRLF. _ Lone CR will be left unmodified. _ Cntl-Z will be ignored. _ If the batch file is written to TTY then no action will be preformed at all. The patch contains also the bzip2 specific changes. Regards, Guerrero, Juan Manuel diff -acprNC5 djgpp.orig/src/utils/djtar/djtar.c djgpp/src/utils/djtar/djtar.c *** djgpp.orig/src/utils/djtar/djtar.c Sat Mar 17 16:33:06 2001 --- djgpp/src/utils/djtar/djtar.c Sun Mar 18 17:19:34 2001 *************** get_new_name(char *name_to_change, int * *** 304,316 **** changed_name = get_entry(name_to_change); if (*should_be_written && !to_stdout && NO_LFN(changed_name)) { static char info_[] = ".info-"; static char _tar_gz[] = ".tar.gz", _tgz[] = ".tgz"; static char xx[] = "++"; ! char *info, *tgz, *plus; strcpy(new, changed_name); info = strstr(new, info_); if (info && isdigit(info[sizeof(info_)-1])) { --- 304,320 ---- changed_name = get_entry(name_to_change); if (*should_be_written && !to_stdout && NO_LFN(changed_name)) { static char info_[] = ".info-"; + static char _bzip2[] = ".bzip2", _bz2[] = ".bz2"; + static char *_tar_bz_extension[] = { ".tar.bz", ".tar.bz2", ".tar.bzip2", NULL}; + static char _tbz[] = ".tbz"; static char _tar_gz[] = ".tar.gz", _tgz[] = ".tgz"; static char xx[] = "++"; ! char *bz2, *info, *tbz, *tgz, *plus; ! int i = 0; strcpy(new, changed_name); info = strstr(new, info_); if (info && isdigit(info[sizeof(info_)-1])) { *************** get_new_name(char *name_to_change, int * *** 321,330 **** --- 325,350 ---- if (tgz && tgz[sizeof(_tar_gz)-1] == '\0') { strcpy(tgz, _tgz); fprintf(log_out, "[ changing %s to %s ]\n", changed_name, new); } + while (_tar_bz_extension[i]) + { + tbz = strstr(new, _tar_bz_extension[i]); + if (tbz && tbz[strlen(_tar_bz_extension[i])] == '\0') + { + strcpy(tbz, _tbz); + fprintf(log_out, "[ changing %s to %s ]\n", changed_name, new); + } + i++; + } + bz2 = strstr(new, _bzip2); + if (bz2 && bz2[sizeof(_bzip2)-1] == '\0') + { + strcpy(bz2, _bz2); + fprintf(log_out, "[ changing %s to %s ]\n", changed_name, new); + } plus = strstr(new, xx); if (plus) { register char *s = plus; diff -acprNC5 djgpp.orig/src/utils/djtar/untar.c djgpp/src/utils/djtar/untar.c *** djgpp.orig/src/utils/djtar/untar.c Sat Mar 17 16:39:54 2001 --- djgpp/src/utils/djtar/untar.c Mon Mar 19 23:00:26 2001 *************** *** 6,15 **** --- 6,16 ---- #include #include #include #include #include + #include #include #include #include #include *************** static int skipping; *** 60,70 **** extern char new[]; int tarread(char *buf, long buf_size) { ! int should_be_written; while (buf_size) { int write_errno = 0; int dsize = 512, wsize; --- 61,71 ---- extern char new[]; int tarread(char *buf, long buf_size) { ! int should_be_written, batch_file_processing = 0; while (buf_size) { int write_errno = 0; int dsize = 512, wsize; *************** tarread(char *buf, long buf_size) *** 89,98 **** --- 90,100 ---- } } if (looking_for_header) { + char *extension; int head_csum = 0; int i; size_t nlen; memcpy(&header, buf, sizeof header); *************** tarread(char *buf, long buf_size) *** 106,115 **** --- 108,136 ---- bytes_out += sizeof header; first_block = 1; file_type = DOS_BINARY; looking_for_header = 0; + /* command.com refuses to run batch files + that have been stored with UNIX-style EOL, + so we will extract them with DOS-style EOL. */ + extension = strrchr(header.name, '.'); + if (extension) + { + char *string = xmalloc(strlen(extension) + 1); + strcpy(string, extension); + + i = 0; + while (string[++i]) + if (string[i] >= 'A' && string[i] <= 'T') + string[i] = string[i] - 'A' + 'a'; + if (strstr(string, ".bat") && string[sizeof(".bat") - 1] == '\0') + batch_file_processing = 1; /* LF -> CRLF */ + + free(string); + } + sscanf(header.operm, " %lo", &perm); sscanf(header.ouid, " %lo", &uid); sscanf(header.ogid, " %lo", &gid); sscanf(header.osize, " %lo", &size); sscanf(header.otime, " %o", &ftime); *************** open_file: *** 223,271 **** } while (size) { ! char tbuf[512]; char *wbuf = buf; if (buf_size <= 0) /* this buffer exhausted */ return 0; if (size < 512) dsize = size; else if (buf_size < 512) dsize = buf_size; else dsize = 512; ! if (first_block && (text_dos || text_unix || to_tty)) { ! file_type = guess_file_type(buf, dsize); ! first_block = 0; ! if (file_type == UNIX_TEXT && text_dos) ! setmode(r, O_TEXT); /* will add CR chars to each line */ ! } ! if ((text_unix || to_tty) && file_type == DOS_TEXT) ! { ! /* If they asked for text files to be written Unix style, or ! we are writing to console, remove the CR and ^Z characters ! from DOS text files. ! Note that we don't alter the original uncompressed data so ! as not to screw up the CRC computations. */ ! char *s=buf, *d=tbuf; ! while (s-buf < dsize) { ! if (*s != '\r' && *s != 26) ! *d++ = *s; ! s++; } ! wsize = d - tbuf; wbuf = tbuf; } else { ! wbuf = buf; ! wsize = dsize; } errno = 0; if (write(r, wbuf, wsize) < wsize) { if (errno == 0) --- 244,312 ---- } while (size) { ! char tbuf[1024]; char *wbuf = buf; if (buf_size <= 0) /* this buffer exhausted */ return 0; if (size < 512) dsize = size; else if (buf_size < 512) dsize = buf_size; else dsize = 512; ! if (batch_file_processing && !to_tty) { ! /* LF -> CRLF. ! Note that we don't alter the original uncompressed ! data so as not to screw up the CRC computations. */ ! char *src = buf, *dest = tbuf; ! int buflen = 0; ! while (buflen < dsize) { ! if (*src == '\n' && *(src - 1) != '\r' && buflen) ! *dest++ = '\r'; ! *dest++ = *src++; ! buflen = src - buf; } ! wsize = dest - tbuf; wbuf = tbuf; } else { ! if (first_block && (text_dos || text_unix || to_tty)) ! { ! file_type = guess_file_type(buf, dsize); ! first_block = 0; ! if (file_type == UNIX_TEXT && text_dos) ! setmode(r, O_TEXT); /* will add CR chars to each line */ ! } ! if ((text_unix || to_tty) && file_type == DOS_TEXT) ! { ! /* If they asked for text files to be written Unix style, or ! we are writing to console, remove the CR and ^Z characters ! from DOS text files. ! Note that we don't alter the original uncompressed data so ! as not to screw up the CRC computations. */ ! char *s=buf, *d=tbuf; ! while (s-buf < dsize) ! { ! if (*s != '\r' && *s != 26) ! *d++ = *s; ! s++; ! } ! wsize = d - tbuf; ! wbuf = tbuf; ! } ! else ! { ! wbuf = buf; ! wsize = dsize; ! } } errno = 0; if (write(r, wbuf, wsize) < wsize) { if (errno == 0) *************** open_file: *** 293,302 **** --- 334,344 ---- ftimes.ft_year = tm->tm_year - 80; setftime(r, &ftimes); close(r); chmod(changed_name, perm); } + batch_file_processing = 0; looking_for_header = 1; if (write_errno == ENOSPC) /* target disk full: quit early */ { bytes_out += buf_size; return EOF;