mirror of
https://git.tartarus.org/simon/putty.git
synced 2025-01-09 09:27:59 +00:00
3396c97da9
Now that the new CMake build system is encouraging us to lay out the code like a set of libraries, it seems like a good idea to make them look more _like_ libraries, by putting things into separate modules as far as possible. This fixes several previous annoyances in which you had to link against some object in order to get a function you needed, but that object also contained other functions you didn't need which included link-time symbol references you didn't want to have to deal with. The usual offender was subsidiary supporting programs including misc.c for some innocuous function and then finding they had to deal with the requirements of buildinfo(). This big reorganisation introduces three new subdirectories called 'utils', one at the top level and one in each platform subdir. In each case, the directory contains basically the same files that were previously placed in the 'utils' build-time library, except that the ones that were extremely miscellaneous (misc.c, utils.c, uxmisc.c, winmisc.c, winmiscs.c, winutils.c) have been split up into much smaller pieces.
73 lines
1.4 KiB
C
73 lines
1.4 KiB
C
/*
|
|
* Implementation of Filename for Unix, including f_open().
|
|
*/
|
|
|
|
#include <fcntl.h>
|
|
#include <unistd.h>
|
|
|
|
#include "putty.h"
|
|
|
|
Filename *filename_from_str(const char *str)
|
|
{
|
|
Filename *ret = snew(Filename);
|
|
ret->path = dupstr(str);
|
|
return ret;
|
|
}
|
|
|
|
Filename *filename_copy(const Filename *fn)
|
|
{
|
|
return filename_from_str(fn->path);
|
|
}
|
|
|
|
const char *filename_to_str(const Filename *fn)
|
|
{
|
|
return fn->path;
|
|
}
|
|
|
|
bool filename_equal(const Filename *f1, const Filename *f2)
|
|
{
|
|
return !strcmp(f1->path, f2->path);
|
|
}
|
|
|
|
bool filename_is_null(const Filename *fn)
|
|
{
|
|
return !fn->path[0];
|
|
}
|
|
|
|
void filename_free(Filename *fn)
|
|
{
|
|
sfree(fn->path);
|
|
sfree(fn);
|
|
}
|
|
|
|
void filename_serialise(BinarySink *bs, const Filename *f)
|
|
{
|
|
put_asciz(bs, f->path);
|
|
}
|
|
Filename *filename_deserialise(BinarySource *src)
|
|
{
|
|
return filename_from_str(get_asciz(src));
|
|
}
|
|
|
|
char filename_char_sanitise(char c)
|
|
{
|
|
if (c == '/')
|
|
return '.';
|
|
return c;
|
|
}
|
|
|
|
FILE *f_open(const Filename *filename, char const *mode, bool is_private)
|
|
{
|
|
if (!is_private) {
|
|
return fopen(filename->path, mode);
|
|
} else {
|
|
int fd;
|
|
assert(mode[0] == 'w'); /* is_private is meaningless for read,
|
|
and tricky for append */
|
|
fd = open(filename->path, O_WRONLY | O_CREAT | O_TRUNC, 0600);
|
|
if (fd < 0)
|
|
return NULL;
|
|
return fdopen(fd, mode);
|
|
}
|
|
}
|