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 
00037 #ifdef ANDROID_COMPILATION
00038 #define TMP_DATA_DIRECTORY "/data/omx/"
00039 #else
00040 #define TMP_DATA_DIRECTORY "/tmp/"
00041 #endif
00042 
00046 char* componentsRegistryGetFilename() {
00047   char *omxregistryfile = NULL;
00048   char *buffer;
00049 
00050   buffer=getenv("OMX_BELLAGIO_REGISTRY");
00051   if(buffer!=NULL&&*buffer!='\0') {
00052     omxregistryfile = strdup(buffer);
00053     return omxregistryfile;
00054   }
00055 
00056   buffer=getenv("XDG_DATA_HOME");
00057   if(buffer!=NULL&&*buffer!='\0') {
00058     omxregistryfile = malloc(strlen(buffer) + strlen(REGISTRY_FILENAME) + 2);
00059     strcpy(omxregistryfile, buffer);
00060     strcat(omxregistryfile, "/");
00061     strcat(omxregistryfile, REGISTRY_FILENAME);
00062     return omxregistryfile;
00063   }
00064 
00065   buffer=getenv("HOME");
00066   if(buffer!=NULL&&*buffer!='\0') {
00067     omxregistryfile  = malloc(strlen(buffer) + strlen(REGISTRY_FILENAME) + 3);
00068     strcpy(omxregistryfile, buffer);
00069     strcat(omxregistryfile, "/");
00070     strcat(omxregistryfile, REGISTRY_FILENAME);
00071   } else {
00072     omxregistryfile  = malloc(strlen(TMP_DATA_DIRECTORY) + strlen(REGISTRY_FILENAME) + 2);
00073     strcpy(omxregistryfile, TMP_DATA_DIRECTORY);
00074     strcat(omxregistryfile, REGISTRY_FILENAME);
00075   }
00076   return omxregistryfile;
00077 }
00078 
00079 /* This function return the absolute path of the registry_name file or
00080  * directory depending by the environment variable set.
00081  * the variables considered in order are:
00082  * $XDG_DATA_HOME
00083  * $HOME
00084  * TMP_DATA_DIRECTORY (/tmp by default on Linux)
00085  * The function does not check for file/dir existence
00086  */
00087 char* loadersRegistryGetFilename(char* registry_name) {
00088     char *omxregistryfile = NULL;
00089     char *buffer;
00090 
00091     buffer=getenv("XDG_DATA_HOME");
00092     if(buffer!=NULL&&*buffer!='\0') {
00093         omxregistryfile = malloc(strlen(buffer) + strlen(registry_name) + 2);
00094         strcpy(omxregistryfile, buffer);
00095         strcat(omxregistryfile, "/");
00096         strcat(omxregistryfile, registry_name);
00097         return omxregistryfile;
00098     }
00099 
00100     buffer=getenv("HOME");
00101     if(buffer!=NULL&&*buffer!='\0') {
00102         omxregistryfile  = malloc(strlen(buffer) + strlen(registry_name) + 3);
00103         strcpy(omxregistryfile, buffer);
00104         strcat(omxregistryfile, "/");
00105         strcat(omxregistryfile, registry_name);
00106     } else {
00107         omxregistryfile  = malloc(strlen(TMP_DATA_DIRECTORY) + strlen(registry_name) + 2);
00108         strcpy(omxregistryfile, TMP_DATA_DIRECTORY);
00109         strcat(omxregistryfile, registry_name);
00110     }
00111     return omxregistryfile;
00112 }
00113 
00118 int makedir (const char *newdir) {
00119   char *buffer;
00120   char *p;
00121   int err;
00122   size_t len;
00123 
00124   buffer = strdup(newdir);
00125   len = strlen(buffer);
00126 
00127   if (len == 0) {
00128     free(buffer);
00129     return 1;
00130   }
00131   if (buffer[len-1] == '/') {
00132     buffer[len-1] = '\0';
00133   }
00134 
00135     err = mkdir(buffer, 0755);
00136     if (err == 0 || (( err == -1) && (errno == EEXIST))) {
00137         free(buffer);
00138         return 0;
00139     }
00140 
00141   p = buffer+1;
00142   while (1) {
00143         char hold;
00144 
00145         while(*p && *p != '\\' && *p != '/')
00146             p++;
00147         hold = *p;
00148         *p = 0;
00149         if ((mkdir(buffer, 0755) == -1) && (errno == ENOENT)) {
00150             free(buffer);
00151             return 1;
00152         }
00153         if (hold == 0)
00154             break;
00155         *p++ = hold;
00156     }
00157   free(buffer);
00158   return 0;
00159 
00160 }
00161 
00162 int exists(const char* fname) {
00163     FILE *file;
00164     if ((file = fopen(fname, "r")))
00165     {
00166         fclose(file);
00167         return 1;
00168     }
00169     return 0;
00170 }
00171 
00172 #ifdef COMMON_TEST
00173 int main (int argc, char*argv[]) {
00174   printf (componentsRegistryGetFilename ());
00175 }
00176 #endif
00177