From: Pekka Riikonen Date: Wed, 5 Jul 2000 06:06:12 +0000 (+0000) Subject: Added file saving with specific mode. X-Git-Tag: SILC.0.1~517 X-Git-Url: http://git.silcnet.org/gitweb/?a=commitdiff_plain;h=294fc0e02f96715d1b26641f518864e08aeab22e;p=silc.git Added file saving with specific mode. --- diff --git a/lib/silccore/silcutil.c b/lib/silccore/silcutil.c index d5611c48..059b5f11 100644 --- a/lib/silccore/silcutil.c +++ b/lib/silccore/silcutil.c @@ -24,8 +24,11 @@ /* * $Id$ * $Log$ - * Revision 1.1 2000/06/27 11:36:55 priikone - * Initial revision + * Revision 1.2 2000/07/05 06:06:12 priikone + * Added file saving with specific mode. + * + * Revision 1.1.1.1 2000/06/27 11:36:55 priikone + * Imported from internal CVS/Added Log headers. * * */ @@ -48,7 +51,10 @@ char *silc_file_read(const char *filename, int *return_len) } filelen = lseek(fd, (off_t)0L, SEEK_END); - lseek(fd, (off_t)0L, SEEK_SET); + if (filelen < 0) + return NULL; + if (lseek(fd, (off_t)0L, SEEK_SET) < 0) + return NULL; if (filelen < 0) { SILC_LOG_ERROR(("Cannot open file %s: %s", filename, strerror(errno))); @@ -93,6 +99,29 @@ int silc_file_write(const char *filename, const char *buffer, int len) return 0; } +/* Writes a buffer to the file. If the file is created specific mode is + set to the file. */ + +int silc_file_write_mode(const char *filename, const char *buffer, + int len, int mode) +{ + int fd; + + if ((fd = creat(filename, mode)) == -1) { + SILC_LOG_ERROR(("Cannot open file %s for writing: %s", strerror(errno))); + return -1; + } + + if ((write(fd, buffer, len)) == -1) { + SILC_LOG_ERROR(("Cannot write to file %s: %s", strerror(errno))); + return -1; + } + + close(fd); + + return 0; +} + /* Gets line from a buffer. Stops reading when a newline or EOF occurs. This doesn't remove the newline sign from the destination buffer. The argument begin is returned and should be passed again for the function. */ diff --git a/lib/silccore/silcutil.h b/lib/silccore/silcutil.h index dc57eeb2..e0694610 100644 --- a/lib/silccore/silcutil.h +++ b/lib/silccore/silcutil.h @@ -24,6 +24,8 @@ /* Prototypes */ char *silc_file_read(const char *filename, int *return_len); int silc_file_write(const char *filename, const char *buffer, int len); +int silc_file_write_mode(const char *filename, const char *buffer, + int len, int mode); int silc_gets(char *dest, int destlen, const char *src, int srclen, int begin); int silc_check_line(char *buf); char *silc_get_time();