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.

216 lines
4.6KB

  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. unsigned long
  26. modification_time ( const char *file )
  27. {
  28. struct stat st;
  29. if ( stat( file, &st ) )
  30. return 0;
  31. return st.st_mtime;
  32. }
  33. /** returns /true/ if /file1/ is newer than /file2/ (or file2 doesn't exist) */
  34. bool
  35. newer ( const char *file1, const char *file2 )
  36. {
  37. return modification_time( file1 ) > modification_time( file2 );
  38. }
  39. unsigned long
  40. size ( const char *file )
  41. {
  42. struct stat st;
  43. if ( stat( file, &st ) )
  44. return 0;
  45. return st.st_size;
  46. }
  47. int
  48. exists ( const char *name )
  49. {
  50. struct stat st;
  51. return 0 == stat( name, &st );
  52. }
  53. bool
  54. acquire_lock ( int *lockfd, const char *filename )
  55. {
  56. struct flock fl;
  57. fl.l_type = F_WRLCK;
  58. fl.l_whence = SEEK_SET;
  59. fl.l_start = 0;
  60. fl.l_len = 0;
  61. *lockfd = ::creat( filename, 0777 );
  62. if ( fcntl( *lockfd, F_SETLK, &fl ) != 0 )
  63. return false;
  64. return true;
  65. }
  66. void
  67. release_lock ( int *lockfd, const char *filename )
  68. {
  69. unlink( filename );
  70. ::close( *lockfd );
  71. *lockfd = 0;
  72. }
  73. int
  74. backwards_fgetc ( FILE *fp )
  75. {
  76. int c;
  77. if ( fseek( fp, -1, SEEK_CUR ) != 0 )
  78. return -1;
  79. c = fgetc( fp );
  80. fseek( fp, -1, SEEK_CUR );
  81. return c;
  82. }
  83. char *
  84. backwards_fgets ( char *s, int size, FILE *fp )
  85. {
  86. if ( fseek( fp, -1, SEEK_CUR ) != 0 )
  87. return NULL;
  88. int c;
  89. while ( ( c = backwards_fgetc( fp ) ) >= 0 )
  90. if ( '\n' == c )
  91. break;
  92. long here = ftell( fp );
  93. fseek( fp, 1, SEEK_CUR );
  94. char *r = fgets( s, size, fp );
  95. fseek( fp, here, SEEK_SET );
  96. return r;
  97. }
  98. /** update the modification time of file referred to by /fd/ */
  99. void
  100. touch ( int fd )
  101. {
  102. struct stat st;
  103. fstat( fd, &st );
  104. fchmod( fd, st.st_mode );
  105. }
  106. /** write a single string to a file */
  107. void
  108. write_line ( const char *dir, const char *name, const char *value )
  109. {
  110. char path[512];
  111. snprintf( path, sizeof( path ), "%s/%s", dir, name );
  112. FILE *fp = fopen( path, "w" );
  113. if ( ! fp )
  114. return;
  115. fputs( value, fp );
  116. fclose( fp );
  117. }
  118. /** write a single string to a file */
  119. void
  120. read_line ( const char *dir, const char *name, char **value )
  121. {
  122. char path[512];
  123. *value = 0;
  124. snprintf( path, sizeof( path ), "%s/%s", dir, name );
  125. FILE *fp = fopen( path, "r" );
  126. if ( ! fp )
  127. return;
  128. *value = (char*)malloc( 512 );
  129. fgets( *value, 512, fp );
  130. fclose( fp );
  131. }
  132. #include <sys/statvfs.h>
  133. /** return the number of blocks free on filesystem containing file named /file/ */
  134. unsigned long
  135. free_space ( const char *file )
  136. {
  137. struct statvfs st;
  138. memset( &st, 0, sizeof( st ) );
  139. statvfs( file, &st );
  140. return st.f_bfree;
  141. }
  142. /** return the total number of blocks on filesystem containing file named /file/ */
  143. unsigned long
  144. total_space ( const char *file )
  145. {
  146. struct statvfs st;
  147. memset( &st, 0, sizeof( st ) );
  148. statvfs( file, &st );
  149. return st.f_blocks;
  150. }
  151. /** return the percentage of usage on filesystem containing file named /file/ */
  152. int
  153. percent_used ( const char *file )
  154. {
  155. const size_t ts = total_space( file );
  156. return 100 - ( ts ? free_space( file ) * 100 / ts : 0 );
  157. }