OpenMAXBellagio 0.9.3
common.c
Go to the documentation of this file.
00001 
00026 #include <stdio.h>
00027 #include <stdlib.h>
00028 #include <string.h>
00029 #include <errno.h>
00030 #include <sys/stat.h>
00031 
00032 #include "config.h"
00033 #include "common.h"
00034 
00035 #define REGISTRY_FILENAME ".omxregister"
00036 #define REGISTRY_DIR "/var/lib/libomxil-bellagio0/"
00037 
00038 #ifdef ANDROID_COMPILATION
00039 #define TMP_DATA_DIRECTORY "/data/omx/"
00040 #else
00041 #define TMP_DATA_DIRECTORY "/tmp/"
00042 #endif
00043 
00047 char* componentsRegistryGetFilename(void) {
00048   return componentsRegistryGetFilenameCheck(0);
00049 }
00050 
00051 char* componentsRegistryGetFilenameCheck(int check_exists) {
00052   char *omxregistryfile = NULL;
00053   char *buffer;
00054   struct stat sb;
00055 
00056   buffer=getenv("OMX_BELLAGIO_REGISTRY");
00057   if(buffer!=NULL&&*buffer!='\0') {
00058     omxregistryfile = strdup(buffer);
00059     if (!check_exists||stat(omxregistryfile, &sb) == 0) {
00060       return omxregistryfile;
00061     } else {
00062       if (omxregistryfile) {
00063     free(omxregistryfile);
00064     omxregistryfile=NULL;
00065       }
00066     }
00067     return NULL;
00068   }
00069 
00070   buffer=getenv("XDG_DATA_HOME");
00071   if(buffer!=NULL&&*buffer!='\0') {
00072     omxregistryfile = malloc(strlen(buffer) + strlen(REGISTRY_FILENAME) + 2);
00073     strcpy(omxregistryfile, buffer);
00074     strcat(omxregistryfile, "/");
00075     strcat(omxregistryfile, REGISTRY_FILENAME);
00076     if (!check_exists||stat(omxregistryfile, &sb) == 0) {
00077       return omxregistryfile;
00078     } else {
00079       if (omxregistryfile) {
00080     free(omxregistryfile);
00081     omxregistryfile=NULL;
00082       }
00083     }
00084   }
00085 
00086   buffer=getenv("HOME");
00087   if(buffer!=NULL&&*buffer!='\0') {
00088     omxregistryfile  = malloc(strlen(buffer) + strlen(REGISTRY_FILENAME) + 3);
00089     strcpy(omxregistryfile, buffer);
00090     strcat(omxregistryfile, "/");
00091     strcat(omxregistryfile, REGISTRY_FILENAME);
00092   } else {
00093     omxregistryfile  = malloc(strlen(TMP_DATA_DIRECTORY) + strlen(REGISTRY_FILENAME) + 2);
00094     strcpy(omxregistryfile, TMP_DATA_DIRECTORY);
00095     strcat(omxregistryfile, REGISTRY_FILENAME);
00096   }
00097   if (!check_exists||stat(omxregistryfile, &sb) == 0) {
00098     return omxregistryfile;
00099   } else {
00100     if (omxregistryfile) {
00101       free(omxregistryfile);
00102       omxregistryfile=NULL;
00103     }
00104   }
00105   omxregistryfile = malloc(strlen(REGISTRY_DIR) + strlen("registry") + 2);
00106   strcpy(omxregistryfile, REGISTRY_DIR);
00107   strcat(omxregistryfile, "registry");
00108   return omxregistryfile;
00109 }
00110 
00111 /* This function return the absolute path of the registry_name file or
00112  * directory depending by the environment variable set.
00113  * the variables considered in order are:
00114  * $XDG_DATA_HOME
00115  * $HOME
00116  * TMP_DATA_DIRECTORY (/tmp by default on Linux)
00117  * The function does not check for file/dir existence
00118  */
00119 char* loadersRegistryGetFilename(char* registry_name) {
00120     char *omxregistryfile = NULL;
00121     char *buffer;
00122 
00123     buffer=getenv("XDG_DATA_HOME");
00124     if(buffer!=NULL&&*buffer!='\0') {
00125         omxregistryfile = malloc(strlen(buffer) + strlen(registry_name) + 2);
00126         strcpy(omxregistryfile, buffer);
00127         strcat(omxregistryfile, "/");
00128         strcat(omxregistryfile, registry_name);
00129         return omxregistryfile;
00130     }
00131 
00132     buffer=getenv("HOME");
00133     if(buffer!=NULL&&*buffer!='\0') {
00134         omxregistryfile  = malloc(strlen(buffer) + strlen(registry_name) + 3);
00135         strcpy(omxregistryfile, buffer);
00136         strcat(omxregistryfile, "/");
00137         strcat(omxregistryfile, registry_name);
00138     } else {
00139         omxregistryfile  = malloc(strlen(TMP_DATA_DIRECTORY) + strlen(registry_name) + 2);
00140         strcpy(omxregistryfile, TMP_DATA_DIRECTORY);
00141         strcat(omxregistryfile, registry_name);
00142     }
00143     return omxregistryfile;
00144 }
00145 
00150 int makedir (const char *newdir) {
00151   char *buffer;
00152   char *p;
00153   int err;
00154   size_t len;
00155 
00156   buffer = strdup(newdir);
00157   len = strlen(buffer);
00158 
00159   if (len == 0) {
00160     free(buffer);
00161     return 1;
00162   }
00163   if (buffer[len-1] == '/') {
00164     buffer[len-1] = '\0';
00165   }
00166 
00167     err = mkdir(buffer, 0755);
00168     if (err == 0 || (( err == -1) && (errno == EEXIST))) {
00169         free(buffer);
00170         return 0;
00171     }
00172 
00173   p = buffer+1;
00174   while (1) {
00175         char hold;
00176 
00177         while(*p && *p != '\\' && *p != '/')
00178             p++;
00179         hold = *p;
00180         *p = 0;
00181         if ((mkdir(buffer, 0755) == -1) && (errno == ENOENT)) {
00182             free(buffer);
00183             return 1;
00184         }
00185         if (hold == 0)
00186             break;
00187         *p++ = hold;
00188     }
00189   free(buffer);
00190   return 0;
00191 
00192 }
00193 
00194 int exists(const char* fname) {
00195     FILE *file;
00196     if ((file = fopen(fname, "r")))
00197     {
00198         fclose(file);
00199         return 1;
00200     }
00201     return 0;
00202 }
00203 
00204 #ifdef COMMON_TEST
00205 int main (int argc, char*argv[]) {
00206   printf (componentsRegistryGetFilename (1));
00207 }
00208 #endif
00209