onesadcookie / fcaseopen Goto Github PK
View Code? Open in Web Editor NEWcase-insensitive fopen for case-sensitive filesystems
License: MIT License
case-insensitive fopen for case-sensitive filesystems
License: MIT License
Any plans of making case-sensitive fopen
for case-insensitive (Windows) filesystems? Sometimes I prefer to demand relaxed API to follow strict ones, across platforms.
Hi!
It's me again I found another bug, let's hope this is the last one (isn't C string and path handling a joy?). The return value of casepath is true even when a file doesn't exist. This doesn't actually break fcaseopen or casechdir since fopen will just return NULL anyway but it was a nasty surprise when I used casepath off label on its own to get the valid file path so I could check the date on it.
Thanks,
Aaron.
The restriction that "r must have strlen(path) + 2 bytes" is incorrect as it fails to account for the null terminator. This is easiest way to see this is to walk through casepath on "a", which would have been alloca'd 3 bytes:
// r must have strlen(path) + 2 bytes
static int casepath(char const *path, char *r) // In the case that path is "a", the length of r is 3 bytes.
{
size_t l = strlen(path);
char *p = alloca(l + 1);
strcpy(p, path);
size_t rl = 0;
DIR *d;
if (p[0] == '/')
{
d = opendir("/");
p = p + 1;
}
else
{
d = opendir(".");
r[0] = '.'; // we take this path an initialise the first two bytes to 'a' and a null.
r[1] = 0;
rl = 1;
}
int last = 0;
char *c = strsep(&p, "/"); // strsep doesn't find a '/' and sets c to the entire string. In this case "a"
while (c)
{
if (!d) // We successfully opened "."
{
return 0;
}
if (last) // last is still 0
{
closedir(d);
return 0;
}
r[rl] = '/'; // rl is 1, r is now "./" (no null terminator)
rl += 1;
r[rl] = 0; // r is "./" (null terminated)
struct dirent *e = readdir(d); // reading the current directory "."
while (e)
{
if (strcasecmp(c, e->d_name) == 0)
{ // found file 'a'
// copy 'a' to where the null terminator was (the last byte in our buffer)
// AND a null terminator in the next byte of the buffer!?!
strcpy(r + rl, e->d_name);
...
This means that the requirement for casepath's buffer size is actually three bytes more than path's strlen(), since strcpy writes a null to the next byte.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.