newlib: add rewind.c

git-svn-id: svn://kolibrios.org@5198 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
Sergey Semyonov (Serge) 2014-11-29 05:40:44 +00:00
parent 40cd438474
commit 84eba6470e
5 changed files with 181 additions and 23 deletions

View File

@ -75,7 +75,7 @@ CORE_SRCS:= \
ctype/iswalnum.c \
ctype/iswalpha.c \
ctype/iswblank.c \
ctype/iswcntrl.c \
ctype/iswcntrl.c \
ctype/iswdigit.c \
ctype/iswgraph.c \
ctype/iswlower.c \
@ -272,6 +272,7 @@ STDIO_SRCS= \
refill.c \
rget.c \
remove.c \
rewind.c \
setvbuf.c \
stdio.c \
tmpfile.c \

View File

@ -37,9 +37,6 @@ extern int main (int, char **, char **);
int _errno;
int _fmode;
char __appcwd[1024];
int __appcwdlen;
int _argc;
char **_argv;
@ -98,13 +95,6 @@ __crt_startup (void)
__do_global_ctors();
__appcwdlen = strrchr(&__pgmname[0], '/') - &__pgmname[0] + 1;
__appcwdlen = __appcwdlen > 1023 ? 1023 : __appcwdlen;
memcpy(__appcwd, &__pgmname, __appcwdlen);
__appcwd[__appcwdlen] = 0;
set_cwd(__appcwd);
arg[0] = &__pgmname[0];
if( __cmdline[0] != 0)

View File

@ -33,8 +33,6 @@ int link_app();
void* get_entry_point(void *raw);
int (*entry)(int, char **, char **);
char __appcwd[1024];
int __appcwdlen;
char* __appenv;
int __appenv_size;
@ -202,11 +200,6 @@ libc_crt_startup (void *libc_base)
if( link_app() == 0)
goto done;
__appcwdlen = strrchr(header->path, '/') - header->path;
__appcwdlen = __appcwdlen > 1022 ? 1022 : __appcwdlen;
memcpy(__appcwd, header->path, __appcwdlen);
set_cwd(__appcwd);
if( header->cmdline[0] != 0)
{
argc = split_cmdline(header->cmdline, NULL) + 1;

View File

@ -0,0 +1,81 @@
/*
* Copyright (c) 1990 The Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms are permitted
* provided that the above copyright notice and this paragraph are
* duplicated in all such forms and that any documentation,
* advertising materials, and other materials related to such
* distribution and use acknowledge that the software was developed
* by the University of California, Berkeley. The name of the
* University may not be used to endorse or promote products derived
* from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
/*
FUNCTION
<<rewind>>---reinitialize a file or stream
INDEX
rewind
INDEX
_rewind_r
ANSI_SYNOPSIS
#include <stdio.h>
void rewind(FILE *<[fp]>);
void _rewind_r(struct _reent *<[ptr]>, FILE *<[fp]>);
TRAD_SYNOPSIS
#include <stdio.h>
void rewind(<[fp]>)
FILE *<[fp]>;
void _rewind_r(<[ptr]>, <[fp]>)
struct _reent *<[ptr]>;
FILE *<[fp]>;
DESCRIPTION
<<rewind>> returns the file position indicator (if any) for the file
or stream identified by <[fp]> to the beginning of the file. It also
clears any error indicator and flushes any pending output.
RETURNS
<<rewind>> does not return a result.
PORTABILITY
ANSI C requires <<rewind>>.
No supporting OS subroutines are required.
*/
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "%W% (Berkeley) %G%";
#endif /* LIBC_SCCS and not lint */
#include <_ansi.h>
#include <reent.h>
#include <stdio.h>
_VOID
_DEFUN(_rewind_r, (ptr, fp),
struct _reent * ptr _AND
register FILE * fp)
{
_CAST_VOID _fseek_r (ptr, fp, 0L, SEEK_SET);
clearerr (fp);
}
#ifndef _REENT_ONLY
_VOID
_DEFUN(rewind, (fp),
register FILE * fp)
{
_rewind_r (_REENT, fp);
}
#endif /* !_REENT_ONLY */

View File

@ -22,8 +22,89 @@
#undef erro
extern int errno;
static inline int is_slash(char c)
{
return c=='/' || c=='\\';
}
void fix_slashes(char * in,char * out)
{
int slash_count;
for(slash_count=1;in && out && *in;in++)
{
if(is_slash(*in))
{
slash_count++;
continue;
}
else
{
if(slash_count)
{
slash_count=0;
*out++='/';
}
*out++=*in;
}
}
*out='\0';
};
void buildpath(char *buf, const char* file)
{
char *ptr;
ptr = buf + strlen(buf);
while (*file)
{
if (file[0] == '.' && file[1] == 0)
break;
if (file[0] == '.' && file[1] == '/')
{
file+=2;
continue;
};
if (file[0] == '.' && file[1] == '.' &&
(file[2] == 0 || file[2] == '/'))
{
while (ptr > buf && ptr[-1] != '/')
--ptr;
file+=2;
if (*file == 0)
break;
++file;
continue;
}
*ptr++ = '/';
if (*file == '/')
++file;
while (*file && *file!='/')
*ptr++ = *file++;
}
*ptr = 0;
};
static char *getccwd(char *buf, size_t size)
{
int bsize;
__asm__ __volatile__(
"int $0x40"
:"=a"(bsize)
:"a"(30),"b"(2),"c"(buf), "d"(size)
:"memory");
return buf;
};
int open (const char * filename, int flags, ...)
{
char buf[1024];
__io_handle *ioh;
fileinfo_t info;
int iomode, rwmode, offset;
@ -37,9 +118,19 @@ int open (const char * filename, int flags, ...)
return (-1);
};
// path = getfullpath(name);
if (filename[0]=='/')
{
strcpy(buf,filename);
}
else
{
getccwd(buf, 1024);
buildpath(buf, filename);
}
err = get_fileinfo(filename, &info);
// printf("%s %s\n", __FUNCTION__, buf);
err = get_fileinfo(buf, &info);
if( flags & O_EXCL &&
flags & O_CREAT )
@ -54,7 +145,7 @@ int open (const char * filename, int flags, ...)
if( err )
{
if(flags & O_CREAT)
err=create_file(filename);
err=create_file(buf);
if( err )
{
errno = EACCES;
@ -63,7 +154,7 @@ int open (const char * filename, int flags, ...)
};
if( flags & O_TRUNC )
set_file_size(filename, 0);
set_file_size(buf, 0);
ioh = &__io_tab[hid];
@ -92,12 +183,14 @@ int open (const char * filename, int flags, ...)
} else
iomode |= _BINARY;
ioh->name = strdup(filename);
ioh->name = strdup(buf);
ioh->offset = offset;
ioh->mode = iomode;
ioh->read = read_file;
ioh->write = write_file;
// printf("%s %s\n", __FUNCTION__, ioh->name);
return hid;
};