2
0

funcs.C 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /*
  24. * $TOG: funcs.C /main/16 1998/04/17 11:51:14 mgreess $
  25. *
  26. * Copyright (c) 1992 HaL Computer Systems, Inc. All rights reserved.
  27. * UNPUBLISHED -- rights reserved under the Copyright Laws of the United
  28. * States. Use of a copyright notice is precautionary only and does not
  29. * imply publication or disclosure.
  30. *
  31. * This software contains confidential information and trade secrets of HaL
  32. * Computer Systems, Inc. Use, disclosure, or reproduction is prohibited
  33. * without the prior express written permission of HaL Computer Systems, Inc.
  34. *
  35. * RESTRICTED RIGHTS LEGEND
  36. * Use, duplication, or disclosure by the Government is subject to
  37. * restrictions as set forth in subparagraph (c)(l)(ii) of the Rights in
  38. * Technical Data and Computer Software clause at DFARS 252.227-7013.
  39. * HaL Computer Systems, Inc.
  40. * 1315 Dell Avenue, Campbell, CA 95008
  41. *
  42. */
  43. #include "utility/funcs.h"
  44. #include "misc/unique_id.h"
  45. #define X_INCLUDE_TIME_H
  46. #define XOS_USE_XT_LOCKING
  47. #include <X11/Xos_r.h>
  48. /* Imake stuff defines SYSV; this code uses SVR4 ... here's the quick-fix */
  49. #if defined(SYSV) && ! defined(SVR4)
  50. #define SVR4
  51. #endif
  52. #ifdef SVR4
  53. #include <sys/utsname.h>
  54. #endif
  55. #if defined(__linux__)
  56. #include <sys/vfs.h>
  57. #include <stdarg.h>
  58. #elif defined(_AIX)
  59. #include <sys/vfs.h>
  60. #include <sys/statfs.h>
  61. #include <sys/statvfs.h>
  62. #else
  63. #include <sys/statvfs.h>
  64. #include <stdio.h>
  65. #include <stdarg.h>
  66. #endif
  67. #include <sys/stat.h>
  68. #define BUFLEN 512
  69. #ifdef mips
  70. #include <sys/utsname.h>
  71. int gethostname(char* name, int namelen)
  72. {
  73. struct utsname myuname;
  74. if ( uname(&myuname) != 0 ) {
  75. MESSAGE(cerr, "gethostname(): uname() failed");
  76. throw(Exception());
  77. }
  78. int l = strlen(myuname.nodename);
  79. if ( l >= namelen ) {
  80. cerr << "gethostname(): name array too short.\n";
  81. throw(Exception());
  82. } else
  83. memcpy(name, myuname.nodename, l);
  84. return 0;
  85. }
  86. #endif
  87. int compare_stream(ostringstream& x, ostringstream& y)
  88. {
  89. string xstr = x.str();
  90. string ystr = y.str();
  91. if ( xstr.size() != ystr.size() ) {
  92. cerr << xstr.size() << "---" << ystr.size() << endl;
  93. //debug(cerr, xstr.c_str());
  94. //debug(cerr, ystr.c_str());
  95. return 1;
  96. } else {
  97. char* u = (char *)xstr.c_str();
  98. char* v = (char *)ystr.c_str();
  99. //debug(cerr, u);
  100. //debug(cerr, v);
  101. //fprintf(stderr, "u=%s, pcount() = %d\n", u, x.pcount());
  102. //fprintf(stderr, "v=%s, pcount() = %d\n", v, y.pcount());
  103. if ( memcmp(u, v, xstr.size()) != 0 ) {
  104. STDERR_MESSAGE("two streams do not match.");
  105. debug(cerr, u);
  106. debug(cerr, v);
  107. return 1;
  108. } else {
  109. //STDERR_MESSAGE("two streams match.");
  110. return 0;
  111. }
  112. }
  113. }
  114. /*
  115. float flog2(unsigned int x)
  116. {
  117. return (float)(log((double)x) / log((double)2));
  118. }
  119. float flog2(const float x)
  120. {
  121. return (float)log((double)x) / (float)log((float)2);
  122. }
  123. int pow2(const int x)
  124. {
  125. return (int)pow((double)2, (double)x);
  126. }
  127. int pow2(const float x)
  128. {
  129. return (int)pow((double)2, (double)x);
  130. }
  131. int bits(const int x)
  132. {
  133. return (int)flog2((unsigned int)x);
  134. }
  135. */
  136. int pos_of_LSB(const unsigned int y)
  137. {
  138. switch (y) {
  139. case 8192: return 13;
  140. case 1024: return 10;
  141. default:
  142. {
  143. unsigned int x = y;
  144. //debug(cerr, x);
  145. //debug(cerr, hex(x));
  146. unsigned int i;
  147. for ( i =0; i<sizeof(x); i++ ) {
  148. if ( ( 0x000000ff & x) == 0 )
  149. x >>= 8;
  150. else
  151. break;
  152. }
  153. //debug(cerr, i);
  154. int j;
  155. for ( j =1; j<=8; j++ )
  156. if ( (0x00000001 & x) == 0 )
  157. x >>= 1;
  158. else
  159. break;
  160. //debug(cerr, j);
  161. //debug(cerr, i*8+j);
  162. return i*8 + j;
  163. }
  164. }
  165. }
  166. /*
  167. void char_swap(char& c1, char& c2)
  168. {
  169. char tmp = c1;
  170. c1 = c2;
  171. c2 = tmp;
  172. }
  173. void short_swap(short& c1, short& c2)
  174. {
  175. short tmp = c1;
  176. c1 = c2;
  177. c2 = tmp;
  178. }
  179. void int_swap(int& c1, int& c2)
  180. {
  181. int tmp = c1;
  182. c1 = c2;
  183. c2 = tmp;
  184. }
  185. */
  186. int ceiling(const float x)
  187. {
  188. if ( int(x) > x )
  189. return int(x)+1;
  190. else
  191. return int(x);
  192. }
  193. unsigned getbits(unsigned x, unsigned p, unsigned n)
  194. {
  195. return((x>> (p-n)) & ~(~0 << n));
  196. }
  197. int del_file(const char* filename, const char* pathname)
  198. {
  199. unsigned int len, slen;
  200. static char buf[BUFLEN];
  201. int ok;
  202. if ( pathname == 0 )
  203. ok = unlink(filename);
  204. else {
  205. if ( strlen(filename) + strlen(pathname) > (BUFLEN - 1) )
  206. throw(boundaryException(1, BUFLEN,
  207. strlen(filename) + strlen(pathname)));
  208. buf[0] = 0;
  209. len = MIN(strlen(pathname), BUFLEN - 1);
  210. *((char *) memcpy(buf, pathname, len) + len) = '\0';
  211. slen = len;
  212. len = MIN(1, BUFLEN - 1 - slen);
  213. *((char *) memcpy(buf + slen, "/", len) + len) = '\0';
  214. slen += len;
  215. len = MIN(strlen(filename), BUFLEN - 1 - slen);
  216. *((char *) memcpy(buf + slen, filename, len) + len) = '\0';
  217. ok = unlink(buf);
  218. }
  219. if ( ok == -1 ) {
  220. debug(cerr, pathname);
  221. debug(cerr, filename);
  222. MESSAGE(cerr, form("unlink %s/%s failed", pathname, filename));
  223. throw(systemException(errno));
  224. }
  225. return 0;
  226. }
  227. Boolean copy_file(const char* source, const char* sink)
  228. {
  229. fstream in(source, ios::in);
  230. fstream out(sink, ios::out);
  231. if ( !in || ! out )
  232. return false;
  233. int c;
  234. while ( (c=in.get()) != EOF ) {
  235. out.put((unsigned char)c);
  236. }
  237. in.close();
  238. if ( out.fail() )
  239. return false;
  240. else
  241. return true;
  242. }
  243. Boolean
  244. copy_file(const char* path, const char* file,
  245. const char* source_ext, const char* target_ext)
  246. {
  247. char source[PATHSIZ];
  248. char target[PATHSIZ];
  249. snprintf(source, sizeof(source), "%s/%s.%s", path, file, source_ext);
  250. snprintf(target, sizeof(target), "%s/%s.%s", path, file, target_ext);
  251. return copy_file(source, target) ;
  252. }
  253. Boolean exist_file(const char* filename, const char* pathname)
  254. {
  255. int ok;
  256. struct stat stat_info;
  257. if ( pathname )
  258. ok = stat(form("%s/%s", pathname, filename), &stat_info);
  259. else
  260. ok = stat( filename, &stat_info );
  261. if ( ok == 0 )
  262. return S_ISREG(stat_info.st_mode) ? true : false ;
  263. switch (errno) {
  264. case ENOENT:
  265. return false;
  266. default:
  267. MESSAGE(cerr, "exist_file(): stat() failed. an exception");
  268. throw(systemException(errno));
  269. }
  270. }
  271. int check_file(istream& in, const char* msg)
  272. {
  273. char c;
  274. in.get(c);
  275. in.putback(c);
  276. cerr << c << " " << (int)c << " <---" << msg << "\n";
  277. return 0;
  278. }
  279. Boolean
  280. cat_file(const char* source1, const char* source2, const char* target)
  281. {
  282. /*
  283. MESSAGE(cerr, "in cat_file");
  284. debug(cerr, source1);
  285. debug(cerr, source2);
  286. debug(cerr, target);
  287. */
  288. fstream in1(source1, ios::in);
  289. fstream out(target, ios::out);
  290. if ( !in1 || ! out )
  291. return false;
  292. char buf[BUFSIZ];
  293. while ( in1.getline(buf, BUFSIZ) ) {
  294. out << buf;
  295. if ( in1.gcount() < BUFSIZ - 1 )
  296. out << '\n';
  297. }
  298. in1.close();
  299. fstream in2(source2, ios::in);
  300. if ( !in2 )
  301. return false;
  302. while ( in2.getline(buf, BUFSIZ) ) {
  303. out << buf;
  304. if ( in2.gcount() < BUFSIZ - 1 )
  305. out << '\n';
  306. }
  307. in2.close();
  308. out.close();
  309. return ( out.fail() ) ? false : true;
  310. }
  311. Boolean exist_dir(const char* pathname)
  312. {
  313. struct stat stat_info;
  314. if ( stat( pathname, &stat_info ) == 0 )
  315. return S_ISDIR(stat_info.st_mode) ? true : false ;
  316. switch ( errno ) {
  317. case ENOENT:
  318. return false;
  319. default:
  320. MESSAGE(cerr, "exist_dir() failed");
  321. debug(cerr, pathname);
  322. throw(systemException(errno));
  323. }
  324. }
  325. Boolean check_and_create_dir(const char* path)
  326. {
  327. if ( exist_dir(path) == true )
  328. return true;
  329. const char* path_tail = path + 1; // skip the first '/'
  330. char* slash_ptr;
  331. // create the subdirecties
  332. while ( path_tail[0] != 0 &&
  333. ( slash_ptr = (char *)strchr(path_tail, '/') ) ) {
  334. path_tail = slash_ptr + 1; // set for the next check
  335. slash_ptr[0] = 0; // temp. set the slash to 0.
  336. //debug(cerr, path);
  337. if ( exist_dir(path) == false ) {
  338. if ( mkdir(path, 0777) != 0 ) {
  339. debug(cerr, path);
  340. slash_ptr[0] = '/'; //reset to '/'
  341. perror(0);
  342. MESSAGE(cerr, form( "mkdir failed on path %s", path));
  343. throw(systemException(errno));
  344. }
  345. }
  346. slash_ptr[0] = '/'; //reset to '/'
  347. }
  348. // create the full path
  349. if ( mkdir(path, 0777) != 0 ) {
  350. cerr << "mkdir failed on path " << path << "\n";
  351. throw(systemException(errno));
  352. }
  353. return true;
  354. }
  355. static
  356. int open_prot(int min, int def)
  357. {
  358. int prot;
  359. umask(prot = umask(0));
  360. prot = min | (def & ~(prot & 0777));
  361. return prot;
  362. }
  363. int open_file_prot()
  364. {
  365. return open_prot(0600,0666);
  366. }
  367. int open_dir_prot()
  368. {
  369. return open_prot(0700,0777);
  370. }
  371. Boolean int_eq(void* x, void* y)
  372. {
  373. if ( *(int*)x == *(int*)y )
  374. return true;
  375. else
  376. return false;
  377. }
  378. Boolean int_ls(void* x, void* y)
  379. {
  380. if ( *(int*)x < *(int*)y )
  381. return true;
  382. else
  383. return false;
  384. }
  385. int ll4(int x)
  386. {
  387. int u = sizeof(void*);
  388. int delta = x % u ;
  389. return ( delta == 0 ) ? x : x + u - delta;
  390. }
  391. //Boolean fcntl_lock( int fd, lock_t lt )
  392. //{
  393. // flock flock_record;
  394. //
  395. // switch ( lt ) {
  396. // case SHARED:
  397. // flock_record.l_type = F_RDLCK;
  398. // break;
  399. // case EXCLUSIVE:
  400. // flock_record.l_type = F_WRLCK;
  401. // break;
  402. // default:
  403. // perror("fcntl_lock(): unknown lock type");
  404. // exit(-2);
  405. // }
  406. //
  407. ///****************************/
  408. //// the entire file is locked
  409. ///****************************/
  410. // flock_record.l_whence = SEEK_SET,
  411. // flock_record.l_start = 0;
  412. // flock_record.l_len = 0;
  413. //
  414. // if ( fcntl(fd, F_SETLKW, (int)&flock_record) != -1 ) {
  415. // return true;
  416. // } else {
  417. // return false;
  418. // }
  419. //}
  420. //
  421. //Boolean fcntl_unlock( int fd )
  422. //{
  423. // flock flock_record;
  424. //
  425. ///****************************/
  426. //// the entire file is unlocked
  427. ///****************************/
  428. // flock_record.l_type = F_UNLCK;
  429. // flock_record.l_whence = SEEK_SET;
  430. // flock_record.l_start = 0;
  431. // flock_record.l_len = 0;
  432. //
  433. // if ( fcntl(fd, F_SETLKW, (int)&flock_record) != -1 ) {
  434. // return true;
  435. // } else
  436. // return false;
  437. //}
  438. //
  439. //static Boolean time_out;
  440. //
  441. //Boolean timed_lock(int fd, lock_t lt, int seconds)
  442. //{
  443. // signal(SIGALRM, (SIG_PF)onalarm);
  444. // alarm(seconds);
  445. // time_out = false;
  446. //
  447. // while ( fcntl_lock(fd, lt) == false ) {
  448. // switch ( errno ) {
  449. // case EINTR:
  450. //
  451. // if ( time_out == true ) {
  452. //#ifdef DEBUG
  453. // MESSAGE(cerr, "time out after");
  454. // debug(cerr, seconds);
  455. //#endif
  456. // return false;
  457. // }
  458. //
  459. // break;
  460. //
  461. // default:
  462. //#ifdef DEBUG
  463. // MESSAGE(cerr, "error in fcntl_lock()");
  464. // perror(0);
  465. // debug(cerr, fd);
  466. //#endif
  467. // return false;
  468. // }
  469. // }
  470. // signal(SIGALRM, SIG_IGN);
  471. // return true;
  472. //}
  473. //
  474. //Boolean timed_unlock(int fd, int seconds)
  475. //{
  476. // signal(SIGALRM, (SIG_PF)onalarm);
  477. // alarm(seconds);
  478. // time_out = false;
  479. //
  480. // while ( fcntl_unlock(fd) == false ) {
  481. // switch ( errno ) {
  482. // case EINTR:
  483. //
  484. // if ( time_out == true )
  485. // return false;
  486. //
  487. // break;
  488. //
  489. // default:
  490. // return false;
  491. // }
  492. // }
  493. // signal(SIGALRM, SIG_IGN);
  494. // return true;
  495. //}
  496. //
  497. //void onalarm(int)
  498. //{
  499. // time_out = true;
  500. //}
  501. static
  502. char* time_stamp(_Xctimeparams *ctime_buf)
  503. {
  504. time_t x;
  505. time(&x);
  506. return _XCtime(&x, *ctime_buf);
  507. }
  508. #ifdef C_API
  509. int bytes(fstream& fs)
  510. {
  511. struct stat file_info;
  512. if ( fstat( fs.rdbuf() -> fd(), &file_info) != 0 )
  513. return 0;
  514. else
  515. return int(file_info.st_size);
  516. }
  517. int bytes(int fd)
  518. {
  519. struct stat file_info;
  520. if ( fstat( fd, &file_info) != 0 )
  521. return 0;
  522. else
  523. return int(file_info.st_size);
  524. }
  525. #else
  526. int bytes(fstream* fs)
  527. {
  528. streampos begin, current, end;
  529. int total_bytes;
  530. current = fs->tellg();
  531. fs->seekg(ios::beg);
  532. begin = fs->tellg();
  533. fs->seekg(ios::end);
  534. end = fs->tellg();
  535. fs->seekg(current);
  536. total_bytes = end - begin;
  537. return int(total_bytes);
  538. }
  539. #endif
  540. int bytes(char * file_name)
  541. {
  542. struct stat file_info;
  543. if ( stat( file_name, &file_info) != 0 )
  544. return 0;
  545. else
  546. return int(file_info.st_size);
  547. }
  548. char* form(const char* fmt, ...)
  549. {
  550. static char formbuf[BUFSIZ];
  551. char tempbuf[BUFSIZ];
  552. va_list args;
  553. int len;
  554. va_start(args, fmt);
  555. len = MIN(strlen(formbuf), BUFSIZ - 1);
  556. *((char *) memcpy(tempbuf, formbuf, len) + len) = '\0';
  557. (void) vsnprintf(tempbuf, sizeof(tempbuf), fmt, args);
  558. va_end(args);
  559. len = MIN(strlen(tempbuf), BUFSIZ - 1);
  560. *((char *) memcpy(formbuf, tempbuf, len) + len) = '\0';
  561. return formbuf;
  562. }
  563. static char info_buf[BUFSIZ];
  564. char* access_info( char* request )
  565. {
  566. #ifndef SVR4
  567. char dm_name[PATHSIZ];
  568. int dm_name_sz = PATHSIZ;
  569. if ( getdomainname(dm_name, dm_name_sz) == -1 ) {
  570. MESSAGE(cerr, "getdomainname() failed");
  571. throw(systemException(errno));
  572. }
  573. #endif
  574. #ifdef SVR4
  575. struct utsname name ;
  576. uname(&name);
  577. #else
  578. char host_name[PATHSIZ];
  579. int host_name_sz = PATHSIZ;
  580. if ( gethostname(host_name, host_name_sz) == -1 ) {
  581. MESSAGE(cerr, "gethostname() failed");
  582. throw(systemException(errno));
  583. }
  584. #endif
  585. _Xctimeparams ctime_buf;
  586. char* x = time_stamp(&ctime_buf);
  587. x[strlen(x)-1] = 0;
  588. #ifndef SVR4
  589. snprintf(info_buf, sizeof(info_buf), "%s-%s-%ld-%s-%s",
  590. host_name, dm_name,
  591. /* getenv("USER"), */
  592. (long)getpid(), x, request
  593. );
  594. #else
  595. char userid[L_cuserid];
  596. snprintf(info_buf, sizeof(info_buf), "%s-%s-%ld-%s-%s",
  597. name.nodename,
  598. ( cuserid(userid)[0] == 0 ) ? "???" : userid,
  599. /* getenv("USER"), */
  600. (long)getpid(), x, request
  601. );
  602. #endif
  603. return info_buf;
  604. }
  605. void lsb_putbits(unsigned& target, unsigned position_from_lsb,
  606. unsigned bits, unsigned source)
  607. {
  608. target |= ((source & ~( ~0 << bits )) << position_from_lsb) ;
  609. }
  610. unsigned lsb_getbits(unsigned source, unsigned position_from_lsb, unsigned bits)
  611. {
  612. return ( ( source >> position_from_lsb ) & ~( ~0 << bits ) );
  613. }
  614. Boolean cc_is_digit(istream& in)
  615. {
  616. int c = in.get();
  617. int ok = isdigit(c);
  618. in.putback(c);
  619. return ( ok ) ? true : false;
  620. }
  621. unsigned long disk_space(const char* path)
  622. {
  623. #if defined (SVR4) || defined(CSRG_BASED)
  624. struct statvfs statfs_buf;
  625. #else
  626. struct statfs statfs_buf;
  627. #endif
  628. long free_bytes;
  629. #if defined (SVR4) || defined(CSRG_BASED)
  630. if ( statvfs(path, &statfs_buf) == 0 ) {
  631. free_bytes = statfs_buf.f_bavail * statfs_buf.f_frsize ;
  632. #else
  633. if ( statfs(path, &statfs_buf) == 0 ) {
  634. free_bytes = statfs_buf.f_bavail * statfs_buf.f_bsize ;
  635. #endif
  636. } else {
  637. throw(stringException(form("statfs failed on %s", path)));
  638. }
  639. return free_bytes;
  640. }
  641. Boolean writeToTmpFile(char* unique_nm, char* str, int size)
  642. {
  643. Boolean ok = false;
  644. fstream *out = 0;
  645. char* tmp_dir_tbl[4];
  646. int len;
  647. tmp_dir_tbl[0] = getenv("TMPDIR");
  648. tmp_dir_tbl[1] = (char*)"/tmp";
  649. tmp_dir_tbl[2] = (char*)"/usr/tmp";
  650. tmp_dir_tbl[3] = getenv("HOME");
  651. int tmp_dir_tbl_size = 4;
  652. const char* uid = unique_id();
  653. for ( int i=0; i<tmp_dir_tbl_size; i++ ) {
  654. if ( tmp_dir_tbl[i] == 0 )
  655. continue;
  656. len = MIN(strlen(tmp_dir_tbl[i]) + strlen(uid) + 5, PATHSIZ - 1);
  657. *((char *) memcpy(unique_nm,
  658. form("%s/tmp.%s", tmp_dir_tbl[i], uid),
  659. len) + len) = '\0';
  660. mtry {
  661. //debug(cerr, tmp_dir_tbl[i]);
  662. //debug(cerr, disk_space(tmp_dir_tbl[i]));
  663. if ( disk_space(tmp_dir_tbl[i]) <= (unsigned long) size )
  664. continue;
  665. out = new fstream(unique_nm, ios::out);
  666. if ( !(*out) ) {
  667. delete out;
  668. continue;
  669. }
  670. if ( ! (out->write(str, size) ) ) {
  671. out -> close();
  672. delete out;
  673. del_file(unique_nm);
  674. continue;
  675. } else {
  676. ok = true;
  677. out -> close();
  678. delete out;
  679. break;
  680. }
  681. }
  682. mcatch_any()
  683. {
  684. continue;
  685. }
  686. end_try;
  687. }
  688. return ok;
  689. }