comparison src/mac.c @ 64247:e55c855616f5

[!TARGET_API_MAC_CARBON] Include charset.h, coding.h, and Endian.h. [!MAC_OSX] (fchmod, fchown): New functions. (mac_get_code_from_arg): Don't accept Lisp integer as argument. Use SBYTES and EndianU32_BtoN. (mac_get_object_from_code): Return 4 byte string even if argument is 0. Use make_unibyte_string and EndianU32_NtoB. (Fmac_get_file_creator, Fmac_get_file_type, Fmac_set_file_creator) (Fmac_set_file_type): Fix documents and argument declarations. Don't specify kFSCatInfoNodeFlags. Support Mac OS Classic.
author YAMAMOTO Mitsuharu <mituharu@math.s.chiba-u.ac.jp>
date Tue, 12 Jul 2005 11:33:42 +0000
parents 0441edbff1bf
children a673f66d6156 fbb2bea03df9
comparison
equal deleted inserted replaced
64246:269f91810e62 64247:e55c855616f5
32 #include "sysselect.h" 32 #include "sysselect.h"
33 #include "blockinput.h" 33 #include "blockinput.h"
34 34
35 #include "macterm.h" 35 #include "macterm.h"
36 36
37 #if TARGET_API_MAC_CARBON
38 #include "charset.h" 37 #include "charset.h"
39 #include "coding.h" 38 #include "coding.h"
40 #else /* not TARGET_API_MAC_CARBON */ 39 #if !TARGET_API_MAC_CARBON
41 #include <Files.h> 40 #include <Files.h>
42 #include <MacTypes.h> 41 #include <MacTypes.h>
43 #include <TextUtils.h> 42 #include <TextUtils.h>
44 #include <Folders.h> 43 #include <Folders.h>
45 #include <Resources.h> 44 #include <Resources.h>
51 #include <Scrap.h> 50 #include <Scrap.h>
52 #include <Events.h> 51 #include <Events.h>
53 #include <Processes.h> 52 #include <Processes.h>
54 #include <EPPC.h> 53 #include <EPPC.h>
55 #include <MacLocales.h> 54 #include <MacLocales.h>
55 #include <Endian.h>
56 #endif /* not TARGET_API_MAC_CARBON */ 56 #endif /* not TARGET_API_MAC_CARBON */
57 57
58 #include <utime.h> 58 #include <utime.h>
59 #include <dirent.h> 59 #include <dirent.h>
60 #include <sys/types.h> 60 #include <sys/types.h>
1019 CFPropertyListRef plist; 1019 CFPropertyListRef plist;
1020 { 1020 {
1021 CFTypeID type_id = CFGetTypeID (plist); 1021 CFTypeID type_id = CFGetTypeID (plist);
1022 1022
1023 if (type_id == CFStringGetTypeID ()) 1023 if (type_id == CFStringGetTypeID ())
1024 return cfstring_to_lisp (plist); 1024 return cfstring_to_lisp (plist);
1025 else if (type_id == CFNumberGetTypeID ()) 1025 else if (type_id == CFNumberGetTypeID ())
1026 { 1026 {
1027 CFStringRef string; 1027 CFStringRef string;
1028 Lisp_Object result = Qnil; 1028 Lisp_Object result = Qnil;
1029 1029
2481 } 2481 }
2482 2482
2483 2483
2484 int 2484 int
2485 chmod (const char *path, mode_t mode) 2485 chmod (const char *path, mode_t mode)
2486 {
2487 /* say it always succeed for now */
2488 return 0;
2489 }
2490
2491
2492 int
2493 fchmod (int fd, mode_t mode)
2494 {
2495 /* say it always succeed for now */
2496 return 0;
2497 }
2498
2499
2500 int
2501 fchown (int fd, uid_t owner, gid_t group)
2486 { 2502 {
2487 /* say it always succeed for now */ 2503 /* say it always succeed for now */
2488 return 0; 2504 return 0;
2489 } 2505 }
2490 2506
3386 as_script_context = kOSANullScript; 3402 as_script_context = kOSANullScript;
3387 /* use default context if create fails */ 3403 /* use default context if create fails */
3388 } 3404 }
3389 3405
3390 3406
3391 void terminate_applescript() 3407 void
3408 terminate_applescript()
3392 { 3409 {
3393 OSADispose (as_scripting_component, as_script_context); 3410 OSADispose (as_scripting_component, as_script_context);
3394 CloseComponent (as_scripting_component); 3411 CloseComponent (as_scripting_component);
3395 } 3412 }
3396 3413
3397 /* Convert a lisp string or integer to the 4 byte character code 3414 /* Convert a lisp string to the 4 byte character code. */
3398 */
3399 3415
3400 OSType mac_get_code_from_arg(Lisp_Object arg, OSType defCode) 3416 OSType
3417 mac_get_code_from_arg(Lisp_Object arg, OSType defCode)
3401 { 3418 {
3402 OSType result; 3419 OSType result;
3403 if (NILP(arg)) 3420 if (NILP(arg))
3404 { 3421 {
3405 result = defCode; 3422 result = defCode;
3406 } 3423 }
3407 else if (INTEGERP(arg))
3408 {
3409 result = XFASTINT(arg);
3410 }
3411 else 3424 else
3412 { 3425 {
3413 /* check type string */ 3426 /* check type string */
3414 CHECK_STRING(arg); 3427 CHECK_STRING(arg);
3415 if (strlen(SDATA(arg)) != 4) 3428 if (SBYTES (arg) != 4)
3416 { 3429 {
3417 error ("Wrong argument: need string of length 4 for code"); 3430 error ("Wrong argument: need string of length 4 for code");
3418 } 3431 }
3419 /* Should work cross-endian */ 3432 result = EndianU32_BtoN (*((UInt32 *) SDATA (arg)));
3420 result = SDATA(arg)[3] + (SDATA(arg)[2] << 8) +
3421 (SDATA(arg)[1] << 16) + (SDATA(arg)[0] << 24);
3422 } 3433 }
3423 return result; 3434 return result;
3424 } 3435 }
3425 3436
3426 /** 3437 /* Convert the 4 byte character code into a 4 byte string. */
3427 Convert the 4 byte character code into a 4 byte string 3438
3428 */ 3439 Lisp_Object
3429 Lisp_Object mac_get_object_from_code(OSType defCode) 3440 mac_get_object_from_code(OSType defCode)
3430 { 3441 {
3431 if (defCode == 0) { 3442 UInt32 code = EndianU32_NtoB (defCode);
3432 return make_specified_string("", -1, 0, 0); 3443
3433 } else { 3444 return make_unibyte_string ((char *)&code, 4);
3434 /* Should work cross-endian */
3435 char code[4];
3436 code[0] = defCode >> 24 & 0xff;
3437 code[1] = defCode >> 16 & 0xff;
3438 code[2] = defCode >> 8 & 0xff;
3439 code[3] = defCode & 0xff;
3440 return make_specified_string(code, -1, 4, 0);
3441 }
3442 } 3445 }
3443 3446
3444 3447
3445 DEFUN ("mac-get-file-creator", Fmac_get_file_creator, Smac_get_file_creator, 1, 1, 0, 3448 DEFUN ("mac-get-file-creator", Fmac_get_file_creator, Smac_get_file_creator, 1, 1, 0,
3446 doc: /* Get the creator code of FILENAME as a four character string. */) 3449 doc: /* Get the creator code of FILENAME as a four character string. */)
3447 (Lisp_Object filename) 3450 (filename)
3451 Lisp_Object filename;
3448 { 3452 {
3449 OSErr status; 3453 OSErr status;
3450 FSRef defLoc; 3454 #ifdef MAC_OSX
3455 FSRef fref;
3456 #else
3457 FSSpec fss;
3458 #endif
3451 OSType cCode; 3459 OSType cCode;
3452 Lisp_Object result = Qnil; 3460 Lisp_Object result = Qnil;
3453 CHECK_STRING (filename); 3461 CHECK_STRING (filename);
3454 3462
3455 if (NILP(Ffile_exists_p(filename)) || !NILP(Ffile_directory_p(filename))) { 3463 if (NILP(Ffile_exists_p(filename)) || !NILP(Ffile_directory_p(filename))) {
3456 return Qnil; 3464 return Qnil;
3457 } 3465 }
3458 filename = Fexpand_file_name (filename, Qnil); 3466 filename = Fexpand_file_name (filename, Qnil);
3459 3467
3460 BLOCK_INPUT; 3468 BLOCK_INPUT;
3461 status = FSPathMakeRef(SDATA(ENCODE_FILE(filename)), &defLoc, NULL); 3469 #ifdef MAC_OSX
3470 status = FSPathMakeRef(SDATA(ENCODE_FILE(filename)), &fref, NULL);
3471 #else
3472 status = posix_pathname_to_fsspec (SDATA (ENCODE_FILE (filename)), &fss);
3473 #endif
3462 3474
3463 if (status == noErr) 3475 if (status == noErr)
3464 { 3476 {
3477 #ifdef MAC_OSX
3465 FSCatalogInfo catalogInfo; 3478 FSCatalogInfo catalogInfo;
3466 FSRef parentDir; 3479
3467 status = FSGetCatalogInfo(&defLoc, kFSCatInfoNodeFlags + kFSCatInfoFinderInfo, 3480 status = FSGetCatalogInfo(&fref, kFSCatInfoFinderInfo,
3468 &catalogInfo, NULL, NULL, &parentDir); 3481 &catalogInfo, NULL, NULL, NULL);
3482 #else
3483 FInfo finder_info;
3484
3485 status = FSpGetFInfo (&fss, &finder_info);
3486 #endif
3469 if (status == noErr) 3487 if (status == noErr)
3470 { 3488 {
3489 #ifdef MAC_OSX
3471 result = mac_get_object_from_code(((FileInfo*)&catalogInfo.finderInfo)->fileCreator); 3490 result = mac_get_object_from_code(((FileInfo*)&catalogInfo.finderInfo)->fileCreator);
3491 #else
3492 result = mac_get_object_from_code (finder_info.fdCreator);
3493 #endif
3472 } 3494 }
3473 } 3495 }
3474 UNBLOCK_INPUT; 3496 UNBLOCK_INPUT;
3475 if (status != noErr) { 3497 if (status != noErr) {
3476 error ("Error while getting file information."); 3498 error ("Error while getting file information.");
3478 return result; 3500 return result;
3479 } 3501 }
3480 3502
3481 DEFUN ("mac-get-file-type", Fmac_get_file_type, Smac_get_file_type, 1, 1, 0, 3503 DEFUN ("mac-get-file-type", Fmac_get_file_type, Smac_get_file_type, 1, 1, 0,
3482 doc: /* Get the type code of FILENAME as a four character string. */) 3504 doc: /* Get the type code of FILENAME as a four character string. */)
3483 (Lisp_Object filename) 3505 (filename)
3506 Lisp_Object filename;
3484 { 3507 {
3485 OSErr status; 3508 OSErr status;
3486 FSRef defLoc; 3509 #ifdef MAC_OSX
3510 FSRef fref;
3511 #else
3512 FSSpec fss;
3513 #endif
3487 OSType cCode; 3514 OSType cCode;
3488 Lisp_Object result = Qnil; 3515 Lisp_Object result = Qnil;
3489 CHECK_STRING (filename); 3516 CHECK_STRING (filename);
3490 3517
3491 if (NILP(Ffile_exists_p(filename)) || !NILP(Ffile_directory_p(filename))) { 3518 if (NILP(Ffile_exists_p(filename)) || !NILP(Ffile_directory_p(filename))) {
3492 return Qnil; 3519 return Qnil;
3493 } 3520 }
3494 filename = Fexpand_file_name (filename, Qnil); 3521 filename = Fexpand_file_name (filename, Qnil);
3495 3522
3496 BLOCK_INPUT; 3523 BLOCK_INPUT;
3497 status = FSPathMakeRef(SDATA(ENCODE_FILE(filename)), &defLoc, NULL); 3524 #ifdef MAC_OSX
3525 status = FSPathMakeRef(SDATA(ENCODE_FILE(filename)), &fref, NULL);
3526 #else
3527 status = posix_pathname_to_fsspec (SDATA (ENCODE_FILE (filename)), &fss);
3528 #endif
3498 3529
3499 if (status == noErr) 3530 if (status == noErr)
3500 { 3531 {
3532 #ifdef MAC_OSX
3501 FSCatalogInfo catalogInfo; 3533 FSCatalogInfo catalogInfo;
3502 FSRef parentDir; 3534
3503 status = FSGetCatalogInfo(&defLoc, kFSCatInfoNodeFlags + kFSCatInfoFinderInfo, 3535 status = FSGetCatalogInfo(&fref, kFSCatInfoFinderInfo,
3504 &catalogInfo, NULL, NULL, &parentDir); 3536 &catalogInfo, NULL, NULL, NULL);
3537 #else
3538 FInfo finder_info;
3539
3540 status = FSpGetFInfo (&fss, &finder_info);
3541 #endif
3505 if (status == noErr) 3542 if (status == noErr)
3506 { 3543 {
3544 #ifdef MAC_OSX
3507 result = mac_get_object_from_code(((FileInfo*)&catalogInfo.finderInfo)->fileType); 3545 result = mac_get_object_from_code(((FileInfo*)&catalogInfo.finderInfo)->fileType);
3546 #else
3547 result = mac_get_object_from_code (finder_info.fdType);
3548 #endif
3508 } 3549 }
3509 } 3550 }
3510 UNBLOCK_INPUT; 3551 UNBLOCK_INPUT;
3511 if (status != noErr) { 3552 if (status != noErr) {
3512 error ("Error while getting file information."); 3553 error ("Error while getting file information.");
3514 return result; 3555 return result;
3515 } 3556 }
3516 3557
3517 DEFUN ("mac-set-file-creator", Fmac_set_file_creator, Smac_set_file_creator, 1, 2, 0, 3558 DEFUN ("mac-set-file-creator", Fmac_set_file_creator, Smac_set_file_creator, 1, 2, 0,
3518 doc: /* Set creator code of file FILENAME to CODE. 3559 doc: /* Set creator code of file FILENAME to CODE.
3519 If non-nil, CODE must be a 32-bit integer or a 4-character string. Otherwise, 3560 If non-nil, CODE must be a 4-character string. Otherwise, 'EMAx' is
3520 'EMAx' is assumed. Return non-nil if successful. 3561 assumed. Return non-nil if successful. */)
3521 */) 3562 (filename, code)
3522 (Lisp_Object filename, Lisp_Object code) 3563 Lisp_Object filename, code;
3523 { 3564 {
3524 OSErr status; 3565 OSErr status;
3525 FSRef defLoc; 3566 #ifdef MAC_OSX
3567 FSRef fref;
3568 #else
3569 FSSpec fss;
3570 #endif
3526 OSType cCode; 3571 OSType cCode;
3527 CHECK_STRING (filename); 3572 CHECK_STRING (filename);
3528 3573
3529 cCode = mac_get_code_from_arg(code, 'EMAx'); 3574 cCode = mac_get_code_from_arg(code, 'EMAx');
3530 3575
3532 return Qnil; 3577 return Qnil;
3533 } 3578 }
3534 filename = Fexpand_file_name (filename, Qnil); 3579 filename = Fexpand_file_name (filename, Qnil);
3535 3580
3536 BLOCK_INPUT; 3581 BLOCK_INPUT;
3537 status = FSPathMakeRef(SDATA(ENCODE_FILE(filename)), &defLoc, NULL); 3582 #ifdef MAC_OSX
3583 status = FSPathMakeRef(SDATA(ENCODE_FILE(filename)), &fref, NULL);
3584 #else
3585 status = posix_pathname_to_fsspec (SDATA (ENCODE_FILE (filename)), &fss);
3586 #endif
3538 3587
3539 if (status == noErr) 3588 if (status == noErr)
3540 { 3589 {
3590 #ifdef MAC_OSX
3541 FSCatalogInfo catalogInfo; 3591 FSCatalogInfo catalogInfo;
3542 FSRef parentDir; 3592 FSRef parentDir;
3543 status = FSGetCatalogInfo(&defLoc, kFSCatInfoNodeFlags + kFSCatInfoFinderInfo, 3593 status = FSGetCatalogInfo(&fref, kFSCatInfoFinderInfo,
3544 &catalogInfo, NULL, NULL, &parentDir); 3594 &catalogInfo, NULL, NULL, &parentDir);
3595 #else
3596 FInfo finder_info;
3597
3598 status = FSpGetFInfo (&fss, &finder_info);
3599 #endif
3545 if (status == noErr) 3600 if (status == noErr)
3546 { 3601 {
3602 #ifdef MAC_OSX
3547 ((FileInfo*)&catalogInfo.finderInfo)->fileCreator = cCode; 3603 ((FileInfo*)&catalogInfo.finderInfo)->fileCreator = cCode;
3548 status = FSSetCatalogInfo(&defLoc, kFSCatInfoFinderInfo, &catalogInfo); 3604 status = FSSetCatalogInfo(&fref, kFSCatInfoFinderInfo, &catalogInfo);
3549 /* TODO: on Mac OS 10.2, we need to touch the parent dir, FNNotify? */ 3605 /* TODO: on Mac OS 10.2, we need to touch the parent dir, FNNotify? */
3606 #else
3607 finder_info.fdCreator = cCode;
3608 status = FSpSetFInfo (&fss, &finder_info);
3609 #endif
3550 } 3610 }
3551 } 3611 }
3552 UNBLOCK_INPUT; 3612 UNBLOCK_INPUT;
3553 if (status != noErr) { 3613 if (status != noErr) {
3554 error ("Error while setting creator information."); 3614 error ("Error while setting creator information.");
3556 return Qt; 3616 return Qt;
3557 } 3617 }
3558 3618
3559 DEFUN ("mac-set-file-type", Fmac_set_file_type, Smac_set_file_type, 2, 2, 0, 3619 DEFUN ("mac-set-file-type", Fmac_set_file_type, Smac_set_file_type, 2, 2, 0,
3560 doc: /* Set file code of file FILENAME to CODE. 3620 doc: /* Set file code of file FILENAME to CODE.
3561 CODE must be a 32-bit integer or a 4-character string. Return non-nil if successful. 3621 CODE must be a 4-character string. Return non-nil if successful. */)
3562 */)
3563 (filename, code) 3622 (filename, code)
3564 Lisp_Object filename; 3623 Lisp_Object filename, code;
3565 Lisp_Object code;
3566 { 3624 {
3567 OSErr status; 3625 OSErr status;
3568 FSRef defLoc; 3626 #ifdef MAC_OSX
3627 FSRef fref;
3628 #else
3629 FSSpec fss;
3630 #endif
3569 OSType cCode; 3631 OSType cCode;
3570 CHECK_STRING (filename); 3632 CHECK_STRING (filename);
3571 3633
3572 cCode = mac_get_code_from_arg(code, 0); /* Default to empty code*/ 3634 cCode = mac_get_code_from_arg(code, 0); /* Default to empty code*/
3573 3635
3575 return Qnil; 3637 return Qnil;
3576 } 3638 }
3577 filename = Fexpand_file_name (filename, Qnil); 3639 filename = Fexpand_file_name (filename, Qnil);
3578 3640
3579 BLOCK_INPUT; 3641 BLOCK_INPUT;
3580 status = FSPathMakeRef(SDATA(ENCODE_FILE(filename)), &defLoc, NULL); 3642 #ifdef MAC_OSX
3643 status = FSPathMakeRef(SDATA(ENCODE_FILE(filename)), &fref, NULL);
3644 #else
3645 status = posix_pathname_to_fsspec (SDATA (ENCODE_FILE (filename)), &fss);
3646 #endif
3581 3647
3582 if (status == noErr) 3648 if (status == noErr)
3583 { 3649 {
3650 #ifdef MAC_OSX
3584 FSCatalogInfo catalogInfo; 3651 FSCatalogInfo catalogInfo;
3585 FSRef parentDir; 3652 FSRef parentDir;
3586 status = FSGetCatalogInfo(&defLoc, kFSCatInfoNodeFlags + kFSCatInfoFinderInfo, 3653 status = FSGetCatalogInfo(&fref, kFSCatInfoFinderInfo,
3587 &catalogInfo, NULL, NULL, &parentDir); 3654 &catalogInfo, NULL, NULL, &parentDir);
3655 #else
3656 FInfo finder_info;
3657
3658 status = FSpGetFInfo (&fss, &finder_info);
3659 #endif
3588 if (status == noErr) 3660 if (status == noErr)
3589 { 3661 {
3662 #ifdef MAC_OSX
3590 ((FileInfo*)&catalogInfo.finderInfo)->fileType = cCode; 3663 ((FileInfo*)&catalogInfo.finderInfo)->fileType = cCode;
3591 status = FSSetCatalogInfo(&defLoc, kFSCatInfoFinderInfo, &catalogInfo); 3664 status = FSSetCatalogInfo(&fref, kFSCatInfoFinderInfo, &catalogInfo);
3592 /* TODO: on Mac OS 10.2, we need to touch the parent dir, FNNotify? */ 3665 /* TODO: on Mac OS 10.2, we need to touch the parent dir, FNNotify? */
3666 #else
3667 finder_info.fdType = cCode;
3668 status = FSpSetFInfo (&fss, &finder_info);
3669 #endif
3593 } 3670 }
3594 } 3671 }
3595 UNBLOCK_INPUT; 3672 UNBLOCK_INPUT;
3596 if (status != noErr) { 3673 if (status != noErr) {
3597 error ("Error while setting creator information."); 3674 error ("Error while setting creator information.");