00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 #if defined(__MINGW__) || defined(__MINGW32__)
00014 #  include <Windows.h>
00015 #  undef ERROR
00016 #endif
00017 
00018 #define USE_STANDARD_NEW_DELETE
00019 #include <basix/system.hpp>
00020 #include <basix/vector_sort.hpp>
00021 #include <stdlib.h>
00022 #include <math.h>
00023 #include <dirent.h>
00024 #include <unistd.h>
00025 
00026 #if defined (BASIX_HAVE_SYS_TYPES_H) && defined (BASIX_HAVE_SYS_STAT_H)
00027 #include <sys/types.h>
00028 #include <sys/stat.h>
00029 #endif
00030 
00031 namespace mmx {
00032 
00033 char* command_name = NULL;
00034 string command_dir ();
00035 string global_path ();
00036 bool var_load (const string& file_name, string& s);
00037 bool var_load (const string& file_path, const string& file_name, string& s);
00038 bool var_mkdir (const string& name);
00039 bool var_rm (const string& name);
00040 string var_pwd ();
00041 static bool is_absolute_name (const string& name);
00042 
00043 
00044 
00045 
00046 
00047 #if defined(__MINGW__) || defined(__MINGW32__)
00048 
00049 static const char path_sep= ';';
00050 
00051 static string
00052 _from_dos_to_unix (const string& x) {
00053   return replace (x, "\\", "/");
00054 }
00055 
00056 #else
00057 
00058 static const char path_sep= ':';
00059 
00060 static string
00061 _from_dos_to_unix (const string& x) {
00062   return x;
00063 }
00064 #endif
00065 
00066 
00067 
00068 
00069 
00070 string
00071 get_env (const string& var) {
00072 #if defined(__MINGW__) || defined(__MINGW32__)
00073   if (var == "PWD") return var_pwd ();
00074 #endif
00075   char* _var= as_charp (var);
00076   char* _ret= getenv (_var);
00077   free_charp (_var);
00078   if (_ret == NULL) return "";
00079   string ret (_ret);
00080   return ret;
00081   
00082 }
00083 
00084 #if defined(__MINGW__) || defined(__MINGW32__)
00085 int
00086 set_env (const string& var, const string& value) {
00087   char* _var= as_charp (var * "=" * value);
00088   int ret= _putenv (_var);
00089   free_charp (_var);
00090   return ret;
00091 }
00092 #else
00093 int
00094 set_env (const string& var, const string& value) {
00095   char* _var= as_charp (var);
00096   char* _value= as_charp (value);
00097   int ret= setenv (_var, _value, 1);
00098   free_charp (_var);
00099   free_charp (_value);
00100   return ret;
00101 }
00102 #endif
00103 
00104 string
00105 get_pid () {
00106   return as_string (getpid ());
00107 }
00108 
00109 
00110 
00111 
00112 
00113 #if defined(__MINGW__) || defined(__MINGW32__)
00114 static bool
00115 file_is_bash_script (const string& file_name) {
00116   char* _file_name= as_charp (file_name);
00117   FILE* f= fopen (_file_name, "r");
00118   free_charp (_file_name);
00119   if (f == NULL) return false;
00120   char c;
00121   string s= string ("");
00122   while ((c = fgetc (f)) != EOF && c != '\n') s << c;
00123   fclose (f);
00124   return starts (s, "#!/bin/bash") ||  starts (s, "#!/bin/sh") ||
00125     starts (s, "#!sh") || starts (s, "#!bash");
00126 }
00127 
00128 int
00129 system (const string& s) {
00130   
00131   vector<string> cmd= tokenize (s, " ", false);
00132   ASSERT (N(cmd) != 0, "wrong system command");
00133   string name= path_name (global_path (), cmd[0]);
00134   if (name == "") name= path_name (global_path (), cmd[0] * ".exe");
00135   if (name == "") {
00136     mmerr << "mmxlight: command not found: " << s << lf;
00137     ERROR ("command not found");
00138   }
00139   if (file_is_bash_script (name)) {
00140     string sh= path_name (global_path (), "bash.exe");
00141     ASSERT (sh != "", "Shell bash not found");
00142     string exe= sh * " " * name * " " * recompose (cdr (cmd), " ", false);
00143     char* _s= as_charp (exe);
00144     int r= ::system (_s);
00145     free_charp (_s);
00146     return r;
00147   }
00148   string exe= name * " " * recompose (cdr (cmd), " ", false);
00149   char* _s= as_charp (exe);
00150   int r= ::system (_s);
00151   free_charp (_s);
00152   return r;
00153 }
00154 #else
00155 int
00156 system (const string& s) {
00157   
00158   char* _s= as_charp (s);
00159   int r= ::system (_s);
00160   free_charp (_s);
00161   return r;
00162 }
00163 #endif
00164 
00165 
00166 
00167 
00168 
00169 void
00170 init_system () {
00171   string path = user_dir ();
00172   if (!file_exists (path)) {
00173     printf ("This is your first use of Mathemagix.\n");
00174     printf ("I am creating a .mathemagix directory in your home directory\n");
00175     ASSERT (var_mkdir (path), "Cannot create .mathemagix user directory");
00176   }
00177   var_mkdir (path * "/etc");
00178   var_mkdir (path * "/lib");
00179   var_mkdir (path * "/var");
00180   var_mkdir (path * "/mmx");
00181   var_mkdir (path * "/tmp");
00182 }
00183 
00184 struct system_instance {
00185   system_instance () {
00186     init_system (); }
00187 };
00188 
00189 system_instance system_inst;
00190 
00191 
00192 
00193 
00194 
00195 string
00196 command_dir () {
00197   return get_directory (_from_dos_to_unix (string (command_name)));
00198 }
00199 
00200 string
00201 global_path () {
00202   string p = _from_dos_to_unix (get_env ("PATH"));
00203   return command_dir () * string (path_sep) * p;
00204 }
00205 
00206 string
00207 user_dir () {
00208   string dir= get_env ("MMX_USER_DIR");
00209   if (dir == "") {
00210 #if defined(__MINGW__) || defined(__MINGW32__)
00211     string home = get_env ("USERPROFILE");
00212     ASSERT (home != "", "Can not determine user's directory.\n Please set MMX_USER_DIR or USERPROFILE.\n");
00213     dir= home * "/AppData/Local/Mathemagix";
00214 #else
00215     string home = get_env ("HOME");
00216     ASSERT (home != "", "Can not determine user's directory.\n Please set $MMX_USER_DIR or $HOME.\n");
00217     dir= home * "/.mathemagix";
00218 #endif
00219   }
00220   return _from_dos_to_unix (dir);
00221 }
00222   
00223 string
00224 prefix_dir () {
00225   string dir= get_env ("MMX_PREFIX_DIR");
00226 #if defined(__MINGW__) || defined(__MINGW32__)
00227   if (!file_exists (dir)) {
00228     try { dir= eval_system ("basix-config --prefix"); }
00229     catch (const exception&) {
00230       dir= get_directory (command_dir ());
00231       if (!is_absolute_name (dir))
00232         dir= get_directory (get_directory (path_name
00233                                            (_from_dos_to_unix (get_env ("PATH")), "mmx-light.exe")));
00234       ASSERT (is_absolute_name (dir), "Cannot determine installation prefix");
00235     }
00236   }
00237 #else
00238   if (dir == "") dir= string (BASIX_PREFIX);
00239   if (!file_exists (dir)) dir= eval_system ("basix-config --prefix");
00240 #endif
00241   return _from_dos_to_unix (dir);
00242 }
00243 
00244 string
00245 sysconf_dir () {
00246   string dir= get_env ("MMX_SYSCONF_DIR");
00247 #if defined(__MINGW__) || defined(__MINGW32__)
00248   if (dir == "") {
00249     try { dir= eval_system ("basix-config --sysconfdir"); }
00250     catch (const exception&) { dir= prefix_dir () * "/etc"; } 
00251   }
00252 #else
00253   if (dir == "") dir= eval_system ("basix-config --sysconfdir");
00254 #endif
00255   return _from_dos_to_unix (dir);
00256 }
00257 
00258 string
00259 load_path () {
00260   string path= _from_dos_to_unix (get_env ("MMX_LOAD_PATH"));
00261   if (path != "" && path[0] != path_sep) path= string (path_sep) * path;
00262   path= "." * string (path_sep) * user_dir () * "/mmx"
00263     * string (path_sep) * prefix_dir () * "/share"
00264     * string (path_sep) * prefix_dir () * "/share/mmx" * path;
00265   return path;
00266 }
00267 
00268 
00269 
00270 
00271 
00272 string
00273 eval_system (const string& s) {
00274   init_system ();
00275   string temp= user_dir () * "/tmp/eval_system";
00276   system (s * " > " * temp);
00277   string r;
00278   bool flag= var_load (temp, r);
00279   ASSERT (var_rm (decode_name ("rm " * temp)),
00280           "Cannot remove file eval_system");
00281   if (flag) return "";
00282   while ((N(r)>0) && (r[N(r)-1] == '\n')) r= r (0, N(r)-1);
00283   return r;
00284 }
00285 
00286 int
00287 exec (const vector<string>& v) {
00288   nat n= N(v);
00289   ASSERT (n>0, "at least one argument expected");
00290   typedef char* charp;
00291   charp _v[n+1];
00292   for (nat i=0; i<n; i++)
00293     _v[i]= as_charp (v[i]);
00294   _v[n]= NULL;
00295 #if defined(__MINGW__) || defined(__MINGW32__)
00296   return _execvp (_v[0], _v);
00297 #else
00298   return execvp (_v[0], _v);
00299 #endif
00300   
00301 }
00302 
00303 
00304 
00305 
00306 
00307 static inline bool
00308 is_absolute_name (const string& name) {
00309 #if defined(__MINGW__) || defined(__MINGW32__)
00310   return N(name) >=2 &&
00311     ((name[0] >= 'A' && name[0] <= 'Z') ||
00312      (name[0] >= 'a' && name[0] <= 'z'))
00313     && name[1] == ':';
00314 #else
00315   return name != "" && name[0] == '/';
00316 #endif
00317 }
00318 
00319 string
00320 decode_name (const string& name) {
00321   if (name == "~") return _from_dos_to_unix (get_env ("HOME"));
00322   if (starts (name, "~/"))
00323     return _from_dos_to_unix (get_env ("HOME")) * "/" * name (2, N(name));
00324   if (starts (name, "$")) {
00325     nat i;
00326     for (i=0; i<N(name); i++)
00327       if (name[i] == '/') break;
00328     string r= _from_dos_to_unix (get_env (name (1, i)));
00329     if (i == N(name)) return r;
00330     return r * "/" * name (i+1, N(name));
00331   }
00332   return name;
00333 }
00334 
00335 bool
00336 file_exists (const string& name) {
00337   
00338   string name_s= decode_name (name);
00339   char* temp= as_charp (name_s);
00340   FILE* f= fopen (temp, "r");
00341   if (f != NULL) {
00342     fclose (f);
00343     free_charp (temp);
00344     return true;
00345   }
00346   DIR* d= opendir (temp);
00347   free_charp (temp);
00348   if (d != NULL) {
00349     closedir (d);
00350     return true;
00351   }
00352   return false;
00353 }
00354 
00355 string
00356 canonical_name (const string& name) {
00357   
00358   for (int i= N(name)-1; i>=0; i--)
00359     if (name[i] == '/') {
00360       nat j=i;
00361       while (j>0 && name[j-1] == '/') j--;
00362       string pre = canonical_name (name (0, j));
00363       string post= name (i+1, N(name));
00364       if (pre == "") return "/" * post;
00365       else if (pre == ".") return post;
00366       else if (post == ".") return pre;
00367       else if (ends (pre, "..") && post == "..") return pre * "/" * post;
00368       else if (post == "..") return get_directory (pre);
00369       else return pre * "/" * post;
00370     }
00371   return name;
00372 }
00373 
00374 string
00375 path_name (const string& file_path, const string& file_name) {
00376   
00377   
00378   if (is_absolute_name (file_name)) {
00379     if (file_exists (file_name)) return canonical_name (file_name);
00380     else return "";
00381   }
00382   else {
00383     nat i, n= N(file_path), start= 0;
00384     for (i=0; i<=n; i++)
00385       if ((i==n) || (file_path[i] == path_sep)) {
00386         string ss= file_path (start, i);
00387         if ((N(ss) > 0) && (ss[0] == '$')) {
00388           init_system ();
00389           if (ss == "$MMX_LOAD_PATH") ss= load_path ();
00390           else if (ss == "$PWD") ss= var_pwd ();
00391           else ss= _from_dos_to_unix (get_env (ss (1, N(ss))));
00392           string r= path_name (ss, file_name);
00393           if (r != "") return r;
00394         }
00395         else {
00396           if (ss == "" || ss == ".") ss= var_pwd ();
00397           if (ss != "" && ss[N(ss)-1] != '/') ss= ss * "/";
00398           if (file_exists (ss * file_name))
00399             return canonical_name (ss * file_name);
00400         }
00401         start= i+1;
00402       }
00403     return "";
00404   }
00405 }
00406 
00407 bool
00408 file_is_script (const string& file_name) {
00409   char* _file_name= as_charp (file_name);
00410   FILE* f= fopen (_file_name, "r");
00411   free_charp (_file_name);
00412   if (f == NULL) return false;
00413   char c;
00414   string s= string ("");
00415   while ((c = fgetc (f)) != EOF && c != '\n') s << c;
00416   fclose (f);
00417   return starts (s, "#!/usr/bin/env mmx-light");
00418 
00419   
00420 
00421 
00422 
00423 
00424 
00425 
00426 
00427 
00428 
00429 
00430 
00431 }
00432 
00433 string
00434 get_directory (const string& name) {
00435   for (int i= N(name)-1; i>=1; i--)
00436     if (name[i]=='/')
00437       return name (0, i);
00438   if (N(name) > 0 && name[0] == '/') return "/";
00439   return ".";
00440 }
00441 
00442 string
00443 strip_directory (const string& name) {
00444   if (N(name) == 1 && name[0] == '/') return name;
00445   for (int i= N(name)-1; i>=0; i--)
00446     if (name[i]=='/')
00447       return name (i+1, N(name));
00448   return name;
00449 }
00450 
00451 string
00452 strip_extension (const string& name) {
00453   for (int i= N(name)-1; i>=0; i--)
00454     if (name[i]=='.')
00455       return name (0, i);
00456     else if (name[i]=='/')
00457       return name;
00458   return name;
00459 }
00460 
00461 string
00462 get_extension (const string& name) {
00463   for (int i= N(name)-1; i>=0; i--)
00464     if (name[i]=='.')
00465       return name (i+1, N(name));
00466     else if (name[i]=='/')
00467       return "";
00468   return "";
00469 }
00470 
00471 string
00472 relative_name (const string& base, const string& name) {
00473   
00474   string file_name= name;
00475   if (base != "" && !is_absolute_name (file_name)) {
00476     int i;
00477     for (i= N(base)-1; i>=0; i--)
00478       if (base[i] == '/') break;
00479     file_name= base (0, i+1) * file_name;
00480   }
00481   return canonical_name (file_name);
00482 }
00483 
00484 #ifdef BASIX_HAVE_SYS_STAT_H
00485 
00486 static bool
00487 get_attributes (const string& name, struct stat* buf) {
00488   string name_s= decode_name (name);
00489   bool flag;
00490   char* temp= as_charp (name_s);
00491   flag= stat (temp, buf);
00492   free_charp (temp);
00493   return flag;
00494 }
00495 
00496 bool
00497 file_test (const string& name, const string& filter) {
00498   if (filter == "") return true;
00499   struct stat buf;
00500   if (get_attributes (name, &buf)) return false;
00501   int i, n= N (filter);
00502   for (i=0; i<n; i++)
00503     switch (filter [i]) {
00504       
00505     case 'f':
00506       if (!S_ISREG (buf.st_mode)) return false;
00507       break;
00508     case 'd':
00509       if (!S_ISDIR (buf.st_mode)) return false;
00510       break;
00511     case 'r':
00512 #ifndef __MINGW32__
00513       if ((buf.st_mode & (S_IRUSR | S_IRGRP | S_IROTH)) == 0) return false;
00514 #else
00515       if ((buf.st_mode & 292) == 0) return false;
00516 #endif
00517       break;
00518     case 'w':
00519 #ifndef __MINGW32__
00520       if ((buf.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)) == 0) return false;
00521 #else
00522       if ((buf.st_mode & 146) == 0) return false;
00523 #endif
00524       break;
00525     case 'x':
00526 #ifdef OS_WIN32
00527       if (suffix(name) == "bat") break;
00528 #endif
00529 #ifndef __MINGW32__
00530       if ((buf.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0) return false;
00531 #else
00532       if ((buf.st_mode & 73) == 0) return false;
00533 #endif
00534       break;
00535     }
00536   return true;
00537 }
00538 
00539 double
00540 file_last_modified (const string& name) {
00541   struct stat u_stat;
00542   if (get_attributes (name, &u_stat)) {
00543     mmerr << "name= " << name << "\n";
00544     ERROR ("could not stat file");
00545   }
00546   return (double) u_stat.st_mtime;
00547 }
00548 
00549 #else // !BASIX_HAVE_SYS_STAT_H
00550 
00551 static bool
00552 get_attributes (const string& name, struct stat* buf) {
00553   (void) name; (void) buf;
00554   return 1;
00555 }
00556 
00557 bool
00558 file_test (const string& name, const string& filter) {
00559   (void) name; (void) buf;
00560 }
00561 
00562 double
00563 file_last_modified (const string& name) {
00564   ERROR ("could not get file's last modification time");
00565 }
00566 
00567 #endif // BASIX_HAVE_SYS_STAT_H
00568 
00569 #if defined(__MINGW__) || defined(__MINGW32__)
00570 string
00571 follow_link (const string& name, const bool& recurse) {
00572   static bool warned= false;
00573   if (!warned) {
00574     mmerr << "Warning: follow_link is not implemented under MinGW" << lf;
00575     warned= true;
00576   }
00577   return name;
00578 }
00579 #else
00580 string
00581 follow_link (const string& name, const bool& recurse) {
00582   char buf[1024];
00583   string name_s= decode_name (name);
00584   char* temp= as_charp (name_s);
00585   int n= readlink (temp, buf, 1024);
00586   free_charp (temp);
00587   if (n >= 0 && n < 1024) {
00588     string r (buf, n);
00589     if (recurse) return follow_link (r, true);
00590     else return r;
00591   }
00592   else {
00593     if (recurse) return name;
00594     else return "";
00595   }
00596 }
00597 #endif
00598 bool file_is_file (const string& name) { return file_test (name, "f"); }
00599 bool file_is_directory (const string& name) { return file_test (name, "d"); }
00600 
00601 
00602 
00603 
00604 
00605 static string
00606 recursive_search_name (const string& dir, const string& name, bool is_dir) {
00607   
00608   if (!file_exists (dir) || !file_is_directory (dir)) return "";
00609   if (get_directory (name) != ".") {
00610     string fdir = get_directory (name);
00611     string fname= strip_directory (name);
00612     string sdir = recursive_search_name (dir, fdir, true);
00613     if (sdir == "") return "";
00614     return recursive_search_name (sdir, fname, is_dir);
00615   }
00616   string fname= canonical_name (dir * "/" * name);
00617   if (file_exists (fname)) {
00618     if (is_dir && file_is_directory (fname)) return fname;
00619     if (!is_dir && file_is_file (fname)) return fname;
00620   }
00621   if (file_exists (dir * "/mmx") && file_is_directory (dir * "/mmx")) {
00622     string fdir= canonical_name (dir * "/mmx");
00623     string sname= recursive_search_name (fdir, name, is_dir);
00624     if (sname != "") return sname;
00625   }
00626   vector<string> v;
00627   if (load_directory (dir, v)) return "";
00628   for (nat i=0; i<N(v); i++)
00629     if (!starts (v[i], ".") && v[i] != "mmx") {
00630       string fdir= canonical_name (dir * "/" * v[i]);
00631       if (file_is_directory (fdir)) {
00632         string sname= recursive_search_name (fdir, name, is_dir);
00633         if (sname != "") return sname;
00634       }
00635     }
00636   return "";
00637 }
00638 
00639 static string
00640 semi_recursive_search_name (const string& dir, const string& name) {
00641   
00642   if (!file_exists (dir) || !file_is_directory (dir)) return "";
00643   nat i;
00644   for (i=0; i<N(name); i++)
00645     if (name[i] == '/') break;
00646   if (i >= N(name)) {
00647     string sname= canonical_name (dir * "/" * name);
00648     if (file_exists (sname) && file_is_file (sname)) return sname;
00649   }
00650   else {
00651     string sdir= canonical_name (dir * "/" * name (0, i));
00652     if (file_exists (sdir) && file_is_directory (sdir)) {
00653       string sname= recursive_search_name (sdir, name (i+1, N(name)), false);
00654       if (sname != "") return sname;
00655     }
00656   }
00657   return "";
00658 }
00659 
00660 static string
00661 ancestor_search_name (const string& dir, const string& name) {
00662   
00663   string sname= semi_recursive_search_name (dir, name);
00664   if (sname != "") return sname;
00665   if (dir == "" || dir == ".") {
00666     string pdir= get_directory (var_pwd ());
00667     if (!starts (pdir, "/")) return "";
00668     return ancestor_search_name (pdir, name);
00669   }
00670   if (dir == "/") return "";
00671   return ancestor_search_name (get_directory (dir), name);
00672 }
00673 
00674 string
00675 resolve_name (const string& base, const string& name) {
00676   
00677   if (name != "" && name[0] == '.')
00678     return resolve_name (base, string ("$PWD") * name (1, N(name)));
00679   string name_bis= name;
00680 #if defined(__MINGW__) || defined(__MINGW32__)
00681   int pos= search_backwards (name, ":", N(name));
00682   if (pos > 1) name_bis= name (pos-1, N(name)); else {
00683     if (name != "" && name[0] == '/') name_bis= "/" * name; }
00684 #else
00685   if (name != "" && name[0] == '/') name_bis= "/" * name;
00686 #endif
00687   string r= relative_name (base, name_bis);
00688   if (file_exists (r) || starts (name_bis, "//")) return r;
00689   r= ancestor_search_name (get_directory (base), name_bis);
00690   if (r != "") return r;
00691   vector<string> v= tokenize (load_path (), string (path_sep), false);
00692   for (nat i=0; i<N(v); i++)
00693     if (v[i] != ".") {
00694       r= semi_recursive_search_name (v[i], name_bis);
00695       if (r != "") return r;
00696     }
00697   return "";
00698 }
00699 
00700 
00701 
00702 
00703 
00704 bool
00705 var_load (const string& file_name, string& s) {
00706   
00707   port f= input_file_port (file_name);
00708   if (error_flag (f)) return true;
00709   char c;
00710   while (can_read (f) > 0) {
00711     f >> c;
00712     s << c;
00713   }
00714   return false;
00715 }
00716 
00717 bool
00718 load (const string& orig_file_name, string& s) {
00719   return var_load (decode_name (orig_file_name), s);
00720 }
00721 
00722 bool
00723 var_load (const string& file_path, const string& file_name, string& s) {
00724   
00725   if (is_absolute_name (file_name))
00726     return var_load (file_name, s);
00727   nat i, n= N(file_path), start= 0;
00728   for (i=0; i<=n; i++)
00729     if ((i==n) || (file_path[i] == path_sep)) {
00730       string ss= file_path (start, i);
00731       if ((N(ss) > 0) && (ss[0] == '$')) {
00732         init_system ();
00733         if (ss == "$MMX_LOAD_PATH") ss= load_path ();
00734         else if (ss == "$PWD") ss= var_pwd ();
00735         else ss= _from_dos_to_unix (get_env (ss (1, N(ss))));
00736         if (!var_load (ss, file_name, s)) return false;
00737       }
00738       else {
00739         if (ss == "") ss= ".";
00740         if (ss[N(ss)-1] != '/') ss= ss * "/";
00741         if (!var_load (ss * file_name, s)) return false;
00742       }
00743       start= i+1;
00744     }
00745   return true;
00746 }
00747 
00748 bool
00749 load (const string& file_path, const string& file_name, string& s) {
00750   
00751   return var_load (file_path, decode_name (file_name), s);
00752 }
00753 
00754 bool
00755 save (const string& orig_file_name, const string& s) {
00756   string file_name= decode_name (orig_file_name);
00757   port f= output_file_port (file_name);
00758   if (error_flag (f)) return true;
00759   f << s;
00760   return false;
00761 }
00762 
00763 bool
00764 load_directory (const string& orig_name, vector<string>& dir) {
00765   string name= decode_name (orig_name);
00766   DIR* dp;
00767   char* temp= as_charp (name);
00768   dp= opendir (temp);
00769   free_charp (temp);
00770   dir= vector<string> ();
00771   if (dp == NULL) return true;
00772 
00773   struct dirent* ep;
00774   while (true) {
00775     ep= readdir (dp);
00776     if (ep == NULL) break;
00777     dir << string (ep->d_name);
00778   }
00779   (void) closedir (dp);
00780   sort (dir);
00781   return false;
00782 }
00783 
00784 
00785 
00786 
00787 
00788 bool
00789 var_mkdir (const string& name) {
00790 #if defined (BASIX_HAVE_SYS_TYPES_H) && defined (BASIX_HAVE_SYS_STAT_H)
00791   if (file_exists (name)) return false;
00792   char* _name= as_charp (name);
00793 #if defined(__MINGW__) || defined(__MINGW32__)
00794   int r= ::mkdir (_name);
00795   ::chmod (_name, 744);
00796 #else
00797   int r= ::mkdir (_name, S_IRWXU + S_IRGRP + S_IROTH);
00798 #endif
00799   free_charp (_name);
00800   return r != 0;
00801 #else  
00802   return system ("mkdir -m 744 -p " * orig_name) != 0;
00803 #endif
00804 }
00805 
00806 bool
00807 mkdir (const string& name) {
00808   return var_mkdir (decode_name (name));
00809 }
00810 
00811 bool
00812 var_rm (const string& name) {
00813 #if defined (BASIX_HAVE_SYS_TYPES_H) && defined (BASIX_HAVE_SYS_STAT_H)
00814   if (!file_exists (name)) return false;
00815   char* _name= as_charp (name);
00816   int r= ::remove (_name);
00817   free_charp (_name);
00818   return r != 0;
00819 #else  
00820   return system ("rm " * orig_name) != 0;
00821 #endif
00822 }
00823 
00824 string
00825 var_pwd () {
00826 #if defined(__MINGW__) || defined(__MINGW32__)
00827   char* ret= getcwd (NULL, 4096);
00828   if (ret == NULL) return "";
00829   string aux (ret);
00830   free (ret);
00831   return _from_dos_to_unix (aux);
00832 #else
00833   return _from_dos_to_unix (get_env ("PWD"));
00834 #endif
00835 }
00836 
00837 }