updates.
[silc.git] / lib / silcutil / silcutil.c
index fa482dcce9c89a4f422762b35ca4a9bb112cd68b..c61e70ce4836026c38789cdefc2250c1f192ee53 100644 (file)
 
 #include "silcincludes.h"
 
-/* Reads a file to a buffer. The allocated buffer is returned. Length of
-   the file read is returned to the return_len argument. */
+/* Opens a file indicated by the filename `filename' with flags indicated
+   by the `flags'. */
 
-char *silc_file_read(const char *filename, uint32 *return_len)
+int silc_file_open(const char *filename, int flags)
 {
   int fd;
-  char *buffer;
-  int filelen;
 
-  fd = open(filename, O_RDONLY);
-  if (fd < 0) {
-    SILC_LOG_ERROR(("Cannot open file %s: %s", filename, strerror(errno)));
-    return NULL;
-  }
+  fd = open(filename, flags, 0600);
 
-  filelen = lseek(fd, (off_t)0L, SEEK_END);
-  if (filelen < 0) {
-    close(fd);
-    return NULL;
-  }
-  if (lseek(fd, (off_t)0L, SEEK_SET) < 0) {
-    close(fd);
-    return NULL;
-  }
+  return fd;
+}
 
-  if (filelen < 0) {
-    SILC_LOG_ERROR(("Cannot open file %s: %s", filename, strerror(errno)));
-    close(fd);
-    return NULL;
-  }
-  
-  buffer = silc_calloc(filelen + 1, sizeof(char));
-  
-  if ((read(fd, buffer, filelen)) == -1) {
-    memset(buffer, 0, sizeof(buffer));
-    close(fd);
-    SILC_LOG_ERROR(("Cannot read from file %s: %s", filename,
-                    strerror(errno)));
-    return NULL;
-  }
+/* Reads data from file descriptor `fd' to `buf'. */
 
-  close(fd);
-  buffer[filelen] = EOF;
+int silc_file_read(int fd, unsigned char *buf, uint32 buf_len)
+{
+  return read(fd, (void *)buf, buf_len);
+}
 
-  if (return_len)
-    *return_len = filelen;
+/* Writes `buffer' of length of `len' to file descriptor `fd. */
 
-  return buffer;
+int silc_file_write(int fd, const char *buffer, uint32 len)
+{
+  return write(fd, (const void *)buffer, len);
+}
+
+/* Closes file descriptor */
+
+int silc_file_close(int fd)
+{
+  return close(fd);
 }
 
 /* Writes a buffer to the file. */
 
-int silc_file_write(const char *filename, const char *buffer, uint32 len)
+int silc_file_writefile(const char *filename, const char *buffer, uint32 len)
 {
   int fd;
         
@@ -101,8 +84,8 @@ int silc_file_write(const char *filename, const char *buffer, uint32 len)
 /* 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, 
-                        uint32 len, int mode)
+int silc_file_writefile_mode(const char *filename, const char *buffer, 
+                            uint32 len, int mode)
 {
   int fd;
         
@@ -123,6 +106,76 @@ int silc_file_write_mode(const char *filename, const char *buffer,
   return 0;
 }
 
+/* Reads a file to a buffer. The allocated buffer is returned. Length of
+   the file read is returned to the return_len argument. */
+
+char *silc_file_readfile(const char *filename, uint32 *return_len)
+{
+  int fd;
+  char *buffer;
+  int filelen;
+
+  fd = silc_file_open(filename, O_RDONLY);
+  if (fd < 0) {
+    if (errno == ENOENT)
+      return NULL;
+    SILC_LOG_ERROR(("Cannot open file %s: %s", filename, strerror(errno)));
+    return NULL;
+  }
+
+  filelen = lseek(fd, (off_t)0L, SEEK_END);
+  if (filelen < 0) {
+    close(fd);
+    return NULL;
+  }
+  if (lseek(fd, (off_t)0L, SEEK_SET) < 0) {
+    close(fd);
+    return NULL;
+  }
+
+  if (filelen < 0) {
+    SILC_LOG_ERROR(("Cannot open file %s: %s", filename, strerror(errno)));
+    close(fd);
+    return NULL;
+  }
+  
+  buffer = silc_calloc(filelen + 1, sizeof(char));
+  
+  if ((read(fd, buffer, filelen)) == -1) {
+    memset(buffer, 0, sizeof(buffer));
+    close(fd);
+    SILC_LOG_ERROR(("Cannot read from file %s: %s", filename,
+                    strerror(errno)));
+    return NULL;
+  }
+
+  close(fd);
+  buffer[filelen] = EOF;
+
+  if (return_len)
+    *return_len = filelen;
+
+  return buffer;
+}
+
+/* Returns files size. Returns 0 on error. */
+
+uint64 silc_file_size(const char *filename)
+{
+  int ret;
+  struct stat stats;
+
+#ifndef SILC_WIN32\r
+  ret = lstat(filename, &stats);
+#else\r
+  ret = stat(filename, &stats);\r
+#endif\r
+  if (ret < 0)\r
+    return 0;\r
+
+  return (uint64)stats.st_size;
+}
+
 /* 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. */
@@ -355,51 +408,30 @@ unsigned char *silc_decode_pem(unsigned char *pem, uint32 pem_len,
   return data;
 }
 
-/* Parse nickname string. The format may be <num>!<nickname>@<server> to
-   support multiple same nicknames. The <num> is the final unifier if same
-   nickname is on same server. Note, this is only local format and server
-   does not know anything about these. */
+/* Parse userfqdn string which is in user@fqdn format */
 
-int silc_parse_nickname(char *string, char **nickname, char **server,
-                       uint32 *num)
+bool silc_parse_userfqdn(const char *string, char **left, char **right)
 {
   uint32 tlen;
 
   if (!string)
     return FALSE;
 
-  if (strchr(string, '!')) {
-    char *tmp;
-    tlen = strcspn(string, "!");
-    tmp = silc_calloc(tlen + 1, sizeof(*tmp));
-    memcpy(tmp, string, tlen);
-
-    if (num)
-      *num = atoi(tmp);
-
-    silc_free(tmp);
-
-    if (tlen >= strlen(string))
-      return FALSE;
-
-    string += tlen + 1;
-  }
-
   if (strchr(string, '@')) {
     tlen = strcspn(string, "@");
     
-    if (nickname) {
-      *nickname = silc_calloc(tlen + 1, sizeof(char));
-      memcpy(*nickname, string, tlen);
+    if (left) {
+      *left = silc_calloc(tlen + 1, sizeof(char));
+      memcpy(*left, string, tlen);
     }
     
-    if (server) {
-      *server = silc_calloc((strlen(string) - tlen) + 1, sizeof(char));
-      memcpy(*server, string + tlen + 1, strlen(string) - tlen - 1);
+    if (right) {
+      *right = silc_calloc((strlen(string) - tlen) + 1, sizeof(char));
+      memcpy(*right, string + tlen + 1, strlen(string) - tlen - 1);
     }
   } else {
-    if (nickname)
-      *nickname = strdup(string);
+    if (left)
+      *left = strdup(string);
   }
 
   return TRUE;