Skip to content

Instantly share code, notes, and snippets.

@skurscheid
Created February 5, 2018 23:29
Show Gist options
  • Save skurscheid/ba1f443b2925f5465c1c191d313cc0c9 to your computer and use it in GitHub Desktop.
Save skurscheid/ba1f443b2925f5465c1c191d313cc0c9 to your computer and use it in GitHub Desktop.
C function to enable bash shells to "cd" into macOS aliases
// getTrueName.c
// http://web.archive.org/web/20100110234300/http://www.macosxhints.com/dlfiles/getTrueName.txt
//
// DESCRIPTION
// Resolve HFS and HFS+ aliased files (and soft links), and return the
// name of the "Original" or actual file. Directories have a "/"
// appended. The error number returned is 255 on error, 0 if the file
// was an alias, or 1 if the argument given was not an alias
//
// BUILD INSTRUCTIONS
// gcc-3.3 -o getTrueName -framework Carbon getTrueName.c
//
// Note: gcc version 4 reports the following warning
// warning: pointer targets in passing argument 1 of 'FSPathMakeRef'
// differ in signedness
//
// COPYRIGHT AND LICENSE
// Copyright 2005 by Thos Davis. All rights reserved.
// 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 2 of the
// License, 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., 59 Temple Place, Suite 330, Boston,
// MA 02111-1307 USA
#include <Carbon/Carbon.h>
#define MAX_PATH_SIZE 1024
#define CHECK(rc,check_value) if ((check_value) != noErr) exit((rc))
int main ( int argc, char * argv[] )
{
FSRef fsRef;
Boolean targetIsFolder;
Boolean wasAliased;
UInt8 targetPath[MAX_PATH_SIZE+1];
char * marker;
// if there are no arguments, go away
if (argc < 2 ) exit(255);
CHECK( 255,
FSPathMakeRef( argv[1], &fsRef, NULL ));
CHECK( 1,
FSResolveAliasFile( &fsRef, TRUE, &targetIsFolder, &wasAliased));
CHECK( 255,
FSRefMakePath( &fsRef, targetPath, MAX_PATH_SIZE));
marker = targetIsFolder ? "/" : "" ;
printf( "%s%s\n", targetPath, marker );
exit( 1 - wasAliased );
}
@skurscheid
Copy link
Author

skurscheid commented Feb 5, 2018

Instructions from: https://superuser.com/questions/253984/mac-terminal-cd-to-a-folder-alias/254005#254005

This is a two-part process requiring a little familiarity with gcc and bash, but I’ll try to make it as simple as possible. Firstly, you need this file: getTrueName.c. This file was created by Thos Davis and is licensed under the GPLv2. Save it anywhere, then compile it with the following command:

gcc -o getTrueName -framework Carbon getTrueName.c
This will create the ‘getTrueName’ executable in the same directory as the source. You can add it to your PATH, or just copy it directly to /usr/bin so it’s easy to access.

Interestingly, when Terminal opens a new shell, .bashrc is not executed as you might expect. Instead, under the login shell, .bash_profile is executed. So, add the following to .bash_profile in your Home directory. You might need to create it first; it isn’t there by default.

cd() { if [[ -f "$1" || -L "$1" ]]; then path=$(getTrueName "$1") builtin cd "$path" else builtin cd "$@" fi }
[edited the function a bit –grawity]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment