Main Page Class Hierarchy Compound List File List Compound Members File Members

# dirops_ew.c

Go to the documentation of this file.

    00001
    00002 /*
    00003  *   THIS FILE IS UNDER RCS - DO NOT MODIFY UNLESS YOU HAVE
    00004  *   CHECKED IT OUT USING THE COMMAND CHECKOUT.
    00005  *
    00006  *    $Id: dirops__ew_8c-source.html 2161 2006-05-19 16:55:03Z paulf $
    00007  *
    00008  *    Revision history:
    00009  *     $Log$
    00009  *     Revision 1.1  2006/05/19 16:55:01  paulf
    00009  *     first inclusion
    00009  *
    00010  *     Revision 1.4  2001/04/05 18:19:31  cjbryan
    00011  *     added RecursiveCreateDir function to recursively create all
    00012  *     directories in a specifed path
    00013  *
    00014  *     Revision 1.3  2000/09/28 23:19:38  dietz
    00015  *     added function rename_ew
    00016  *
    00017  *     Revision 1.2  2000/03/10 23:36:48  davidk
    00018  *     added an include of <direct.h> to resolve a compiler warning about
    00019  *     Create_dir()
    00020  *
    00021  *     Revision 1.1  2000/02/14 18:53:30  lucky
    00022  *     Initial revision
    00023  *
    00024  *
    00025  */
    00026
    00027          /************************************************************
    00028           *            dirops_ew.c  Windows NT version               *
    00029           *                                                          *
    00030           *  Contains system-specific functions for dealing with     *
    00031           *  directories                                             *
    00032           ************************************************************/
    00033
    00034 #include <windows.h>
    00035 #include <sys/stat.h>
    00036 #include <errno.h>
    00037 #include <earthworm.h>
    00038 #include <stdio.h>
    00039 #include <direct.h>
    00040
    00041
    00042            /*******************************************************
    00043             *  chdir_ew( )  changes current working directory     *
    00044             *                                                     *
    00045             *  Returns 0 if all went well                         *
    00046             *         -1 if an error occurred                     *
    00047             *******************************************************/
    00048
    00049 int chdir_ew( char *path )
    00050 {
    00051    BOOL success;
    00052
    00053    success = SetCurrentDirectory( path );
    00054
    00055    if ( success )
    00056       return 0;
    00057    else
    00058       return -1;
    00059 }
    00060
    00061
    00062
    00063 /*****************************************************************************
    00064  *  CreateDir ()  Creates a directory. Solaris version.                      *
    00065  *                                                                           *
    00066  *  if dirname exists and is accessible, EW_SUCCESS is returned. Otherwise,  *
    00067  *  we attempt to create it. If it all goes well, we return EW_SUCCESS,      *
    00068  *  otherwise we report error and return EW_FAILURE.                         *
    00069  *       LV 8/16/1999                                                        *
    00070  *****************************************************************************/
    00071 int CreateDir (char *dirname)
    00072 {
    00073    struct stat buf;
    00074
    00075    if (dirname == NULL)
    00076    {
    00077       logit ("e", "Invalid argument passed in; exiting!\n");
    00078       return EW_FAILURE;
    00079    }
    00080
    00081 /* does it already exist? */
    00082    if (stat (dirname, &buf) != 0)
    00083    {
    00084       if (errno == ENOENT)
    00085       {
    00086          if (mkdir (dirname) != 0)
    00087          {
    00088             logit ("e", "CreateDir: Cannot create %s: %s\n",
    00089                     dirname, strerror(errno) );
    00090             return( EW_FAILURE);
    00091          }
    00092       }
    00093       else
    00094       {
    00095          logit ("e", "CreateDir: Cannot stat %s - %s \n",
    00096                  dirname, strerror (errno));
    00097          return EW_FAILURE;
    00098       }
    00099
    00100    }
    00101    else
    00102    {
    00103       /* do nothing - hope that this is a directory */
    00104    }
    00105
    00106    return EW_SUCCESS;
    00107
    00108 }
    00109
    00110 /*****************************************************************************
    00111  *  Recursive CreateDir ()  Recursively creates all directories in a         *
    00112  *  specified path. First checks to see if only directory to create is last  *
    00113  *  one in path; if not creates directories one at a time through calls to   *
    00114  *  to CreateDir().
    00115  *                                                                           *
    00116  *  if dirname exists and is accessible, EW_SUCCESS is returned. Otherwise,  *
    00117  *  we attempt to create it. If it all goes well, we return EW_SUCCESS,      *
    00118  *  otherwise we report error and return EW_FAILURE.                         *
    00119  *       CJB 3/29/01                                                         *
    00120  *****************************************************************************/
    00121 int RecursiveCreateDir(char *dirname) {
    00122
    00123         struct  stat    buf;
    00124         char            *dname;
    00125         char            *where;
    00126         int             place;
    00127         int             len;
    00128         int             find = '\\';
    00129         char            directory[MAX_DIR_LEN];
    00130
    00131         /* check to make sure directory name is not too long */
    00132         if (strlen(dirname) > (MAX_DIR_LEN - 1))
    00133         {
    00134                 logit("e", "RecursiveCreateDir: directory name %s exceeds allowed length %d \n",
    00135                                 dirname, MAX_DIR_LEN);
    00136                 return EW_FAILURE;
    00137         }
    00138
    00139         strcpy(directory, "");
    00140         if (dirname == NULL)
    00141         {
    00142                 return EW_FAILURE;
    00143         }
    00144
    00145         /* does it already exist? */
    00146         if (stat (dirname, &buf) != 0)
    00147         {
    00148                 if (errno == ENOENT)
    00149                 {
    00150                         /* first lets just try and make it; if this doesn't work
    00151                                 we'll start at the beginning and make each subdirectory */
    00152                         if (mkdir (dirname) != 0)
    00153                         {
    00154                                 dname = dirname;
    00155                                 while ((where = strchr(dname, find)) != NULL) {
    00156                                         place = where - dname;
    00157                                         len = strlen(directory);
    00158                                         strncat(directory, dname, place);
    00159                                         directory[len + place] = '\0';
    00160                                         if (directory[strlen(directory) - 1] != ':')
    00161                                         {
    00162                                                 if(CreateDir(directory) != EW_SUCCESS)
    00163                                                 {
    00164                                                         logit ("e", "RecursiveCreateDir: Cannot create %s\n",
    00165                                                                 directory);
    00166                                                         return EW_FAILURE;
    00167                                                 }
    00168                                         }
    00169                                         /* add directory delimiter '\'  */
    00170                                         strcat(directory, "\\");
    00171                                         dname += place + 1;
    00172
    00173                                 } /* end of while */
    00174                                 /* finally add name of final subdirectory */
    00175                                 strcat(directory, dname);
    00176                                 if(CreateDir(directory) != EW_SUCCESS)
    00177                                 {
    00178                                         logit ("e", "RecursiveCreateDir: Cannot create %s\n",
    00179                                                 directory);
    00180                                         return EW_FAILURE;
    00181                                 }
    00182                         }
    00183                 }
    00184                 else
    00185                 {
    00186                         return EW_FAILURE;
    00187                 }
    00188
    00189         }
    00190         else
    00191         {
    00192                 /* do nothing - hope that this is a directory */
    00193         }
    00194
    00195    return EW_SUCCESS;
    00196 }
    00197
    00198 /***************************************************************
    00199  *  GetFileName   (from Will Kohler's sendfile)                *
    00200  *                                                             *
    00201  *  Function to get the name of a file in directory "Path".    *
    00202  *                                                             *
    00203  *  Returns 0 if all ok                                        *
    00204  *          1 if no files were found                           *
    00205  ***************************************************************/
    00206
    00207 int GetFileName( char fname[] )
    00208 {
    00209    extern char Path[80];     /* Directory containing files to be sent */
    00210    char            fileMask[] = "*";
    00211    WIN32_FIND_DATA findData;
    00212    HANDLE          fileHandle;
    00213    FILE            *fp;
    00214
    00215    strcpy( fileMask, "*" );
    00216
    00217 /* Get the name of the first file.
    00218    The file may be a directory or a partially-written file.
    00219    If so, skip this file and look for others.
    00220    *******************************************************/
    00221    fileHandle = FindFirstFile( fileMask, &findData );
    00222    if ( fileHandle == INVALID_HANDLE_VALUE )         /* No files found */
    00223       return 1;
    00224
    00225    fp = fopen( findData.cFileName, "rb" );
    00226    if ( fp != NULL )                 /* File can be opened */
    00227    {
    00228       fclose( fp );
    00229       strcpy( fname, findData.cFileName );
    00230       FindClose( fileHandle );
    00231       return 0;
    00232    }
    00233
    00234 /* First file is a directory or it is otherwise unopenable.
    00235    Find another file, if any.
    00236    *******************************************************/
    00237    while ( FindNextFile( fileHandle, &findData ) )
    00238    {
    00239       fp = fopen( findData.cFileName, "rb" );
    00240
    00241       if ( fp != NULL )                 /* File can be opened */
    00242       {
    00243          fclose( fp );
    00244          strcpy( fname, findData.cFileName );       /* Found a file */
    00245          FindClose( fileHandle );
    00246          return 0;
    00247       }
    00248    }
    00249
    00250    FindClose( fileHandle );          /* No files found */
    00251    return 1;
    00252 }
    00253
    00254
    00255 /**********************************************************************
    00256  *  rename_ew( )  Moves a file                   Windows version      *
    00257  *  path1 = name of file to be moved.  path2 = destination name       *
    00258  *  Returns -1 if an error occurred; 0 otherwise                      *
    00259  **********************************************************************/
    00260
    00261 int rename_ew( char *path1, char *path2 )
    00262 {
    00263    if ( MoveFileEx( path1, path2, MOVEFILE_REPLACE_EXISTING ) == 0 )
    00264       return -1;
    00265
    00266    return 0;
    00267 }
    00268