Exemple #1
0
/** Free subnodes. */
void gbmtool_free_all_subnodes(GBMTOOL_FILE * firstNode, unsigned int * filecount)
{
  GBMTOOL_FILE * currentNode = firstNode->next;
  GBMTOOL_FILE * nextNode    = NULL;

  *filecount = 0;
  while(currentNode != NULL)
  {
    nextNode = currentNode->next;
    gbmtool_free_node(currentNode);
    *filecount += 1;
    currentNode = nextNode;
  }
}
Exemple #2
0
/**
 *  Frees file arguments.
 *
 * @param gbmfilearg  Struct that initially has been allocated by
 *                    the function gbmtool_parse_argument.
 */
void gbmtool_free_argument(GBMTOOL_FILEARG * gbmfilearg)
{
  /* Don't free argin as this is a const pointer only ! */

  /* free dynamically allocated elements */
  if (gbmfilearg->files != NULL)
  {
    unsigned int count;
    gbmtool_free_all_subnodes(gbmfilearg->files, &count);
    gbmfilearg->filecount -= count;
    gbmtool_free_node(gbmfilearg->files);
    gbmfilearg->filecount -= 1;
    gbmfilearg->files = NULL;
  }
  assert(gbmfilearg->filecount == 0);

  if (gbmfilearg->options != NULL)
  {
    free(gbmfilearg->options);
    gbmfilearg->options = NULL;
  }
}
Exemple #3
0
/**
 * Implements extension for resolving filename with regular expressions.
 *
 * @param filename    Filename (can contain regular expressions)
 *
 * @param filearray   Resolved filename array, will be allocated.
 *                    For the number of entries see filearray_length.
 *                    The client has to take care of freeing it with
 *                    the C library free() function.
 *
 * @param filearray_length  Number of filenames contained in filearray.
 *
 * @retval GBM_TRUE   Success.
 * @retval GBM_FALSE  An error occured.
 */
gbm_boolean gbmtool_findFiles(const char * filename, GBMTOOL_FILE ** files, unsigned int * filecount)
{
  HDIR          hdirFindHandle = HDIR_CREATE;
  FILEFINDBUF3  findBuffer     = {0};      /* Returned from FindFirst/Next */
  ULONG         ulResultBufLen = sizeof(FILEFINDBUF3);
  ULONG         ulFindCount    = 1;        /* Look for 1 file at a time    */
  APIRET        rc             = NO_ERROR; /* Return code                  */

  char   buffer[1025] = { 0 };
  char * path = NULL;
#ifndef NDEBUG
  int    pathlength = 0;
#endif

  GBMTOOL_FILE * firstNode   = NULL;
  GBMTOOL_FILE * currentNode = NULL;

  *filecount = 0;
  *files     = NULL;
  
  if (! getPathFromFullFilename(filename, &path))
  {
    return GBM_FALSE;
  }
#ifndef NDEBUG
  pathlength = strlen(path);
#endif

  rc = DosFindFirst((char *)filename, /* File pattern              */
                    &hdirFindHandle,  /* Directory search handle   */
                    FILE_NORMAL,      /* Search attribute          */
                    &findBuffer,      /* Result buffer             */
                    ulResultBufLen,   /* Result buffer length      */
                    &ulFindCount,     /* Number of entries to find */
                    FIL_STANDARD);    /* Return Level 1 file info  */
  if ((rc != NO_ERROR) || (ulFindCount != 1))
  {
    DosFindClose(hdirFindHandle);
    free(path);
    return GBM_FALSE;
  }

  /* add first found filename */
  assert(pathlength + strlen(findBuffer.achName) + 1 <= sizeof(buffer));
  sprintf(buffer, "%s%s", path, findBuffer.achName);

  *filecount = 0;
  *files = gbmtool_createFileNode(buffer);
  if (*files == NULL)
  {
    DosFindClose(hdirFindHandle);
    free(path);
    return GBM_FALSE;
  }
  *filecount += ulFindCount;

  firstNode   = *files;
  currentNode = firstNode;

  /* Keep finding the next file until there are no more files */
  while (rc != ERROR_NO_MORE_FILES)
  {
    ulFindCount = 1;                  /* Reset find count.         */

    rc = DosFindNext(hdirFindHandle,  /* Directory handle          */
                     &findBuffer,     /* Result buffer             */
                     ulResultBufLen,  /* Result buffer length      */
                     &ulFindCount);   /* Number of entries to find */

     if ((rc != NO_ERROR && rc != ERROR_NO_MORE_FILES) || (ulFindCount > 1))
     {
       unsigned int count;
       gbmtool_free_all_subnodes(firstNode, &count);
       *filecount -= count;
       gbmtool_free_node(firstNode);
       (*filecount)--;
       DosFindClose(hdirFindHandle);
       free(path);
       return GBM_FALSE;
     }

     if ((rc != ERROR_NO_MORE_FILES) && (ulFindCount == 1))
     {
       /* add found filename */
       assert(pathlength + strlen(findBuffer.achName) + 1 <= sizeof(buffer));
       sprintf(buffer, "%s%s", path, findBuffer.achName);

       currentNode->next = gbmtool_createFileNode(buffer);
       if (currentNode->next == NULL)
       {
         unsigned int count;
         gbmtool_free_all_subnodes(firstNode, &count);
         *filecount -= count;
         gbmtool_free_node(firstNode);
         (*filecount)--;
         DosFindClose(hdirFindHandle);
         free(path);
         return GBM_FALSE;
       }
       *filecount += ulFindCount;
       currentNode = currentNode->next;
     }
  } /* endwhile */

  DosFindClose(hdirFindHandle); /* Close our directory handle */
  free(path);

  return GBM_TRUE;
}