2014-05-11 00:12:19 +02:00
|
|
|
/* open.c -- open a file.
|
|
|
|
*
|
|
|
|
* Copyright (c) 1995 Cygnus Support
|
|
|
|
*
|
|
|
|
* The authors hereby grant permission to use, copy, modify, distribute,
|
|
|
|
* and license this software and its documentation for any purpose, provided
|
|
|
|
* that existing copyright notices are retained in all copies and that this
|
|
|
|
* notice is included verbatim in any distributions. No written agreement,
|
|
|
|
* license, or royalty fee is required for any of the authorized uses.
|
|
|
|
* Modifications to this software may be copyrighted by their authors
|
|
|
|
* and need not follow the licensing terms described here, provided that
|
|
|
|
* the new terms are clearly indicated on the first page of each file where
|
|
|
|
* they apply.
|
|
|
|
*/
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
2024-01-04 23:20:35 +01:00
|
|
|
#include <sys/ksys.h>
|
|
|
|
|
2014-05-11 00:12:19 +02:00
|
|
|
#include "glue.h"
|
|
|
|
#include "io.h"
|
|
|
|
|
2024-01-04 23:20:35 +01:00
|
|
|
#undef errno
|
2014-05-11 00:12:19 +02:00
|
|
|
extern int errno;
|
|
|
|
|
2024-01-04 23:20:35 +01:00
|
|
|
extern int write_file(const char *path,const void *buff,
|
|
|
|
size_t offset, size_t count, size_t *writes);
|
|
|
|
|
|
|
|
extern int read_file(const char *path, void *buff,
|
|
|
|
size_t offset, size_t count, size_t *reads);
|
|
|
|
|
2014-11-29 06:40:44 +01:00
|
|
|
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';
|
2024-01-04 23:20:35 +01:00
|
|
|
}
|
2014-11-29 06:40:44 +01:00
|
|
|
|
|
|
|
|
|
|
|
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;
|
2021-11-29 12:39:21 +01:00
|
|
|
//continue;
|
|
|
|
goto __do_until_slash;
|
2014-11-29 06:40:44 +01:00
|
|
|
}
|
|
|
|
*ptr++ = '/';
|
2021-11-29 12:39:21 +01:00
|
|
|
__do_until_slash:
|
2014-11-29 06:40:44 +01:00
|
|
|
if (*file == '/')
|
|
|
|
++file;
|
|
|
|
while (*file && *file!='/')
|
|
|
|
*ptr++ = *file++;
|
|
|
|
}
|
|
|
|
*ptr = 0;
|
2024-01-04 23:20:35 +01:00
|
|
|
}
|
2014-11-29 06:40:44 +01:00
|
|
|
|
2014-05-11 00:12:19 +02:00
|
|
|
int open (const char * filename, int flags, ...)
|
|
|
|
{
|
2014-11-29 06:40:44 +01:00
|
|
|
char buf[1024];
|
|
|
|
|
2014-05-11 00:12:19 +02:00
|
|
|
__io_handle *ioh;
|
2024-01-04 23:20:35 +01:00
|
|
|
ksys_file_info_t info;
|
2014-05-11 00:12:19 +02:00
|
|
|
int iomode, rwmode, offset;
|
|
|
|
int hid;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
hid = __io_alloc();
|
|
|
|
if(hid < 0)
|
|
|
|
{
|
|
|
|
errno = EMFILE;
|
2016-11-23 12:13:09 +01:00
|
|
|
__io_free(hid);
|
2014-05-11 00:12:19 +02:00
|
|
|
return (-1);
|
|
|
|
};
|
|
|
|
|
2014-11-29 06:40:44 +01:00
|
|
|
if (filename[0]=='/')
|
|
|
|
{
|
|
|
|
strcpy(buf,filename);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-01-04 23:20:35 +01:00
|
|
|
_ksys_getcwd(buf, 1024);
|
2014-11-29 06:40:44 +01:00
|
|
|
buildpath(buf, filename);
|
|
|
|
}
|
|
|
|
|
2024-01-04 23:20:35 +01:00
|
|
|
err = _ksys_file_info(buf, &info);
|
2014-05-11 00:12:19 +02:00
|
|
|
|
2024-01-04 23:20:35 +01:00
|
|
|
if (flags & O_EXCL &&
|
2014-05-11 00:12:19 +02:00
|
|
|
flags & O_CREAT )
|
|
|
|
{
|
2024-01-04 23:20:35 +01:00
|
|
|
if (!err)
|
2014-05-11 00:12:19 +02:00
|
|
|
{
|
|
|
|
errno = EEXIST;
|
2016-11-23 12:13:09 +01:00
|
|
|
__io_free(hid);
|
2014-05-11 00:12:19 +02:00
|
|
|
return (-1);
|
2024-01-04 23:20:35 +01:00
|
|
|
}
|
2014-05-11 00:12:19 +02:00
|
|
|
}
|
|
|
|
|
2024-01-04 23:20:35 +01:00
|
|
|
if (err)
|
2014-05-11 00:12:19 +02:00
|
|
|
{
|
|
|
|
if(flags & O_CREAT)
|
2024-01-04 23:20:35 +01:00
|
|
|
err = _ksys_file_create(buf).status;
|
|
|
|
if(err)
|
2014-05-11 00:12:19 +02:00
|
|
|
{
|
|
|
|
errno = EACCES;
|
2016-11-23 12:13:09 +01:00
|
|
|
__io_free(hid);
|
2014-05-11 00:12:19 +02:00
|
|
|
return -1;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2024-01-04 23:20:35 +01:00
|
|
|
if (flags & O_TRUNC)
|
|
|
|
_ksys_file_set_size(buf, 0);
|
2014-05-11 00:12:19 +02:00
|
|
|
|
|
|
|
ioh = &__io_tab[hid];
|
|
|
|
|
|
|
|
rwmode = flags & ( O_RDONLY | O_WRONLY | O_RDWR );
|
|
|
|
|
|
|
|
iomode = 0;
|
|
|
|
offset = 0;
|
|
|
|
|
2024-01-04 23:20:35 +01:00
|
|
|
if (rwmode == O_RDWR)
|
2014-05-11 00:12:19 +02:00
|
|
|
iomode |= _READ | _WRITE;
|
2024-01-04 23:20:35 +01:00
|
|
|
else if (rwmode == O_RDONLY)
|
2014-05-11 00:12:19 +02:00
|
|
|
iomode |= _READ;
|
2024-01-04 23:20:35 +01:00
|
|
|
else if (rwmode == O_WRONLY)
|
2014-05-11 00:12:19 +02:00
|
|
|
iomode |= _WRITE;
|
|
|
|
|
2024-01-04 23:20:35 +01:00
|
|
|
if (flags & O_APPEND)
|
2014-05-11 00:12:19 +02:00
|
|
|
{
|
|
|
|
iomode |= _APPEND;
|
|
|
|
offset = info.size;
|
2024-01-04 23:20:35 +01:00
|
|
|
}
|
2014-05-11 00:12:19 +02:00
|
|
|
|
2024-01-04 23:20:35 +01:00
|
|
|
if (flags & (O_BINARY|O_TEXT))
|
2014-05-11 00:12:19 +02:00
|
|
|
{
|
2024-01-04 23:20:35 +01:00
|
|
|
if (flags & O_BINARY)
|
2014-05-11 00:12:19 +02:00
|
|
|
iomode |= _BINARY;
|
2024-01-04 23:20:35 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-05-11 00:12:19 +02:00
|
|
|
iomode |= _BINARY;
|
2024-01-04 23:20:35 +01:00
|
|
|
}
|
2014-05-11 00:12:19 +02:00
|
|
|
|
2014-11-29 06:40:44 +01:00
|
|
|
ioh->name = strdup(buf);
|
2014-05-11 00:12:19 +02:00
|
|
|
ioh->offset = offset;
|
|
|
|
ioh->mode = iomode;
|
|
|
|
ioh->read = read_file;
|
|
|
|
ioh->write = write_file;
|
|
|
|
|
|
|
|
return hid;
|
2024-01-04 23:20:35 +01:00
|
|
|
}
|