1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
|
/*
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 1, or (at your option)
** any later version.
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*
* Author : Alexandre Parenteau <aubonbeurre@hotmail.com> --- December 1997
*/
/*
* TextBinary.cpp --- utilities to ckeck if files are binary or text
*/
#include "stdafx.h"
#ifdef WIN32
# include <sys/stat.h>
# include <direct.h>
# include <io.h>
#endif /* WIN32 */
#ifdef macintosh
# include <Resources.h>
# include <unistd.h>
# include <sys/types.h>
# include <sys/stat.h>
# include "MacMisc.h"
#endif /* macintosh */
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include "TextBinary.h"
#include "FileTraversal.h"
#include "AppConsole.h"
#include "CPStr.h"
#include "CvsPrefs.h"
#if !defined(S_ISLNK) && defined(S_IFLNK)
# if defined(S_IFMT)
# define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
# else
# define S_ISLNK(m) ((m) & S_IFLNK)
# endif
#endif
#define MAX_TEST_SIZE 100000
#ifdef WIN32
# ifdef _DEBUG
# define new DEBUG_NEW
# undef THIS_FILE
static char THIS_FILE[] = __FILE__;
# endif
#endif /* WIN32 */
static CStaticAllocT<char> sBuf;
static int sBufSize;
static kTextBinTYPE FillBuffer(const char *arg, const char *dir, const UFSSpec * spec, bool bin)
{
sBuf.AdjustSize(MAX_TEST_SIZE);
if(chdir(dir) != 0)
{
cvs_err("Unable to chdir to ""%s"" (error %d)\n", dir, errno);
return kFileMissing;
}
FILE *samp = fopen(arg, bin ? "rb" : "r");
if(samp == 0L)
{
return kFileMissing;
}
sBufSize = fread(sBuf, sizeof(char), MAX_TEST_SIZE, samp);
if(sBufSize < 0)
{
cvs_err("Unable to read ""%s"" (error %d)\n", arg, errno);
return kFileMissing;
}
fclose(samp);
return kFileIsOK;
}
static kTextBinTYPE TestBuffer(const char *arg, const char *dir, const UFSSpec * spec, kFileType expectType)
{
if(sBufSize == 0)
return kFileIsOK;
bool isUnicode = false;
#ifdef WIN32
isUnicode = gCvsPrefs.WhichVersion() == kWin32 ? false : IsTextUnicode(sBuf, sBufSize, NULL);
#endif /* WIN32 */
// Windows : fread skips the \r, so we don't care.
// Mac : MSL exchanges when reading the \r and \n,
// so we look for \n
// Others : default to \n
const unsigned char nativeNL = '\n';
const unsigned char nonnativeNL = '\r';
float percent = 0;
int numBinChars = 0;
int numTextChars = 0;
if( !isUnicode )
{
// figure if it is binary
int i;
unsigned char c;
char *tmp;
for(tmp = sBuf, i = 0; i < sBufSize; i++)
{
c = (unsigned char)*tmp++;
if((c < 0x20 && c != nativeNL && c != nonnativeNL && c != '\t') || (c >= 0x80
#ifdef macintosh
&& c != (unsigned char)'' && c != (unsigned char)'' /* powerplant */
#endif /* macintosh */
))
numBinChars++;
else
numTextChars++;
}
percent = (float)numBinChars / (float)sBufSize;
}
// more than 5% binary makes binary
bool isbin = percent >= 0.05;
switch( expectType )
{
case kFileTypeText:
if( isUnicode )
{
return kTextIsUnicode;
}
else if( isbin )
{
return kTextIsBinary;
}
break;
case kFileTypeBin:
if( isUnicode )
{
return kBinIsUnicode;
}
else if( !isbin )
{
return kBinIsText;
}
break;
case kFileTypeUnicode:
if( !isUnicode )
{
return isbin ? kUnicodeIsBinary : kUnicodeIsText;
}
break;
default:
#ifdef WIN32
ASSERT(FALSE); // uknown type
#endif
break;
}
// for the text files, check if the new lines are good
if(kFileTypeText == expectType)
{
int i;
unsigned char c;
char *tmp;
int numNonNative = 0, numNative = 0;
for(tmp = sBuf, i = 0; i < sBufSize; i++)
{
c = (unsigned char)*tmp++;
if(c == nonnativeNL)
numNonNative++;
else if(c == nativeNL)
numNative++;
}
if(numNative == 0)
{
if(numNonNative > 0)
return kTextWrongLF;
}
else
{
percent = (float)numNonNative / (float)numNative;
if(percent >= 0.1)
return kTextWrongLF;
}
if(numBinChars > 0)
return kTextEscapeChar;
}
return kFileIsOK;
}
#if TARGET_RT_MAC_CFM
static kTextBinTYPE GetMacSig(const char *arg, const char *dir, const UFSSpec * spec, bool expectbin)
{
CInfoPBRec info;
info.dirInfo.ioVRefNum = spec->vRefNum;
info.dirInfo.ioDrDirID = spec->parID;
info.dirInfo.ioNamePtr = (StringPtr)spec->name;
info.dirInfo.ioFDirIndex = 0;
info.dirInfo.ioACUser = 0;
OSErr err = PBGetCatInfoSync(&info);
if(err != noErr || (info.dirInfo.ioFlAttrib & 0x10)/*folder*/)
{
cvs_err("Unable to get info on ""%s"" (error %d)\n", arg, err);
return kFileMissing;
}
if(info.hFileInfo.ioFlFndrInfo.fdFlags & (1 << 15))
{
return kFileIsAlias;
}
if(expectbin)
{
if(info.hFileInfo.ioFlFndrInfo.fdType == 'TEXT')
return kBinWrongSig;
}
else
{
if(info.hFileInfo.ioFlRLgLen > 0 && info.hFileInfo.ioFlLgLen == 0)
return kTextIsBinary;
if(info.hFileInfo.ioFlFndrInfo.fdType != 'TEXT')
return kTextWrongSig;
}
return kFileIsOK;
}
#endif /* TARGET_RT_MAC_CFM */
#if TARGET_RT_MAC_MACHO
static kTextBinTYPE GetMacSig(const char *arg, const char *dir, const UFSSpec * spec, bool expectbin)
{
FSCatalogInfo info;
FInfo finfo;
OSErr err = FSGetCatalogInfo(spec, kFSCatInfoNodeFlags | kFSCatInfoFinderInfo |
kFSCatInfoDataSizes | kFSCatInfoRsrcSizes, &info, NULL, NULL, NULL);
if(err != noErr || (info.nodeFlags & kFSNodeIsDirectoryMask))
{
cvs_err("Unable to get info on ""%s"" (error %d)\n", arg, err);
return kFileMissing;
}
memcpy(&finfo, &info.finderInfo, 2 * sizeof(OSType));
if(expectbin)
{
if(finfo.fdType == 'TEXT')
return kBinWrongSig;
}
else
{
if(info.rsrcPhysicalSize > 0 && info.dataPhysicalSize == 0)
return kTextIsBinary;
if(finfo.fdType != 'TEXT' && finfo.fdType != ' ')
return kTextWrongSig;
}
return kFileIsOK;
}
#endif /* TARGET_RT_MAC_MACHO */
static kTextBinTYPE TestValidName(const char *filename)
{
kTextBinTYPE result = kFileIsOK;
if(strchr(filename, '/') != 0L)
result = kFileInvalidName;
if(strchr(filename, '\n') != 0L)
result = kFileInvalidName;
#ifdef macintosh
if(strchr(filename, '\r') != 0L)
result = kFileInvalidName;
if(strcmp(filename, ".") == 0 || strcmp(filename, "..") == 0)
result = kFileInvalidName;
if(strcmp(filename, "CVS") != 0 && stricmp(filename, "cvs") == 0)
result = kFileInvalidName;
#endif /* !macintosh */
return result;
}
static kTextBinTYPE TestFileIsAlias(const char *arg, const char *dir, const UFSSpec * spec)
{
kTextBinTYPE result = kFileIsOK;
#ifdef S_ISLNK
CPStr fullname;
struct stat sb;
fullname = dir;
if(!fullname.endsWith(kPathDelimiter))
fullname << kPathDelimiter;
fullname << arg;
if (stat(fullname, &sb) == -1)
result = kFileMissing;
else if(S_ISLNK(sb.st_mode))
result = kFileIsAlias;
#endif /* S_ISLNK */
return result;
}
kTextBinTYPE FileIsText(const char *arg, const char *dir, const UFSSpec * spec)
{
kTextBinTYPE state;
#ifdef macintosh
state = GetMacSig(arg, dir, spec, false);
if(state != kFileIsOK)
return state;
#endif /* macintosh */
state = FillBuffer(arg, dir, spec, false);
if(state != kFileIsOK)
return state;
if(sBufSize == 0)
return kFileIsOK;
state = TestBuffer(arg, dir, spec, kFileTypeText);
if(state != kFileIsOK)
return state;
state = TestValidName(arg);
if(state != kFileIsOK)
return state;
state = TestFileIsAlias(arg, dir, spec);
if(state != kFileIsOK)
return state;
return kFileIsOK;
}
kTextBinTYPE FileIsBinary(const char *arg, const char *dir, const UFSSpec * spec)
{
kTextBinTYPE state;
state = FillBuffer(arg, dir, spec, true);
if(state != kFileIsOK)
return state;
state = TestBuffer(arg, dir, spec, kFileTypeBin);
#ifdef macintosh
kTextBinTYPE sigstate = GetMacSig(arg, dir, spec, true);
if(sigstate != kFileIsOK)
return state;
if(state != kFileIsOK && state != kBinIsText)
return state;
#else /* !macintosh */
if(state != kFileIsOK)
return state;
#endif /* !macintosh */
state = TestValidName(arg);
if(state != kFileIsOK)
return state;
state = TestFileIsAlias(arg, dir, spec);
if(state != kFileIsOK)
return state;
return kFileIsOK;
}
kTextBinTYPE FileIsUnicode(const char *arg, const char *dir, const UFSSpec * spec /*= 0L*/)
{
kTextBinTYPE state;
state = FillBuffer(arg, dir, spec, true);
if(state != kFileIsOK)
return state;
state = TestBuffer(arg, dir, spec, kFileTypeUnicode);
#ifdef macintosh
kTextBinTYPE sigstate = GetMacSig(arg, dir, spec, true);
if(sigstate != kFileIsOK)
return state;
if(state != kFileIsOK && state != kBinIsText)
return state;
#else /* !macintosh */
if(state != kFileIsOK)
return state;
#endif /* !macintosh */
state = TestValidName(arg);
if(state != kFileIsOK)
return state;
state = TestFileIsAlias(arg, dir, spec);
if(state != kFileIsOK)
return state;
return kFileIsOK;
}
bool SplitPath(const char *dir, CStr & uppath, CStr & folder)
{
char newdir[512];
char olddir[512];
char newfile[256];
int lendir = strlen(dir);
strcpy(olddir, dir);
if(dir[lendir - 1] == kPathDelimiter)
{
olddir[lendir - 1] = '\0';
--lendir;
}
const char *tmp = olddir, *prev = olddir;
while((tmp = strchr(tmp, kPathDelimiter)) != 0L)
prev = ++tmp;
strcpy(newdir, olddir);
newdir[prev - olddir] = '\0';
strcpy(newfile, prev);
uppath = newdir;
folder = newfile;
return true;
}
void GetExtension(const char *file, CStr & base, CStr & ext)
{
char *tmp = strrchr(file, '.');
if(tmp == 0L)
{
ext = "";
base = file;
}
else
{
ext = tmp + 1;
base.set(file, tmp - file - 1);
}
}
bool MakeTmpFile(CStr & file, const char *prefix, const char *extension /*= 0L*/, bool create /*= false*/)
{
static int count = 0;
char filename[256];
CStr tmpPath;
CStr ext;
CStr prf;
if(extension != 0L && extension[0] != '\0')
{
if(extension[0] != '.')
ext = ".";
ext << extension;
}
if(prefix != 0L && prefix[0] != '\0')
{
prf = prefix;
}
else
prf = "cvsgui";
#ifdef WIN32
char *ptr;
ptr = getenv(_T("TMP"));
if (ptr != 0L && _access( ptr, 0 ) != -1 )
tmpPath = ptr;
else
tmpPath = ".";
#endif /* !WIN32 */
#ifdef macintosh
UFSSpec theFolder;
OSErr err;
if((err = MacGetTempFolder(theFolder, tmpPath)) != noErr)
{
cvs_err("Unable to locate the preferences folder (error %d) !\n", err);
return 0L;
}
for (;;) {
sprintf(filename, "%s%d%s", (const char *)prf, count, (const char *)ext);
if (::strlen (filename) <= 31) break;
if (prf.length () == 0) {
cvs_err("Unable to truncate temp file !\n");
return 0L;
} // if
prf.set (prf, prf.length () - 1);
} // for
#endif /* !macintosh */
#if qUnix
tmpPath = "/tmp";
#endif
struct stat sb;
if (stat(tmpPath, &sb) == -1 || !S_ISDIR(sb.st_mode))
{
cvs_err("Unable to access '%s' (error %d) !\n", (const char *)tmpPath, errno);
return false;
}
if(!tmpPath.endsWith(kPathDelimiter))
tmpPath << kPathDelimiter;
int maxtry = 10000;
while(1)
{
sprintf(filename, "%s%d%s", (const char *)prf, count++, (const char *)ext);
file = tmpPath;
file << filename;
if (stat(file, &sb) == -1 && errno == ENOENT)
{
if( create )
{
FILE* stream = fopen(file, "w");
if( stream )
{
fclose(stream);
}
}
return true;
}
if(maxtry-- == 0)
{
cvs_err("Time out to access '%s' !\n", (const char *)tmpPath);
break;
}
}
return false;
}
bool GetEnvValue(const char *cmd, const char *key, UStr & value)
{
const char *tmp = strstr(cmd, key);
if(tmp == 0L)
return false;
tmp = strchr(tmp, '=');
if(tmp == 0L)
return false;
tmp++;
const char *tmp2 = strchr(tmp, '&');
if(tmp2 == 0L)
value = tmp;
else
value.set(tmp, tmp2 - tmp);
return true;
}
int StringToArgv(const char *cmdLine, char **argv)
{
int argc = 0;
char Quote = '\0'; /* Contains the quote character if we are
inside "" or '', or \0 otherwise */
const char* walker1 = cmdLine; /* walker1 traverses the original
command line */
char* walker2; /* walker2 traverses the parsed copy of
the command line */
char theChar; /* theChar is the character we are currently
parsing */
char arguments [MAX_CMD_LEN]; /* will contain a parsed copy of the
command line */
walker2 = arguments; /* copy into arguments */
while ((theChar = *walker1++) != '\0') { /* parse command line to \0 */
if (theChar == ' ') /* skip leading space before an argument */
continue;
argv [argc++] = walker2; /* set argv to point to the space
where we copy the next argument */
if (argc > MAX_ARGS) { /* if we filled argv, skip the rest */
argc--;
break;
}
do { /* parse an argument */
if (theChar == '\\' && *walker1++) /* skip \s */
theChar = *walker1++;
else
if ((theChar == '"') || (theChar == '\'')) { /* when we find a quote... */
if (Quote == '\0') { /* if we are not inside quotes already, */
Quote = theChar; /* remember we are inside quotes now */
continue; /* go to the next character */
}
if (theChar == Quote) { /* if we are already inside that kind of quotes */
Quote = '\0'; /* close quotes */
continue; /* go to the next character */
}
}
*walker2++ = theChar; /* copy the character into args */
/* repeat until we reach the end of arguments
or we reach a space that is not inside quotes */
} while ((*walker1 != '\0') && (((theChar = *walker1++) != ' ') || (Quote != '\0')));
*walker2++ = 0; /* null-terminate the last argument */
}
return argc;
}
|