//////////////////////////////////////////////////////////////////////////////// // // This file is part of Toolkit for Conceptual Modeling (TCM). // (c) copyright 1995, Vrije Universiteit Amsterdam. // Author: Frank Dehne (frank@cs.vu.nl). // // TCM is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2 of the License, or // (at your option) any later version. // // TCM is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with TCM; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA // 02111-1307, USA. //////////////////////////////////////////////////////////////////////////////// #include "system.h" #include "util.h" #include "lstring.h" #include "version.h" #include #include #include #if defined(AIX) #include #else #include #if defined(LINUX) #include #endif #endif #include #include #include #include #include #include #if !defined(__GNUC__) #include #endif const char System::TOOLKIT_STAT_HEADER[] = "TCM-STAT"; const char System::TOOLKIT_BUG_HEADER[] = "TCM-BUG"; const char System::TOOLKIT_HOME_VAR_NAME[] = "TCM_HOME"; const char System::TOOLKIT_HOME_DEFAULT[] = "/home/tcm"; bool System::sendBugEmail = False; char System::emailAddress[MAXNAME] = "tcm@cs.utwente.nl"; char System::currentProgram[MAXNAME] = ""; #if defined(BSD) extern "C" int GetHostName(char *host, int namelen); #else #include #endif // extern int uname(struct utsname *name); // most common Unix directories for system wide available binaries. const char *System::STD_SEARCH_DIRS[] = {"./", "/bin/", "/usr/bin/", "/usr/local/bin/", "/usr/local/tcm/bin/", "/home/tcm/bin/", "/usr/bin/X11/", "/usr/ucb/", "/usr/sbin/", "/usr/X11/bin/", "/usr/openwin/bin/", "/opt/tcm/bin/", "/usr/local/X11/bin/", "/usr/local/bin/X11/", "/usr/openwin/contrib/bin/", "/Window/X11/bin/", "/Window/X11/contrib/bin/", 0}; #if defined(LINUX) || defined(AIX) || defined(HPUX) || defined(OSF1) || defined(BSD) || defined(__CYGWIN__) typedef void (*SIG_PF)(int); #endif bool System::GetHostName(char *host) { #if defined(BSD) char buf[MAXNAME]; if (gethostname(buf, MAXNAME-1) == 0) { strncpy(host, buf, MAXNAME-1); return 1; } #else struct utsname name; if (uname(&name)) { strncpy(host, name.nodename, MAXNAME-1); return 1; } #endif return 0; } bool System::GetLoginName(char *login) { static bool newError = True; if (login == 0) return 0; static struct passwd *myentry = 0; static struct passwd pwdentry; if (myentry == 0) myentry = getpwuid(geteuid()); if (myentry != 0) { memcpy (&pwdentry, myentry, sizeof (struct passwd)); myentry = &pwdentry; } if (myentry) { strcpy(login, pwdentry.pw_name); } else { if (newError) { error("Warning: uid %d has no passwd entry\n", geteuid()); newError = False; } strcpy(login, "nobody"); } return 1; } bool System::GetTime(char *time) { struct timeval tp; #ifdef BSD struct timezone tzp; gettimeofday(&tp, &tzp); #else gettimeofday(&tp, 0); #endif time_t secs = tp.tv_sec; // ctime puts the time in a 26 char time string. strcpy(time, ctime(&secs)); // remove newline at end of string. if (time != 0) time[24] = '\0'; return (time == 0); } bool System::GetPrinter(char *printer) { char *p = getenv("PRINTER"); if (!p || equal(p, "")) { // error("Warning: PRINTER variable not set \n"); return False; } strcpy(printer, p); return True; } bool System::GetCwd(char *dir) { // current directory has to be found. #ifdef BSD if (!getwd(dir)) { #else if (!getcwd(dir, MAXNAME-1)) { #endif strcpy(dir, ""); return False; } strcat(dir, "/"); return True; } bool System::GetHome(char *home) { char *p = getenv("HOME"); if (!p || equal(p, "")) return False; strncpy(home, p, MAXNAME-1); strcat(home, "/"); return True; } void System::GetToolkitHome(char *home) { char *p = getenv(TOOLKIT_HOME_VAR_NAME); if (!p || equal(p, "")) { // try first TCM_HOME for compatibility. char *p2 = getenv("TCM_HOME"); if (p2 && !equal(p2, "")) { strncpy(home, p2, MAXNAME-1); } else { const char tmp[]=TCM_INSTALL_DIR; strcpy(home, tmp); } } else strncpy(home, p, MAXNAME-1); } void System::GetToolkitLib(char *lib) { char *p = getenv("TCM_LIB"); if (!p || equal(p, "")) { // try TCM_HOME/lib for compatibility. char *p2 = getenv("TCM_HOME"); if (p2 && !equal(p2, "")) { strncpy(lib, p2, MAXNAME-1); strcat(lib, "/lib/"); } else { const char tmp[]=TCM_INSTALL_LIB; strcpy(lib, tmp); } } else strncpy(lib, p, MAXNAME-1); } void System::GetToolkitConfig(char *conf) { char *p = getenv("TCM_CONFIG"); if (!p || equal(p, "")) { // try TCM_HOME/lib for compatibility. char *p2 = getenv("TCM_HOME"); if (p2 && !equal(p2, "")) { strncpy(conf, p2, MAXNAME-1); strcat(conf, "/lib/"); } else { const char tmp[]=CONFIG_INSTALL; strcpy(conf, tmp); } } else strncpy(conf, p, MAXNAME-1); } void System::GetToolkitShare(char *share) { char *p = getenv("TCM_SHARE"); if (!p || equal(p, "")) { // try TCM_HOME/lib for compatibility. char *p2 = getenv("TCM_HOME"); if (p2 && !equal(p2, "")) { strncpy(share, p2, MAXNAME-1); strcat(share, "/lib/"); } else { const char tmp[]=TCM_INSTALL_SHARE; strcpy(share, tmp); } } else strncpy(share, p, MAXNAME-1); } void System::GetToolkitHelp(char *help) { char *p = getenv("TCM_HELP"); if (!p || equal(p, "")) { // try TCM_HOME/lib for compatibility. char *p2 = getenv("TCM_HOME"); if (p2 && !equal(p2, "")) { strncpy(help, p2, MAXNAME-1); strcat(help, "/lib/help/"); } else { const char tmp[]=HELP_DIR; strcpy(help, tmp); } } else strncpy(help, p, MAXNAME-1); } unsigned long System::GenerateId(unsigned long s) { long id = abs ((long)time((time_t *)0) - ((time_t)s)); srand48(id); id = lrand48(); return id; } void System::GiveFile(const char *path, char *dir, char *file) { const char *p = strrchr(path, '/'); if (p == 0) { strcpy(dir, ""); strncpy(file, path, MAXNAME-1); } else { strncpy(file, ++p, MAXNAME-1); int len = strlen(path)-strlen(p); strncpy(dir, path, len); dir[len] = '\0'; } } void System::GiveFile(const char *path, char *file) { const char *p = strrchr(path, '/'); if (p == 0) strncpy(file, path, MAXNAME-1); else strncpy(file, ++p, MAXNAME-1); } bool System::FileRegular(const char *file) { struct stat status; if (stat(file, &status) != 0) return False; return S_ISREG(status.st_mode); } bool System::FileExecutable(const char *file) { struct stat status; if (stat(file, &status) == 0) { mode_t mode = status.st_mode; if (mode & S_IXOTH) return True; } return False; } bool System::FileExists(const char *path) { struct stat status; return (stat(path, &status) == 0); } bool System::DirExists(const char *dir) { struct stat status; if (stat(dir, &status) != 0) return False; return S_ISDIR(status.st_mode); } bool System::FindProgram(char *program, const char *command) { // strcpy(program, ""); // // first try this: // char *t = getenv(TOOLKIT_HOME_VAR_NAME); // if (!t || equal(t,"")) { // t = getenv("TCM_HOME"); // if (!t || equal(t,"")) // return False; // } const char *t = TCM_INSTALL_DIR; strcpy(program, t); strcat(program, "/bin/"); strcat(program, command); if (FileExecutable(program)) { return True; } // try some common directories. for (const char **p = &STD_SEARCH_DIRS[0]; *p; p++) { strcpy(program, *p); strcat(program, command); if (FileExecutable(program)) { return True; } } return False; } bool System::NxnParse(const char *parsestring, int &num1, int &num2) { char description[MAXNAME]; strcpy(description, parsestring); char *p = description; while (*p != 'x') p++; if (!*p) { error("parse %s: no x found\n", parsestring); return False; } *p = '\0'; num1 = atoi(description); if (num1 <= 0) { error("parse %s: no num1 found\n", parsestring); return False; } num2 = atoi(++p); if (num2 <= 0) { error("parse %s: no num2 found\n", parsestring); return False; } return True; } bool System::FindArg(char **argv, int argc, const char *arg, int &index) { for (int i=0; igetstr(), t1, t2); *dir = t1; *file = t2; } void System::GiveFile(const string *path, string *file) { char t1[MAXNAME]; GiveFile(path->getstr(), t1); *file = t1; } bool System::FindProgram(string *program, const char *command) { char t1[MAXNAME]; bool b = FindProgram(t1, command); *program = t1; return b; }