Contiki 2.6

cfs-win32-dir.c

00001 /*
00002  * Copyright (c) 2006, Swedish Institute of Computer Science.
00003  * All rights reserved. 
00004  *
00005  * Redistribution and use in source and binary forms, with or without 
00006  * modification, are permitted provided that the following conditions 
00007  * are met: 
00008  * 1. Redistributions of source code must retain the above copyright 
00009  *    notice, this list of conditions and the following disclaimer. 
00010  * 2. Redistributions in binary form must reproduce the above copyright 
00011  *    notice, this list of conditions and the following disclaimer in the 
00012  *    documentation and/or other materials provided with the distribution. 
00013  * 3. Neither the name of the Institute nor the names of its contributors 
00014  *    may be used to endorse or promote products derived from this software 
00015  *    without specific prior written permission. 
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
00027  * SUCH DAMAGE. 
00028  *
00029  * This file is part of the Contiki operating system.
00030  * 
00031  * Author: Oliver Schmidt <ol.sc@web.de>
00032  *
00033  * $Id: cfs-win32-dir.c,v 1.1 2008/07/06 10:24:38 oliverschmidt Exp $
00034  */
00035 
00036 #define WIN32_LEAN_AND_MEAN
00037 #include <windows.h>
00038 #include <stdlib.h>
00039 #include <stdio.h>
00040 
00041 #include "contiki.h"
00042 
00043 #include "cfs/cfs.h"
00044 
00045 struct cfs_win32_dir {
00046   HANDLE       handle;
00047   char*        name;
00048   unsigned int size;
00049 };
00050 
00051 /*---------------------------------------------------------------------------*/
00052 int
00053 cfs_opendir(struct cfs_dir *p, const char *n)
00054 {
00055   struct cfs_win32_dir *dir = (struct cfs_win32_dir *)p;
00056   WIN32_FIND_DATA data;
00057   char dirname[MAX_PATH];
00058 
00059   if(*n == '/') {
00060     GetModuleFileName(NULL, dirname, sizeof(dirname));
00061     strcpy(strrchr(dirname, '\\'), "/*");
00062   }else{
00063     sprintf(dirname, "%s/*", n);
00064   }
00065   
00066   dir->handle = FindFirstFile(dirname, &data);
00067   if(dir->handle == INVALID_HANDLE_VALUE) {
00068     dir->name = NULL;
00069     return -1;
00070   }
00071   dir->name = strdup(data.cFileName);
00072   dir->size = ((data.nFileSizeLow + 511) / 512) % 1000; 
00073   return 0;
00074 }
00075 /*---------------------------------------------------------------------------*/
00076 int
00077 cfs_readdir(struct cfs_dir *p, struct cfs_dirent *e)
00078 {
00079   struct cfs_win32_dir *dir = (struct cfs_win32_dir *)p;
00080   WIN32_FIND_DATA data;
00081   
00082   if(dir->name == NULL) {
00083     return -1;
00084   }
00085 
00086   strncpy(e->name, dir->name, sizeof(e->name));
00087   e->size = dir->size;
00088 
00089   free(dir->name);
00090   if(FindNextFile(dir->handle, &data) == 0) {
00091     dir->name = NULL;
00092     return 0;
00093   }
00094   dir->name = strdup(data.cFileName);
00095   dir->size = ((data.nFileSizeLow + 511) / 512) % 1000;
00096   return 0;
00097 }
00098 /*---------------------------------------------------------------------------*/
00099 void
00100 cfs_closedir(struct cfs_dir *p)
00101 {
00102   struct cfs_win32_dir *dir = (struct cfs_win32_dir *)p;
00103 
00104   free(dir->name);
00105   FindClose(dir->handle);
00106 }
00107 /*---------------------------------------------------------------------------*/