src/share/bin/wildcard.c

Print this page


   1 /*
   2  * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 119  * of an entry in the wildcard's directory.  The basename's memory
 120  * belongs to the iterator.  The caller is responsible for prepending
 121  * the directory name and file separator, if necessary.
 122  * When done with the iterator, call the close method to clean up.
 123  */
 124 typedef struct WildcardIterator_* WildcardIterator;
 125 
 126 #ifdef _WIN32
 127 struct WildcardIterator_
 128 {
 129     HANDLE handle;
 130     char *firstFile; /* Stupid FindFirstFile...FindNextFile */
 131 };
 132 // since this is used repeatedly we keep it here.
 133 static WIN32_FIND_DATA find_data;
 134 static WildcardIterator
 135 WildcardIterator_for(const char *wildcard)
 136 {
 137     WildcardIterator it = NEW_(WildcardIterator);
 138     HANDLE handle = FindFirstFile(wildcard, &find_data);
 139     if (handle == INVALID_HANDLE_VALUE)

 140         return NULL;

 141     it->handle = handle;
 142     it->firstFile = find_data.cFileName;
 143     return it;
 144 }
 145 
 146 static char *
 147 WildcardIterator_next(WildcardIterator it)
 148 {
 149     if (it->firstFile != NULL) {
 150         char *firstFile = it->firstFile;
 151         it->firstFile = NULL;
 152         return firstFile;
 153     }
 154     return FindNextFile(it->handle, &find_data)
 155         ? find_data.cFileName : NULL;
 156 }
 157 
 158 static void
 159 WildcardIterator_close(WildcardIterator it)
 160 {


   1 /*
   2  * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 119  * of an entry in the wildcard's directory.  The basename's memory
 120  * belongs to the iterator.  The caller is responsible for prepending
 121  * the directory name and file separator, if necessary.
 122  * When done with the iterator, call the close method to clean up.
 123  */
 124 typedef struct WildcardIterator_* WildcardIterator;
 125 
 126 #ifdef _WIN32
 127 struct WildcardIterator_
 128 {
 129     HANDLE handle;
 130     char *firstFile; /* Stupid FindFirstFile...FindNextFile */
 131 };
 132 // since this is used repeatedly we keep it here.
 133 static WIN32_FIND_DATA find_data;
 134 static WildcardIterator
 135 WildcardIterator_for(const char *wildcard)
 136 {
 137     WildcardIterator it = NEW_(WildcardIterator);
 138     HANDLE handle = FindFirstFile(wildcard, &find_data);
 139     if (handle == INVALID_HANDLE_VALUE) {
 140         JLI_MemFree(it);
 141         return NULL;
 142     }
 143     it->handle = handle;
 144     it->firstFile = find_data.cFileName;
 145     return it;
 146 }
 147 
 148 static char *
 149 WildcardIterator_next(WildcardIterator it)
 150 {
 151     if (it->firstFile != NULL) {
 152         char *firstFile = it->firstFile;
 153         it->firstFile = NULL;
 154         return firstFile;
 155     }
 156     return FindNextFile(it->handle, &find_data)
 157         ? find_data.cFileName : NULL;
 158 }
 159 
 160 static void
 161 WildcardIterator_close(WildcardIterator it)
 162 {