Update NTK.
[nondaw.git] / nonlib / file.C
blob42fb8ea595a41d655227e1ff7cacf7db210b2371
2 /*******************************************************************************/
3 /* Copyright (C) 2008 Jonathan Moore Liles                                     */
4 /*                                                                             */
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.                                                  */
9 /*                                                                             */
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   */
13 /* more details.                                                               */
14 /*                                                                             */
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 /*******************************************************************************/
20 #include <fcntl.h>
21 #include <sys/stat.h>
22 #include <sys/types.h>
23 #include <unistd.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include <sys/vfs.h>
29 unsigned long
30 modification_time ( const char *file )
32     struct stat st;
34     if ( stat( file, &st ) )
35         return 0;
37     return st.st_mtime;
40 /** returns /true/ if /file1/ is newer than /file2/ (or file2 doesn't exist) */
41 bool
42 newer ( const char *file1, const char *file2 )
44     return modification_time( file1 ) > modification_time( file2 );
47 unsigned long
48 size ( const char *file )
50     struct stat st;
52     if ( stat( file, &st ) )
53         return 0;
55     return st.st_size;
58 int
59 exists ( const char *name )
61     struct stat st;
63     return 0 == stat( name, &st );
66 bool
67 acquire_lock ( int *lockfd, const char *filename )
69     struct flock fl;
71     fl.l_type = F_WRLCK;
72     fl.l_whence = SEEK_SET;
73     fl.l_start = 0;
74     fl.l_len = 0;
76     *lockfd = ::creat( filename, 0777 );
78     if ( fcntl( *lockfd, F_SETLK, &fl ) != 0 )
79         return false;
81     return true;
84 void
85 release_lock ( int *lockfd, const char *filename )
87     unlink( filename );
89     ::close( *lockfd );
91     *lockfd = 0;
94 int
95 backwards_fgetc ( FILE *fp )
97     int c;
99     if ( fseek( fp, -1, SEEK_CUR ) != 0 )
100         return -1;
102     c = fgetc( fp );
104     fseek( fp, -1, SEEK_CUR );
106     return c;
109 char *
110 backwards_fgets ( char *s, int size, FILE *fp )
112     if ( fseek( fp, -1, SEEK_CUR ) != 0 )
113         return NULL;
115     int c;
116     while ( ( c = backwards_fgetc( fp ) ) >= 0 )
117         if ( '\n' == c )
118             break;
120     long here = ftell( fp );
122     fseek( fp, 1, SEEK_CUR );
124     char *r = fgets( s, size, fp );
126     fseek( fp, here, SEEK_SET );
128     return r;
132 /** update the modification time of file referred to by /fd/ */
133 void
134 touch ( int fd )
136     struct stat st;
138     fstat( fd, &st );
140     fchmod( fd, st.st_mode );
143 /** write a single string to a file */
144 void
145 write_line ( const char *dir, const char *name, const char *value )
147     char path[512];
149     snprintf( path, sizeof( path ), "%s/%s", dir, name );
151     FILE *fp = fopen( path, "w" );
153     if ( ! fp )
154         return;
156     fputs( value, fp );
158     fclose( fp );
161 /** write a single string to a file */
162 void
163 read_line ( const char *dir, const char *name, char **value )
165     char path[512];
167     *value = 0;
169     snprintf( path, sizeof( path ), "%s/%s", dir, name );
171     FILE *fp = fopen( path, "r" );
173     if ( ! fp )
174         return;
176     *value = (char*)malloc( 512 );
178     fgets( *value, 512, fp );
180     fclose( fp );
183 #include <sys/statvfs.h>
185 /** return the number of blocks free on filesystem containing file named /file/ */
186 fsblkcnt_t
187 free_space ( const char *file )
189     struct statfs st;
190     memset( &st, 0, sizeof( st ) );
192     statfs( file, &st );
194     return st.f_bavail;
197 /** return the total number of blocks on filesystem containing file named /file/ */
198 fsblkcnt_t
199 total_space ( const char *file )
201     struct statfs st;
202     memset( &st, 0, sizeof( st ) );
204     statfs( file, &st );
206     return st.f_blocks;
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);