home *** CD-ROM | disk | FTP | other *** search
- ///////////////////////////////////////////////////////////////////////////////
- // LISPCMDS.CPP
- //
- // Release 1.0
- //
- // .............................................................................
- //
- // Copyright (c) 1998 by Visio Corporation. All rights reserved.
- //
- // The Software is subject to the license agreement that accompanies
- // or is included with the Software, which specifies the permitted
- // and prohibited uses of the Software. Any unauthorized duplication
- // or use of Visio Corporation Software, in whole or in part, in print,
- // or in any other storage and retrieval system is prohibited.
- //
- // To the maximum extent permitted by applicable law, Visio Corporation
- // and its suppliers disclaim any and all warranties and conditions,
- // either express or implied, including, without limitation, implied
- // warranties of merchantability, fitness for a particular purpose,
- // title, and non-infringement, and those arising out of usage of trade
- // or course of dealing, concerning these materials. These materials
- // are provided "as is" without warranty of any kind.
- // .............................................................................
- //
- // Description:
- // This SDS program contains LISP extension functions to enhance LISP functionality.
- //
- ///////////////////////////////////////////////////////////////////////////////
-
-
-
- #include "afx.h"
- #include "afxwin.h"
- #include <io.h>
- #include <direct.h> // Directory stuff
- #include <errno.h>
- #include <sds.h>
-
-
- #define EOS '\0'
- #define BACKWARD 1
- #define FORWARD 0
- #define MAXBUFFER 255
- #define MINBUFFER 50
- #define FORWARDSLASHCHAR '/'
- #define FORWARDSLASHSTRING "/"
- #define BACKWARDSLASHCHAR '\\'
- #define BACKWARDSLASHSTRING "\\"
-
- #define ARRAYCOUNT(array) (sizeof(array)/sizeof((array)[0]))
-
- // Functions declarations;
- int deletefile (struct sds_resbuf *rb = NULL);
- int deltree (struct sds_resbuf *rb = NULL);
- int filexists (struct sds_resbuf *rb = NULL);
- int listfiles (struct sds_resbuf *rb = NULL);
- int getcwd (struct sds_resbuf *rb = NULL);
- int chelp (struct sds_resbuf *rb = NULL);
- int makedir (struct sds_resbuf *rb = NULL);
- int renamedir (struct sds_resbuf *rb = NULL);
- int renamefile (struct sds_resbuf *rb = NULL);
- int copyfile (struct sds_resbuf *rb = NULL);
-
- // A few structures used.
- struct functionDef{ char *function_name; int (*function) (struct sds_resbuf *); };
- struct directoryList{ char* sourceDir; char* targetDir; struct directoryList* dir_next;};
- static struct functionDef funcTable[] =
- {
- // Allow for these functions to be call with parens. Note that the functions
- // take care of situation where no arguments are supplied.
- {"deletefile", deletefile},
- {"deltree", deltree},
- {"filexists", filexists},
- {"listfiles", listfiles},
- {"getcwd", getcwd},
- {"chelp", chelp},
- {"makedir", makedir},
- {"renamedir", renamedir},
- {"renamefile", renamefile},
- {"copyfile", copyfile},
-
- // Allow for these fuctions to be called from the command line
- // without parens.
- {"c:deletefile", deletefile},
- {"c:deltree", deltree},
- {"c:filexists", filexists},
- {"c:listfiles", listfiles},
- {"c:getcwd", getcwd},
- {"c:chelp", chelp},
- {"c:makedir", makedir},
- {"c:renamedir", renamedir},
- {"c:renamefile", renamefile},
- {"c:copyfile", copyfile}
- };
-
- // Global declarations
- struct directoryList* DirectoryList = NULL;
- struct directoryList* TopDirectoryList = NULL;
- struct sds_resbuf* TopOfLList = NULL;
- struct sds_resbuf* returnLList = NULL;
-
- // Local support function declarations.
- static int funcload (void);
- static int funcunload (void);
- static int invokefun (void);
- static int DirsList (char* Root, char* Target);
- static void CorrectSlashes (char* fName, int slashDirection);
- static BOOL CreateNestedDirs (const char* directoryBeingCreated);
-
- int main (int, char **);
-
- ///////////////////////////////////////////////////////////////////////////////
- ///////////////////////////////////////////////////////////////////////////////
- // MAIN - the main entry point
- ///////////////////////////////////////////////////////////////////////////////
- int main(int argc, char** argv)
- {
- int status;
- char errorstr[80];
- short scode = RSRSLT;
-
- sds_init(argc, argv); // Start communications with lisp
-
- for ( ;; ) { // Communication loop
- if ((status = sds_link(scode)) < 0) {
- sprintf(errorstr, "Lisp extension commands: bad status from sds_link() = %d\n", status);
- sds_printf(errorstr);
- sds_exit(0);
- }
- scode = RSRSLT;
-
- switch (status) {
- case SDS_RQXLOAD: // Load & define functions
- scode = funcload() == RTNORM ? RSRSLT : RSERR;
- sds_printf("Lisp extension commands - loaded\nEnter \"(chelp)\" for function list\n");
- break;
- case SDS_RQXUNLD: // Load & define functions
- case SDS_RQQUIT:
- scode = funcunload() == RTNORM ? RSRSLT : RSERR;
- sds_printf("Lisp extension commands - unloaded\n");
- break;
- case SDS_RQSUBR:
- scode = invokefun() == RTNORM ? RSRSLT : RSERR;
- break;
- default:
- break;
- }
- }
- return 1;
- }
-
- ///////////////////////////////////////////////////////////////////////////////
- // FUNCLOAD -- Defun extension functions.
- ///////////////////////////////////////////////////////////////////////////////
- static int funcload()
- {
- short i;
-
- // Loop through the array and defun each command.
- for (i = 0; i < ARRAYCOUNT(funcTable); i++) {
- if (!sds_defun(funcTable[i].function_name, i))
- return RTERROR;
- }
- return RTNORM;
- }
-
- ///////////////////////////////////////////////////////////////////////////////
- // FUNCUNLOAD -- Defun extension functions.
- ///////////////////////////////////////////////////////////////////////////////
- static int funcunload()
- {
- short i;
-
- // Loop through the array and defun each command.
- for (i = 0; i < ARRAYCOUNT(funcTable); i++) {
- if (!sds_undef(funcTable[i].function_name, i))
- return RTERROR;
- }
- return RTNORM;
- }
-
- ///////////////////////////////////////////////////////////////////////////////
- // invokefun -- Execute extension functions.
- ///////////////////////////////////////////////////////////////////////////////
- static int invokefun()
- {
- struct sds_resbuf *rb;
- int val;
-
- // Verify that function code is valid.
- if ((val = sds_getfuncode()) < 0 || val >= ARRAYCOUNT(funcTable)) {
- sds_fail("Invalid function code.");
- return RTERROR;
- }
-
- // Get any arguments.
- rb = sds_getargs();
-
- // Call the function and return is return value
- val = (*funcTable[val].function)(rb);
- sds_relrb(rb);
-
- return val;
- }
-
- ///////////////////////////////////////////////////////////////////////////////
- // deletefile -- Deletes a file using lisp syntax
- // usage; (deletefile "f:/somefile.exe")
- ///////////////////////////////////////////////////////////////////////////////
- static int deletefile(struct sds_resbuf *rb)
- {
- char tmpBuffer[MAXBUFFER];
-
- // If rb is NULL, prompt for needed information.
- if (rb == NULL) {
- int result = sds_getstring(TRUE, "Enter file name to delete: ", tmpBuffer);
- if (result != RTNORM) {
- sds_retnil();
- return RTERROR;
- }
- } else if (rb->restype == RTSTR) {
- // We have a string.
- strcpy(tmpBuffer,rb->resval.rstring);
- } else {
- // Bad Argument type, return nil to lisp.
- sds_retnil();
- return RTERROR;
- }
-
- sds_retint(unlink(tmpBuffer) == 0);
- return RTNORM;
- }
-
- ///////////////////////////////////////////////////////////////////////////////
- // deltree -- deletes a directory tree
- // usage; (deltree "d:\\somedir")
- ///////////////////////////////////////////////////////////////////////////////
- static int deltree(struct sds_resbuf *rb)
- {
- char tmpBuffer[MAXBUFFER], TmpFile[MAXBUFFER];
- struct _finddata_t c_file;
- struct sds_resbuf tmpResBuf;
- long hFile;
-
- // If the user doesn't add args, prompt.
- if (rb == NULL) {
- int result = sds_getstring(TRUE, "Enter directory name to delete: ", tmpBuffer);
- if (result != RTNORM) {
- sds_retnil();
- return RTERROR;
- }
- } else if (rb->restype == RTSTR) {
- // We have a string.
- strcpy(tmpBuffer,rb->resval.rstring);
- } else {
- // Bad Argument type, return nil to lisp.
- sds_retnil();
- return RTERROR;
- }
-
- CorrectSlashes(tmpBuffer, BACKWARD);
- strcpy(TmpFile, tmpBuffer);
-
- if (TmpFile[strlen(TmpFile) - 1] != BACKWARDSLASHCHAR)
- strcat(TmpFile, "\\*.*");
- else
- strcat(TmpFile, "*.*");
-
- tmpResBuf.restype = RTSTR;
- tmpResBuf.resval.rstring = (char*)malloc(MAXBUFFER);
-
- // Find first *.* file in the directory.
- if( (hFile = _findfirst( TmpFile, &c_file )) != -1L ) {
- if (c_file.name[0] != '.') {
- // if this is a directory, step into it and remove everything.
- if ((c_file.attrib & _A_SUBDIR)) {
- sprintf(tmpResBuf.resval.rstring, "%s%s%s%s", tmpBuffer, BACKWARDSLASHSTRING, c_file.name);
- deltree(&tmpResBuf);
- RemoveDirectory(tmpResBuf.resval.rstring);
- }
- sprintf(TmpFile, "%s%s%s", tmpBuffer, BACKWARDSLASHSTRING, c_file.name);
- SetFileAttributes(TmpFile, FILE_ATTRIBUTE_ARCHIVE);
- DeleteFile(TmpFile);
- }
- // Find the other files.
- while( _findnext( hFile, &c_file ) == 0 ) {
- if (c_file.name[0] != '.') {
- // if this is a directory, step into it and remove everything.
- if (c_file.attrib & _A_SUBDIR) {
- sprintf(tmpResBuf.resval.rstring, "%s%s%s", tmpBuffer, BACKWARDSLASHSTRING, c_file.name);
- deltree(&tmpResBuf);
- RemoveDirectory(tmpResBuf.resval.rstring);
- }
- sprintf(TmpFile, "%s%s%s", tmpBuffer, BACKWARDSLASHSTRING, c_file.name);
- SetFileAttributes(TmpFile, FILE_ATTRIBUTE_ARCHIVE);
- DeleteFile(TmpFile);
- }
- }
- _findclose( hFile );
- }
-
- BOOL res = RemoveDirectory(tmpBuffer);
-
- if (res == 0) {
- if (GetLastError() == (DWORD)32/*Sharing violation*/)
- ads_printf("Share violation: Can't remove %s\n", tmpBuffer);
- else
- ads_printf("Directory \'%s\' not found!\n", tmpBuffer);
-
- ads_retint(0);
- } else
- sds_retint(1);
- return RTNORM;
- }
-
- ///////////////////////////////////////////////////////////////////////////////
- // filexists(const char* FileName)
- // usage; (filexists "f:/somedir/somefile.ext")
- ///////////////////////////////////////////////////////////////////////////////
- static int filexists(struct sds_resbuf *rb)
- {
- OFSTRUCT ofstruct;
-
- // Bail if the resbuf isn't the right type.
- if (rb->restype != RTSTR) {
- sds_printf("error: bad arguent type\n");
- return RTERROR;
- }
-
- if (!(OpenFile(rb->resval.rstring, &ofstruct, OF_EXIST) == HFILE_ERROR))
- ads_rett();
- else
- ads_retnil();
-
- return RTNORM;
-
- }
-
- ///////////////////////////////////////////////////////////////////////////////
- // listfiles -- create a list of files in a given directory
- // usage; (listfiles "f:/somdir/*.ext")
- ///////////////////////////////////////////////////////////////////////////////
- static int listfiles(struct sds_resbuf *rb)
- {
- char tmpBuffer[MAXBUFFER];
- char fileName[MAXBUFFER];
- struct _finddata_t c_file;
- long hFile;
- struct sds_resbuf* returnLList = NULL;
-
- TopOfLList = NULL;
-
- // If the user doesn't add args, prompt.
- if (rb == NULL) {
- int result = sds_getstring(TRUE, "Enter directory and file type specification: ", tmpBuffer);
- if (result != RTNORM) {
- sds_retnil();
- return RTERROR;
- }
- } else if (rb->restype == RTSTR) {
- // We have a string.
- strcpy(tmpBuffer,rb->resval.rstring);
- } else {
- // Bad Argument type, return nil to lisp.
- sds_retnil();
- return RTERROR;
- }
-
- CorrectSlashes(tmpBuffer, BACKWARD);
-
- // Find first file that matches the file description..
- if( (hFile = _findfirst(tmpBuffer, &c_file )) != -1L ) {
- if (!(c_file.attrib & _A_SUBDIR)) {
- // Since this is the first time through we can allocate the buffer.
- if ((returnLList = sds_newrb(RTSTR)) == NULL) {
- sds_fail("Unable to allocate linked list buffer\n");
- sds_retnil();
- return RTERROR;
- } else {
- // First time through save off pointer to the top of the linked list.
- if (TopOfLList == NULL)
- TopOfLList = returnLList;
- }
-
- strcpy(fileName, tmpBuffer);
-
- // Strip off the wild card and replace it with the file name.
- if (strstr(fileName, BACKWARDSLASHSTRING) != NULL)
- strcpy(strrchr(fileName, BACKWARDSLASHCHAR) + 1, c_file.name);
- else
- strcpy(fileName, c_file.name);
-
- // Allocate string space and save the file name into the linked list.
- if ((returnLList->resval.rstring = (char*)malloc(sizeof(fileName) + 1)) == NULL) {
- sds_fail("Unable to allocate string buffer.\n");
- sds_retnil();
- return RTERROR;
- }
-
- strcpy(returnLList->resval.rstring, strlwr(fileName));
- returnLList->rbnext = NULL;
- }
-
- // Now find any other files that match the wildcard.
- while( _findnext( hFile, &c_file ) == 0 ) {
- if (!(c_file.attrib & _A_SUBDIR)) {
- // If returnLList is NULL, the first pass above didn't find anything
- // to match the wild card, so we will need to allocate a new buffer.
- // If it's not NULL, then we need to allocate a new buffer on the
- // ->rbnext and move to it.
- if (returnLList == NULL) {
- if ((returnLList = sds_newrb(RTSTR)) == NULL) {
- sds_fail("Unable to allocate linked list buffer\n");
- sds_retnil();
- return RTERROR;
- }
- } else {
- if ((returnLList->rbnext = sds_newrb(RTSTR)) == NULL) {
- sds_fail("Unable to allocate linked list buffer\n");
- sds_retnil();
- return RTERROR;
- }
- returnLList = returnLList->rbnext;
- }
- // Again checking to make sure we can get to the top of the linked list.
- if (TopOfLList == NULL)
- TopOfLList = returnLList;
-
- strcpy(fileName, tmpBuffer);
-
- // Strip off the wild card and replace it with the file name.
- if (strstr(fileName, BACKWARDSLASHSTRING) != NULL)
- strcpy(strrchr(fileName, BACKWARDSLASHCHAR) + 1, c_file.name);
- else
- strcpy(fileName, c_file.name);
-
- // Allocate string space and save the file name into the linked list.
- if ((returnLList->resval.rstring = (char*)malloc(sizeof(fileName) + 1)) == NULL) {
- sds_fail("Unable to allocate string buffer.\n");
- sds_retnil();
- return RTERROR;
- }
-
- strcpy(returnLList->resval.rstring, strlwr(fileName));
- returnLList->rbnext = NULL;
- }
- }
- _findclose( hFile );
- if (TopOfLList == NULL && returnLList == NULL) {
- ads_printf("File type specification must be supplied.\n");
- return RTNORM;
- } else {
- // Now return what we have found to LISP. If anything.
- if (TopOfLList != NULL)
- sds_retlist(TopOfLList);
- else
- sds_retnil();
- }
- }
- // If all this has happened, then the user passed in a bogus directory.
- if (hFile == -1 && TopOfLList == NULL && returnLList == NULL) {
- ads_printf("Directory \'%s\' not found.\n", tmpBuffer);
- ads_retnil();
- } else {
- // Now clean up after ourselves.
- returnLList = TopOfLList;
- while (returnLList != NULL) {
- // Need to manually free up the string space allocated.
- free(returnLList->resval.rstring);
- returnLList->resval.rstring = NULL;
- returnLList = returnLList->rbnext;
- }
- // Now we can release the resbuf chain.
- sds_relrb(returnLList);
- }
- return RTNORM;
- }
-
- ///////////////////////////////////////////////////////////////////////////////
- // getcwd -- get the current working directory.
- ///////////////////////////////////////////////////////////////////////////////
- static int getcwd(struct sds_resbuf *rb)
- {
- char tmpBuffer[MAXBUFFER];
-
- getcwd(tmpBuffer, MAXBUFFER);
-
- sds_retstr(tmpBuffer);
-
- return RTNORM;
- }
-
- //-----------------------------------------------------------------------------
- // chelp -- Displays help screen.
- //
- static int chelp(struct sds_resbuf *rb)
- {
-
- sds_textscr();
- sds_printf("\nCommand: Syntax: Return value:\n");
- sds_printf("----------------------------------------------------------------------\n");
- sds_printf("deletefile (deletefile \"sFilename\") 1 = success.\n");
- sds_printf("deltree (deltree \"sDirectory\") 1 = success.\n");
- sds_printf("filexists (filexists \"sFilename\") T = success.\n");
- sds_printf("listfiles (listfiles \"path\\*.*\") (list) = success.\n");
- sds_printf("getcwd (getcwd) path = success.\n");
- sds_printf("makedir (makedir \"drive:\\dir\") 1 = success.\n");
- sds_printf("renamedir (renamedir \"source\" \"target\") 1 = success.\n");
- sds_printf("renamefile (renamefile \"source\" \"target\") 1 = success.\n");
- sds_printf("copyfile (copyfile \"sFrom\" \"sTo\" [\"/s\"]) number of files = success.\n");
- sds_printf("----------------------------------------------------------------------\n");
- sds_printf(" NOTE:\n");
- sds_printf(" All extension commands can be called with or without arguments.\n");
- sds_printf(" If no arguments are supplied, the command will prompt you for any\n");
- sds_printf(" information it needs to complete the task. Also all output will be\n");
- sds_printf(" echoed to the history window, and can be used as return values for setq.\n\n");
-
- sds_retvoid();
- return RTNORM;
- }
-
- ///////////////////////////////////////////////////////////////////////////////
- // makedir -- create a directory usage; (makedir "f:/junk")
- ///////////////////////////////////////////////////////////////////////////////
- static int makedir(struct sds_resbuf *rb)
- {
- char tmpBuffer[MAXBUFFER];
-
- // If rb is NULL, prompt for needed information.
- if (rb == NULL) {
- int result = sds_getstring(TRUE, "Enter directory name: ", tmpBuffer);
- if (result != RTNORM) {
- sds_retnil();
- return RTERROR;
- }
- } else if (rb->restype == RTSTR) {
- // We have a string.
- strcpy(tmpBuffer,rb->resval.rstring);
- } else {
- // Bad Argument type, return nil to lisp.
- sds_retnil();
- return RTERROR;
- }
-
- CorrectSlashes(tmpBuffer, BACKWARD);
-
- sds_retint(CreateNestedDirs(tmpBuffer));
-
- return RTNORM;
- }
-
- ///////////////////////////////////////////////////////////////////////////////
- // renamedir -- rename a directory usage; (renamedir "f:/junk" "f:/mbjunk").
- ///////////////////////////////////////////////////////////////////////////////
- static int renamedir(struct sds_resbuf *rb)
- {
- return renamefile(rb);
- }
-
- ///////////////////////////////////////////////////////////////////////////////
- // renamefile -- rename a file usage; (renamefile "f:/junk" "f:/mbjunk").
- ///////////////////////////////////////////////////////////////////////////////
- static int renamefile(struct sds_resbuf *rb)
- {
- char oldfname[MAXBUFFER], newfname[MAXBUFFER];
-
- // If rb is NULL, prompt for needed information.
- if (rb == NULL) {
- int result = sds_getstring(TRUE, "Enter old file name: ", oldfname);
- if (result != RTNORM) {
- sds_retnil();
- return RTERROR;
- }
- result = sds_getstring(TRUE, "Enter new file name: ", newfname);
- if (result != RTNORM) {
- sds_retnil();
- return RTERROR;
- }
- } else if (rb->restype == RTSTR && rb->rbnext->restype == RTSTR) {
- // We have a string.
- strcpy(oldfname,rb->resval.rstring);
- strcpy(newfname,rb->rbnext->resval.rstring);
- } else {
- // Bad Argument type, return nil to lisp.
- sds_retnil();
- return RTERROR;
- }
-
- CorrectSlashes(oldfname, BACKWARD);
- CorrectSlashes(newfname, BACKWARD);
-
- if (!rename(oldfname, newfname)) {
- sds_retint(1);
- return RTNORM;
- } else {
- sds_retnil();
- return RTERROR;
- }
- }
-
- ///////////////////////////////////////////////////////////////////////////////
- // copyfile -- copies a directory tree usage; (copyfile "d:\\junk\file.txt "g:/junk")
- ///////////////////////////////////////////////////////////////////////////////
- static int copyfile(struct sds_resbuf *rb)
- {
- char source[MAXBUFFER], target[MAXBUFFER], tmpBuffer[MINBUFFER];
-
- // If the user doesn't add args, prompt.
- if (rb == NULL) {
- int result = sds_getstring(TRUE, "Enter source file name: ", source);
- if (result != RTNORM) {
- sds_retnil();
- return RTERROR;
- }
- result = sds_getstring(TRUE, "Enter target directory: ", target);
- if (result != RTNORM) {
- sds_retnil();
- return RTERROR;
- }
- } else if (rb->restype == RTSTR && rb->rbnext->restype == RTSTR) {
- // We have a string.
- strncpy(source,rb->resval.rstring, MAXBUFFER - 1);
- strncpy(target,rb->rbnext->resval.rstring, MAXBUFFER - 1);
- } else {
- // Bad Argument type, return nil to lisp.
- sds_retnil();
- return RTERROR;
- }
-
- CorrectSlashes(source, BACKWARD);
- CorrectSlashes(target, BACKWARD);
-
- // I'm going to assume that if there is no '.' in the target name that it is
- // a directory.
- if (strstr(target, ".") == NULL) {
- // Just in case the user has a trailing back slash on the output path.
- if(target[strlen(target) - 1] == '\\')
- *(strrchr(target, '\\')) = '\0';
-
-
- CreateNestedDirs(target);
- // Here the user has not given the output file a name which means it should have
- // the same name as the source file. So we will append the file name to the output
- // path.
-
- strcat(target, strrchr(source, '\\'));
- } else {
- // Here the user gave us a file name for the target file. So we will use it.
- strcpy(tmpBuffer, target);
- *(strrchr(tmpBuffer, '\\')) = '\0';
- CreateNestedDirs(tmpBuffer);
- }
-
- if (CopyFile(source, target, FALSE) == TRUE) {
- sds_rett();
- return RTNORM;
- } else {
- sds_printf("Unable to copy %s to %s, verify file names.\n",source, target);
- sds_retnil();
- return RTERROR;
- }
-
- return RTNORM;
- }
-
- ///////////////////////////////////////////////////////////////////////////////
- // Helper functions.
- ///////////////////////////////////////////////////////////////////////////////
- ///////////////////////////////////////////////////////////////////////////////
- // CorrectSlashes -- Reverses forward slashes.
- ///////////////////////////////////////////////////////////////////////////////
- void CorrectSlashes(char* fName, int slashDirection)
- {
- char* tmpPtr = NULL;
- if (slashDirection == BACKWARD) {
- // Change forward slashes to backslashes.
- while ((tmpPtr = strstr(fName, FORWARDSLASHSTRING)) != NULL) {
- *tmpPtr = BACKWARDSLASHCHAR;
- }
- } else {
- // Change backward slashes to forwardslashes.
- while ((tmpPtr = strstr(fName, BACKWARDSLASHSTRING)) != NULL) {
- *tmpPtr = FORWARDSLASHCHAR;
- }
- }
- }
-
- ///////////////////////////////////////////////////////////////////////////////
- // CreateNestedDirs -- Loops down path creating each directory in turn.
- ///////////////////////////////////////////////////////////////////////////////
- BOOL CreateNestedDirs(const char* directoryBeingCreated)
- {
- char subDirectory[MAXBUFFER];
- char tmpBuffer[MAXBUFFER];
- char *Ptr;
- int Len = 0, i;
- BOOL bFirstTime = TRUE;
-
- strcpy(tmpBuffer, directoryBeingCreated);
- Ptr = tmpBuffer;
- Len = strlen(tmpBuffer);
-
- for (i = 0; i < Len; i++) {
- if (*(Ptr) != BACKWARDSLASHCHAR) {
- subDirectory[i] = *Ptr;
- } else {
- subDirectory[i] = EOS;
-
- if (_mkdir(subDirectory) == -1) {
- if (bFirstTime == TRUE) {
- bFirstTime = FALSE;
- } else {
- if (errno != EEXIST && errno != EACCES) {
- sprintf(tmpBuffer, "Cannot create this directory %s:\n"
- "This is probably a permissions error, or\n"
- "you may be experiencing network problems.\n"
- "Abort.\n",
- subDirectory);
- MessageBox(NULL, tmpBuffer, "Directory Creation Problem!", MB_OK | MB_ICONHAND | MB_TASKMODAL);
- return (FALSE);
- }
- }
- }
- subDirectory[i] = *Ptr;
- }
- ++Ptr;
- }
- subDirectory[i] = EOS;
- if (_mkdir(subDirectory) == -1) {
- if (errno != EEXIST && errno != EACCES) {
- sprintf(tmpBuffer, "Cannot create this directory %s:\n"
- "This is probably a permissions error, or\n"
- "you may be experiencing network problems.\n"
- "Abort.\n",
- subDirectory);
- MessageBox(NULL, tmpBuffer, "Directory Creation Problem!", MB_OK | MB_ICONHAND | MB_TASKMODAL);
- return (FALSE);
- }
- }
- return (TRUE);
- }
-
- ///////////////////////////////////////////////////////////////////////////////
- // DirsList -- Creates linked list of directories to be copied.
- ///////////////////////////////////////////////////////////////////////////////
- static int DirsList(char* Root, char* Target)
- {
- char tmpBuffer[MAXBUFFER];
- char dirName[MAXBUFFER], targenName[MAXBUFFER];
- struct _finddata_t c_file;
- long hFile;
-
- strcpy(tmpBuffer, Root);
- strcat(tmpBuffer, "\\*.*");
-
-
- // Find first *.* file in requested directory.
- if( (hFile = _findfirst(tmpBuffer, &c_file )) != -1L ) {
-
- if (c_file.attrib & _A_SUBDIR && c_file.name[0] != '.') {
- strcpy(dirName, tmpBuffer);
- strcpy(strrchr(dirName, '\\') + 1, c_file.name);
- if ((DirectoryList->sourceDir = (char*)malloc(sizeof(dirName) + 1)) == NULL) {
- ads_fail("Unable to allocate string buffer.\n");
- ads_retnil();
- return RTERROR;
- }
- if ((DirectoryList->targetDir = (char*)malloc(sizeof(dirName) + 1)) == NULL) {
- ads_fail("Unable to allocate string buffer.\n");
- ads_retnil();
- return RTERROR;
- }
-
- strcpy(DirectoryList->sourceDir, strlwr(dirName));
- strcpy(DirectoryList->targetDir, strlwr(Target));
- DirectoryList->dir_next = NULL;
- DirsList(DirectoryList->sourceDir, DirectoryList->targetDir);
- }
-
- // Find the rest of the files.
- while( _findnext( hFile, &c_file ) == 0 ) {
- if (c_file.attrib & _A_SUBDIR && c_file.name[0] != '.') {
- if ((DirectoryList->dir_next = (struct directoryList*)malloc(sizeof(struct directoryList))) == NULL) {
- ads_fail("Unable to allocate buffer\n");
- ads_retnil();
- return RTERROR;
- }
- DirectoryList = DirectoryList->dir_next;
- DirectoryList->sourceDir = DirectoryList->targetDir = NULL;
- DirectoryList->dir_next = NULL;
-
- strcpy(dirName, tmpBuffer);
- strcpy(strrchr(dirName, BACKWARDSLASHCHAR) + 1, c_file.name);
-
- if ((DirectoryList->sourceDir = (char*)malloc(sizeof(dirName) + 1)) == NULL) {
- ads_fail("Unable to allocate string buffer.\n");
- ads_retnil();
- return RTERROR;
- }
- if ((DirectoryList->targetDir = (char*)malloc(sizeof(dirName) + 1)) == NULL) {
- ads_fail("Unable to allocate string buffer.\n");
- ads_retnil();
- return RTERROR;
- }
- sprintf(targenName, "%s%s%s", Target, BACKWARDSLASHSTRING, c_file.name);
-
- strcpy(DirectoryList->sourceDir , strlwr(dirName));
- strcpy(DirectoryList->targetDir , strlwr(targenName));
- DirectoryList->dir_next = NULL;
- DirsList(DirectoryList->sourceDir, DirectoryList->targetDir);
- }
- }
- _findclose( hFile );
- }
- return RTNORM;
- }
-