Main Page | Modules | Data Structures | File List | Data Fields | Related Pages

file.c

00001 /***************************************************************************
00002  * CVSID: $Id: file.c,v 1.7 2004/08/07 15:10:47 stefanb Exp $
00003  *
00004  * file.c : Functions for handling files
00005  *
00006  * Copyright (C) 2004 Stefan Bambach, <sbambach@gmx.net>
00007  *
00008  * Licensed under the GNU General Public License 2.0
00009  *
00010  * This program is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version.
00014  *
00015  * This program is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License
00021  * along with this program; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023  *
00024  **************************************************************************/
00025  
00026 #ifdef HAVE_CONFIG_H
00027 #  include <config.h>
00028 #endif
00029 
00030 #include <stdlib.h>
00031 #include <stdio.h>
00032 #include <sys/stat.h>
00033 #include <sys/types.h>
00034 #include <fcntl.h>
00035 #include <unistd.h>
00036 #include <string.h>
00037 #include <stdio.h>
00038 
00039 #include "file.h"
00040 #include "dbug_mem.h"
00041 
00042 
00043 
00044 int
00045 sb_file_is_fifo(const char *filepath)
00046 {
00047     struct stat st;
00048     
00049     if (stat(filepath, &st) != 0) {
00050         return 0;
00051     }
00052 
00053     if (!S_ISFIFO(st.st_mode)) {
00054         return 0;
00055     }
00056     
00057     return 1;
00058 }
00059 
00060 
00061 
00062 int
00063 sb_file_is_file(const char *filepath)
00064 {
00065     struct stat st;
00066     
00067     if (stat(filepath, &st) != 0) {
00068         return 0;
00069     }
00070 
00071     if (!S_ISREG(st.st_mode)) {
00072         return 0;
00073     }
00074     
00075     return 1;
00076 }
00077 
00078 
00079 
00080 int
00081 sb_file_get_stamp(const char *filepath)
00082 {
00083     struct stat st;
00084     
00085     if (stat(filepath, &st) != 0) {
00086         return -1;
00087     }
00088 
00089     if (!S_ISREG(st.st_mode)) {
00090         return -1;
00091     }
00092     
00093     return st.st_mtime;
00094 }
00095 
00096 
00097 
00098 int
00099 sb_file_get_size(const char *filepath)
00100 {
00101     struct stat st;
00102     
00103     if (stat(filepath, &st) != 0) {
00104         return -1;
00105     }
00106 
00107     if (!S_ISREG(st.st_mode)) {
00108         return -1;
00109     }
00110     
00111     return st.st_size;
00112 }
00113 
00114 
00115 
00116 char *
00117 sb_file_read(const char *filepath)
00118 {
00119     char *data = NULL;
00120     FILE *fp = NULL;
00121     int size = 0;
00122     int rsize = 0;
00123     int retval = 0;
00124     
00125     // opening file for reading
00126     fp = fopen(filepath, "r");
00127     if (fp != NULL) {
00128         // getting filesize
00129         size = sb_file_get_size(filepath);
00130         if (size != -1) {
00131             data = (char *) malloc(sizeof(char)*(size+1));
00132             if (data != NULL) {
00133                 // reading file
00134                 if ((rsize = fread(data, sizeof(char), size, fp)) >= 0) {
00135                     // EOF reached ?
00136                     if (rsize == size) {
00137                         retval = 1; // file successfully read
00138                     }
00139                 }
00140                 data[size]='\0';
00141             }
00142         }
00143 
00144         // closing file
00145         fclose(fp);
00146     }
00147     
00148 
00149     // freeing memory, if error
00150     if (retval == 0) {
00151         if (data != NULL) {
00152             free(data);
00153             data = NULL;
00154         }
00155         
00156         return NULL;
00157     }
00158     
00159     return data;
00160 }
00161 
00162 
00163 
00164 int
00165 sb_file_write(const char *filepath, char *data)
00166 {
00167     FILE *fp = NULL;
00168     int retval = 0;
00169     int size = strlen(data);
00170     int wsize = 0;
00171     
00172     // opening file for writing
00173     fp = fopen(filepath, "w");
00174     if (fp != NULL) {
00175         // writing to file
00176         if ((wsize = fwrite(data, sizeof(char), size, fp)) >= 0) {
00177             // EOF reached ?
00178             if (wsize == size) {
00179                 retval = 1; // file successfully written
00180             }
00181         }
00182 
00183         // closing file
00184         fclose(fp);
00185     }
00186     
00187     return retval;
00188 }
00189 
00190 
00191 
00192 int
00193 sb_file_remove(const char *filepath)
00194 {
00195     if(sb_file_is_file(filepath)) {
00196         if(unlink(filepath)==0)
00197             return 1;
00198     }
00199     
00200     return 0;
00201 }
00202 
00203 
00204 
00205 int
00206 sb_file_open(const char *filepath, int flags)
00207 {
00208     int fd = -1;
00209     
00210     if ((fd = open(filepath, flags)) == -1) {
00211         return -1;
00212     }
00213     
00214     return fd;
00215 }
00216 
00217 
00218 
00219 int
00220 sb_file_open_ext(const char *filepath, int flags, mode_t mode)
00221 {
00222     int fd = -1;
00223     
00224     if ((fd = open(filepath, flags, mode)) == -1) {
00225         return -1;
00226     }
00227     
00228     return fd;
00229 }
00230 
00231 
00232 
00233 int
00234 sb_file_close(int fd)
00235 {
00236     return close(fd);
00237 }
00238 
00239 
00240 
00241 int
00242 sb_file_set_lock(int fd, int type, int wait)
00243 {
00244     struct flock lock[1];
00245     
00246     lock->l_type = type;
00247     lock->l_whence = SEEK_SET;
00248     lock->l_start = 0;
00249     lock->l_len = 0;
00250     
00251     if (wait == 1) {
00252         return fcntl(fd, F_SETLKW, lock);
00253     } else {
00254         return fcntl(fd, F_SETLK, lock);
00255     }
00256 }
00257 
00258 
00259 
00260 int
00261 sb_file_lock_acquire_read(int fd)
00262 {
00263     return sb_file_set_lock(fd, F_RDLCK, 0);
00264 }
00265 
00266 
00267 
00268 int
00269 sb_file_lock_acquire_read_wait(int fd)
00270 {
00271     return sb_file_set_lock(fd, F_RDLCK, 1);
00272 }
00273 
00274 
00275 
00276 int
00277 sb_file_lock_acquire_write(int fd)
00278 {
00279     return sb_file_set_lock(fd, F_WRLCK, 0);
00280 }
00281 
00282 
00283 
00284 int
00285 sb_file_lock_acquire_write_wait(int fd)
00286 {
00287     return sb_file_set_lock(fd, F_WRLCK, 1);
00288 }
00289 
00290 
00291 
00292 int
00293 sb_file_lock_release(int fd)
00294 {
00295     return sb_file_set_lock(fd, F_UNLCK, 0);
00296 }

Generated on Wed Mar 30 13:43:26 2005 for Mntd by  doxygen 1.3.9.1