00001
00002
00003
00004
00005
00006 #include "common.h"
00007 #include <dirent.h>
00008 #include <sys/utsname.h>
00009 #include <sys/time.h>
00010 #include <pwd.h>
00011 #include <termios.h>
00012 #include "portable.h"
00013 #include "portimpl.h"
00014
00015 static char const rcsid[] = "$Id: osunix.c,v 1.31 2007/01/30 19:43:46 kate Exp $";
00016
00017
00018 off_t fileSize(char *pathname)
00019
00020 {
00021 struct stat mystat;
00022 ZeroVar(&mystat);
00023 if (stat(pathname,&mystat)==-1)
00024 {
00025 return -1;
00026 }
00027 return mystat.st_size;
00028 }
00029
00030
00031 long clock1000()
00032
00033 {
00034 struct timeval tv;
00035 static long origSec;
00036 gettimeofday(&tv, NULL);
00037 if (origSec == 0)
00038 origSec = tv.tv_sec;
00039 return (tv.tv_sec-origSec)*1000 + tv.tv_usec / 1000;
00040 }
00041
00042 void sleep1000(int milli)
00043
00044 {
00045 if (milli > 0)
00046 {
00047 struct timeval tv;
00048 tv.tv_sec = milli/1000;
00049 tv.tv_usec = (milli%1000)*1000;
00050 select(0, NULL, NULL, NULL, &tv);
00051 }
00052 }
00053
00054 long clock1()
00055
00056 {
00057 struct timeval tv;
00058 gettimeofday(&tv, NULL);
00059 return tv.tv_sec;
00060 }
00061
00062 void uglyfBreak()
00063
00064 {
00065 static char *nullPt = NULL;
00066 nullPt[0] = 0;
00067 }
00068
00069 char *getCurrentDir()
00070
00071 {
00072 static char dir[1024];
00073
00074 if (getcwd( dir, sizeof(dir) ) == NULL )
00075 {
00076 warn("No current directory");
00077 return NULL;
00078 }
00079 return dir;
00080 }
00081
00082 boolean setCurrentDir(char *newDir)
00083
00084 {
00085 if (chdir(newDir) != 0)
00086 {
00087 warn("Unable to set dir %s", newDir);
00088 return FALSE;
00089 }
00090 return TRUE;
00091 }
00092
00093 struct slName *listDir(char *dir, char *pattern)
00094
00095
00096 {
00097 struct slName *list = NULL, *name;
00098 struct dirent *de;
00099 DIR *d;
00100
00101 if ((d = opendir(dir)) == NULL)
00102 return NULL;
00103 while ((de = readdir(d)) != NULL)
00104 {
00105 char *fileName = de->d_name;
00106 if (differentString(fileName, ".") && differentString(fileName, ".."))
00107 {
00108 if (pattern == NULL || wildMatch(pattern, fileName))
00109 {
00110 name = newSlName(fileName);
00111 slAddHead(&list, name);
00112 }
00113 }
00114 }
00115 closedir(d);
00116 slNameSort(&list);
00117 return list;
00118 }
00119
00120 struct fileInfo *newFileInfo(char *name, off_t size, bool isDir)
00121
00122 {
00123 int len = strlen(name);
00124 struct fileInfo *fi = needMem(sizeof(*fi) + len);
00125 fi->size = size;
00126 fi->isDir = isDir;
00127 strcpy(fi->name, name);
00128 return fi;
00129 }
00130
00131 int cmpFileInfo(const void *va, const void *vb)
00132
00133 {
00134 const struct fileInfo *a = *((struct fileInfo **)va);
00135 const struct fileInfo *b = *((struct fileInfo **)vb);
00136 return strcmp(a->name, b->name);
00137 }
00138
00139 boolean makeDir(char *dirName)
00140
00141
00142
00143 {
00144 int err;
00145 if ((err = mkdir(dirName, 0777)) < 0)
00146 {
00147 if (errno != EEXIST)
00148 {
00149 perror("");
00150 errAbort("Couldn't make directory %s", dirName);
00151 }
00152 return FALSE;
00153 }
00154 return TRUE;
00155 }
00156
00157
00158 struct fileInfo *listDirX(char *dir, char *pattern, boolean fullPath)
00159
00160
00161
00162 {
00163 struct fileInfo *list = NULL, *el;
00164 struct dirent *de;
00165 DIR *d;
00166 int dirNameSize = strlen(dir);
00167 int fileNameOffset = dirNameSize+1;
00168 char pathName[512];
00169
00170 if ((d = opendir(dir)) == NULL)
00171 return NULL;
00172 memcpy(pathName, dir, dirNameSize);
00173 pathName[dirNameSize] = '/';
00174
00175 while ((de = readdir(d)) != NULL)
00176 {
00177 char *fileName = de->d_name;
00178 if (differentString(fileName, ".") && differentString(fileName, ".."))
00179 {
00180 if (pattern == NULL || wildMatch(pattern, fileName))
00181 {
00182 struct stat st;
00183 bool isDir = FALSE;
00184 strcpy(pathName+fileNameOffset, fileName);
00185 if (stat(pathName, &st) < 0)
00186 errAbort("stat failed in listDirX");
00187 if (S_ISDIR(st.st_mode))
00188 isDir = TRUE;
00189 if (fullPath)
00190 fileName = pathName;
00191 el = newFileInfo(fileName, st.st_size, isDir);
00192 slAddHead(&list, el);
00193 }
00194 }
00195 }
00196 closedir(d);
00197 slSort(&list, cmpFileInfo);
00198 return list;
00199 }
00200
00201 unsigned long fileModTime(char *pathName)
00202
00203
00204
00205 {
00206 struct stat st;
00207 if (stat(pathName, &st) < 0)
00208 errAbort("stat failed in fileModTime: %s", pathName);
00209 return st.st_mtime;
00210 }
00211
00212
00213 char *getHost()
00214
00215 {
00216 static char *hostName = NULL;
00217 static char buf[128];
00218 if (hostName == NULL)
00219 {
00220 hostName = getenv("HTTP_HOST");
00221 if (hostName == NULL)
00222 {
00223 hostName = getenv("HOST");
00224 if (hostName == NULL)
00225 {
00226 if (hostName == NULL)
00227 {
00228 static struct utsname unamebuf;
00229 if (uname(&unamebuf) >= 0)
00230 hostName = unamebuf.nodename;
00231 else
00232 hostName = "unknown";
00233 }
00234 }
00235 }
00236 strncpy(buf, hostName, sizeof(buf));
00237 chopSuffix(buf);
00238 hostName = buf;
00239 }
00240 return hostName;
00241 }
00242
00243 char *mysqlHost()
00244
00245 {
00246 boolean gotIt = FALSE;
00247 static char *host = NULL;
00248 if (!gotIt)
00249 {
00250 static char hostBuf[128];
00251 gotIt = TRUE;
00252 if (fileExists("mysqlHost"))
00253 {
00254 return (host = firstWordInFile("mysqlHost", hostBuf, sizeof(hostBuf)));
00255 }
00256 else
00257 return (host = getenv("MYSQLHOST"));
00258 }
00259 return host;
00260 }
00261
00262 char *semiUniqName(char *base)
00263
00264
00265
00266
00267 {
00268 int pid = getpid();
00269 int num = time(NULL)&0xFFFFF;
00270 char host[512];
00271 strcpy(host, getHost());
00272 char *s = strchr(host, '.');
00273 if (s != NULL)
00274 *s = 0;
00275 subChar(host, '-', '_');
00276 static char name[PATH_LEN];
00277 safef(name, sizeof(name), "%s_%s_%x_%x",
00278 base, host, pid, num);
00279 return name;
00280 }
00281
00282 char *rTempName(char *dir, char *base, char *suffix)
00283
00284 {
00285 char *x;
00286 static char fileName[PATH_LEN];
00287 int i;
00288 for (i=0;;++i)
00289 {
00290 x = semiUniqName(base);
00291 safef(fileName, sizeof(fileName), "%s/%s%d%s",
00292 dir, x, i, suffix);
00293 if (!fileExists(fileName))
00294 break;
00295 }
00296 return fileName;
00297 }
00298
00299 char *getUser()
00300
00301 {
00302 uid_t uid = geteuid();
00303 struct passwd *pw = getpwuid(uid);
00304 if (pw == NULL)
00305 errnoAbort("can't get user name for uid %d", (int)uid);
00306 return pw->pw_name;
00307 }
00308
00309 int mustFork()
00310
00311 {
00312 int childId = fork();
00313 if (childId == -1)
00314 errnoAbort("Unable to fork");
00315 return childId;
00316 }
00317
00318 int rawKeyIn()
00319
00320 {
00321 struct termios attr;
00322 tcflag_t old;
00323 char c;
00324
00325
00326 if (tcgetattr(STDIN_FILENO, &attr) != 0)
00327 errAbort("Couldn't do tcgetattr");
00328 old = attr.c_lflag;
00329 attr.c_lflag &= ~ICANON;
00330 attr.c_lflag &= ~ECHO;
00331 if (tcsetattr(STDIN_FILENO, TCSANOW, &attr) == -1)
00332 errAbort("Couldn't do tcsetattr");
00333
00334
00335 if (read(STDIN_FILENO,&c,1) != 1)
00336 errnoAbort("I/O error");
00337
00338
00339 attr.c_lflag = old;
00340 if (tcsetattr(STDIN_FILENO, TCSANOW, &attr) == -1)
00341 errAbort("Couldn't do tcsetattr2");
00342 return c;
00343 }
00344
00345 boolean isPipe(int fd)
00346
00347 {
00348 struct stat buf;
00349 if (fstat(fd, &buf) < 0)
00350 errnoAbort("fstat failed");
00351 return S_ISFIFO(buf.st_mode);
00352 }