2 /*******************************************************************************/
3 /* Copyright (C) 2008 Jonathan Moore Liles */
5 /* This program is free software; you can redistribute it and/or modify it */
6 /* under the terms of the GNU General Public License as published by the */
7 /* Free Software Foundation; either version 2 of the License, or (at your */
8 /* option) any later version. */
10 /* This program is distributed in the hope that it will be useful, but WITHOUT */
11 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
12 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for */
15 /* You should have received a copy of the GNU General Public License along */
16 /* with This program; see the file COPYING. If not,write to the Free Software */
17 /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /*******************************************************************************/
22 #include <sys/types.h>
30 modification_time ( const char *file )
34 if ( stat( file, &st ) )
40 /** returns /true/ if /file1/ is newer than /file2/ (or file2 doesn't exist) */
42 newer ( const char *file1, const char *file2 )
44 return modification_time( file1 ) > modification_time( file2 );
48 size ( const char *file )
52 if ( stat( file, &st ) )
59 exists ( const char *name )
63 return 0 == stat( name, &st );
67 acquire_lock ( int *lockfd, const char *filename )
72 fl.l_whence = SEEK_SET;
76 *lockfd = ::creat( filename, 0777 );
78 if ( fcntl( *lockfd, F_SETLK, &fl ) != 0 )
85 release_lock ( int *lockfd, const char *filename )
95 backwards_fgetc ( FILE *fp )
99 if ( fseek( fp, -1, SEEK_CUR ) != 0 )
104 fseek( fp, -1, SEEK_CUR );
110 backwards_fgets ( char *s, int size, FILE *fp )
112 if ( fseek( fp, -1, SEEK_CUR ) != 0 )
116 while ( ( c = backwards_fgetc( fp ) ) >= 0 )
120 long here = ftell( fp );
122 fseek( fp, 1, SEEK_CUR );
124 char *r = fgets( s, size, fp );
126 fseek( fp, here, SEEK_SET );
132 /** update the modification time of file referred to by /fd/ */
140 fchmod( fd, st.st_mode );
143 /** write a single string to a file */
145 write_line ( const char *dir, const char *name, const char *value )
149 snprintf( path, sizeof( path ), "%s/%s", dir, name );
151 FILE *fp = fopen( path, "w" );
161 /** write a single string to a file */
163 read_line ( const char *dir, const char *name, char **value )
169 snprintf( path, sizeof( path ), "%s/%s", dir, name );
171 FILE *fp = fopen( path, "r" );
176 *value = (char*)malloc( 512 );
178 fgets( *value, 512, fp );
183 #include <sys/statvfs.h>
185 /** return the number of blocks free on filesystem containing file named /file/ */
187 free_space ( const char *file )
190 memset( &st, 0, sizeof( st ) );
197 /** return the total number of blocks on filesystem containing file named /file/ */
199 total_space ( const char *file )
202 memset( &st, 0, sizeof( st ) );
209 /** return the percentage of usage on filesystem containing file named /file/ */
211 percent_used ( const char *file )
213 const double ts = total_space( file );
214 const double fs = free_space( file );
216 double percent_free = ( ( fs / ts ) * 100.0f );
218 return (int) (100.0f - percent_free);