From: <ped...@us...> - 2007-06-25 01:14:18
|
Revision: 1007 http://svn.sourceforge.net/cegcc/?rev=1007&view=rev Author: pedroalves Date: 2007-06-24 18:14:15 -0700 (Sun, 24 Jun 2007) Log Message: ----------- Initial import. * LICENSE, README, TODO, ChangeLog, Makefile, cwd.c cwd.h: New files. Added Paths: ----------- trunk/cegcc/tools/libcwd/ trunk/cegcc/tools/libcwd/ChangeLog trunk/cegcc/tools/libcwd/LICENSE trunk/cegcc/tools/libcwd/Makefile trunk/cegcc/tools/libcwd/README trunk/cegcc/tools/libcwd/TODO trunk/cegcc/tools/libcwd/cwd.c trunk/cegcc/tools/libcwd/cwd.h Added: trunk/cegcc/tools/libcwd/ChangeLog =================================================================== --- trunk/cegcc/tools/libcwd/ChangeLog (rev 0) +++ trunk/cegcc/tools/libcwd/ChangeLog 2007-06-25 01:14:15 UTC (rev 1007) @@ -0,0 +1,13 @@ +2007-06-25 Pedro Alves <ped...@po...> + + Initial import. + + * LICENSE, README, TODO, ChangeLog, Makefile, cwd.c cwd.h: New files. + + +Local Variables: +mode: change-log +left-margin: 8 +fill-column: 74 +version-control: never +End: Property changes on: trunk/cegcc/tools/libcwd/ChangeLog ___________________________________________________________________ Name: svn:eol-style + native Added: trunk/cegcc/tools/libcwd/LICENSE =================================================================== --- trunk/cegcc/tools/libcwd/LICENSE (rev 0) +++ trunk/cegcc/tools/libcwd/LICENSE 2007-06-25 01:14:15 UTC (rev 1007) @@ -0,0 +1,25 @@ +/* Copyright (c) 2007, Pedro Alves + +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. +3. The name of the author may not be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR +IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ Added: trunk/cegcc/tools/libcwd/Makefile =================================================================== --- trunk/cegcc/tools/libcwd/Makefile (rev 0) +++ trunk/cegcc/tools/libcwd/Makefile 2007-06-25 01:14:15 UTC (rev 1007) @@ -0,0 +1,62 @@ +TARGET=arm-wince-mingw32ce +LDFLAGS= +INCLUDES=-I. +CFLAGS=-O0 -g3 +WARNFLAGS=-Wall -Wextra + +ALLFLAGS=$(WARNFLAGS) $(CFLAGS) $(INCLUDES) + +CC=$(TARGET)-gcc +AR=$(TARGET)-ar + +LIBS = libcwd.a +TARGETS = $(LIBS) + +OBJECTS = cwd.o + +all: $(TARGETS) +libs: $(LIBS) + +.c.o: + $(CC) -c -o $@ $< $(ALLFLAGS) + +cwd.o: cwd.c cwd.h Makefile +stat.o: stat.o cwd.h Makefile +open.o: open.o cwd.h Makefile +rename.o: rename.o cwd.h Makefile +stat.o: stat.o cwd.h Makefile +tempnam.o: tempnam.o cwd.h Makefile +unlink.o: unlink.o cwd.h Makefile + +libcwd.a: $(OBJECTS) Makefile + rm -f $@ + $(AR) r $@ $(OBJECTS) + +install: + +clean: + rm -f $(LIBS) $(OBJECTS) + +dist: srcdist bindist + +srcdist: all + rm -rf $(distdir) + mkdir $(distdir) + chmod 755 $(distdir) + for i in $(SRCDIST_FILES); do \ + cp -p $(srcdir)/$$i $(distdir)/$$i ; \ + done + rm -f $(distdir).tar.gz + $(TAR) $(TARFLAGS)cf $(distdir)-src$(TARFILEEXT) $(distdir) + +bindist: all + rm -rf $(distdir) + mkdir $(distdir) + chmod 755 $(distdir) + for i in $(BINDIST_FILES); do \ + cp -p $(srcdir)/$$i $(distdir)/$$i ; \ + done + rm -f $(distdir).tar.gz + $(TAR) $(TARFLAGS)cf $(distdir)-bin$(TARFILEEXT) $(distdir) + +.PHONY: all install clean dist bindist srcdist Property changes on: trunk/cegcc/tools/libcwd/Makefile ___________________________________________________________________ Name: svn:eol-style + native Added: trunk/cegcc/tools/libcwd/README =================================================================== --- trunk/cegcc/tools/libcwd/README (rev 0) +++ trunk/cegcc/tools/libcwd/README 2007-06-25 01:14:15 UTC (rev 1007) @@ -0,0 +1,22 @@ +This is a cwd lib for WinCE. + +This is a lib that serves as a porting aid. It provides chdir and +getcwd calls, thus a current working directory notion. + +Note that this lib is built as a static lib, so be careful with +linking the lib into several dlls/exe. Don't expect the same cwd +to be seen across dlls. For that to work, you'll need to build a +libcwd.dll. + +Have fun! + +Pedro Alves + +-- + +See the file LICENSE in the various directories, for a description of +the license the software is licensed in. + +REPORTING BUGS: +ce...@so... +pe...@po... Property changes on: trunk/cegcc/tools/libcwd/README ___________________________________________________________________ Name: svn:eol-style + native Added: trunk/cegcc/tools/libcwd/TODO =================================================================== --- trunk/cegcc/tools/libcwd/TODO (rev 0) +++ trunk/cegcc/tools/libcwd/TODO 2007-06-25 01:14:15 UTC (rev 1007) @@ -0,0 +1,2 @@ + + * Add a mutex locking access to cwd. Property changes on: trunk/cegcc/tools/libcwd/TODO ___________________________________________________________________ Name: svn:eol-style + native Added: trunk/cegcc/tools/libcwd/cwd.c =================================================================== --- trunk/cegcc/tools/libcwd/cwd.c (rev 0) +++ trunk/cegcc/tools/libcwd/cwd.c 2007-06-25 01:14:15 UTC (rev 1007) @@ -0,0 +1,320 @@ +#include <unistd.h> +#include <string.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <wchar.h> + +#include <windows.h> + +#include "cwd.h" + +static wchar_t *XCEFixPathW (const wchar_t *wpathin, wchar_t *wpathout); +static wchar_t *XCECanonicalizePathW (wchar_t *target); +static DWORD XCEGetCurrentDirectoryW (DWORD dwSize, wchar_t *buf); + +#define COUNTOF(X) (sizeof (X) / sizeof ((X)[0])) +#define MIN(A, B) ((A) < (B) ? (A) : (B)) + +#define IS_DIRECTORY_SEP(X) (X == '\\') +#define IS_ANY_SEP(X) (X == '\\') + +/* Per process as on WinNT + ### TODO, we need a mutex here if we ever go multithread. */ + +static wchar_t _current_dirw[MAX_PATH+1] = L"\\"; +#define _CURRENTDIRW _current_dirw + +static DWORD +XCEGetFileAttributesW (const wchar_t *wfname) +{ + wchar_t wpath[MAX_PATH]; + DWORD dwRes; + + XCEFixPathW (wfname, wpath); + dwRes = GetFileAttributesW (wpath); + + return dwRes; +} + +static char * +XCEToUnixPath (char *s, int len) +{ + char *p; + + if (len < 0) + { + for(p = s; *p; p++) + if(*p == '\\') + *p = '/'; + } + else + { + int i; + for(i = 0; i < len ; i++) + if(s[i] == '\\') + s[i] = '/'; + } + + return s; +} + +static void +append_slash_if_needed (wchar_t* wbuf) +{ + int len = wcslen (wbuf); + if (len > 0 + && wbuf[len - 1] != '\\' + && wbuf[len - 1] != '/') + wcscat (wbuf, L"\\"); +} + +static wchar_t * +XCEFixPathW (const wchar_t *wpathin, wchar_t *wpathout) +{ + wchar_t wdir[MAX_PATH+1]; + wchar_t *p; + + wpathout[0] = 0; + append_slash_if_needed (wpathout); + + if(wpathin[0] != '\\' && wpathin[0] != '/') + { + XCEGetCurrentDirectoryW (sizeof(wdir), wdir); + wcscat (wpathout, wdir); + append_slash_if_needed (wpathout); + } + + wcscat (wpathout, wpathin); + + for(p = wpathout; *p; p++) + { + if(*p == '/') + *p = '\\'; + } + + /* don't allow slash at end of directory name... */ + if(p[-1] == '\\' && p != wpathout + 1) + p[-1] = 0; + + /* now remove . and .. */ + XCECanonicalizePathW (wpathout); + + return wpathout; +} + +static wchar_t * +XCECanonicalizePathW (wchar_t *target) +{ + wchar_t *p = target; + wchar_t *o = target; + + while (*p) + { + if (!IS_DIRECTORY_SEP (*p)) + { + *o++ = *p++; + } + else if (IS_DIRECTORY_SEP (p[0]) + && p[1] == '.' + && (IS_DIRECTORY_SEP (p[2]) + || p[2] == 0)) + { + /* If "/." is the entire filename, keep the "/". Otherwise, + just delete the whole "/.". */ + if (o == target && p[2] == '\0') + *o++ = *p; + p += 2; + } + else if (IS_DIRECTORY_SEP (p[0]) && p[1] == '.' && p[2] == '.' + /* `/../' is the "superroot" on certain file systems. */ + /* disabled this test, as it wasn't filtering '/..' because of it. + cegcc should also have the same problem. */ + /* && o != target */ + && (IS_DIRECTORY_SEP (p[3]) || p[3] == 0)) + { + while (o != target && (--o) && !IS_DIRECTORY_SEP (*o)) + ; + /* Keep initial / only if this is the whole name. */ + if (o == target && IS_ANY_SEP (*o) && p[3] == 0) + ++o; + p += 3; + } + else + { + *o++ = *p++; + } + } + + *o = 0; + + return target; +} + +static DWORD +XCEGetCurrentDirectoryW (DWORD dwSize, wchar_t *buf) +{ + size_t len = wcslen(_CURRENTDIRW); + if (dwSize == 0 && buf == 0) + return len+1; + wcsncpy (buf, _CURRENTDIRW, dwSize - 1); + if (dwSize > len) + return len; + else + return len+1; +} + +static DWORD +XCEGetCurrentDirectoryA (DWORD dwSize, char *buf) +{ + DWORD dwLen; + wchar_t wbuf[MAX_PATH+1]; + + dwLen = XCEGetCurrentDirectoryW (dwSize, wbuf); + if (dwSize == 0 && buf == 0) + return dwSize; + WideCharToMultiByte (CP_ACP, 0, wbuf, -1, + buf, MIN(dwLen, dwSize), NULL, NULL); + buf[MIN(dwLen, dwSize)] = 0; + return dwLen; +} + +static BOOL +XCESetCurrentDirectoryW (const wchar_t *wdir) +{ + DWORD dwAttr; + wchar_t wtmp[MAX_PATH]; + + int wlen = wcslen (wdir); + if (wlen > (MAX_PATH - 1)) + return 0; + else if (wlen > 0 + && !(wdir[wlen-1] == '\\' || wdir[wlen-1] == '/') + && wlen > (MAX_PATH - 2)) + return 0; + + /* Oh great... There is some code generation bug in mingw32ce. Inlining + the call hides the problem. */ + // XCEFixPathW (wdir, wtmp); + const wchar_t *wpathin = wdir; + wchar_t *wpathout = wtmp; + { + wchar_t wdir[MAX_PATH+1]; + wchar_t *p; + + wpathout[0] = 0; + + if(wpathin[0] != '\\' && wpathin[0] != '/') + { + XCEGetCurrentDirectoryW (sizeof(wdir), wdir); + wcscat (wpathout, wdir); + append_slash_if_needed (wpathout); + } + + wcscat (wpathout, wpathin); + + for(p = wpathout; *p; p++) + { + if(*p == '/') + *p = '\\'; + } + + /* Don't allow slash at end of directory name... */ + if(p[-1] == '\\' && p != wpathout + 1) + p[-1] = 0; + + /* Now remove . and .. */ + XCECanonicalizePathW (wpathout); + } + + if((dwAttr = XCEGetFileAttributesW (wtmp)) == 0xFFFFFFFF) + return FALSE; + + if((dwAttr & FILE_ATTRIBUTE_DIRECTORY) == 0) + { +// errno = ENOTDIR; + return FALSE; + } + + wcscpy (_CURRENTDIRW, wtmp); + return TRUE; +} + +static BOOL +XCESetCurrentDirectoryA (const char *dir) +{ + wchar_t wdir[MAX_PATH]; + int len = strlen(dir); + if (len > (MAX_PATH - 1)) + return 0; + else if (len>0 + && !(dir[len-1] == '\\' || dir[len-1] == '/') + && len > (MAX_PATH - 2)) + return 0; + + MultiByteToWideChar (CP_ACP, 0, dir, -1, wdir, COUNTOF (wdir)); + return XCESetCurrentDirectoryW (wdir); +} + +int +_chdir (const char *path) +{ + char fpath[MAX_PATH+1]; + + if (path == NULL || strlen (path) == 0) + { + /* errno? */ + return -1; + } + + if (!XCESetCurrentDirectoryA (path)) + return -1; + + XCEGetCurrentDirectoryA (MAX_PATH + 1, fpath); + XCEToUnixPath (fpath, -1); + return 0; +} + +char * +_getcwd (char *buf, int size) +{ + int len; + char *alloced = NULL; + + if (buf == NULL || size == 0) + { + size = MAX_PATH + 1; + buf = alloced = malloc (size); + if (!buf) + return NULL; + } + + len = XCEGetCurrentDirectoryA ((DWORD) size, buf); + + if (len > size) + { + XCEToUnixPath (buf, size - 1); + return NULL; + } + + if (len == 0) + { + if (alloced != NULL) + free (alloced); + return NULL; + } + + XCEToUnixPath (buf, -1); + return buf; +} + +int +chdir (const char *path) +{ + return _chdir (path); +} + +char * +getcwd (char *buf, int size) +{ + return _getcwd (buf, size); +} Property changes on: trunk/cegcc/tools/libcwd/cwd.c ___________________________________________________________________ Name: svn:eol-style + native Added: trunk/cegcc/tools/libcwd/cwd.h =================================================================== --- trunk/cegcc/tools/libcwd/cwd.h (rev 0) +++ trunk/cegcc/tools/libcwd/cwd.h 2007-06-25 01:14:15 UTC (rev 1007) @@ -0,0 +1,19 @@ +#ifndef __LIB_CE_CWD_H_ +#define __LIB_CE_CWD_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include <stdlib.h> + +extern char *getcwd (char *buf, int size); +extern char *_getcwd (char *buf, int size); +extern int chdir (const char *path); +extern int _chdir (const char *path); + +#ifdef __cplusplus +} +#endif + +#endif /* __LIB_CE_CWD_H_ */ Property changes on: trunk/cegcc/tools/libcwd/cwd.h ___________________________________________________________________ Name: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |