Exemple #1
0
ALERROR WriteModuleImages (CXMLElement *pModule, const CString &sFolder, CSymbolTable &Resources, CDataFile &Out)
	{
	ALERROR error;
	int i;

	for (i = 0; i < pModule->GetContentElementCount(); i++)
		{
		CXMLElement *pItem = pModule->GetContentElement(i);
		if (strEquals(pItem->GetTag(), TAG_IMAGES))
			{
			CString sSubFolder = pathAddComponent(sFolder, pItem->GetAttribute(ATTRIB_FOLDER));

			if (error = WriteModuleImages(pItem, sSubFolder, Resources, Out))
				return error;
			}
		else if (strEquals(pItem->GetTag(), TAG_IMAGE))
			{
			CString sFilename = pItem->GetAttribute(ATTRIB_BITMAP);
			if (!sFilename.IsBlank())
				{
				if (error = WriteResource(sFilename, sFolder, Resources, Out))
					continue;
				}

			sFilename = pItem->GetAttribute(ATTRIB_BITMASK);
			if (!sFilename.IsBlank())
				{
				if (error = WriteResource(sFilename, sFolder, Resources, Out))
					continue;
				}
			}
		}

	return NOERROR;
	}
OSErr find_exec_spec(FSSpec *result, FSSpec *myself)
{
  OSErr err;
  CInfoPBRec pbrec;
  short find_position = 0;
  AliasHandle execAlias;
  Boolean aliasChanged;

  // first we try the stored alias.
  
  execAlias = (AliasHandle)GetResource('alis',128);
  
  if (execAlias != NULL) {
    
    err = ResolveAlias(NULL,execAlias,result,&aliasChanged);
  
    if (err == noErr) {
      
      if (aliasChanged) { // need to write it back to disk
        WriteResource((Handle)execAlias);
      }
       	
      return noErr;
    }
  }
    
  // if that doesn't work, drop back and look for the file in the current directory.

  result->vRefNum = myself->vRefNum;
  result->parID = myself->parID;
  
  while (1) {
    pbrec.hFileInfo.ioVRefNum = result->vRefNum;
    pbrec.hFileInfo.ioDirID = result->parID;
    pbrec.hFileInfo.ioFDirIndex = find_position + 1;
    pbrec.hFileInfo.ioNamePtr = result->name;
    if (PBGetCatInfo(&pbrec, 0) || !*result->name)
      break;
    
    find_position++;

    if (pbrec.hFileInfo.ioFlFndrInfo.fdType == 'APPL') {
      if (pbrec.hFileInfo.ioFlFndrInfo.fdCreator == DEST_CREATOR) { 
        // found it!
        Str255 newName = "\pExecutable Location";
        
	if (NewAlias(NULL,result,&execAlias) == noErr) {
	  AddResource((Handle)execAlias,'alis',128,newName);
	  WriteResource((Handle)execAlias);
	} 

	return noErr;
      }
		
    }
  }
Exemple #3
0
ALERROR WriteModuleImages (CTDBCompiler &Ctx, CXMLElement *pModule, const CString &sFolder, CDataFile &Out)
	{
	ALERROR error;
	int i;

	for (i = 0; i < pModule->GetContentElementCount(); i++)
		{
		CXMLElement *pItem = pModule->GetContentElement(i);
		if (strEquals(pItem->GetTag(), TAG_IMAGES))
			{
			CString sSubFolder = pathAddComponent(sFolder, pItem->GetAttribute(ATTRIB_FOLDER));

			if (error = WriteModuleImages(Ctx, pItem, sSubFolder, Out))
				return error;
			}
		else if (strEquals(pItem->GetTag(), TAG_IMAGE))
			{
			CString sFilename = pItem->GetAttribute(ATTRIB_BITMAP);
			if (!sFilename.IsBlank())
				{
				bool bCompress = strEquals(strToLower(pathGetExtension(sFilename)), CONSTLIT("bmp"));
				if (error = WriteResource(Ctx, sFilename, sFolder, bCompress, Out))
					continue;
				}

			sFilename = pItem->GetAttribute(ATTRIB_BITMASK);
			if (!sFilename.IsBlank())
				{
				bool bCompress = strEquals(strToLower(pathGetExtension(sFilename)), CONSTLIT("bmp"));
				if (error = WriteResource(Ctx, sFilename, sFolder, bCompress, Out))
					continue;
				}

			sFilename = pItem->GetAttribute(ATTRIB_SHADOW_MASK);
			if (!sFilename.IsBlank())
				{
				bool bCompress = strEquals(strToLower(pathGetExtension(sFilename)), CONSTLIT("bmp"));
				if (error = WriteResource(Ctx, sFilename, sFolder, bCompress, Out))
					continue;
				}

			sFilename = pItem->GetAttribute(ATTRIB_HIT_MASK);
			if (!sFilename.IsBlank())
				{
				bool bCompress = strEquals(strToLower(pathGetExtension(sFilename)), CONSTLIT("bmp"));
				if (error = WriteResource(Ctx, sFilename, sFolder, bCompress, Out))
					continue;
				}
			}
		}

	return NOERROR;
	}
Exemple #4
0
static NTSTATUS ExtractHelperProgram(PWSTR FileName)
{
    HANDLE Handle;
    ULONG BytesTransferred;
    NTSTATUS Result;

    Handle = CreateFileW(FileName,
        FILE_WRITE_DATA, FILE_SHARE_WRITE, 0,
        CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
    if (INVALID_HANDLE_VALUE == Handle)
        return FspNtStatusFromWin32(GetLastError());

    Result = WriteResource(
        Handle,
        0,
#if defined(_WIN64)
        L"winfsp-tests-helper-x64.exe",
#elif defined(_WIN32)
        L"winfsp-tests-helper-x86.exe",
#else
#error
#endif
        &BytesTransferred);

    CloseHandle(Handle);

    return Result;
}
void WriteThumbnailToResourceFork(FSRef *ref, int destWidth, int destHeight)
{
	OSStatus		err;
	PicHandle		pict;
	HFSUniStr255	fork;
	SInt16			resf;
	
	err = FSGetResourceForkName(&fork);
	if (err == noErr)
	{
		err = FSCreateResourceFork(ref, fork.length, fork.unicode, 0);
		if ((err == noErr) || (err == errFSForkExists))
		{
			err = FSOpenResourceFile(ref, fork.length, fork.unicode, fsWrPerm, &resf);
			if (err == noErr)
			{
				pict = GetScreenAsPicHandle(IPPU.RenderedScreenWidth, IPPU.RenderedScreenHeight, destWidth, destHeight);
				if (pict)
				{
					AddResource((Handle) pict, 'PICT', 128, "\p");
					WriteResource((Handle) pict);
					ReleaseResource((Handle) pict);
				}
				
				CloseResFile(resf);
			}
VError VMacResFile::AddResource(const VString& inType, const void* inData, sLONG inDataLen, const VString* inName, sLONG* outID)
{
	VError error = VE_OK;

	*outID = 0;

	if (!testAssert(fRefNum != -1))
		return VE_STREAM_NOT_OPENED;
	
	ResType type = inType.GetOsType();
	if (type == 0)
		return VE_INVALID_PARAMETER;

	// test if the res file is not read-only
	if (!testAssert(!fReadOnly))
		return VE_ACCESS_DENIED;
	
	sWORD	curres = ::CurResFile();
	::UseResFile(fRefNum);
	
	sWORD	id = ::Unique1ID(type);
	OSErr macError = ::ResError();
	if (testAssert(macError == noErr))
	{
		error = WriteResource(inType, (sLONG) id, inData, inDataLen, inName);
		if (error == VE_OK)
			*outID = (sLONG) id;
	} else
		error = VErrorBase::NativeErrorToVError((VNativeError)macError);

	::UseResFile(curres);
	
	return error;
}
Exemple #7
0
ALERROR WriteModuleSounds (CTDBCompiler &Ctx, CXMLElement *pModule, const CString &sFolder, CDataFile &Out)
	{
	ALERROR error;
	int i;

	for (i = 0; i < pModule->GetContentElementCount(); i++)
		{
		CXMLElement *pItem = pModule->GetContentElement(i);
		if (strEquals(pItem->GetTag(), TAG_SOUNDS))
			{
			CString sSubFolder = pathAddComponent(sFolder, pItem->GetAttribute(ATTRIB_FOLDER));

			if (error = WriteModuleSounds(Ctx, pItem, sSubFolder, Out))
				return error;
			}
		else if (strEquals(pItem->GetTag(), TAG_SOUND))
			{
			CString sFilename = pItem->GetAttribute(ATTRIB_FILENAME);
			if (error = WriteResource(Ctx, sFilename, sFolder, false, Out))
				continue;
			}
		}

	return NOERROR;
	}
Exemple #8
0
void dump_colors(
	struct rgb_color_value *colors, 
	short color_count)
{
	CTabHandle new_table;
	Handle old_bad_clut;
	struct rgb_color_value *color;
	short loop;
	FSSpec file;
	short refnum;
	
	file.vRefNum= -1;
	file.parID= 2;
	strcpy((char *)file.name, (const char *)"\pMarathon2 Clut\0");

	FSpCreateResFile(&file, 'RSED', 'rsrc', smSystemScript);
	refnum= FSpOpenResFile(&file, fsWrPerm);
	if(refnum>=0)
	{
		new_table= (CTabHandle) NewHandleClear(sizeof(ColorTable)+color_count*sizeof(ColorSpec));
		HLock((Handle) new_table);
		(*new_table)->ctSeed= GetCTSeed();
		(*new_table)->ctFlags= 0;
		(*new_table)->ctSize= color_count-1;
		
		/* Slam the colors.. */
		color= colors;
		for(loop=0; loop<=color_count; ++loop)
		{
			(*new_table)->ctTable[loop].rgb.red= color->red;
			(*new_table)->ctTable[loop].rgb.green= color->green;
			(*new_table)->ctTable[loop].rgb.blue= color->blue;
			(*new_table)->ctTable[loop].value= loop;
			color++;
		}
		HUnlock((Handle) new_table);
	
		old_bad_clut= GetResource('clut', 5454);
		if (old_bad_clut)
		{
			RmveResource((Handle) old_bad_clut);
			DisposeHandle((Handle) old_bad_clut);
			UpdateResFile(CurResFile());
		}
		
		AddResource((Handle) new_table, 'clut', 5454, "\pMarathon2 Color Table");
		if(ResError()) dprintf("Err adding it: %d", ResError());
		WriteResource((Handle) new_table);
		ReleaseResource((Handle) new_table);
		
		CloseResFile(refnum);
	}
Exemple #9
0
/* Copy a resource into the current resource file */
static OSErr copy_resource(ResType restype, short resid)
{
    Handle h;
    Str255 resname;

    h = GetResource(restype, resid);
    if (h != NULL) {
	GetResInfo(h, &resid, &restype, resname);
	DetachResource(h);
	AddResource(h, restype, resid, resname);
	if (ResError() == noErr)
	    WriteResource(h);
    }
    return ResError();
}
Exemple #10
0
void stdXmlReader::SaveFile( const std::string& file , stdObject* resource )
{
	m_doc = xmlNewDoc((const xmlChar*) "1.0");
	
	if( m_doc == NULL )
		STD_THROW("Error cannot open document!");
	
	m_root = WriteResource( resource );
	
	if( m_root == NULL )
		STD_THROW("Error cannot get your document root !");
		
	xmlDocSetRootElement(m_doc,m_root);
	xmlSaveFormatFileEnc( file.c_str() , m_doc , "UTF-8" , 1 );
	
	xmlFreeDoc(m_doc);
}
Exemple #11
0
static PyObject *ResObj_WriteResource(ResourceObject *_self, PyObject *_args)
{
    PyObject *_res = NULL;
#ifndef WriteResource
    PyMac_PRECHECK(WriteResource);
#endif
    if (!PyArg_ParseTuple(_args, ""))
        return NULL;
    WriteResource(_self->ob_itself);
    {
        OSErr _err = ResError();
        if (_err != noErr) return PyMac_Error(_err);
    }
    Py_INCREF(Py_None);
    _res = Py_None;
    return _res;
}
Exemple #12
0
std::string stdXmlReader::StringSerialize( stdObject* resource ) {
	
    m_doc = xmlNewDoc((const xmlChar*) "1.0");
	
	if( m_doc == NULL )
		STD_THROW("Error cannot open document!");
	
	m_root = WriteResource( resource );
	
	if( m_root == NULL )
		STD_THROW("Error cannot get your document root !");
		
	xmlDocSetRootElement(m_doc,m_root);
    xmlChar* data;
    int size;
    xmlDocDumpMemory(m_doc,&data,&size);
    std::string ret = (char*) data;
	xmlFreeDoc(m_doc);
    xmlFree(data);

    return ret;
}
Exemple #13
0
// pass resFile as -1 to use the current file
OSErr BetterAddResource(short resFile,Handle theData,ResType type,short *id,StringPtr name,short mode)
{
	short		oldResFile=::CurResFile();
	OSErr		err=noErr;
	
	if (resFile!=-1)
		UseResFile(resFile);
	// If this resource exists then we may have some problems
	if (ResourceExists(-1,type,*id))
	{
		switch (mode)
		{
			case kOverwriteResource:
				if (!DeleteResource(-1,type,*id))
					err=rmvResFailed;
				break;
				
			case kUniqueID:
				*id=Unique1ID(type);
				break;
				
			case kBottleIt:
				err=userCanceledErr;
				break;
		}
	}
	
	if (!err)
	{
		// Now there is space for the resource lets add it
		AddResource(theData,type,*id,name ? name:"\p");
		err=ResError();
		if (err==noErr)
		{
			WriteResource(theData);
			err=ResError();
		}
	}
Exemple #14
0
OSErr CreateToolNameRes(StringPtr toolName) {
	short			ref;
	FSSpec		prefs;
	OSErr			error;
	Handle		theStrings;

	//	build the 'STR ' resource

	error = FindPrefs(&prefs);
	if (error == noErr) {
		ref = FSpOpenResFile(&prefs, fsRdWrPerm);
		if (ref != -1) {
			theStrings = Get1Resource('STR ',kToolNameRes);
			if (theStrings != nil) {
				RemoveResource(theStrings);
				error = ResError();
			}

			error = PtrToHand(toolName, &theStrings, toolName[0] + 1);

			AddResource(theStrings, 'STR ', kToolNameRes, "\ptoolName");
			error = ResError();

			ChangedResource(theStrings);
			error = ResError();

			WriteResource(theStrings);
			error = ResError();

			//ReleaseResource(theStrings);		// Release the resource from memory.
			//error = ResError();
			UpdateResFile(ref);						// No screwing around; write that resource!
			error = ResError();
			FlushVol(nil, prefs.vRefNum);

			CloseResFile(ref);
		} else {
Exemple #15
0
OSErr PAS_decodeResource(PASEntry *entry, FSSpec *outFile, short inRefNum)
{
	OSErr 			err = noErr;	
	short			outRefNum;
	PASResFork		info;
	SInt32			infoSize;
	short			oldResFile;
	
	PASResource		pasRes;
	SInt32			pasResSize;
	
	long			bufSize;
	Handle			buffer;
	long			counter=0;
	
	infoSize	=	sizeof(PASResFork);
	
	err = SetFPos(inRefNum, fsFromStart, (*entry).entryOffset );
	if (err != noErr)	return err;

	err	= FSRead( inRefNum, &infoSize, &info);
	if (err != noErr)	return err;

	if(infoSize != sizeof(PASResFork))
	{
		err = -1;
		goto error;
	}
	
	oldResFile=CurResFile();
	
	outRefNum = FSpOpenResFile(outFile, fsRdWrPerm);
	if (outRefNum < noErr)	return outRefNum;
				
	UseResFile(outRefNum);
	
	
	while (1)
	{
		pasResSize	=	sizeof(PASResource);
		err	= FSRead( inRefNum, &pasResSize, &pasRes);
		
		if (err != noErr)
		{
			if(err == eofErr)
				err = noErr;
				
			break;
		}
		
		bufSize	=	pasRes.length;
		buffer	=	NewHandle(bufSize);
		HLock(buffer);
		
		if(buffer == NULL)
		{
			/*  if we did not get our memory, try updateresfile */
		
			HUnlock(buffer);


			UpdateResFile(outRefNum);
			counter=0;
			
			buffer	=	NewHandle(bufSize);
			HLock(buffer);
			
			if(buffer == NULL)
			{
				err = memFullErr;
				break;
			}
		}
		
		err	= FSRead( inRefNum, &bufSize, &(**buffer));
		if (err != noErr && err != eofErr)	break;
		
		AddResource(buffer, pasRes.attrType, pasRes.attrID, pasRes.attrName);
		WriteResource(buffer);
		
		SetResAttrs(buffer, pasRes.attr);
		ChangedResource(buffer);	
		WriteResource(buffer);
		
		ReleaseResource(buffer);	
		
		if (counter++ > 100)
		{
			UpdateResFile(outRefNum);
			counter=0;
		}
	
	}

error:
		
	UseResFile(oldResFile);	
	CloseResFile(outRefNum);
		
	return err;
}
Exemple #16
0
int DoHostListDialog()
	{
		short itemHit,okay=FALSE,keepGoing=TRUE;
		DialogPtr dlog=NIL; GrafPtr oldPort;
		ModalFilterUPP MyFilterUPP;

		GetPort(&oldPort);

		/* On PowerPC, need a RoutineDescriptor from heap; on 68K, no allocation */
		
		MyFilterUPP = NewModalFilterProc(MyFilterHD);
		if (MyFilterUPP == NIL) goto cleanUp;

		/* Build dialog window and install its item values */
		
		OpenPrefsFile();
		
		dlog = OpenThisDialog();
		if (dlog == NIL) goto cleanUp;

		/* Entertain filtered user events until dialog is dismissed */
		
		while (keepGoing) {
			ModalDialog(MyFilterUPP,&itemHit);
			keepGoing = DoDialogItem(dlog,itemHit);
			}
		
		/*
		 *	Do final processing of item values, such as exporting them to caller.
		 *	DoDialogItem() has already called AnyBadValues().
		 */
		
		if (itemHit == OK_ITEM) {
			Point	theCell;
			short	dataLen;
			OSErr	anErr;
			Handle	aHand;
			
			//	get the current selection and store it in a global which can be accessed
			//	from the application
			
			theCell.h = 0;
			theCell.v = list4.currentRow;
			
			dataLen = 255;
			LGetCell(&gSavedSelection[1], &dataLen, theCell, list4.hndl);
			gSavedSelection[0] = dataLen;

			//	we save the current selection for the next time we use this transport
			
			//	remove the current resource
			
			aHand = Get1Resource('mw2H', 1000);
			if (aHand != nil) {
				RemoveResource(aHand);
				DisposeHandle(aHand);
				aHand = nil;
			}
			
			//	create a handle and add this resource to the resource file
			
			anErr = PtrToHand(&gSavedSelection[0], &aHand, gSavedSelection[0] + 1);		//	name and length byte
			if ( (anErr == noErr) && (aHand != nil) ) {
				AddResource(aHand, 'mw2H', 1000, "\pDefault Host");
				WriteResource(aHand);
			}
Exemple #17
0
static void AddFolderIcon (FSRef *fref, const char *folderName)
{
	OSStatus			err;
	FSCatalogInfo		fcat, icat;
	FSRef				bref, iref;
	CFStringRef			str;
	CFURLRef			url;
	IconFamilyHandle	family;
	IconRef				icon;
	HFSUniStr255		fork;
	Boolean				r;
	SInt16				resf;
	char				name[64];
	UniChar				iconName[5] = { 'I', 'c', 'o', 'n', '\r' };

	strcpy(name, "folder_");
	strcat(name, folderName);

	str = CFStringCreateWithCString(kCFAllocatorDefault, name, CFStringGetSystemEncoding());
	if (str)
	{
		url = CFBundleCopyResourceURL(CFBundleGetMainBundle(), str, CFSTR("icns"), NULL);
		if (url)
		{
			r = CFURLGetFSRef(url, &bref);
			if (r)
			{
				err = RegisterIconRefFromFSRef('~9X~', 'TEMP', &bref, &icon);
				if (err == noErr)
				{
					err = FSGetResourceForkName(&fork);
					if (err == noErr)
					{
						err = FSCreateResourceFile(fref, 5, iconName, kFSCatInfoNone, NULL, fork.length, fork.unicode, &iref, NULL);
						if (err == noErr)
						{
							err = FSOpenResourceFile(&iref, fork.length, fork.unicode, fsWrPerm, &resf);
							if (err == noErr)
							{
								err = IconRefToIconFamily(icon, kSelectorAllAvailableData, &family);
								if (err == noErr)
								{
									AddResource((Handle) family, 'icns', -16455, "\p");
									WriteResource((Handle) family);
									ReleaseResource((Handle) family);

									err = FSGetCatalogInfo(&iref, kFSCatInfoFinderInfo, &icat, NULL, NULL, NULL);
									((FileInfo *) &icat.finderInfo)->finderFlags |= kIsInvisible;
									((FileInfo *) &icat.finderInfo)->fileCreator = 'MACS';
									((FileInfo *) &icat.finderInfo)->fileType    = 'icon';
									err = FSSetCatalogInfo(&iref, kFSCatInfoFinderInfo, &icat);

									err = FSGetCatalogInfo(fref, kFSCatInfoFinderInfo, &fcat, NULL, NULL, NULL);
									((FolderInfo *) &fcat.finderInfo)->finderFlags |=  kHasCustomIcon;
									((FolderInfo *) &fcat.finderInfo)->finderFlags &= ~kHasBeenInited;
									err = FSSetCatalogInfo(fref, kFSCatInfoFinderInfo, &fcat);
								}

								CloseResFile(resf);
							}
						}
					}
Exemple #18
0
/* alias
   -----
   Will create a Macintosh HFS  specific  alias at the path
   pointed to by dest.  The  alias  will  point to the file
   located at target. The target must exist.

   The return value is zero upon success.  If unsuccessful,
   a  non-zero value will be  returned and both  errno  and
   mac_errno should be consulted.
*/
int alias (const char *target, const char *dest)
{
   char *rtarget;
   char *destparentdir, *destname;
#ifdef HAVE_CORESERVICES
   char *targetsuffix;
   UniChar *udestname;
   FSRef *fstarget, *fsdest, *fsnewfile;
   FSCatalogInfo *fscdestinfo, *fsctargetinfo;
   FileInfo destinfo, targetinfo;
   AliasHandle aliasrec;
   u_int32_t infomap = 0;
   int resfrk;
   u_int8_t targetisdir = 0;
#endif
   u_int8_t destisdir = 0;
   size_t len = 0;

#ifndef HAVE_CORESERVICES
   /* this function is for creating macintosh aliases.
      if we don't have the CoreServices API it's impossible,
      so why fool ourselves, let's just return error now
      and save the cpu cycles for something else */

   errno = EIO; /* returning a generic I/O error */
   return errno;
#endif

   /* As of this writing,  this  function is not  complete.
      Things left to  complete are to  check the  target to
      see if it has a custom icon (if so,  copy the  custom
      to the alias file so that they appear the same. Also,
      copy custom icons of bundles to the alias.

      Another very  important thing left to  complete is to
      retire the process of making the alias  manually to a
      last resort if we cannot create the  alias by sending
      an apple event to the finder.
   */

   /* check for null parameters and return accordingly */
   if (!target || !dest) return 0;

#ifdef HAVE_CORESERVICES

   rtarget = (char *)malloc(PATH_MAX);
   if (!rtarget) return errno; /* ENOMEM */

   if (!resolve_alias(target, rtarget) || mac_errno)
      return errno;

   /* convert the target path into an FSRef */
  
   fstarget = (FSRef *)malloc(sizeof(FSRef));
   if (!fstarget) return errno; /* ENOMEM */
 
   mac_errno = FSPathMakeRef(rtarget, fstarget, &targetisdir);
   if (mac_errno) return 0;

   fsdest = (FSRef *)malloc(sizeof(FSRef));
   if (!fsdest) return errno; /* ENOMEM */

   /* convert the destination into an FSRef so that we can test if it exists
      and if it is a directory (in which we should save the new alias file
      inside the directory specified) */
   mac_errno = FSPathMakeRef(dest, fsdest, &destisdir);

#endif

   /* if the destination is not a directory, we'll disassemble the path we
      were given for the destination into a parent directory and a name */
   if (!destisdir) {

      /* obtain the name of the destination file */
      len = strlen(dest);
      destname = (char *)dest + len;
      while (destname > dest && *(destname - 1) != '/') destname--, len--;

      /* obtain the parent directory of the destination file */
      destparentdir = (char *)malloc(len);
      snprintf(destparentdir, len ? len + 1 : 3, "%s", len ? dest : "./");

   } else {

      /* the parent directory should be the destination we were passed */
      destparentdir = (char *)dest;

      /* and the name should be the real name of the target */
      destname = (char *)rtarget + strlen(rtarget);
      while (destname > rtarget && *(destname - 1) != '/') destname--;

   }

#ifdef HAVE_CORESERVICES

   /* attempt to convert the parent directory into an FSRef */
   mac_errno = FSPathMakeRef(destparentdir, fsdest, 0);
   if (mac_errno) return 0;

   /* infomap is an integer telling FSCreateResFile which settings from
      the FSCatalogInfo paramater you want to set */
   infomap |= kFSCatInfoFinderInfo;

   /* if the target is not a directory, get its file type/creator */
   if (!targetisdir) {

      fsctargetinfo = (FSCatalogInfo *)malloc(sizeof(FSCatalogInfo));
      if (!fsctargetinfo) return errno; /* ENOMEM */

      /* get the target files information */
      mac_errno = FSGetCatalogInfo(fstarget, infomap, fsctargetinfo, 0,0,0);
      if (mac_errno) return 0;

      /* I don't know why Apple did not declare the structure member
         'finderInfo' as an FileInfo type (it is an array of 16 single bytes).
         This makes it impossible to address elements of the finderInfo
         directly. So, we move it into an FileInfo data type we allocated */
      memmove(&targetinfo, fsctargetinfo->finderInfo, sizeof(FileInfo));

      /* this is not really necessary for the creation of the file, since
         a null type or creator will be transposed to '????', but should any
         functions try to display the type/creator, it is much friendlier to
         display '????' than an empty string (which hardcore mac people will
         understand better */
      if (!targetinfo.fileType) {
         targetinfo.fileType = '\?\?\?\?';
         targetinfo.fileCreator = '\?\?\?\?';
      }

   } else {

      /* folders natively return null as their file type and creator but to
         make an alias appear as a folder, we use the following settings */
      targetinfo.fileType = 'fldr';
      targetinfo.fileCreator = 'MACS';

      /* there is one exception, when the folder ends in .app it is an
         application package and should get the following settings */
      targetsuffix = suffix(rtarget);
      if (!strcmp(targetsuffix, "app")) {
         targetinfo.fileType = 'fapa';
         targetinfo.fileCreator = '\?\?\?\?';
         *--targetsuffix = '\0'; /* this will cut off the .app from the name */
      }

   }

   fscdestinfo = (FSCatalogInfo *)malloc(sizeof(FSCatalogInfo));
   if (!fscdestinfo) return errno; /* ENOMEM */

   /* set the file type, creator, and alias flag for the file */
   destinfo.fileType    = targetinfo.fileType;
   destinfo.fileCreator = targetinfo.fileCreator;
   destinfo.finderFlags = kIsAlias;
   memcpy(fscdestinfo->finderInfo, &destinfo, sizeof(FileInfo));

   fsnewfile = (FSRef *)malloc(sizeof(FSRef));
   if (!fsnewfile) return errno; /* ENOMEM */

   udestname = (UniChar *)malloc(PATH_MAX);
   len = utf8to16(destname, udestname, PATH_MAX);
   if (len < 0) return errno; /* caller: consult both errno and mac_errno */

   /* attempt to create the resource file (fails if it already exists) */
   FSCreateResFile(fsdest, len, udestname, infomap, fscdestinfo, fsnewfile, 0);

   free(udestname);

   /* test for error creating the file */
   mac_errno = ResError();
   if (mac_errno) return 0;

   /* attempt to open the resource fork of the file now */
   resfrk = FSOpenResFile(fsnewfile, fsRdWrPerm);

   /* test for errors */
   mac_errno = ResError();
   if (mac_errno) return 0;

   /* attempt to create an alias record to save in the resource file */
   mac_errno = FSNewAlias(0, fstarget, &aliasrec);
   if (mac_errno) return CloseResFile(resfrk), 0;

   /* add the alias record to the resource fork */
   AddResource((Handle)aliasrec, rAliasType, 0, 0);

   /* test for errors */
   mac_errno = ResError();
   if (mac_errno) return CloseResFile(resfrk), 0;

   /* write the resource fork data */
   WriteResource((Handle)aliasrec);

   /* test for errors */
   mac_errno = ResError();
   if (mac_errno) return CloseResFile(resfrk), 0;

   /* clean up */
   CloseResFile(resfrk);

#endif /* HAVE_CORESERVICES */

   /* return success */
   return 0;

}