--- fsl.c 2002/07/09 09:42:09 1.1
+++ fsl.c 2002/07/11 15:43:15 1.2
@@ -239,3 +239,181 @@
return;
}
+/* ------------------------------------------------------------------ */
+
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <stdio.h> /* strerror() */
+#include <dirent.h>
+
+#include "config.h"
+#define FSL_PREFIX "l2."
+
+#define STMT(stuff) do { stuff } while (0)
+#define CU(returncode) STMT( rc = returncode; goto CUS; )
+#define VCU STMT( goto CUS; )
+
+typedef struct {
+ char *base;
+ int used;
+ int size;
+} buf_t;
+
+/* general return codes */
+typedef enum {
+ FSL_OK = 0, /* everything ok */
+ FSL_ERR_ARG, /* invalid argument */
+ FSL_ERR_USE, /* invalid use */
+ FSL_ERR_MEM, /* no more memory available */
+ FSL_ERR_SYS /* operating system error, see errno */
+ //FSL_ERR_FMT, /* formatting error */
+ //FSL_ERR_INT, /* internal error */
+ //FSL_ERR_SYN, /* syntax error */
+} fsl_rc_t;
+
+fsl_rc_t readfileorallfiles(buf_t *, const char *);
+fsl_rc_t readfile (buf_t *, const char *);
+fsl_rc_t readallfiles (buf_t *);
+fsl_rc_t appendfiletobuffer(buf_t *, const char *);
+
+int main(int argc, char **argv)
+{
+ buf_t buf;
+ fsl_rc_t rv;
+
+ buf.base = NULL;
+ buf.used = 0;
+ buf.size = 0;
+
+ if ((rv = readfileorallfiles(&buf, "config.log")) != FSL_OK)
+ fprintf(stderr, "DEBUG: error#%d, system#%s(%d)\n", rv, strerror(errno), errno);
+
+ return 0;
+}
+
+fsl_rc_t readfileorallfiles(buf_t *buffer, const char *ident)
+{
+ fsl_rc_t rv;
+
+ if ((rv = readfile(buffer, ident)) == FSL_OK)
+ return FSL_OK;
+
+ if (rv != FSL_ERR_SYS || errno != ENOENT)
+ return rv;
+
+ if ((rv = readallfiles(buffer)) != FSL_OK)
+ return rv;
+
+ return FSL_OK;
+}
+
+fsl_rc_t readfile(buf_t *buffer, const char *ident)
+{
+ fsl_rc_t rc;
+ char *filename = NULL;
+
+ if (ident == NULL || buffer == NULL)
+ CU(FSL_ERR_ARG);
+
+ if ((filename = (char *)malloc(strlen(FSL_CFGDIR) + 1 + strlen(FSL_PREFIX) + strlen(ident) + 1)) == NULL)
+ CU(FSL_ERR_MEM);
+ filename[0] = '\0';
+ strcat(filename, FSL_CFGDIR);
+ strcat(filename, "/");
+ strcat(filename, FSL_PREFIX);
+ strcat(filename, ident);
+
+ CU(appendfiletobuffer(buffer, filename));
+CUS:
+ if (filename != NULL)
+ free(filename);
+ return rc;
+}
+
+fsl_rc_t readallfiles(buf_t *buffer)
+{
+ fsl_rc_t rc;
+ DIR *dp;
+ struct dirent *de;
+ char *filename = NULL;
+
+ if (buffer == NULL)
+ CU(FSL_ERR_ARG);
+
+ if ((dp = opendir(FSL_CFGDIR)) == NULL)
+ CU(FSL_ERR_SYS);
+
+ while ((de = readdir(dp)) != NULL) {
+ if ( (de->d_type == DT_REG)
+ && (de->d_namlen >= strlen(FSL_PREFIX))
+ && (strncmp(de->d_name, FSL_PREFIX, strlen(FSL_PREFIX)) == 0)
+ ) {
+ if ((filename = (char *)malloc(strlen(FSL_CFGDIR) + 1 + de->d_namlen + 1)) == NULL)
+ CU(FSL_ERR_MEM);
+ filename[0] = '\0';
+ strcat(filename, FSL_CFGDIR);
+ strcat(filename, "/");
+ strncat(filename, de->d_name, de->d_namlen);
+ (void /*FIXME*/)appendfiletobuffer(buffer, filename);
+ free(filename);
+ filename = NULL;
+ }
+ }
+ (void)closedir(dp);
+ CU(FSL_OK);
+CUS:
+ if (filename != NULL)
+ free(filename);
+ return rc;
+}
+
+fsl_rc_t appendfiletobuffer(buf_t *buffer, const char *filename)
+{
+ fsl_rc_t rc;
+ int fd = -1;
+ int filesize;
+ int fileread;
+ int bufferneed;
+
+ if (filename == NULL || buffer == NULL)
+ CU(FSL_ERR_ARG);
+
+ if ((fd = open(filename, O_RDONLY)) == -1)
+ CU(FSL_ERR_SYS);
+
+ if ((filesize = (int)lseek(fd, 0, SEEK_END)) == -1)
+ CU(FSL_ERR_SYS);
+
+ bufferneed = buffer->used + filesize;
+ if (bufferneed > buffer->size) {
+ if (buffer->base == NULL) {
+ if ((buffer->base = (char *)malloc(bufferneed)) == NULL)
+ CU(FSL_ERR_MEM);
+ buffer->size = bufferneed;
+ buffer->used = 0;
+ }
+ else {
+ if ((buffer->base = (char *)realloc(buffer->base, bufferneed)) == NULL)
+ CU(FSL_ERR_MEM);
+ buffer->size = bufferneed;
+ }
+ }
+
+ if (lseek(fd, 0, SEEK_SET) == -1)
+ CU(FSL_ERR_SYS);
+
+ if ((fileread = (int)read(fd, buffer->base + buffer->used, (size_t)filesize)) == -1)
+ CU(FSL_ERR_SYS);
+
+ if (fileread != filesize)
+ CU(FSL_ERR_USE);
+
+ buffer->used += filesize;
+ CU(FSL_OK);
+CUS:
+ if (fd != -1)
+ close(fd);
+ return rc;
+}
|