POSIX.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Description
25  POSIX versions of the functions declared in OSspecific.H
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #ifdef solarisGcc
30  #define _SYS_VNODE_H
31 #endif
32 
33 #include "OSspecific.H"
34 #include "POSIX.H"
35 #include "foamVersion.H"
36 #include "fileName.H"
37 #include "fileStat.H"
38 #include "timer.H"
39 #include "IFstream.H"
40 #include "DynamicList.H"
41 
42 #include <fstream>
43 #include <cstdlib>
44 #include <cctype>
45 
46 #include <stdio.h>
47 #include <unistd.h>
48 #include <dirent.h>
49 #include <pwd.h>
50 #include <errno.h>
51 #include <sys/types.h>
52 #include <sys/stat.h>
53 #include <sys/socket.h>
54 #include <netdb.h>
55 #include <dlfcn.h>
56 #include <link.h>
57 
58 #include <netinet/in.h>
59 
60 #ifdef USE_RANDOM
61  #include <climits>
62  #if INT_MAX != 2147483647
63  #error "INT_MAX != 2147483647"
64  #error "The random number generator may not work!"
65  #endif
66 #endif
67 
68 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
69 
70 namespace Foam
71 {
72  defineTypeNameAndDebug(POSIX, 0);
73 }
74 
75 
76 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
77 
78 pid_t Foam::pid()
79 {
80  return ::getpid();
81 }
82 
83 
84 pid_t Foam::ppid()
85 {
86  return ::getppid();
87 }
88 
89 
90 pid_t Foam::pgid()
91 {
92  return ::getpgrp();
93 }
94 
95 
96 bool Foam::env(const word& envName)
97 {
98  return ::getenv(envName.c_str()) != NULL;
99 }
100 
101 
103 {
104  char* env = ::getenv(envName.c_str());
105 
106  if (env)
107  {
108  return string(env);
109  }
110  else
111  {
112  // Return null-constructed string rather than string::null
113  // to avoid cyclic dependencies in the construction of globals
114  return string();
115  }
116 }
117 
118 
119 bool Foam::setEnv
120 (
121  const word& envName,
122  const std::string& value,
123  const bool overwrite
124 )
125 {
126  return setenv(envName.c_str(), value.c_str(), overwrite) == 0;
127 }
128 
129 
131 {
132  char buf[128];
133  ::gethostname(buf, sizeof(buf));
134 
135  // implementation as per hostname from net-tools
136  if (full)
137  {
138  struct hostent *hp = ::gethostbyname(buf);
139  if (hp)
140  {
141  return hp->h_name;
142  }
143  }
144 
145  return buf;
146 }
147 
148 
150 {
151  char buf[128];
152  ::gethostname(buf, sizeof(buf));
153 
154  // implementation as per hostname from net-tools
155  struct hostent *hp = ::gethostbyname(buf);
156  if (hp)
157  {
158  char *p = ::strchr(hp->h_name, '.');
159  if (p)
160  {
161  ++p;
162  return p;
163  }
164  }
165 
166  return string::null;
167 }
168 
169 
171 {
172  struct passwd* pw = ::getpwuid(::getuid());
173 
174  if (pw != NULL)
175  {
176  return pw->pw_name;
177  }
178  else
179  {
180  return string::null;
181  }
182 }
183 
184 
186 {
187  return (::geteuid() == 0);
188 }
189 
190 
192 {
193  char* env = ::getenv("HOME");
194 
195  if (env != NULL)
196  {
197  return fileName(env);
198  }
199  else
200  {
201  struct passwd* pw = ::getpwuid(getuid());
202 
203  if (pw != NULL)
204  {
205  return pw->pw_dir;
206  }
207  else
208  {
209  return fileName::null;
210  }
211  }
212 }
213 
214 
216 {
217  struct passwd* pw;
218 
219  if (userName.size())
220  {
221  pw = ::getpwnam(userName.c_str());
222  }
223  else
224  {
225  char* env = ::getenv("HOME");
226 
227  if (env != NULL)
228  {
229  return fileName(env);
230  }
231 
232  pw = ::getpwuid(::getuid());
233  }
234 
235  if (pw != NULL)
236  {
237  return pw->pw_dir;
238  }
239  else
240  {
241  return fileName::null;
242  }
243 }
244 
245 
247 {
248  label pathLengthLimit = POSIX::pathLengthChunk;
249  List<char> path(pathLengthLimit);
250 
251  // Resize path if getcwd fails with an ERANGE error
252  while(pathLengthLimit == path.size())
253  {
254  if (::getcwd(path.data(), path.size()))
255  {
256  return path.data();
257  }
258  else if(errno == ERANGE)
259  {
260  // Increment path length upto the pathLengthMax limit
261  if
262  (
263  (pathLengthLimit += POSIX::pathLengthChunk)
265  )
266  {
268  << "Attempt to increase path length beyond limit of "
270  << exit(FatalError);
271  }
272 
273  path.setSize(pathLengthLimit);
274  }
275  else
276  {
277  break;
278  }
279  }
280 
282  << "Couldn't get the current working directory"
283  << exit(FatalError);
284 
285  return fileName::null;
286 }
287 
288 
289 bool Foam::chDir(const fileName& dir)
290 {
291  return ::chdir(dir.c_str()) == 0;
292 }
293 
294 
295 bool Foam::mkDir(const fileName& pathName, mode_t mode)
296 {
297  // empty names are meaningless
298  if (pathName.empty())
299  {
300  return false;
301  }
302 
303  // Construct instance path directory if does not exist
304  if (::mkdir(pathName.c_str(), mode) == 0)
305  {
306  // Directory made OK so return true
307  return true;
308  }
309  else
310  {
311  switch (errno)
312  {
313  case EPERM:
314  {
316  << "The filesystem containing " << pathName
317  << " does not support the creation of directories."
318  << exit(FatalError);
319 
320  return false;
321  }
322 
323  case EEXIST:
324  {
325  // Directory already exists so simply return true
326  return true;
327  }
328 
329  case EFAULT:
330  {
332  << "" << pathName
333  << " points outside your accessible address space."
334  << exit(FatalError);
335 
336  return false;
337  }
338 
339  case EACCES:
340  {
342  << "The parent directory does not allow write "
343  "permission to the process,"<< nl
344  << "or one of the directories in " << pathName
345  << " did not allow search (execute) permission."
346  << exit(FatalError);
347 
348  return false;
349  }
350 
351  case ENAMETOOLONG:
352  {
354  << "" << pathName << " is too long."
355  << exit(FatalError);
356 
357  return false;
358  }
359 
360  case ENOENT:
361  {
362  // Part of the path does not exist so try to create it
363  if (pathName.path().size() && mkDir(pathName.path(), mode))
364  {
365  return mkDir(pathName, mode);
366  }
367  else
368  {
370  << "Couldn't create directory " << pathName
371  << exit(FatalError);
372 
373  return false;
374  }
375  }
376 
377  case ENOTDIR:
378  {
380  << "A component used as a directory in " << pathName
381  << " is not, in fact, a directory."
382  << exit(FatalError);
383 
384  return false;
385  }
386 
387  case ENOMEM:
388  {
390  << "Insufficient kernel memory was available to make "
391  "directory " << pathName << '.'
392  << exit(FatalError);
393 
394  return false;
395  }
396 
397  case EROFS:
398  {
400  << "" << pathName
401  << " refers to a file on a read-only filesystem."
402  << exit(FatalError);
403 
404  return false;
405  }
406 
407  case ELOOP:
408  {
410  << "Too many symbolic links were encountered in resolving "
411  << pathName << '.'
412  << exit(FatalError);
413 
414  return false;
415  }
416 
417  case ENOSPC:
418  {
420  << "The device containing " << pathName
421  << " has no room for the new directory or "
422  << "the user's disk quota is exhausted."
423  << exit(FatalError);
424 
425  return false;
426  }
427 
428  default:
429  {
431  << "Couldn't create directory " << pathName
432  << exit(FatalError);
433 
434  return false;
435  }
436  }
437  }
438 }
439 
440 
441 bool Foam::chMod(const fileName& name, const mode_t m)
442 {
443  return ::chmod(name.c_str(), m) == 0;
444 }
445 
446 
447 mode_t Foam::mode(const fileName& name)
448 {
449  fileStat fileStatus(name);
450  if (fileStatus.isValid())
451  {
452  return fileStatus.status().st_mode;
453  }
454  else
455  {
456  return 0;
457  }
458 }
459 
460 
462 {
463  mode_t m = mode(name);
464 
465  if (S_ISREG(m))
466  {
467  return fileName::FILE;
468  }
469  else if (S_ISDIR(m))
470  {
471  return fileName::DIRECTORY;
472  }
473  else
474  {
475  return fileName::UNDEFINED;
476  }
477 }
478 
479 
480 bool Foam::exists(const fileName& name, const bool checkGzip)
481 {
482  return mode(name) || isFile(name, checkGzip);
483 }
484 
485 
487 {
488  return S_ISDIR(mode(name));
489 }
490 
491 
492 bool Foam::isFile(const fileName& name, const bool checkGzip)
493 {
494  return S_ISREG(mode(name)) || (checkGzip && S_ISREG(mode(name + ".gz")));
495 }
496 
497 
499 {
500  fileStat fileStatus(name);
501  if (fileStatus.isValid())
502  {
503  return fileStatus.status().st_size;
504  }
505  else
506  {
507  return -1;
508  }
509 }
510 
511 
513 {
514  fileStat fileStatus(name);
515  if (fileStatus.isValid())
516  {
517  return fileStatus.status().st_mtime;
518  }
519  else
520  {
521  return 0;
522  }
523 }
524 
525 
527 (
528  const fileName& directory,
529  const fileName::Type type,
530  const bool filtergz
531 )
532 {
533  // Initial filename list size
534  // also used as increment if initial size found to be insufficient
535  static const int maxNnames = 100;
536 
537  if (POSIX::debug)
538  {
540  << "reading directory " << directory << endl;
541  }
542 
543  // Setup empty string list MAXTVALUES long
544  fileNameList dirEntries(maxNnames);
545 
546  // Pointers to the directory entries
547  DIR *source;
548  struct dirent *list;
549 
550  // Temporary variables and counters
551  label nEntries = 0;
552 
553  // Attempt to open directory and set the structure pointer
554  if ((source = ::opendir(directory.c_str())) == NULL)
555  {
556  dirEntries.setSize(0);
557 
558  if (POSIX::debug)
559  {
561  << "cannot open directory " << directory << endl;
562  }
563  }
564  else
565  {
566  // Read and parse all the entries in the directory
567  while ((list = ::readdir(source)) != NULL)
568  {
569  fileName fName(list->d_name);
570 
571  // ignore files begining with ., i.e. '.', '..' and '.*'
572  if (fName.size() && fName[0] != '.')
573  {
574  word fExt = fName.ext();
575 
576  if
577  (
578  (type == fileName::DIRECTORY)
579  ||
580  (
581  type == fileName::FILE
582  && fName[fName.size()-1] != '~'
583  && fExt != "bak"
584  && fExt != "BAK"
585  && fExt != "old"
586  && fExt != "save"
587  )
588  )
589  {
590  if ((directory/fName).type() == type)
591  {
592  if (nEntries >= dirEntries.size())
593  {
594  dirEntries.setSize(dirEntries.size() + maxNnames);
595  }
596 
597  if (filtergz && fExt == "gz")
598  {
599  dirEntries[nEntries++] = fName.lessExt();
600  }
601  else
602  {
603  dirEntries[nEntries++] = fName;
604  }
605  }
606  }
607  }
608  }
609 
610  // Reset the length of the entries list
611  dirEntries.setSize(nEntries);
612 
613  ::closedir(source);
614  }
615 
616  return dirEntries;
617 }
618 
619 
620 bool Foam::cp(const fileName& src, const fileName& dest)
621 {
622  // Make sure source exists.
623  if (!exists(src))
624  {
625  return false;
626  }
627 
628  fileName destFile(dest);
629 
630  // Check type of source file.
631  if (src.type() == fileName::FILE)
632  {
633  // If dest is a directory, create the destination file name.
634  if (destFile.type() == fileName::DIRECTORY)
635  {
636  destFile = destFile/src.name();
637  }
638 
639  // Make sure the destination directory exists.
640  if (!isDir(destFile.path()) && !mkDir(destFile.path()))
641  {
642  return false;
643  }
644 
645  // Open and check streams.
646  std::ifstream srcStream(src.c_str());
647  if (!srcStream)
648  {
649  return false;
650  }
651 
652  std::ofstream destStream(destFile.c_str());
653  if (!destStream)
654  {
655  return false;
656  }
657 
658  // Copy character data.
659  char ch;
660  while (srcStream.get(ch))
661  {
662  destStream.put(ch);
663  }
664 
665  // Final check.
666  if (!srcStream.eof() || !destStream)
667  {
668  return false;
669  }
670  }
671  else if (src.type() == fileName::DIRECTORY)
672  {
673  // If dest is a directory, create the destination file name.
674  if (destFile.type() == fileName::DIRECTORY)
675  {
676  destFile = destFile/src.component(src.components().size() -1);
677  }
678 
679  // Make sure the destination directory exists.
680  if (!isDir(destFile) && !mkDir(destFile))
681  {
682  return false;
683  }
684 
685  // Copy files
686  fileNameList contents = readDir(src, fileName::FILE, false);
687  forAll(contents, i)
688  {
689  if (POSIX::debug)
690  {
692  << "Copying : " << src/contents[i]
693  << " to " << destFile/contents[i] << endl;
694  }
695 
696  // File to file.
697  cp(src/contents[i], destFile/contents[i]);
698  }
699 
700  // Copy sub directories.
701  fileNameList subdirs = readDir(src, fileName::DIRECTORY);
702  forAll(subdirs, i)
703  {
704  if (POSIX::debug)
705  {
707  << "Copying : " << src/subdirs[i]
708  << " to " << destFile << endl;
709  }
710 
711  // Dir to Dir.
712  cp(src/subdirs[i], destFile);
713  }
714  }
715 
716  return true;
717 }
718 
719 
720 bool Foam::ln(const fileName& src, const fileName& dst)
721 {
722  if (POSIX::debug)
723  {
725  << "Create softlink from : " << src << " to " << dst
726  << endl;
727  }
728 
729  if (exists(dst))
730  {
732  << "destination " << dst << " already exists. Not linking."
733  << endl;
734  return false;
735  }
736 
737  if (src.isAbsolute() && !exists(src))
738  {
740  << "source " << src << " does not exist." << endl;
741  return false;
742  }
743 
744  if (::symlink(src.c_str(), dst.c_str()) == 0)
745  {
746  return true;
747  }
748  else
749  {
751  << "symlink from " << src << " to " << dst << " failed." << endl;
752  return false;
753  }
754 }
755 
756 
757 bool Foam::mv(const fileName& src, const fileName& dst)
758 {
759  if (POSIX::debug)
760  {
762  << "Move : " << src << " to " << dst << endl;
763  }
764 
765  if
766  (
767  dst.type() == fileName::DIRECTORY
768  && src.type() != fileName::DIRECTORY
769  )
770  {
771  const fileName dstName(dst/src.name());
772 
773  return ::rename(src.c_str(), dstName.c_str()) == 0;
774  }
775  else
776  {
777  return ::rename(src.c_str(), dst.c_str()) == 0;
778  }
779 }
780 
781 
782 bool Foam::mvBak(const fileName& src, const std::string& ext)
783 {
784  if (POSIX::debug)
785  {
787  << "mvBak : " << src << " to extension " << ext << endl;
788  }
789 
790  if (exists(src, false))
791  {
792  const int maxIndex = 99;
793  char index[3];
794 
795  for (int n = 0; n <= maxIndex; n++)
796  {
797  fileName dstName(src + "." + ext);
798  if (n)
799  {
800  sprintf(index, "%02d", n);
801  dstName += index;
802  }
803 
804  // avoid overwriting existing files, except for the last
805  // possible index where we have no choice
806  if (!exists(dstName, false) || n == maxIndex)
807  {
808  return ::rename(src.c_str(), dstName.c_str()) == 0;
809  }
810 
811  }
812  }
813 
814  // fall-through: nothing to do
815  return false;
816 }
817 
818 
819 bool Foam::rm(const fileName& file)
820 {
821  if (POSIX::debug)
822  {
824  << "Removing : " << file << endl;
825  }
826 
827  // Try returning plain file name; if not there, try with .gz
828  if (remove(file.c_str()) == 0)
829  {
830  return true;
831  }
832  else
833  {
834  return ::remove(string(file + ".gz").c_str()) == 0;
835  }
836 }
837 
838 
839 bool Foam::rmDir(const fileName& directory)
840 {
841  if (POSIX::debug)
842  {
844  << "removing directory " << directory << endl;
845  }
846 
847  // Pointers to the directory entries
848  DIR *source;
849  struct dirent *list;
850 
851  // Attempt to open directory and set the structure pointer
852  if ((source = ::opendir(directory.c_str())) == NULL)
853  {
855  << "cannot open directory " << directory << endl;
856 
857  return false;
858  }
859  else
860  {
861  // Read and parse all the entries in the directory
862  while ((list = ::readdir(source)) != NULL)
863  {
864  fileName fName(list->d_name);
865 
866  if (fName != "." && fName != "..")
867  {
868  fileName path = directory/fName;
869 
870  if (path.type() == fileName::DIRECTORY)
871  {
872  if (!rmDir(path))
873  {
875  << "failed to remove directory " << fName
876  << " while removing directory " << directory
877  << endl;
878 
879  ::closedir(source);
880 
881  return false;
882  }
883  }
884  else
885  {
886  if (!rm(path))
887  {
889  << "failed to remove file " << fName
890  << " while removing directory " << directory
891  << endl;
892 
893  ::closedir(source);
894 
895  return false;
896  }
897  }
898  }
899 
900  }
901 
902  if (!rm(directory))
903  {
905  << "failed to remove directory " << directory << endl;
906 
907  ::closedir(source);
908 
909  return false;
910  }
911 
912  ::closedir(source);
913 
914  return true;
915  }
916 }
917 
918 
919 unsigned int Foam::sleep(const unsigned int s)
920 {
921  return ::sleep(s);
922 }
923 
924 
925 void Foam::fdClose(const int fd)
926 {
927  if (close(fd) != 0)
928  {
930  << "close error on " << fd << endl
931  << abort(FatalError);
932  }
933 }
934 
935 
936 bool Foam::ping
937 (
938  const string& destName,
939  const label destPort,
940  const label timeOut
941 )
942 {
943  struct hostent *hostPtr;
944  volatile int sockfd;
945  struct sockaddr_in destAddr; // will hold the destination addr
946  u_int addr;
947 
948  if ((hostPtr = ::gethostbyname(destName.c_str())) == NULL)
949  {
951  << "gethostbyname error " << h_errno << " for host " << destName
952  << abort(FatalError);
953  }
954 
955  // Get first of the SLL of addresses
956  addr = (reinterpret_cast<struct in_addr*>(*(hostPtr->h_addr_list)))->s_addr;
957 
958  // Allocate socket
959  sockfd = ::socket(AF_INET, SOCK_STREAM, 0);
960  if (sockfd < 0)
961  {
963  << "socket error"
964  << abort(FatalError);
965  }
966 
967  // Fill sockaddr_in structure with dest address and port
968  memset(reinterpret_cast<char *>(&destAddr), '\0', sizeof(destAddr));
969  destAddr.sin_family = AF_INET;
970  destAddr.sin_port = htons(ushort(destPort));
971  destAddr.sin_addr.s_addr = addr;
972 
973 
974  timer myTimer(timeOut);
975 
976  if (timedOut(myTimer))
977  {
978  // Setjmp from timer jumps back to here
979  fdClose(sockfd);
980  return false;
981  }
982 
983  if
984  (
985  ::connect
986  (
987  sockfd,
988  reinterpret_cast<struct sockaddr*>(&destAddr),
989  sizeof(struct sockaddr)
990  ) != 0
991  )
992  {
993  // Connection refused. Check if network was actually used or not.
994 
995  int connectErr = errno;
996 
997  fdClose(sockfd);
998 
999  if (connectErr == ECONNREFUSED)
1000  {
1001  return true;
1002  }
1003  //perror("connect");
1004 
1005  return false;
1006  }
1007 
1008  fdClose(sockfd);
1009 
1010  return true;
1011 }
1012 
1013 
1014 bool Foam::ping(const string& hostname, const label timeOut)
1015 {
1016  return ping(hostname, 222, timeOut) || ping(hostname, 22, timeOut);
1017 }
1018 
1019 
1020 int Foam::system(const std::string& command)
1021 {
1022  return ::system(command.c_str());
1023 }
1024 
1025 
1026 void* Foam::dlOpen(const fileName& lib, const bool check)
1027 {
1028  if (POSIX::debug)
1029  {
1030  std::cout<< "dlOpen(const fileName&)"
1031  << " : dlopen of " << lib << std::endl;
1032  }
1033  void* handle = ::dlopen(lib.c_str(), RTLD_LAZY|RTLD_GLOBAL);
1034 
1035  if (!handle && check)
1036  {
1038  << "dlopen error : " << ::dlerror()
1039  << endl;
1040  }
1041 
1042  if (POSIX::debug)
1043  {
1044  std::cout
1045  << "dlOpen(const fileName&)"
1046  << " : dlopen of " << lib
1047  << " handle " << handle << std::endl;
1048  }
1049 
1050  return handle;
1051 }
1052 
1053 
1054 bool Foam::dlClose(void* handle)
1055 {
1056  if (POSIX::debug)
1057  {
1058  std::cout
1059  << "dlClose(void*)"
1060  << " : dlclose of handle " << handle << std::endl;
1061  }
1062  return ::dlclose(handle) == 0;
1063 }
1064 
1065 
1066 void* Foam::dlSym(void* handle, const std::string& symbol)
1067 {
1068  if (POSIX::debug)
1069  {
1070  std::cout
1071  << "dlSym(void*, const std::string&)"
1072  << " : dlsym of " << symbol << std::endl;
1073  }
1074  // clear any old errors - see manpage dlopen
1075  (void) ::dlerror();
1076 
1077  // get address of symbol
1078  void* fun = ::dlsym(handle, symbol.c_str());
1079 
1080  // find error (if any)
1081  char *error = ::dlerror();
1082 
1083  if (error)
1084  {
1086  << "Cannot lookup symbol " << symbol << " : " << error
1087  << endl;
1088  }
1089 
1090  return fun;
1091 }
1092 
1093 
1094 bool Foam::dlSymFound(void* handle, const std::string& symbol)
1095 {
1096  if (handle && !symbol.empty())
1097  {
1098  if (POSIX::debug)
1099  {
1100  std::cout
1101  << "dlSymFound(void*, const std::string&)"
1102  << " : dlsym of " << symbol << std::endl;
1103  }
1104 
1105  // clear any old errors - see manpage dlopen
1106  (void) ::dlerror();
1107 
1108  // get address of symbol
1109  (void) ::dlsym(handle, symbol.c_str());
1110 
1111  // symbol can be found if there was no error
1112  return !::dlerror();
1113  }
1114  else
1115  {
1116  return false;
1117  }
1118 }
1119 
1120 
1121 static int collectLibsCallback
1123  struct dl_phdr_info *info,
1124  size_t size,
1125  void *data
1126 )
1127 {
1129  reinterpret_cast<Foam::DynamicList<Foam::fileName>*>(data);
1130  ptr->append(info->dlpi_name);
1131  return 0;
1132 }
1133 
1134 
1136 {
1137  DynamicList<fileName> libs;
1138  dl_iterate_phdr(collectLibsCallback, &libs);
1139  if (POSIX::debug)
1140  {
1141  std::cout
1142  << "dlLoaded()"
1143  << " : determined loaded libraries :" << libs.size() << std::endl;
1144  }
1145  return libs;
1146 }
1147 
1148 
1149 void Foam::osRandomSeed(const label seed)
1150 {
1151  #ifdef USE_RANDOM
1152  srandom((unsigned int)seed);
1153  #else
1154  srand48(seed);
1155  #endif
1156 }
1157 
1158 
1160 {
1161  #ifdef USE_RANDOM
1162  return random();
1163  #else
1164  return lrand48();
1165  #endif
1166 }
1167 
1168 
1169 Foam::scalar Foam::osRandomDouble()
1170 {
1171  #ifdef USE_RANDOM
1172  return (scalar)random()/INT_MAX;
1173  #else
1174  return drand48();
1175  #endif
1176 }
1177 
1178 
1179 // ************************************************************************* //
string getEnv(const word &)
Return environment variable of given name.
Definition: POSIX.C:102
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
scalar random
A class for handling file names.
Definition: fileName.H:69
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
const label pathLengthMax
Definition: POSIX.H:53
Type type() const
Return the file type: FILE, DIRECTORY or UNDEFINED.
Definition: fileName.C:51
pid_t ppid()
Return the parent PID of this process.
Definition: POSIX.C:84
Implements a timeout mechanism via sigalarm.
Definition: timer.H:81
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:76
scalar osRandomDouble()
Return random double precision (uniform distribution between 0 and 1)
Definition: POSIX.C:1169
static const fileName null
An empty fileName.
Definition: fileName.H:97
bool isFile(const fileName &, const bool checkGzip=true)
Does the name exist as a FILE in the file system?
Definition: POSIX.C:492
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
bool chDir(const fileName &dir)
Change the current directory to the one given and return true,.
Definition: POSIX.C:289
bool ping(const string &, const label port, const label timeOut)
Check if machine is up by pinging given port.
Definition: POSIX.C:937
bool cp(const fileName &src, const fileName &dst)
Copy, recursively if necessary, the source to the destination.
Definition: POSIX.C:620
fileNameList dlLoaded()
Return all loaded libraries.
Definition: POSIX.C:1135
bool isDir(const fileName &)
Does the name exist as a DIRECTORY in the file system?
Definition: POSIX.C:486
const label pathLengthChunk
Definition: POSIX.H:52
fileName home()
Return home directory path name for the current user.
Definition: POSIX.C:191
static int collectLibsCallback(struct dl_phdr_info *info, size_t size, void *data)
Definition: POSIX.C:1122
bool mvBak(const fileName &, const std::string &ext="bak")
Rename to a corresponding backup file.
Definition: POSIX.C:782
bool mv(const fileName &src, const fileName &dst)
Rename src to dst.
Definition: POSIX.C:757
Class to handle errors and exceptions in a simple, consistent stream-based manner.
Definition: error.H:66
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
time_t lastModified(const fileName &)
Return time of last file modification.
Definition: POSIX.C:512
word name() const
Return file name (part beyond last /)
Definition: fileName.C:179
gmvFile<< "tracers "<< particles.size()<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){gmvFile<< iter().position().x()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){gmvFile<< iter().position().y()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){gmvFile<< iter().position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
string hostName(const bool full=false)
Return the system&#39;s host name, as per hostname(1)
Definition: POSIX.C:130
bool dlClose(void *)
Close a dlopened library using handle. Return true if successful.
Definition: POSIX.C:1054
A class for handling words, derived from string.
Definition: word.H:59
string domainName()
Return the system&#39;s domain name, as per hostname(1) with the &#39;-d&#39; option.
Definition: POSIX.C:149
label osRandomInteger()
Return random integer (uniform distribution between 0 and 2^31)
Definition: POSIX.C:1159
wordList components(const char delimiter='/') const
Return path components as wordList.
Definition: fileName.C:298
DynamicList< T, SizeInc, SizeMult, SizeDiv > & append(const T &)
Append an element at the end of the list.
Definition: DynamicListI.H:292
string userName()
Return the user&#39;s login name.
Definition: POSIX.C:170
word component(const size_type, const char delimiter='/') const
Return a single component of the path.
Definition: fileName.C:326
bool isValid() const
Did constructor fail.
Definition: fileStat.H:99
errorManip< error > abort(error &err)
Definition: errorManip.H:131
off_t fileSize(const fileName &)
Return size of file.
Definition: POSIX.C:498
bool ln(const fileName &src, const fileName &dst)
Create a softlink. dst should not exist. Returns true if successful.
Definition: POSIX.C:720
bool isAbsolute() const
Return true if file name is absolute.
Definition: fileName.C:57
static const string null
An empty string.
Definition: string.H:86
bool rmDir(const fileName &)
Remove a dirctory and its contents.
Definition: POSIX.C:839
pid_t pid()
Return the PID of this process.
Definition: POSIX.C:78
static const char nl
Definition: Ostream.H:262
fileName path(UMean.rootPath()/UMean.caseName()/"graphs"/UMean.instance())
defineTypeNameAndDebug(combustionModel, 0)
Wrapper for stat() system call.
Definition: fileStat.H:65
fileName lessExt() const
Return file name without extension (part before last .)
Definition: fileName.C:268
#define timedOut(x)
Check it a timeout has occured.
Definition: timer.H:71
bool mkDir(const fileName &, mode_t=0777)
Make a directory and return an error if it could not be created.
Definition: POSIX.C:295
pid_t pgid()
Return the group PID of this process.
Definition: POSIX.C:90
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
bool isAdministrator()
Is user administrator.
Definition: POSIX.C:185
void setSize(const label)
Reset size of List.
Definition: List.C:295
T * data()
Return a pointer to the first data element,.
Definition: UListI.H:149
bool exists(const fileName &, const bool checkGzip=true)
Does the name exist (as DIRECTORY or FILE) in the file system?
Definition: POSIX.C:480
fileName::Type type(const fileName &)
Return the file type: DIRECTORY or FILE.
Definition: POSIX.C:461
#define WarningInFunction
Report a warning using Foam::Warning.
mode_t mode(const fileName &)
Return the file mode.
Definition: POSIX.C:447
bool chMod(const fileName &, const mode_t)
Set the file mode.
Definition: POSIX.C:441
word ext() const
Return file name extension (part after last .)
Definition: fileName.C:283
fileName cwd()
Return current working directory path name.
Definition: POSIX.C:246
void fdClose(const int)
Close file descriptor.
Definition: POSIX.C:925
label n
bool env(const word &)
Return true if environment variable of given name is defined.
Definition: POSIX.C:96
volScalarField & p
bool rm(const fileName &)
Remove a file, returning true if successful otherwise false.
Definition: POSIX.C:819
fileName path() const
Return directory path name (part before last /)
Definition: fileName.C:249
A class for handling character strings derived from std::string.
Definition: string.H:74
unsigned int sleep(const unsigned int)
Sleep for the specified number of seconds.
Definition: POSIX.C:919
const struct stat & status() const
Raw status.
Definition: fileStat.H:93
void * dlOpen(const fileName &lib, const bool check=true)
Open a shared library. Return handle to library. Print error message.
Definition: POSIX.C:1026
fileNameList readDir(const fileName &, const fileName::Type=fileName::FILE, const bool filtergz=true)
Read a directory and return the entries as a string list.
Definition: POSIX.C:527
void osRandomSeed(const label seed)
Seed random number generator.
Definition: POSIX.C:1149
bool setEnv(const word &name, const std::string &value, const bool overwrite)
Set an environment variable.
Definition: POSIX.C:120
void * dlSym(void *handle, const std::string &symbol)
Lookup a symbol in a dlopened library using handle to library.
Definition: POSIX.C:1066
bool dlSymFound(void *handle, const std::string &symbol)
Report if symbol in a dlopened library could be found.
Definition: POSIX.C:1094
Type
Enumerations to handle file types and modes.
Definition: fileName.H:82
Namespace for OpenFOAM.
int system(const std::string &command)
Execute the specified command.
Definition: POSIX.C:1020
#define InfoInFunction
Report an information message using Foam::Info.