[syslinux] [PATCH 4/5] COM32: add trivial stat() function
Keith Schincke
keith.schincke at gmail.com
Wed Mar 4 17:42:26 PST 2009
Hi Gene,
1) Could you set ret equal to -1 when you define the variable? This would
keep you from needing to use code space, program time to set it later.
2) Why do you set errno to 0 before your call to opendir()? Shouldn't this
be set to 0 if the opendir() call is successful?
Keith
On Wed, Mar 4, 2009 at 6:32 PM, Gene Cumm <gene.cumm at gmail.com> wrote:
> From: Gene Cumm <gene.cumm at gmail.com>
>
> COM32: add trivial stat() function; uses open(), fstat() then close()
> or opendir(), closedir().
>
> Signed-off-by: Gene Cumm <gene.cumm at gmail.com>
>
> ---
>
> Not the most elegant thing in the world but it's been working for me.
>
>
> diff --git a/com32/include/sys/stat.h b/com32/include/sys/stat.h
> index ffc4105..2a06a68 100644
> --- a/com32/include/sys/stat.h
> +++ b/com32/include/sys/stat.h
> @@ -49,4 +49,6 @@ struct stat {
> /* Only fstat() supported */
> int fstat(int, struct stat *);
>
> +int stat(const char *, struct stat *);
> +
> #endif /* _SYS_STAT_H */
> diff --git a/com32/lib/Makefile b/com32/lib/Makefile
> index 0cc4061..21379b4 100644
> --- a/com32/lib/Makefile
> +++ b/com32/lib/Makefile
> @@ -32,7 +32,7 @@ LIBOBJS = \
> sys/entry.o sys/exit.o sys/argv.o sys/times.o \
> sys/fileinfo.o sys/opendev.o sys/read.o sys/write.o sys/ftell.o \
> sys/close.o sys/open.o sys/fileread.o sys/fileclose.o \
> - sys/isatty.o sys/fstat.o \
> + sys/isatty.o sys/fstat.o sys/stat.o \
> \
> sys/zfile.o sys/zfopen.o \
> \
> diff --git a/com32/lib/sys/stat.c b/com32/lib/sys/stat.c
> new file mode 100644
> index 0000000..d1b2629
> --- /dev/null
> +++ b/com32/lib/sys/stat.c
> @@ -0,0 +1,73 @@
> +/* -----------------------------------------------------------------------
> *
> + *
> + * Copyright 2009 Gene Cumm - All Rights Reserved
> + *
> + * Permission is hereby granted, free of charge, to any person
> + * obtaining a copy of this software and associated documentation
> + * files (the "Software"), to deal in the Software without
> + * restriction, including without limitation the rights to use,
> + * copy, modify, merge, publish, distribute, sublicense, and/or
> + * sell copies of the Software, and to permit persons to whom
> + * the Software is furnished to do so, subject to the following
> + * conditions:
> + *
> + * The above copyright notice and this permission notice shall
> + * be included in all copies or substantial portions of the Software.
> + *
> + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
> + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
> + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
> + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
> + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
> + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
> + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
> + * OTHER DEALINGS IN THE SOFTWARE.
> + *
> + * -----------------------------------------------------------------------
> */
> +
> +/*
> + * stat.c
> + *
> + * Very trivial stat emulation by using open(), fstat(), close()
> + */
> +
> +#include <sys/stat.h>
> +#include <errno.h>
> +#include <dirent.h>
> +#include <fcntl.h>
> +#include <string.h>
> +#include <unistd.h>
> +// #include "file.h"
> +
> +int stat(const char *pathname, struct stat *buf)
> +{
> + int fd, status, ret;
> + DIR *d;
> + mode_t st_mode;
> +
> + fd = open(pathname, O_RDONLY);
> + if (fd != -1){
> + status = fstat(fd, buf);
> + close(fd);
> + ret = 0;
> + } else {
> + if ((errno == 0) || (errno == ENOENT)) {
> + errno = 0;
> + st_mode = (S_IFDIR | S_IRUSR | S_IXUSR);
> + /* | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
> + */
> + d = opendir(pathname);
> + if (d != NULL) {
> + buf->st_size = 0;
> + buf->st_mode = st_mode;
> + closedir(d);
> + ret = 0;
> + } else {
> + ret = -1; /* Preserve errno */
> + }
> + } else {
> + ret = -1; /* Preserve errno */
> + }
> + }
> + return ret;
> +} /* stat */
>
> _______________________________________________
> Syslinux mailing list
> Submissions to Syslinux at zytor.com
> Unsubscribe or set options at:
> http://www.zytor.com/mailman/listinfo/syslinux
> Please do not send private replies to mailing list traffic.
>
>
More information about the Syslinux
mailing list