vpl.cxx
Go to the documentation of this file.
1 // This is core/vpl/os_unix/vpl.cxx
2 // Include system headers for UNIX-like operating system :
3 #undef _XOPEN_SOURCE
4 #define _XOPEN_SOURCE 1 // necessary on alpha
5 #undef _XOPEN_SOURCE_EXTENDED
6 #define _XOPEN_SOURCE_EXTENDED 1 // usleep is not declared
7 #include <cstdlib>
8 #include <cstring>
9 extern "C" {
10 #include <unistd.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 }
14 #include <vxl_config.h> // for VXL_UNISTD_*
15 
16 char *
17 vpl_getcwd( char *buf, std::size_t buf_size )
18 {
19  return getcwd( buf, buf_size );
20 }
21 
22 int
23 vpl_mkdir( const char *dir, unsigned short mode )
24 {
25  return mkdir( dir, (mode_t)mode );
26 }
27 
28 int
29 vpl_rmdir( const char *dir )
30 {
31  return rmdir( dir );
32 }
33 
34 int
35 vpl_chdir( const char *dir )
36 {
37  return chdir( dir );
38 }
39 
40 int
41 vpl_unlink( const char *file )
42 {
43  return unlink( file );
44 }
45 
46 unsigned int
47 vpl_sleep( unsigned int t )
48 {
49  return sleep( t );
50 }
51 
52 int
53 vpl_usleep( unsigned int t )
54 {
55  // some implementations require argument to usleep < 1000000 :
56  if (t > 1000000) sleep( t/1000000 ); t %= 1000000;
57 #if VXL_UNISTD_HAS_USLEEP
58  #if VXL_UNISTD_USLEEP_IS_VOID
59  usleep( t ); return 0;
60  #else
61  return usleep( t );
62  #endif
63 #else
64  return 0;
65 #endif
66 }
67 
68 unsigned
70 {
71 #if VXL_UNISTD_HAS_GETPID
72  return getpid();
73 #else
74  return 0;
75 #endif
76 }
77 
78 int
79 vpl_putenv ( const char * envvar )
80 {
81  char * storage_space = strdup(envvar); // This causes a memory leak
82  // but this can't be helped
83  // Why copy the string? putenv is meant to take a const char* of the
84  // form "name=value". Also, putenv is neither ANSI C nor ANSI C++, but
85  // is often present in stdlib on most Unix-like systems. -- AGAP.
86  // But on some platforms (Linux), putenv "captures" the string. -- AGAP.
87 
88  return putenv(storage_space);
89 }
90 
91 int
92 vpl_gethostname(char *name, size_t len)
93 {
94 #if VXL_UNISTD_HAS_GETHOSTNAME
95  return gethostname(name, len);
96 #else
97  if (len) *name=0;
98  return -1;
99 #endif
100 }
unsigned int vpl_sleep(unsigned int t)
Sleep for t seconds.
Definition: vpl.cxx:47
int vpl_usleep(unsigned int t)
Sleep for t microseconds.
Definition: vpl.cxx:53
char * vpl_getcwd(char *buf, std::size_t buf_size)
Get the pathname of the current working directory.
Definition: vpl.cxx:17
unsigned vpl_getpid()
Get the process identifier.
Definition: vpl.cxx:69
int vpl_putenv(const char *envvar)
Set environment variable.
Definition: vpl.cxx:79
int vpl_rmdir(const char *dir)
Remove the directory dir.
Definition: vpl.cxx:29
int vpl_mkdir(const char *dir, unsigned short mode)
Create a new directory dir with permissions mode.
Definition: vpl.cxx:23
int vpl_chdir(const char *dir)
Change the working directory to dir.
Definition: vpl.cxx:35
int vpl_gethostname(char *name, size_t len)
Definition: vpl.cxx:92
int vpl_unlink(const char *file)
Remove the file file.
Definition: vpl.cxx:41