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
|
/*
* tclMacUnix.c --
*
* This file contains routines to implement several features
* available to the Unix implementation, but that require
* extra work to do on a Macintosh. These include routines
* Unix Tcl normally hands off to the Unix OS.
*
* Copyright (c) 1993-1994 Lockheed Missle & Space Company, AI Center
* Copyright (c) 1994-1996 Sun Microsystems, Inc.
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
* SCCS: @(#) tclMacUnix.c 1.56 96/12/12 19:38:08
*/
#include <Files.h>
#include <Strings.h>
#include <TextUtils.h>
#include <Finder.h>
#include <FSpCompat.h>
#include <Aliases.h>
#include <Errors.h>
#include "tclInt.h"
#include "tclMacInt.h"
/*
* The following two Includes are from the More Files package
*/
#include "FileCopy.h"
#include "MoreFiles.h"
#include "MoreFilesExtras.h"
/*
* The following may not be defined in some versions of
* MPW header files.
*/
#ifndef kIsInvisible
#define kIsInvisible 0x4000
#endif
#ifndef kIsAlias
#define kIsAlias 0x8000
#endif
/*
* Missing error codes
*/
#define usageErr 500
#define noSourceErr 501
#define isDirErr 502
/*
* Static functions in this file.
*/
static int GlobArgs _ANSI_ARGS_((Tcl_Interp *interp,
int *argc, char ***argv));
/*
*----------------------------------------------------------------------
*
* GlobArgs --
*
* The following function was taken from Peter Keleher's Alpha
* Editor. *argc should only count the end arguments that should
* be globed. argv should be incremented to point to the first
* arg to be globed.
*
* Results:
* Returns 'true' if it worked & memory was allocated, else 'false'.
*
* Side effects:
* argv will be alloced, the call will need to release the memory
*
*----------------------------------------------------------------------
*/
static int
GlobArgs(
Tcl_Interp *interp, /* Tcl interpreter. */
int *argc, /* Number of arguments. */
char ***argv) /* Argument strings. */
{
int res, len;
char *list;
/*
* Places the globbed args all into 'interp->result' as a list.
*/
res = Tcl_GlobCmd(NULL, interp, *argc + 1, *argv - 1);
if (res != TCL_OK) {
return false;
}
len = strlen(interp->result);
list = (char *) ckalloc(len + 1);
strcpy(list, interp->result);
Tcl_ResetResult(interp);
res = Tcl_SplitList(interp, list, argc, argv);
ckfree((char *) list);
if (res != TCL_OK) {
return false;
}
return true;
}
/*
*----------------------------------------------------------------------
*
* Tcl_EchoCmd --
*
* Implements the TCL echo command:
* echo ?str ...?
*
* Results:
* Always returns TCL_OK.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
int
Tcl_EchoCmd(
ClientData dummy, /* Not used. */
Tcl_Interp *interp, /* Current interpreter. */
int argc, /* Number of arguments. */
char **argv) /* Argument strings. */
{
Tcl_Channel chan;
int mode, result, i;
chan = Tcl_GetChannel(interp, "stdout", &mode);
if (chan == (Tcl_Channel) NULL) {
return TCL_ERROR;
}
for (i = 1; i < argc; i++) {
result = Tcl_Write(chan, argv[i], -1);
if (result < 0) {
Tcl_AppendResult(interp, "echo: ", Tcl_GetChannelName(chan),
": ", Tcl_PosixError(interp), (char *) NULL);
return TCL_ERROR;
}
if (i < (argc - 1)) {
Tcl_Write(chan, " ", -1);
}
}
Tcl_Write(chan, "\n", -1);
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* Tcl_LsCmd --
*
* This procedure is invoked to process the "ls" Tcl command.
* See the user documentation for details on what it does.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* See the user documentation.
*
*----------------------------------------------------------------------
*/
int
Tcl_LsCmd(
ClientData dummy, /* Not used. */
Tcl_Interp *interp, /* Current interpreter. */
int argc, /* Number of arguments. */
char **argv) /* Argument strings. */
{
#define STRING_LENGTH 80
#define CR '\n'
int i, j;
int fieldLength, len = 0, maxLen = 0, perLine;
char **origArgv = argv;
OSErr err;
CInfoPBRec paramBlock;
HFileInfo *hpb = (HFileInfo *)¶mBlock;
DirInfo *dpb = (DirInfo *)¶mBlock;
char theFile[256];
char theLine[STRING_LENGTH + 2];
int fFlag = false, pFlag = false, aFlag = false, lFlag = false,
cFlag = false, hFlag = false;
/*
* Process command flags. End if argument doesn't start
* with a dash or is a dash by itself. The remaining arguments
* should be files.
*/
for (i = 1; i < argc; i++) {
if (argv[i][0] != '-') {
break;
}
if (!strcmp(argv[i], "-")) {
i++;
break;
}
for (j = 1 ; argv[i][j] ; ++j) {
switch(argv[i][j]) {
case 'a':
case 'A':
aFlag = true;
break;
case '1':
cFlag = false;
break;
case 'C':
cFlag = true;
break;
case 'F':
fFlag = true;
break;
case 'H':
hFlag = true;
break;
case 'p':
pFlag = true;
break;
case 'l':
pFlag = false;
lFlag = true;
break;
default:
Tcl_AppendResult(interp, "error - unknown flag ",
"usage: ls -apCFHl1 ?files? ", NULL);
return TCL_ERROR;
}
}
}
argv += i;
argc -= i;
/*
* No file specifications means we search for all files.
* Glob will be doing most of the work.
*/
if (!argc) {
argc = 1;
argv = origArgv;
strcpy(argv[0], "*");
}
if (!GlobArgs(interp, &argc, &argv)) {
Tcl_ResetResult(interp);
return TCL_ERROR;
}
/*
* There are two major methods for listing files: the long
* method and the normal method.
*/
if (lFlag) {
char creator[5], type[5], time[16], date[16];
char lineTag;
long size;
unsigned short flags;
/*
* Print the header for long listing.
*/
if (hFlag) {
sprintf(theLine, "T %7s %8s %8s %4s %4s %6s %s",
"Size", "ModTime", "ModDate",
"CRTR", "TYPE", "Flags", "Name");
Tcl_AppendResult(interp, theLine, "\n", NULL);
Tcl_AppendResult(interp,
"-------------------------------------------------------------\n",
NULL);
}
for (i = 0; i < argc; i++) {
strcpy(theFile, argv[i]);
c2pstr(theFile);
hpb->ioCompletion = NULL;
hpb->ioVRefNum = 0;
hpb->ioFDirIndex = 0;
hpb->ioNamePtr = (StringPtr) theFile;
hpb->ioDirID = 0L;
err = PBGetCatInfoSync(¶mBlock);
p2cstr((StringPtr) theFile);
if (hpb->ioFlAttrib & 16) {
/*
* For directories use zero as the size, use no Creator
* type, and use 'DIR ' as the file type.
*/
if ((aFlag == false) && (dpb->ioDrUsrWds.frFlags & 0x1000)) {
continue;
}
lineTag = 'D';
size = 0;
IUTimeString(dpb->ioDrMdDat, false, (unsigned char *)time);
p2cstr((StringPtr)time);
IUDateString(dpb->ioDrMdDat, shortDate, (unsigned char *)date);
p2cstr((StringPtr)date);
strcpy(creator, " ");
strcpy(type, "DIR ");
flags = dpb->ioDrUsrWds.frFlags;
if (fFlag || pFlag) {
strcat(theFile, ":");
}
} else {
/*
* All information for files should be printed. This
* includes size, modtime, moddate, creator type, file
* type, flags, anf file name.
*/
if ((aFlag == false) &&
(hpb->ioFlFndrInfo.fdFlags & kIsInvisible)) {
continue;
}
lineTag = 'F';
size = hpb->ioFlLgLen + hpb->ioFlRLgLen;
IUTimeString(hpb->ioFlMdDat, false, (unsigned char *)time);
p2cstr((StringPtr)time);
IUDateString(hpb->ioFlMdDat, shortDate, (unsigned char *)date);
p2cstr((StringPtr)date);
strncpy(creator, (char *) &hpb->ioFlFndrInfo.fdCreator, 4);
creator[4] = 0;
strncpy(type, (char *) &hpb->ioFlFndrInfo.fdType, 4);
type[4] = 0;
flags = hpb->ioFlFndrInfo.fdFlags;
if (fFlag) {
if (hpb->ioFlFndrInfo.fdFlags & kIsAlias) {
strcat(theFile, "@");
} else if (hpb->ioFlFndrInfo.fdType == 'APPL') {
strcat(theFile, "*");
}
}
}
sprintf(theLine, "%c %7ld %8s %8s %-4.4s %-4.4s 0x%4.4X %s",
lineTag, size, time, date, creator, type, flags, theFile);
Tcl_AppendResult(interp, theLine, "\n", NULL);
}
if ((interp->result != NULL) && (*(interp->result) != '\0')) {
int slen = strlen(interp->result);
if (interp->result[slen - 1] == '\n') {
interp->result[slen - 1] = '\0';
}
}
} else {
/*
* Not in long format. We only print files names. If the
* -C flag is set we need to print in multiple coloumns.
*/
int argCount, linePos;
Boolean needNewLine = false;
/*
* Fiend the field length: the length each string printed
* to the terminal will be.
*/
if (!cFlag) {
perLine = 1;
fieldLength = STRING_LENGTH;
} else {
for (i = 0; i < argc; i++) {
len = strlen(argv[i]);
if (len > maxLen) {
maxLen = len;
}
}
fieldLength = maxLen + 3;
perLine = STRING_LENGTH / fieldLength;
}
argCount = 0;
linePos = 0;
memset(theLine, ' ', STRING_LENGTH);
while (argCount < argc) {
strcpy(theFile, argv[argCount]);
c2pstr(theFile);
hpb->ioCompletion = NULL;
hpb->ioVRefNum = 0;
hpb->ioFDirIndex = 0;
hpb->ioNamePtr = (StringPtr) theFile;
hpb->ioDirID = 0L;
err = PBGetCatInfoSync(¶mBlock);
p2cstr((StringPtr) theFile);
if (hpb->ioFlAttrib & 16) {
/*
* Directory. If -a show hidden files. If -f or -p
* denote that this is a directory.
*/
if ((aFlag == false) && (dpb->ioDrUsrWds.frFlags & 0x1000)) {
argCount++;
continue;
}
if (fFlag || pFlag) {
strcat(theFile, ":");
}
} else {
/*
* File: If -a show hidden files, if -f show links
* (aliases) and executables (APPLs).
*/
if ((aFlag == false) &&
(hpb->ioFlFndrInfo.fdFlags & kIsInvisible)) {
argCount++;
continue;
}
if (fFlag) {
if (hpb->ioFlFndrInfo.fdFlags & kIsAlias) {
strcat(theFile, "@");
} else if (hpb->ioFlFndrInfo.fdType == 'APPL') {
strcat(theFile, "*");
}
}
}
/*
* Print the item, taking into account multi-
* coloum output.
*/
strncpy(theLine + (linePos * fieldLength), theFile,
strlen(theFile));
linePos++;
if (linePos == perLine) {
theLine[STRING_LENGTH] = '\0';
if (needNewLine) {
Tcl_AppendResult(interp, "\n", theLine, NULL);
} else {
Tcl_AppendResult(interp, theLine, NULL);
needNewLine = true;
}
linePos = 0;
memset(theLine, ' ', STRING_LENGTH);
}
argCount++;
}
if (linePos != 0) {
theLine[STRING_LENGTH] = '\0';
if (needNewLine) {
Tcl_AppendResult(interp, "\n", theLine, NULL);
} else {
Tcl_AppendResult(interp, theLine, NULL);
}
}
}
ckfree((char *) argv);
return TCL_OK;
}
|