123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826 |
- /*
- * CDE - Common Desktop Environment
- *
- * Copyright (c) 1993-2012, The Open Group. All rights reserved.
- *
- * These libraries and programs are free software; you can
- * redistribute them and/or modify them under the terms of the GNU
- * Lesser General Public License as published by the Free Software
- * Foundation; either version 2 of the License, or (at your option)
- * any later version.
- *
- * These libraries and programs are distributed in the hope that
- * they will be useful, but WITHOUT ANY WARRANTY; without even the
- * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
- * PURPOSE. See the GNU Lesser General Public License for more
- * details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with these libraries and programs; if not, write
- * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
- * Floor, Boston, MA 02110-1301 USA
- */
- /*
- * $TOG: funcs.C /main/16 1998/04/17 11:51:14 mgreess $
- *
- * Copyright (c) 1992 HaL Computer Systems, Inc. All rights reserved.
- * UNPUBLISHED -- rights reserved under the Copyright Laws of the United
- * States. Use of a copyright notice is precautionary only and does not
- * imply publication or disclosure.
- *
- * This software contains confidential information and trade secrets of HaL
- * Computer Systems, Inc. Use, disclosure, or reproduction is prohibited
- * without the prior express written permission of HaL Computer Systems, Inc.
- *
- * RESTRICTED RIGHTS LEGEND
- * Use, duplication, or disclosure by the Government is subject to
- * restrictions as set forth in subparagraph (c)(l)(ii) of the Rights in
- * Technical Data and Computer Software clause at DFARS 252.227-7013.
- * HaL Computer Systems, Inc.
- * 1315 Dell Avenue, Campbell, CA 95008
- *
- */
- #include "utility/funcs.h"
- #include "misc/unique_id.h"
- #define X_INCLUDE_TIME_H
- #define XOS_USE_XT_LOCKING
- #include <X11/Xos_r.h>
- /* Imake stuff defines SYSV; this code uses SVR4 ... here's the quick-fix */
- #if defined(SYSV) && ! defined(SVR4)
- #define SVR4
- #endif
- #ifdef SVR4
- #include <sys/utsname.h>
- #endif
- #if defined(__linux__)
- #include <sys/vfs.h>
- #include <stdarg.h>
- #elif defined(_AIX)
- #include <sys/vfs.h>
- #include <sys/statfs.h>
- #include <sys/statvfs.h>
- #else
- #include <sys/statvfs.h>
- #include <stdio.h>
- #include <stdarg.h>
- #endif
- #include <sys/stat.h>
- #define BUFLEN 512
- #ifdef mips
- #include <sys/utsname.h>
- int gethostname(char* name, int namelen)
- {
- struct utsname myuname;
- if ( uname(&myuname) != 0 ) {
- MESSAGE(cerr, "gethostname(): uname() failed");
- throw(Exception());
- }
- int l = strlen(myuname.nodename);
- if ( l >= namelen ) {
- cerr << "gethostname(): name array too short.\n";
- throw(Exception());
- } else
- memcpy(name, myuname.nodename, l);
-
- return 0;
- }
- #endif
- int compare_stream(ostringstream& x, ostringstream& y)
- {
- string xstr = x.str();
- string ystr = y.str();
- if ( xstr.size() != ystr.size() ) {
- cerr << xstr.size() << "---" << ystr.size() << endl;
- //debug(cerr, xstr.c_str());
- //debug(cerr, ystr.c_str());
- return 1;
- } else {
- char* u = (char *)xstr.c_str();
- char* v = (char *)ystr.c_str();
- //debug(cerr, u);
- //debug(cerr, v);
- //fprintf(stderr, "u=%s, pcount() = %d\n", u, x.pcount());
- //fprintf(stderr, "v=%s, pcount() = %d\n", v, y.pcount());
- if ( memcmp(u, v, xstr.size()) != 0 ) {
- STDERR_MESSAGE("two streams do not match.");
- debug(cerr, u);
- debug(cerr, v);
- return 1;
- } else {
- //STDERR_MESSAGE("two streams match.");
- return 0;
- }
- }
- }
- /*
- float flog2(unsigned int x)
- {
- return (float)(log((double)x) / log((double)2));
- }
- float flog2(const float x)
- {
- return (float)log((double)x) / (float)log((float)2);
- }
- int pow2(const int x)
- {
- return (int)pow((double)2, (double)x);
- }
- int pow2(const float x)
- {
- return (int)pow((double)2, (double)x);
- }
- int bits(const int x)
- {
- return (int)flog2((unsigned int)x);
- }
- */
- int pos_of_LSB(const unsigned int y)
- {
- switch (y) {
- case 8192: return 13;
- case 1024: return 10;
- default:
- {
- unsigned int x = y;
-
- //debug(cerr, x);
- //debug(cerr, hex(x));
-
- unsigned int i;
- for ( i =0; i<sizeof(x); i++ ) {
- if ( ( 0x000000ff & x) == 0 )
- x >>= 8;
- else
- break;
- }
-
- //debug(cerr, i);
-
- int j;
- for ( j =1; j<=8; j++ )
- if ( (0x00000001 & x) == 0 )
- x >>= 1;
- else
- break;
-
- //debug(cerr, j);
- //debug(cerr, i*8+j);
-
- return i*8 + j;
- }
- }
-
- }
- /*
- void char_swap(char& c1, char& c2)
- {
- char tmp = c1;
- c1 = c2;
- c2 = tmp;
- }
- void short_swap(short& c1, short& c2)
- {
- short tmp = c1;
- c1 = c2;
- c2 = tmp;
- }
- void int_swap(int& c1, int& c2)
- {
- int tmp = c1;
- c1 = c2;
- c2 = tmp;
- }
- */
- int ceiling(const float x)
- {
- if ( int(x) > x )
- return int(x)+1;
- else
- return int(x);
- }
- unsigned getbits(unsigned x, unsigned p, unsigned n)
- {
- return((x>> (p-n)) & ~(~0 << n));
- }
- int del_file(const char* filename, const char* pathname)
- {
- unsigned int len, slen;
- static char buf[BUFLEN];
- int ok;
- if ( pathname == 0 )
- ok = unlink(filename);
- else {
- if ( strlen(filename) + strlen(pathname) > (BUFLEN - 1) )
- throw(boundaryException(1, BUFLEN,
- strlen(filename) + strlen(pathname)));
- buf[0] = 0;
- len = MIN(strlen(pathname), BUFLEN - 1);
- *((char *) memcpy(buf, pathname, len) + len) = '\0';
- slen = len;
- len = MIN(1, BUFLEN - 1 - slen);
- *((char *) memcpy(buf + slen, "/", len) + len) = '\0';
- slen += len;
- len = MIN(strlen(filename), BUFLEN - 1 - slen);
- *((char *) memcpy(buf + slen, filename, len) + len) = '\0';
- ok = unlink(buf);
- }
-
- if ( ok == -1 ) {
- debug(cerr, pathname);
- debug(cerr, filename);
- MESSAGE(cerr, form("unlink %s/%s failed", pathname, filename));
- throw(systemException(errno));
- }
- return 0;
- }
- Boolean copy_file(const char* source, const char* sink)
- {
- fstream in(source, ios::in);
- fstream out(sink, ios::out);
- if ( !in || ! out )
- return false;
- int c;
- while ( (c=in.get()) != EOF ) {
- out.put((unsigned char)c);
- }
- in.close();
- if ( out.fail() )
- return false;
- else
- return true;
- }
- Boolean
- copy_file(const char* path, const char* file,
- const char* source_ext, const char* target_ext)
- {
- char source[PATHSIZ];
- char target[PATHSIZ];
- snprintf(source, sizeof(source), "%s/%s.%s", path, file, source_ext);
- snprintf(target, sizeof(target), "%s/%s.%s", path, file, target_ext);
- return copy_file(source, target) ;
- }
- Boolean exist_file(const char* filename, const char* pathname)
- {
- int ok;
- struct stat stat_info;
- if ( pathname )
- ok = stat(form("%s/%s", pathname, filename), &stat_info);
- else
- ok = stat( filename, &stat_info );
- if ( ok == 0 )
- return S_ISREG(stat_info.st_mode) ? true : false ;
- switch (errno) {
- case ENOENT:
- return false;
- default:
- MESSAGE(cerr, "exist_file(): stat() failed. an exception");
- throw(systemException(errno));
- }
- }
- int check_file(istream& in, const char* msg)
- {
- char c;
- in.get(c);
- in.putback(c);
- cerr << c << " " << (int)c << " <---" << msg << "\n";
- return 0;
- }
- Boolean
- cat_file(const char* source1, const char* source2, const char* target)
- {
- /*
- MESSAGE(cerr, "in cat_file");
- debug(cerr, source1);
- debug(cerr, source2);
- debug(cerr, target);
- */
- fstream in1(source1, ios::in);
- fstream out(target, ios::out);
- if ( !in1 || ! out )
- return false;
- char buf[BUFSIZ];
- while ( in1.getline(buf, BUFSIZ) ) {
- out << buf;
- if ( in1.gcount() < BUFSIZ - 1 )
- out << '\n';
- }
- in1.close();
- fstream in2(source2, ios::in);
- if ( !in2 )
- return false;
- while ( in2.getline(buf, BUFSIZ) ) {
- out << buf;
- if ( in2.gcount() < BUFSIZ - 1 )
- out << '\n';
- }
- in2.close();
- out.close();
- return ( out.fail() ) ? false : true;
- }
- Boolean exist_dir(const char* pathname)
- {
- struct stat stat_info;
- if ( stat( pathname, &stat_info ) == 0 )
- return S_ISDIR(stat_info.st_mode) ? true : false ;
- switch ( errno ) {
- case ENOENT:
- return false;
- default:
- MESSAGE(cerr, "exist_dir() failed");
- debug(cerr, pathname);
- throw(systemException(errno));
- }
- }
- Boolean check_and_create_dir(const char* path)
- {
- if ( exist_dir(path) == true )
- return true;
- const char* path_tail = path + 1; // skip the first '/'
- char* slash_ptr;
- // create the subdirecties
- while ( path_tail[0] != 0 &&
- ( slash_ptr = (char *)strchr(path_tail, '/') ) ) {
- path_tail = slash_ptr + 1; // set for the next check
- slash_ptr[0] = 0; // temp. set the slash to 0.
- //debug(cerr, path);
- if ( exist_dir(path) == false ) {
- if ( mkdir(path, 0777) != 0 ) {
- debug(cerr, path);
- slash_ptr[0] = '/'; //reset to '/'
- perror(0);
- MESSAGE(cerr, form( "mkdir failed on path %s", path));
- throw(systemException(errno));
- }
- }
- slash_ptr[0] = '/'; //reset to '/'
- }
- // create the full path
- if ( mkdir(path, 0777) != 0 ) {
- cerr << "mkdir failed on path " << path << "\n";
- throw(systemException(errno));
- }
- return true;
- }
- static
- int open_prot(int min, int def)
- {
- int prot;
- umask(prot = umask(0));
- prot = min | (def & ~(prot & 0777));
-
- return prot;
- }
- int open_file_prot()
- {
- return open_prot(0600,0666);
- }
- int open_dir_prot()
- {
- return open_prot(0700,0777);
- }
- Boolean int_eq(void* x, void* y)
- {
- if ( *(int*)x == *(int*)y )
- return true;
- else
- return false;
- }
- Boolean int_ls(void* x, void* y)
- {
- if ( *(int*)x < *(int*)y )
- return true;
- else
- return false;
- }
- int ll4(int x)
- {
- int u = sizeof(void*);
- int delta = x % u ;
- return ( delta == 0 ) ? x : x + u - delta;
- }
- //Boolean fcntl_lock( int fd, lock_t lt )
- //{
- // flock flock_record;
- //
- // switch ( lt ) {
- // case SHARED:
- // flock_record.l_type = F_RDLCK;
- // break;
- // case EXCLUSIVE:
- // flock_record.l_type = F_WRLCK;
- // break;
- // default:
- // perror("fcntl_lock(): unknown lock type");
- // exit(-2);
- // }
- //
- ///****************************/
- //// the entire file is locked
- ///****************************/
- // flock_record.l_whence = SEEK_SET,
- // flock_record.l_start = 0;
- // flock_record.l_len = 0;
- //
- // if ( fcntl(fd, F_SETLKW, (int)&flock_record) != -1 ) {
- // return true;
- // } else {
- // return false;
- // }
- //}
- //
- //Boolean fcntl_unlock( int fd )
- //{
- // flock flock_record;
- //
- ///****************************/
- //// the entire file is unlocked
- ///****************************/
- // flock_record.l_type = F_UNLCK;
- // flock_record.l_whence = SEEK_SET;
- // flock_record.l_start = 0;
- // flock_record.l_len = 0;
- //
- // if ( fcntl(fd, F_SETLKW, (int)&flock_record) != -1 ) {
- // return true;
- // } else
- // return false;
- //}
- //
- //static Boolean time_out;
- //
- //Boolean timed_lock(int fd, lock_t lt, int seconds)
- //{
- // signal(SIGALRM, (SIG_PF)onalarm);
- // alarm(seconds);
- // time_out = false;
- //
- // while ( fcntl_lock(fd, lt) == false ) {
- // switch ( errno ) {
- // case EINTR:
- //
- // if ( time_out == true ) {
- //#ifdef DEBUG
- // MESSAGE(cerr, "time out after");
- // debug(cerr, seconds);
- //#endif
- // return false;
- // }
- //
- // break;
- //
- // default:
- //#ifdef DEBUG
- // MESSAGE(cerr, "error in fcntl_lock()");
- // perror(0);
- // debug(cerr, fd);
- //#endif
- // return false;
- // }
- // }
- // signal(SIGALRM, SIG_IGN);
- // return true;
- //}
- //
- //Boolean timed_unlock(int fd, int seconds)
- //{
- // signal(SIGALRM, (SIG_PF)onalarm);
- // alarm(seconds);
- // time_out = false;
- //
- // while ( fcntl_unlock(fd) == false ) {
- // switch ( errno ) {
- // case EINTR:
- //
- // if ( time_out == true )
- // return false;
- //
- // break;
- //
- // default:
- // return false;
- // }
- // }
- // signal(SIGALRM, SIG_IGN);
- // return true;
- //}
- //
- //void onalarm(int)
- //{
- // time_out = true;
- //}
- static
- char* time_stamp(_Xctimeparams *ctime_buf)
- {
- time_t x;
- time(&x);
- return _XCtime(&x, *ctime_buf);
- }
- #ifdef C_API
- int bytes(fstream& fs)
- {
- struct stat file_info;
- if ( fstat( fs.rdbuf() -> fd(), &file_info) != 0 )
- return 0;
- else
- return int(file_info.st_size);
- }
- int bytes(int fd)
- {
- struct stat file_info;
- if ( fstat( fd, &file_info) != 0 )
- return 0;
- else
- return int(file_info.st_size);
- }
- #else
- int bytes(fstream* fs)
- {
- streampos begin, current, end;
- int total_bytes;
- current = fs->tellg();
- fs->seekg(ios::beg);
- begin = fs->tellg();
- fs->seekg(ios::end);
- end = fs->tellg();
- fs->seekg(current);
- total_bytes = end - begin;
- return int(total_bytes);
- }
- #endif
- int bytes(char * file_name)
- {
- struct stat file_info;
- if ( stat( file_name, &file_info) != 0 )
- return 0;
- else
- return int(file_info.st_size);
- }
- char* form(const char* fmt, ...)
- {
- static char formbuf[BUFSIZ];
- char tempbuf[BUFSIZ];
- va_list args;
- int len;
- va_start(args, fmt);
- len = MIN(strlen(formbuf), BUFSIZ - 1);
- *((char *) memcpy(tempbuf, formbuf, len) + len) = '\0';
- (void) vsnprintf(tempbuf, sizeof(tempbuf), fmt, args);
- va_end(args);
- len = MIN(strlen(tempbuf), BUFSIZ - 1);
- *((char *) memcpy(formbuf, tempbuf, len) + len) = '\0';
- return formbuf;
- }
- static char info_buf[BUFSIZ];
- char* access_info( char* request )
- {
- #ifndef SVR4
- char dm_name[PATHSIZ];
- int dm_name_sz = PATHSIZ;
- if ( getdomainname(dm_name, dm_name_sz) == -1 ) {
- MESSAGE(cerr, "getdomainname() failed");
- throw(systemException(errno));
- }
- #endif
- #ifdef SVR4
- struct utsname name ;
- uname(&name);
- #else
- char host_name[PATHSIZ];
- int host_name_sz = PATHSIZ;
- if ( gethostname(host_name, host_name_sz) == -1 ) {
- MESSAGE(cerr, "gethostname() failed");
- throw(systemException(errno));
- }
- #endif
- _Xctimeparams ctime_buf;
- char* x = time_stamp(&ctime_buf);
- x[strlen(x)-1] = 0;
- #ifndef SVR4
- snprintf(info_buf, sizeof(info_buf), "%s-%s-%ld-%s-%s",
- host_name, dm_name,
- /* getenv("USER"), */
- (long)getpid(), x, request
- );
- #else
- char userid[L_cuserid];
- snprintf(info_buf, sizeof(info_buf), "%s-%s-%ld-%s-%s",
- name.nodename,
- ( cuserid(userid)[0] == 0 ) ? "???" : userid,
- /* getenv("USER"), */
- (long)getpid(), x, request
- );
- #endif
- return info_buf;
- }
- void lsb_putbits(unsigned& target, unsigned position_from_lsb,
- unsigned bits, unsigned source)
- {
- target |= ((source & ~( ~0 << bits )) << position_from_lsb) ;
- }
- unsigned lsb_getbits(unsigned source, unsigned position_from_lsb, unsigned bits)
- {
- return ( ( source >> position_from_lsb ) & ~( ~0 << bits ) );
- }
- Boolean cc_is_digit(istream& in)
- {
- int c = in.get();
- int ok = isdigit(c);
- in.putback(c);
- return ( ok ) ? true : false;
- }
- unsigned long disk_space(const char* path)
- {
- #if defined (SVR4) || defined(CSRG_BASED)
- struct statvfs statfs_buf;
- #else
- struct statfs statfs_buf;
- #endif
- long free_bytes;
- #if defined (SVR4) || defined(CSRG_BASED)
- if ( statvfs(path, &statfs_buf) == 0 ) {
- free_bytes = statfs_buf.f_bavail * statfs_buf.f_frsize ;
- #else
- if ( statfs(path, &statfs_buf) == 0 ) {
- free_bytes = statfs_buf.f_bavail * statfs_buf.f_bsize ;
- #endif
- } else {
- throw(stringException(form("statfs failed on %s", path)));
- }
- return free_bytes;
- }
- Boolean writeToTmpFile(char* unique_nm, char* str, int size)
- {
- Boolean ok = false;
- fstream *out = 0;
- char* tmp_dir_tbl[4];
- int len;
- tmp_dir_tbl[0] = getenv("TMPDIR");
- tmp_dir_tbl[1] = (char*)"/tmp";
- tmp_dir_tbl[2] = (char*)"/usr/tmp";
- tmp_dir_tbl[3] = getenv("HOME");
- int tmp_dir_tbl_size = 4;
- const char* uid = unique_id();
- for ( int i=0; i<tmp_dir_tbl_size; i++ ) {
- if ( tmp_dir_tbl[i] == 0 )
- continue;
- len = MIN(strlen(tmp_dir_tbl[i]) + strlen(uid) + 5, PATHSIZ - 1);
- *((char *) memcpy(unique_nm,
- form("%s/tmp.%s", tmp_dir_tbl[i], uid),
- len) + len) = '\0';
- mtry {
- //debug(cerr, tmp_dir_tbl[i]);
- //debug(cerr, disk_space(tmp_dir_tbl[i]));
- if ( disk_space(tmp_dir_tbl[i]) <= (unsigned long) size )
- continue;
- out = new fstream(unique_nm, ios::out);
- if ( !(*out) ) {
- delete out;
- continue;
- }
-
- if ( ! (out->write(str, size) ) ) {
- out -> close();
- delete out;
- del_file(unique_nm);
- continue;
- } else {
- ok = true;
- out -> close();
- delete out;
- break;
- }
-
- }
- mcatch_any()
- {
- continue;
- }
- end_try;
- }
- return ok;
- }
|