/*
- silcfileutil.c
+ silcfileutil.c
Author: Pekka Riikonen <priikone@silcnet.org>
return read(fd, (void *)buf, buf_len);
}
-/* Writes `buffer' of length of `len' to file descriptor `fd. */
+/* Writes `buffer' of length of `len' to file descriptor `fd'. */
int silc_file_write(int fd, const char *buffer, SilcUInt32 len)
{
/* Writes a buffer to the file. */
-int silc_file_writefile(const char *filename, const char *buffer,
+int silc_file_writefile(const char *filename, const char *buffer,
SilcUInt32 len)
{
int fd;
-
+
if ((fd = creat(filename, 0644)) == -1) {
SILC_LOG_ERROR(("Cannot open file %s for writing: %s", filename,
strerror(errno)));
return -1;
}
-
- if ((silc_file_write(fd, buffer, len)) == -1) {
+
+ if (silc_file_write(fd, buffer, len) == -1) {
SILC_LOG_ERROR(("Cannot write to file %s: %s", filename, strerror(errno)));
silc_file_close(fd);
return -1;
}
silc_file_close(fd);
-
+
return 0;
}
/* Writes a buffer to the file. If the file is created specific mode is
set to the file. */
-int silc_file_writefile_mode(const char *filename, const char *buffer,
+int silc_file_writefile_mode(const char *filename, const char *buffer,
SilcUInt32 len, int mode)
{
int fd;
-
+
if ((fd = creat(filename, mode)) == -1) {
SILC_LOG_ERROR(("Cannot open file %s for writing: %s", filename,
strerror(errno)));
return -1;
}
-
+
if ((silc_file_write(fd, buffer, len)) == -1) {
SILC_LOG_ERROR(("Cannot write to file %s: %s", filename, strerror(errno)));
silc_file_close(fd);
}
silc_file_close(fd);
-
+
return 0;
}
silc_file_close(fd);
return NULL;
}
-
+
buffer = silc_calloc(filelen + 1, sizeof(char));
-
+
if ((silc_file_read(fd, buffer, filelen)) == -1) {
memset(buffer, 0, sizeof(buffer));
silc_file_close(fd);
return buffer;
}
-/* Returns files size. Returns 0 on error. */
+/* Returns the size of `filename'. Returns 0 on error. */
SilcUInt64 silc_file_size(const char *filename)
{
/*
- silcfileutil.h
+ silcfileutil.h
Author: Pekka Riikonen <priikone@silcnet.org>
*/
-/****h* silcutil/Silc File Util Interface
+/****h* silcutil/SILC File Util Interface
*
* DESCRIPTION
*
+ * The SILC File Util Interface is a small set of functions that provides a
+ * portable access method to the filesystem.
*
***/
/* Prototypes */
+/****f* silcutil/SilcFileUtilAPI/silc_file_open
+ *
+ * SYNOPSIS
+ *
+ * int silc_file_open(const char *filename, int flags);
+ *
+ * DESCRIPTION
+ *
+ * Opens a file indicated by the filename `filename' with flags indicated
+ * by `flags'. The opening permission defaults to 0600.
+ *
+ ***/
int silc_file_open(const char *filename, int flags);
+
+/****f* silcutil/SilcFileUtilAPI/silc_file_open_mode
+ *
+ * SYNOPSIS
+ *
+ * int silc_file_open_mode(const char *filename, int flags, int mode);
+ *
+ * DESCRIPTION
+ *
+ * Opens a file indicated by the filename `filename' with flags indicated
+ * by `flags'. The argument `mode' specifies the permissions to use in
+ * case a new file is created.
+ *
+ ***/
int silc_file_open_mode(const char *filename, int flags, int mode);
+
+/****f* silcutil/SilcFileUtilAPI/silc_file_read
+ *
+ * SYNOPSIS
+ *
+ * int silc_file_read(int fd, unsigned char *buf, SilcUInt32 buf_len);
+ *
+ * DESCRIPTION
+ *
+ * Reads data from file descriptor `fd' to `buf'.
+ *
+ ***/
int silc_file_read(int fd, unsigned char *buf, SilcUInt32 buf_len);
+
+/****f* silcutil/SilcFileUtilAPI/silc_file_write
+ *
+ * SYNOPSIS
+ *
+ * int silc_file_write(int fd, const char *buffer, SilcUInt32 len);
+ *
+ * DESCRIPTION
+ *
+ * Writes `buffer' of length of `len' to file descriptor `fd'.
+ *
+ ***/
int silc_file_write(int fd, const char *buffer, SilcUInt32 len);
+
+/****f* silcutil/SilcFileUtilAPI/silc_file_close
+ *
+ * SYNOPSIS
+ *
+ * int silc_file_close(int fd);
+ *
+ * DESCRIPTION
+ *
+ * Closes file descriptor previously opened with silc_file_open().
+ *
+ ***/
int silc_file_close(int fd);
+
+/****f* silcutil/SilcFileUtilAPI/silc_file_readfile
+ *
+ * SYNOPSIS
+ *
+ * char *silc_file_readfile(const char *filename, SilcUInt32 *return_len);
+ *
+ * DESCRIPTION
+ *
+ * Reads the content of `filename' to a buffer. The allocated buffer is
+ * returned.
+ * If the `return_len' pointer is not NULL, it's filled with the length of
+ * the file.
+ *
+ ***/
char *silc_file_readfile(const char *filename, SilcUInt32 *return_len);
-int silc_file_writefile(const char *filename, const char *buffer, SilcUInt32 len);
-int silc_file_writefile_mode(const char *filename, const char *buffer,
+
+/****f* silcutil/SilcFileUtilAPI/silc_file_writefile
+ *
+ * SYNOPSIS
+ *
+ * int silc_file_writefile(const char *filename, const char *buffer,
+ * SilcUInt32 len);
+ *
+ * DESCRIPTION
+ *
+ * Writes a buffer to the file. If the file is created specific mode is
+ * set to the file.
+ *
+ ***/
+int silc_file_writefile(const char *filename, const char *buffer,
+ SilcUInt32 len);
+
+/****f* silcutil/SilcFileUtilAPI/silc_file_writefile_mode
+ *
+ * SYNOPSIS
+ *
+ * int silc_file_writefile_mode(const char *filename, const char *buffer,
+ * SilcUInt32 len, int mode);
+ *
+ * DESCRIPTION
+ *
+ * Writes a buffer to the file. If the file is created the specified `mode'
+ * is set to the file.
+ *
+ ***/
+int silc_file_writefile_mode(const char *filename, const char *buffer,
SilcUInt32 len, int mode);
+
+/****f* silcutil/SilcFileUtilAPI/silc_file_size
+ *
+ * SYNOPSIS
+ *
+ * SilcUInt64 silc_file_size(const char *filename);
+ *
+ * DESCRIPTION
+ *
+ * Returns the size of `filename'. Returns 0 on error.
+ *
+ ***/
SilcUInt64 silc_file_size(const char *filename);
-#endif /* SILCFILEUTIL_H */
+#endif /* !SILCFILEUTIL_H */