home *** CD-ROM | disk | FTP | other *** search
/ Mac Magazin/MacEasy 48 / Mac Magazin CD 48.iso / Software / Entwickler / Drop UNIX 1.3 / DropUNIX Library ƒ / Lib Sources / DSUserProcs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-07-15  |  19.4 KB  |  675 lines  |  [TEXT/CWIE]

  1. /******************************************************************************
  2. **
  3. **  Project Name:    DropShell
  4. **     File Name:    DSUserProcs.c
  5. **
  6. **   Description:    Specific AppleEvent handlers used by the DropBox
  7. **
  8. *******************************************************************************
  9. **                       A U T H O R   I D E N T I T Y
  10. *******************************************************************************
  11. **
  12. **    Initials    Name
  13. **    --------    -----------------------------------------------
  14. **    LDR            Leonard Rosenthol
  15. **    MTC            Marshall Clow
  16. **    SCS            Stephan Somogyi
  17. **    ZSS/RWD        Zen Spider Software/Ryan Davis
  18. **
  19. *******************************************************************************
  20. **                      R E V I S I O N   H I S T O R Y
  21. *******************************************************************************
  22. **
  23. **      Date        Time    Author    Description
  24. **    --------    -----    ------    ---------------------------------------------
  25. **    06/23/94            LDR        Added support for ProcessItem and ProcessFolder handling
  26. **    02/20/94            LDR        Modified Preflight & Postflight to take item count
  27. **    01/25/92            LDR        Removed the use of const on the userDataHandle
  28. **    12/09/91            LDR        Added the new SelectFile userProc
  29. **                                Added the new Install & DisposeUserGlobals procs
  30. **                                Modified PostFlight to only autoquit on odoc, not pdoc
  31. **    11/24/91            LDR        Added the userProcs for pdoc handler
  32. **                                Cleaned up the placement of braces
  33. **                                Added the passing of a userDataHandle
  34. **    10/29/91            SCS        Changes for THINK C 5
  35. **    10/28/91            LDR        Officially renamed DropShell (from QuickShell)
  36. **                                Added a bunch of comments for clarification
  37. **    10/06/91    00:02    MTC        Converted to MPW C
  38. **    04/09/91    00:02    LDR        Added to Projector
  39. **
  40. ******************************************************************************/
  41.  
  42. /******************************************************************************
  43. **
  44. **    DropUnix history:    KRB = Kevin R. Boyce, RWD = Ryan Davis
  45. **
  46. **    04/11/95        RWD        First release.
  47. **    04/21/95        KRB        Added conditional compilation for Think C (Universal headers,
  48. **                             but old ANSI/console library).
  49. **    02/15/96        RWD        Updated for CW8 & added interface changes for better
  50. **                            integration to SIOUX
  51. **
  52. ********************************************************************************/
  53.  
  54. #include <Files.h>
  55. #include <StandardFile.h>
  56.  
  57. #if !OLDROUTINELOCATIONS
  58.     #include <Processes.h>
  59.     #include <TextUtils.h>
  60. #else
  61.     #include <SegLoad.h>
  62.     #include <Strings.h>
  63. #endif
  64.  
  65. #include <stdlib.h>            /* For malloc & free */
  66. #include <string.h>            /* For strcpy */
  67. #include <stdio.h>
  68.  
  69. #ifdef __MWERKS__
  70. #include <SIOUX.h>            /* To manipulate the console */
  71. #endif
  72.  
  73. #ifdef THINK_C
  74.     #include <pascal.h>
  75.     extern WindowPtr    gSplashScreen;        /* Needed for unhiliting THINK C console --KRB */
  76. #endif
  77.  
  78. #include "DSGlobals.h"
  79. #include "DSUserProcs.h"
  80.  
  81. // #define malloc    NewPtr
  82. // #define free    DisposePtr
  83. #define ValidatePtr(ptr) \
  84.     if (!ptr) {\
  85.         printf("Error allocating memory! Error #%d\n", MemError() );\
  86.         exit(-1);\
  87.     }
  88.  
  89. /* User Structs */
  90.     typedef struct {
  91.         unsigned int count;
  92.         char ** names;
  93.     } fileNameList, *fileNamePtr, **fileNameHandle;
  94.  
  95. /* Static Prototypes */
  96.     static OSErr ProcessFolder(FSSpecPtr myFSSPtr, Handle userDataHandle);
  97.     static OSErr ProcessItem(FSSpecPtr myFSSPtr, Handle userDataHandle);
  98.     static void SetApplicationName(Handle userDataHandle);
  99.     static void AllocateAppNames(Handle *userDataHandle, unsigned long numOfFiles);
  100.     static void DeallocateAppNames(Handle userDataHandle);
  101.  
  102. /*
  103.     This routine is called during init time.
  104.     
  105.     It allows you to install more AEVT Handlers beyond the standard four
  106. */
  107.  
  108. pascal void InstallOtherEvents (void) {
  109. }
  110.  
  111.  
  112. /*    
  113.     This routine is called when an OAPP event is received.
  114.     
  115.     Currently, all it does is set the gOApped flag, so you know that
  116.     you were called initally with no docs, and therefore you shouldn't 
  117.     quit when done processing any following odocs.
  118. */
  119.  
  120. pascal void OpenApp (void) {
  121.     gOApped = true;
  122. }
  123.  
  124.  
  125. /*    
  126.     This routine is called when an QUIT event is received.
  127.     
  128.     We simply set the global done flag so that the main event loop can
  129.     gracefully exit.  We DO NOT call ExitToShell for two reasons:
  130.     1) It is a pretty ugly thing to do, but more importantly
  131.     2) The Apple event manager will get REAL upset!
  132. */
  133.  
  134. pascal void QuitApp (void) {
  135.     gDone = true;    /*    All Done! */
  136. }
  137.  
  138.  
  139. /*    
  140.     This routine is the first one called when an ODOC or PDOC event is received.
  141.     
  142.     In this routine you would place code used to setup structures, etc. 
  143.     which would be used in a 'for all docs' situation (like "Archive all
  144.     dropped files")
  145.  
  146.     Obviously, the opening boolean tells you whether you should be opening
  147.     or printing these files based on the type of event recieved.
  148.     
  149.     NEW IN 2.0!
  150.     The itemCount parameter is simply the number of items that were dropped on
  151.     the application and that you will be processing.  This gives you the ability
  152.     to do a single preflight for memory allocation needs, rather than doing it
  153.     once for each item as in previous versions.
  154.     
  155.     userDataHandle is a handle that you can create & use to store your own
  156.     data structs.  This dataHandle will be passed around to the other 
  157.     odoc/pdoc routines so that you can get at your data without using
  158.     globals - just like the new StandardFile.  
  159.     
  160.     We also return a boolean to tell the caller if you support this type
  161.     of event.  By default, our dropboxes don't support the pdoc, so when
  162.     opening is FALSE, we return FALSE to let the caller send back the
  163.     proper error code to the AEManager.
  164. */
  165.  
  166. pascal Boolean PreFlightDocs (Boolean opening, short itemCount, Handle *userDataHandle) {
  167.  
  168.     /* unsigned int index; */
  169.  
  170.     if ((opening) && (itemCount > 0)) {    /* Don't support printing, so don't waste our time */
  171.  
  172.         /*
  173.         ** The following is a bug, it is the responsibility of ProcessItem to increment g_argc
  174.         ** g_argc += itemCount;
  175.         */
  176.  
  177.         AllocateAppNames(userDataHandle, itemCount);
  178.         SetApplicationName(*userDataHandle);
  179.     }
  180.  
  181.     return opening;        // we support opening, but not printing - see above
  182. }
  183.  
  184. /*    
  185.     This routine is called for each file passed in the ODOC event.
  186.     
  187.     In this routine you would place code for processing each file/folder/disk that
  188.     was dropped on top of you.
  189. */
  190.  
  191. pascal void OpenDoc ( FSSpecPtr myFSSPtr, Boolean /* opening */, Handle userDataHandle ) {
  192.  
  193.     OSErr    err = noErr;
  194.     
  195.     #if defined(qWalkFolders) && qWalkFolders
  196.         /*
  197.             For this case we need to determine if the FSSpec is a file or folder.
  198.             If it's a folder, we then need to process each item in that folder,
  199.             otherwise just process the item.
  200.         */
  201.         if (FSpIsFolder(myFSSPtr))
  202.             err = ProcessFolder(myFSSPtr, userDataHandle);
  203.         else
  204.             err = ProcessItem(myFSSPtr, userDataHandle);
  205.     #else
  206.         /*
  207.             For this case we just call ProcessItem on the FSSpec above.
  208.         */
  209.         err = ProcessItem(myFSSPtr, userDataHandle);
  210.     #endif
  211.     
  212.     // you should probably do something if you get back an error ;)
  213. }
  214.  
  215.  
  216. /*    
  217.     This routine is the last routine called as part of an ODOC event.
  218.     
  219.     In this routine you would place code to process any structures, etc. 
  220.     that you setup in the PreflightDocs routine.
  221.  
  222.     NEW IN 2.0!
  223.     The itemCount parameter was the number of items that you processed.
  224.     It is passed here just in case you need it ;)  
  225.     
  226.     If you created a userDataHandle in the PreFlightDocs routines, this is
  227.     the place to dispose of it since the Shell will NOT do it for you!
  228. */
  229.  
  230. pascal void PostFlightDocs ( Boolean opening, short /* itemCount */, Handle userDataHandle ) {
  231.  
  232.     /* int index; */
  233.     int argc;
  234.     char ** argv;
  235.     fileNamePtr fileNames;
  236.     
  237.     extern int Main(int argc, char **argv);
  238.  
  239.     #ifdef __MWERKS__
  240.     ClearMenuBar();        /* 1.2 ZSS - if we use SIOUX, we clear the menubar and let it take over... */
  241.     #endif                /* __MWERKS__ */    
  242.     /*
  243.     ** OK, this is IT! We have finally processed all the files, now we send the
  244.     ** pathnames & count off to the REAL unix function...
  245.     */
  246.     HLock(userDataHandle);
  247.         fileNames = *((fileNameHandle) userDataHandle);
  248.         argc = fileNames->count;
  249.         argv = fileNames->names;
  250.  
  251.         (void) Main(argc, argv);
  252.     HUnlock(userDataHandle);
  253.     /*
  254.     ** Now deallocate the memory stored by PreFlightDocs
  255.     */
  256.     
  257.     DeallocateAppNames(userDataHandle);
  258.     
  259.     /*
  260.     ** Reset the globals, this is in case the user invoked us by the menuitem,
  261.     ** the user _could_ run through again if wanted...
  262.     */
  263.     
  264.     #ifdef THINK_C
  265.     /*
  266.     ** Unhilite the stupid THINK C console, so we get keypresses.
  267.     ** This may not be necessary if you have an up-to-date (8.0 or
  268.     ** paid-for 7.0) ANSI/console library.  Or it might, who knows?
  269.     */
  270.     SelectWindow( gSplashScreen );
  271.     #endif
  272.  
  273.     if ( (opening) && (!gOApped) )
  274.         gDone = true;    //    close everything up!
  275.  
  276.     /*
  277.         The reason we do not auto quit is based on a recommendation in the
  278.         Apple event Registry which specifically states that you should NOT
  279.         quit on a 'pdoc' as the Finder will send you a 'quit' when it is 
  280.         ready for you to do so.
  281.     */
  282. }
  283.  
  284. /*
  285.     This routine gets called for any folder (or disk) that the caller wants 
  286.     processed as a set of component items, instead of as a single entity.
  287.     The determining factor is the definition of the qWalkFolders compiler directive.
  288. */
  289. static OSErr ProcessFolder(FSSpecPtr myFSSPtr, Handle userDataHandle)
  290. {
  291.     OSErr        err = noErr;
  292.     short        index, oldIndex, localIndex;
  293.     FSSpec        localFSSpec, curFSSpec;
  294.     CInfoPBRec    cipb;
  295.     Str255        fName, vFName;
  296.     long        dirID, origDirID;
  297.     Boolean        foundPosition;
  298.  
  299.      // copy the source locally to avoid recursion problems
  300.      BlockMoveData(myFSSPtr, &localFSSpec, sizeof(FSSpec));
  301.      
  302.     //    get the dirID for THIS folder, not it's parent!
  303.     BlockMoveData(localFSSpec.name, fName, 32);
  304.     
  305.     cipb.hFileInfo.ioCompletion    = 0L;
  306.     cipb.hFileInfo.ioNamePtr    = fName;
  307.     cipb.hFileInfo.ioVRefNum    = localFSSpec.vRefNum;
  308.     cipb.hFileInfo.ioFDirIndex    = 0;    // use the dir & vRefNum;
  309.     cipb.hFileInfo.ioDirID        = localFSSpec.parID;
  310.     err = PBGetCatInfoSync(&cipb);
  311.     
  312.     if (!err) {        
  313.         origDirID = cipb.dirInfo.ioDrDirID; // copy the sucker
  314.         index = 1;
  315.                 
  316.         // index through all contents of this folder
  317.         while (err == noErr) {
  318.             dirID = origDirID;
  319.             localIndex = index;
  320.             fName [0] = 0;
  321.             cipb.hFileInfo.ioCompletion    = 0L;
  322.             cipb.hFileInfo.ioNamePtr    = fName;
  323.             cipb.hFileInfo.ioVRefNum    = localFSSpec.vRefNum;
  324.             cipb.hFileInfo.ioFDirIndex    = localIndex;    // use a real index
  325.             cipb.hFileInfo.ioDirID        = dirID;
  326.             err = PBGetCatInfoSync(&cipb);
  327.  
  328.             if (!err) {
  329.                 BlockMoveData(fName, curFSSpec.name, 32);
  330.                 curFSSpec.vRefNum    = cipb.hFileInfo.ioVRefNum;
  331.                 curFSSpec.parID        = dirID;
  332.             
  333.                 /*    
  334.                     Check to see if this entry is a folder.
  335.                 */
  336.                 if (cipb.hFileInfo.ioFlAttrib & ioDirMask) {
  337.                     err = ProcessFolder(&curFSSpec, userDataHandle);
  338.                  } else
  339.                     err = ProcessItem(&curFSSpec, userDataHandle);
  340.             
  341.                 /*    If we've had an error, get out! */
  342.                 if (err)    break;
  343.  
  344.                 // dirID = origDirID;    
  345.                 localIndex = index;    
  346.  
  347.                 /*    
  348.                     Now take into account new files being created
  349.                     in the current directory & messing up our index.
  350.                     See Dev.CD Vol. XI:Tools & Apps (Moof!):Misc Utilities:
  351.                     Disinfectant & Source 2.5.1:Sample:Notes:Scan Alg    
  352.                 */
  353.                 vFName [0] = 0;
  354.                 cipb.hFileInfo.ioCompletion    = 0L;
  355.                 cipb.hFileInfo.ioNamePtr    = vFName;
  356.                 cipb.hFileInfo.ioVRefNum    = localFSSpec.vRefNum;
  357.                 cipb.hFileInfo.ioFDirIndex    = localIndex;    // use a real index
  358.                 cipb.hFileInfo.ioDirID        = dirID;
  359.                 err = PBGetCatInfoSync(&cipb);
  360.                 oldIndex = index;
  361.                 if (!err) {
  362.                     /*    If they're equal - same place, go to next */
  363.                     if (EqualString (vFName, fName, false, false))
  364.                         index++;
  365.                 }
  366.                 
  367.                 /*    If we didn't advance, then perhaps a file was created or deleted */
  368.                 if (oldIndex == index) {
  369.                     oldIndex        = index;    /* save off the old */
  370.                     index            = 0;        /* and start at the beginning */
  371.                     err                = noErr;
  372.                     vFName [0]        = 0;
  373.                     foundPosition    = false;
  374.                     
  375.                     while (!foundPosition) {
  376.                         index++;
  377.                         vFName [0] = 0;
  378.                         cipb.hFileInfo.ioCompletion    = 0L;
  379.                         cipb.hFileInfo.ioNamePtr    = vFName;
  380.                         cipb.hFileInfo.ioVRefNum    = localFSSpec.vRefNum;
  381.                         cipb.hFileInfo.ioFDirIndex    = index;    /* now use a real index */
  382.                         cipb.hFileInfo.ioDirID        = dirID;
  383.                         err = PBGetCatInfoSync(&cipb);
  384.                         
  385.                         if (err == fnfErr) {  // we've just been deleted
  386.                             index = oldIndex;
  387.                             foundPosition = true;
  388.                             err = noErr;    // have to remember to reset this!
  389.                         }
  390.                         
  391.                     /*    found same file & same index position */
  392.                     /*    so try the next item */
  393.                         if ((!foundPosition) && EqualString(fName, vFName, false, false)) {
  394.                             index++;
  395.                             foundPosition = true;
  396.                         }
  397.                     }
  398.                 }
  399.             }
  400.         }
  401.     }
  402.     
  403.     return(err);
  404. }
  405.  
  406. /*
  407.     This routine is called when the user chooses "Select File…" from the
  408.     File Menu.
  409.     
  410.     Currently it simply calls the new StandardGetFile routine to have the
  411.     user select a single file (any type, numTypes = -1) and then calls the
  412.     SendODOCToSelf routine in order to process it.  
  413.             
  414.     The reason we send an odoc to ourselves is two fold: 1) it keeps the code
  415.     cleaner as all file openings go through the same process, and 2) if events
  416.     are ever recordable, the right things happen (this is called Factoring!)
  417.  
  418.     Modification of this routine to only select certain types of files, selection
  419.     of multiple files, and/or handling of folder & disk selection is left 
  420.     as an exercise to the reader.
  421. */
  422. pascal void SelectFile (void)
  423. {
  424.     StandardFileReply    stdReply;
  425.     SFTypeList            theTypeList;
  426.  
  427.     StandardGetFile(NULL, -1, theTypeList, &stdReply);
  428.     if (stdReply.sfGood)    // user did not cancel
  429.         SendODOCToSelf(&stdReply.sfFile);    // so send me an event!
  430. }
  431.  
  432. /*
  433.     This routine is called during the program's initialization and gives you
  434.     a chance to allocate or initialize any of your own globals that your
  435.     dropbox needs.
  436.     
  437.     You return a boolean value which determines if you were successful.
  438.     Returning false will cause DropShell to exit immediately.
  439. */
  440.  
  441. pascal Boolean InitUserGlobals(void)
  442. {
  443.     /* Str255 appName; */
  444.     
  445.     #ifdef __MWERKS__
  446.     SIOUXSettings.initializeTB    = false;
  447.     SIOUXSettings.standalone    = true;
  448.     SIOUXSettings.setupmenus    = true;
  449.     #endif
  450.  
  451.     return(true);    // nothing to do, it we must be successful!
  452. }
  453.  
  454. /*
  455.     This routine is called during the program's cleanup and gives you
  456.     a chance to deallocate any of your own globals that you allocated 
  457.     in the above routine.
  458. */
  459. pascal void DisposeUserGlobals(void)
  460. {
  461. }
  462.  
  463. long GetFullPath (FSSpec *fsspec, char *path, OSErr *err);
  464. unsigned short ReverseCopyP2CStr (unsigned char *pas, char *c);
  465. void ReverseCStr (char *str);
  466.  
  467. /*
  468.     This routine gets called for each item (which could be either a file or a folder)
  469.     that the caller wants dropped.  The determining factor is the definition of the 
  470.     qWalkFolder compiler directive.   Either way, the item in question should be
  471.     processed as a single item and not "dissected" into component units (like subfiles
  472.     of a folder!)
  473. */
  474. OSErr ProcessItem(FSSpecPtr myFSSPtr, Handle userDataHandle)
  475. {
  476.     char    path[1000];
  477.     OSErr    err = noErr;
  478.     unsigned long    len;
  479.     char * temp = NULL;
  480.     
  481.     len = GetFullPath (myFSSPtr, path, &err);
  482.     if (err != noErr) {
  483.         return err;
  484.     }
  485.  
  486.     if (path != NULL) {
  487.         fileNamePtr fileNames;
  488.         
  489.         temp = (char *) NewPtr((len+1) * sizeof(char));
  490.         ValidatePtr(temp);
  491.         HLock(userDataHandle);
  492.             fileNames = *((fileNameHandle) userDataHandle);
  493.             fileNames->names[(fileNames->count)] = temp;
  494.             strcpy(fileNames->names[(fileNames->count)], path);
  495.             (fileNames->count)++;
  496.         HUnlock(userDataHandle);
  497.     }
  498.  
  499.     return(err);
  500. }
  501.  
  502. long GetFullPath (FSSpec *fsspec, char *path, OSErr *err)
  503. {
  504.     short        volume = fsspec->vRefNum;
  505.     OSErr        fsErr = noErr;
  506.     CInfoPBRec    catinfo;
  507.     char            dirName[64];
  508.     long            retLen = 0L, len;
  509.     register char    *p = path;
  510.     
  511.     // All comments assume a  file whose full path is
  512.     //    HD:Fonts:Garamond
  513.     
  514.     // If fsspec doesn't designate a file (i.e., it's a volume or directory), we write a colon
  515.     if ( FSpIsFolder (fsspec)) {
  516.         *p++ = ':';
  517.         retLen++;
  518.     }
  519.     
  520.     // First, copy the file name backwards from a Pascal to a C string:
  521.     //    "\pGaramond"  becomes "dnomaraG"
  522.  
  523.     len = (long) ReverseCopyP2CStr (fsspec->name, p);
  524.     p += len;
  525.     retLen += len;
  526.     
  527.     /* Yea, I know this is cheap, but it is used ONCE AND it makes it clearer {RWD} */
  528.     
  529.     #define FSpIsVolume(FSSpec) ((FSSpec)->parID == fsRtParID)
  530.     
  531.     if ( ! FSpIsVolume (fsspec)) {        // Don't do anything more if we've got a volume
  532.     
  533.         catinfo.dirInfo.ioVRefNum = volume;
  534.         catinfo.dirInfo.ioNamePtr = (StringPtr) dirName;
  535.         catinfo.dirInfo.ioDrParID = fsspec->parID;
  536.         
  537.         // Now copy the rest of the path, one level at a time, backwards:
  538.         //    "dnomaraG:stnoF:DH"
  539.         do {
  540.             catinfo.dirInfo.ioFDirIndex = -1;
  541.             catinfo.dirInfo.ioDrDirID = catinfo.dirInfo.ioDrParID;    // <= This is the key — go from the
  542.                                                         //    current folder to its parent
  543.             fsErr = PBGetCatInfoSync (&catinfo);
  544.             if (fsErr == noErr) {
  545.                 *p++ = ':';
  546.                 retLen += 1 + (len = ReverseCopyP2CStr ((StringPtr) dirName, p));
  547.                 p += len;
  548.             } else {
  549.                 *err = fsErr;
  550.                 return retLen;
  551.             }
  552.         } while (catinfo.dirInfo.ioDrDirID != fsRtDirID);
  553.     }
  554.     
  555.     // Finally, reverse the string and return its length and any error that might have occurred (ReverseCopyP2CStr has
  556.     //    already appended a null character, so we don't have to worry about it here)
  557.     ReverseCStr (path);
  558.     *err = fsErr;
  559.     return retLen;
  560. }
  561.  
  562. unsigned short ReverseCopyP2CStr (register unsigned char *pas, register char *c)
  563. {
  564.     register short    i, len = *pas++;
  565.     
  566.     for (i = len, c += len; i > 0; i--)
  567.         *--c = *pas++;
  568.     c += len;
  569.     *c = '\0';
  570.     return len;
  571. }
  572.  
  573. void ReverseCStr (register char *str)
  574. {
  575.     register short        n = 0L, i;
  576.     register char        t, *p1, *p2;
  577.     
  578.     p1 = p2 = str;
  579.     while (*p2++)
  580.         ;
  581.     p2--;
  582.     n = (p2 - p1) / 2;
  583.     for (i = n; i > 0; i--) {
  584.         t = *--p2;
  585.         *p2 = *p1;
  586.         *p1++ = t;
  587.     }
  588. }
  589.  
  590. static void SetApplicationName(Handle userDataHandle) {
  591.  
  592.     Str255 appName;
  593.     fileNamePtr fileNames;
  594.     
  595.     if (userDataHandle == NULL || *userDataHandle == NULL) {
  596.         Panic(kMemoryError);
  597.     }
  598.  
  599.     GetMyAppName(appName); // FIX: is this right?
  600.  
  601.     if (appName == NULL) {
  602.         Panic(kMemoryError);
  603.     }    
  604.     
  605.     HLock(userDataHandle);
  606.         fileNames = *((fileNameHandle) userDataHandle);
  607.         if ( fileNames->names[0] == NULL) {
  608. //            fileNames->names[0] = (char *) NewPtr(**fileNames->names + 1); /* pstr's size in byte 0 */
  609.             fileNames->names[0] = (char *) NewPtr((size_t) appName[0]); /* pstr's size in byte 0 */
  610.             if (fileNames->names[0] == NULL || MemError()) {
  611.                 Panic(kMemoryError);
  612.             }
  613.             strcpy(fileNames->names[0], p2cstr(appName)); // BAD
  614.         }
  615.     HUnlock(userDataHandle);
  616. }
  617.  
  618. static void AllocateAppNames(Handle *userDataHandle, unsigned long numOfFiles) {
  619.  
  620.     fileNameHandle fileNameHdl;
  621.     fileNamePtr fileNames;
  622.     fileNameHdl = (fileNameHandle) NewHandleClear(sizeof(fileNameList));
  623.     if (fileNameHdl == NULL || (unsigned long) fileNameHdl == 1L || MemError() ) {
  624.         Panic(kMemoryError);
  625.     }
  626.     
  627.     HLock((Handle) fileNameHdl);
  628.         fileNames = *fileNameHdl;
  629.         
  630.         fileNames->count = 1; /* we count one right off for the app name */
  631.         /* create numOfFiles + 1 (for application name) slots to fill */
  632.         fileNames->names = (char **) NewPtrClear((numOfFiles + 1) * sizeof(char **));
  633.         if (fileNames->names == NULL) {
  634.             Panic(kMemoryError);
  635.         }
  636.         fileNames = NULL;
  637.     HUnlock((Handle) fileNameHdl);
  638.     
  639.     *userDataHandle = (Handle) fileNameHdl;
  640.     
  641.     if (*userDataHandle == NULL || **userDataHandle == NULL) {
  642.         Panic(kMemoryError);
  643.     }
  644. }
  645.  
  646. static void DeallocateAppNames(Handle userDataHandle) {
  647.  
  648.     fileNamePtr fileNames;
  649.     unsigned int index;
  650.     
  651.     HLock(userDataHandle);
  652.         fileNames = *((fileNameHandle) userDataHandle);
  653.         if (fileNames == NULL) {
  654.             Panic(kMemoryError);
  655.         }
  656.         for (index = 0; index < fileNames->count; index++) {
  657.             if (fileNames->names[index] != NULL) {
  658.                 DisposePtr(fileNames->names[index]);
  659.                 FailOnError(MemError(), kMemoryError);
  660.                 fileNames->names[index] = NULL;
  661.             }
  662.         }
  663.         DisposePtr((Ptr) fileNames->names);
  664.         if (MemError()) {
  665.             Panic(kMemoryError);
  666.         }
  667.         fileNames = NULL;
  668.     HUnlock(userDataHandle);
  669.     
  670.     DisposeHandle(userDataHandle);
  671.     if (MemError()) {
  672.         Panic(kMemoryError);
  673.     }
  674. }
  675.