config_fs.c

00001 /* 
00002  * Copyright (c) 2005-2012 by KoanLogic s.r.l. - All rights reserved.  
00003  */
00004 
00005 #include <sys/types.h>
00006 #include <stdlib.h>
00007 #include <unistd.h>
00008 #include <string.h>
00009 #include <stdio.h>
00010 #include <fcntl.h>
00011 
00012 #include <toolbox/carpal.h>
00013 #include <toolbox/config.h>
00014 #include <toolbox/misc.h>
00015 #include <toolbox/memory.h>
00016 #include <toolbox/str.h>
00017 
00018 static int drv_fs_open (const char *path, void **parg)
00019 {
00020     int fd = -1;
00021     FILE *file = NULL;
00022 
00023     dbg_return_if (path == NULL, ~0);
00024     dbg_return_if (parg == NULL, ~0);
00025 
00026     /* open the file */
00027 again:
00028     fd = open(path, O_RDONLY);
00029     if(fd == -1 && (errno == EINTR))
00030         goto again; /* interrupted */
00031 
00032     crit_err_sif(fd < 0);
00033 
00034     /* stdio will handle buffering */
00035     file = fdopen(fd, "r");
00036     dbg_err_if(file == NULL);
00037 
00038     *parg = file;
00039 
00040     return 0;
00041 err:
00042     if(file)
00043         fclose(file);
00044     if(fd >= 0)
00045         close(fd);
00046     return ~0;
00047 }
00048 
00049 static int drv_fs_close (void *arg)
00050 {
00051     int rc;
00052     FILE *file = (FILE *) arg;
00053 
00054     dbg_return_if (file == NULL, ~0);
00055 
00056     if ((rc = fclose(file)) != 0)
00057         dbg_strerror(errno);
00058 
00059     return (rc == 0) ? 0 : ~0;
00060 }
00061 
00062 static char *drv_fs_gets (void *arg, char *buf, size_t size)
00063 {
00064     FILE *file = (FILE *) arg;
00065 
00066     dbg_return_if (file == NULL, NULL);
00067     dbg_return_if (buf == NULL, NULL);
00068 
00069     return fgets(buf, size, file);
00070 }
00071 
00072 /* pre-set driver for filesystem access */
00073 u_config_driver_t u_config_drv_fs = 
00074 { 
00075     drv_fs_open,
00076     drv_fs_close,
00077     drv_fs_gets,
00078     NULL    /* no resolver */
00079 };

←Products
© 2005-2012 - KoanLogic S.r.l. - All rights reserved