Assists music production by grouping standalone programs into sessions. Community version of "Non Session Manager".
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

243 lines
5.0KB

  1. /*******************************************************************************/
  2. /* Copyright (C) 2008 Jonathan Moore Liles */
  3. /* */
  4. /* This program is free software; you can redistribute it and/or modify it */
  5. /* under the terms of the GNU General Public License as published by the */
  6. /* Free Software Foundation; either version 2 of the License, or (at your */
  7. /* option) any later version. */
  8. /* */
  9. /* This program is distributed in the hope that it will be useful, but WITHOUT */
  10. /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
  11. /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for */
  12. /* more details. */
  13. /* */
  14. /* You should have received a copy of the GNU General Public License along */
  15. /* with This program; see the file COPYING. If not,write to the Free Software */
  16. /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  17. /*******************************************************************************/
  18. #include <fcntl.h>
  19. #include <sys/stat.h>
  20. #include <sys/types.h>
  21. #include <unistd.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <stdlib.h>
  25. #include <sys/vfs.h>
  26. unsigned long
  27. modification_time ( const char *file )
  28. {
  29. struct stat st;
  30. if ( stat( file, &st ) )
  31. return 0;
  32. return st.st_mtime;
  33. }
  34. /** returns /true/ if /file1/ is newer than /file2/ (or file2 doesn't exist) */
  35. bool
  36. newer ( const char *file1, const char *file2 )
  37. {
  38. return modification_time( file1 ) > modification_time( file2 );
  39. }
  40. unsigned long
  41. size ( const char *file )
  42. {
  43. struct stat st;
  44. if ( stat( file, &st ) )
  45. return 0;
  46. return st.st_size;
  47. }
  48. int
  49. exists ( const char *name )
  50. {
  51. struct stat st;
  52. return 0 == stat( name, &st );
  53. }
  54. bool
  55. acquire_lock ( int *lockfd, const char *filename )
  56. {
  57. struct flock fl;
  58. fl.l_type = F_WRLCK;
  59. fl.l_whence = SEEK_SET;
  60. fl.l_start = 0;
  61. fl.l_len = 0;
  62. *lockfd = ::creat( filename, 0777 );
  63. if ( fcntl( *lockfd, F_SETLK, &fl ) != 0 )
  64. return false;
  65. return true;
  66. }
  67. void
  68. release_lock ( int *lockfd, const char *filename )
  69. {
  70. unlink( filename );
  71. ::close( *lockfd );
  72. *lockfd = 0;
  73. }
  74. int
  75. backwards_fgetc ( FILE *fp )
  76. {
  77. int c;
  78. if ( fseek( fp, -1, SEEK_CUR ) != 0 )
  79. return -1;
  80. c = fgetc( fp );
  81. fseek( fp, -1, SEEK_CUR );
  82. return c;
  83. }
  84. char *
  85. backwards_afgets ( FILE *fp )
  86. {
  87. size_t size = 0;
  88. char *s = NULL;
  89. size_t i = 0;
  90. int c;
  91. while ( ( c = backwards_fgetc( fp ) ) >= 0 )
  92. {
  93. if ( i > 0 && '\n' == c )
  94. break;
  95. if ( i >= size )
  96. {
  97. size += 256;
  98. s = (char*)realloc( s, size );
  99. }
  100. s[i++] = c;
  101. }
  102. if ( s )
  103. {
  104. s[i] = 0;
  105. int len = strlen(s) ;
  106. int c, i, j;
  107. for (i = 0, j = len - 1; i < j; i++, j--)
  108. {
  109. c = s[i];
  110. s[i] = s[j];
  111. s[j] = c;
  112. }
  113. }
  114. fseek( fp, 1, SEEK_CUR );
  115. return s;
  116. }
  117. /** update the modification time of file referred to by /fd/ */
  118. void
  119. touch ( int fd )
  120. {
  121. struct stat st;
  122. fstat( fd, &st );
  123. fchmod( fd, st.st_mode );
  124. }
  125. /** write a single string to a file */
  126. void
  127. write_line ( const char *dir, const char *name, const char *value )
  128. {
  129. char path[512];
  130. snprintf( path, sizeof( path ), "%s/%s", dir, name );
  131. FILE *fp = fopen( path, "w" );
  132. if ( ! fp )
  133. return;
  134. fputs( value, fp );
  135. fclose( fp );
  136. }
  137. /** write a single string to a file */
  138. char *
  139. read_line ( const char *dir, const char *name )
  140. {
  141. char path[512];
  142. snprintf( path, sizeof( path ), "%s/%s", dir, name );
  143. FILE *fp = fopen( path, "r" );
  144. if ( ! fp )
  145. return 0;
  146. char *value = (char*)malloc( 512 );
  147. value[0] = 0;
  148. fgets( value, 512, fp );
  149. fclose( fp );
  150. return value;
  151. }
  152. #include <sys/statvfs.h>
  153. /** return the number of blocks free on filesystem containing file named /file/ */
  154. fsblkcnt_t
  155. free_space ( const char *file )
  156. {
  157. struct statfs st;
  158. memset( &st, 0, sizeof( st ) );
  159. statfs( file, &st );
  160. return st.f_bavail;
  161. }
  162. /** return the total number of blocks on filesystem containing file named /file/ */
  163. fsblkcnt_t
  164. total_space ( const char *file )
  165. {
  166. struct statfs st;
  167. memset( &st, 0, sizeof( st ) );
  168. statfs( file, &st );
  169. return st.f_blocks;
  170. }
  171. /** return the percentage of usage on filesystem containing file named /file/ */
  172. int
  173. percent_used ( const char *file )
  174. {
  175. const double ts = total_space( file );
  176. const double fs = free_space( file );
  177. double percent_free = ( ( fs / ts ) * 100.0f );
  178. return (int) (100.0f - percent_free);
  179. }