diff --git a/newlib/libc/tinystdio/fgetpos.c b/newlib/libc/tinystdio/fgetpos.c new file mode 100644 index 0000000000..91fdde7b49 --- /dev/null +++ b/newlib/libc/tinystdio/fgetpos.c @@ -0,0 +1,77 @@ +/* + * 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, + * and/or 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 +<>---record position in a stream or file + +INDEX + fgetpos +INDEX + _fgetpos_r + +SYNOPSIS + #include + int fgetpos(FILE *restrict <[fp]>, fpos_t *restrict <[pos]>); + int fgetpos( FILE *restrict <[fp]>, fpos_t *restrict <[pos]>); + +DESCRIPTION +Objects of type <> can have a ``position'' that records how much +of the file your program has already read. Many of the <> functions +depend on this position, and many change it as a side effect. + +You can use <> to report on the current position for a file +identified by <[fp]>; <> will write a value +representing that position at <<*<[pos]>>>. Later, you can +use this value with <> to return the file to this +position. + +In the current implementation, <> simply uses a character +count to represent the file position; this is the same number that +would be returned by <>. + +RETURNS +<> returns <<0>> when successful. If <> fails, the +result is <<1>>. Failure occurs on streams that do not support +positioning; the global <> indicates this condition with the +value <>. + +PORTABILITY +<> is required by the ANSI C standard, but the meaning of the +value it records is not specified beyond requiring that it be +acceptable as an argument to <>. In particular, other +conforming C implementations may return a different result from +<> than what <> writes at <<*<[pos]>>>. + +No supporting OS subroutines are required. +*/ + +#include + +int +fgetpos ( + FILE *__restrict fp, + _fpos_t *__restrict pos) +{ + *pos = ftell ( fp); + + if (*pos != -1) + { + return 0; + } + return 1; +} diff --git a/newlib/libc/tinystdio/fsetpos.c b/newlib/libc/tinystdio/fsetpos.c new file mode 100644 index 0000000000..a230a72122 --- /dev/null +++ b/newlib/libc/tinystdio/fsetpos.c @@ -0,0 +1,69 @@ +/* + * 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, + * and/or 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 +<>---restore position of a stream or file + +INDEX + fsetpos +INDEX + _fsetpos_r + +SYNOPSIS + #include + int fsetpos(FILE *<[fp]>, const fpos_t *<[pos]>); + int fsetpos( FILE *<[fp]>, + const fpos_t *<[pos]>); + +DESCRIPTION +Objects of type <> can have a ``position'' that records how much +of the file your program has already read. Many of the <> functions +depend on this position, and many change it as a side effect. + +You can use <> to return the file identified by <[fp]> to a previous +position <<*<[pos]>>> (after first recording it with <>). + +See <> for a similar facility. + +RETURNS +<> returns <<0>> when successful. If <> fails, the +result is <<1>>. The reason for failure is indicated in <>: +either <> (the stream identified by <[fp]> doesn't support +repositioning) or <> (invalid file position). + +PORTABILITY +ANSI C requires <>, but does not specify the nature of +<<*<[pos]>>> beyond identifying it as written by <>. + +Supporting OS subroutines required: <>, <>, <>, +<>, <>, <>, <>. +*/ + +#include + +int +fsetpos ( + FILE * iop, + const _fpos_t * pos) +{ + int x = fseek ( iop, *pos, SEEK_SET); + + if (x != 0) + return 1; + return 0; +} diff --git a/newlib/libc/tinystdio/meson.build b/newlib/libc/tinystdio/meson.build index 5433f8a764..f7f404dd68 100644 --- a/newlib/libc/tinystdio/meson.build +++ b/newlib/libc/tinystdio/meson.build @@ -57,6 +57,7 @@ srcs_tinystdio = [ 'ferror.c', 'fflush.c', 'fgetc.c', + 'fgetpos.c', 'fgets.c', 'fgetwc.c', 'fgetws.c', @@ -76,6 +77,7 @@ srcs_tinystdio = [ 'fscanf.c', 'fseek.c', 'fseeko.c', + 'fsetpos.c', 'ftell.c', 'ftello.c', 'ftox_engine.c', diff --git a/newlib/libc/tinystdio/stdio.h b/newlib/libc/tinystdio/stdio.h index 32b6a157af..e7f0f56be4 100644 --- a/newlib/libc/tinystdio/stdio.h +++ b/newlib/libc/tinystdio/stdio.h @@ -300,14 +300,14 @@ int ferror(FILE *__stream); #endif __extension__ typedef _fpos_t fpos_t; -int fgetpos(FILE *stream, fpos_t *pos); +int fgetpos(FILE * __restrict stream, fpos_t * __restrict pos); FILE *fopen(const char *path, const char *mode) __malloc_like_with_free(fclose, 1); FILE *freopen(const char *path, const char *mode, FILE *stream); FILE *fdopen(int, const char *) __malloc_like_with_free(fclose, 1); FILE *fmemopen(void *buf, size_t size, const char *mode) __malloc_like_with_free(fclose, 1); int fseek(FILE *stream, long offset, int whence); int fseeko(FILE *stream, __off_t offset, int whence); -int fsetpos(FILE *stream, fpos_t *pos); +int fsetpos(FILE *stream, const fpos_t *pos); long ftell(FILE *stream); __off_t ftello(FILE *stream); int fileno(FILE *);