core/vpl/os_unix/vpl.cxx
Go to the documentation of this file.
00001 // This is core/vpl/os_unix/vpl.cxx
00002 // Include system headers for UNIX-like operating system :
00003 #undef _XOPEN_SOURCE
00004 #define _XOPEN_SOURCE 1 // necessary on alpha and on SGI since otherwise
00005 #undef _XOPEN_SOURCE_EXTENDED
00006 #define _XOPEN_SOURCE_EXTENDED 1 // usleep is not declared
00007 extern "C" {
00008 #include <unistd.h>
00009 #include <sys/types.h>
00010 #include <sys/stat.h>
00011 #include <stdlib.h>
00012 #include <string.h>  // for strdup
00013 }
00014 #include <vxl_config.h> // for VXL_UNISTD_*
00015 
00016 char *
00017 vpl_getcwd( char *buf, vcl_size_t buf_size )
00018 {
00019   return getcwd( buf, buf_size );
00020 }
00021 
00022 int
00023 vpl_mkdir( const char *dir, unsigned short mode )
00024 {
00025   return mkdir( dir, (mode_t)mode );
00026 }
00027 
00028 int
00029 vpl_rmdir( const char *dir )
00030 {
00031   return rmdir( dir );
00032 }
00033 
00034 int
00035 vpl_chdir( const char *dir )
00036 {
00037   return chdir( dir );
00038 }
00039 
00040 int
00041 vpl_unlink( const char *file )
00042 {
00043   return unlink( file );
00044 }
00045 
00046 unsigned int
00047 vpl_sleep( unsigned int t )
00048 {
00049   return sleep( t );
00050 }
00051 
00052 int
00053 vpl_usleep( unsigned int t )
00054 {
00055   // some implementations require argument to usleep < 1000000 :
00056   if (t > 1000000) sleep( t/1000000 ); t %= 1000000;
00057 #if VXL_UNISTD_HAS_USLEEP
00058  #if VXL_UNISTD_USLEEP_IS_VOID
00059   usleep( t ); return 0;
00060  #else
00061   return usleep( t );
00062  #endif
00063 #else
00064   return 0;
00065 #endif
00066 }
00067 
00068 unsigned
00069 vpl_getpid( )
00070 {
00071 #if VXL_UNISTD_HAS_GETPID
00072   return getpid();
00073 #else
00074   return 0;
00075 #endif
00076 }
00077 
00078 int
00079 vpl_putenv ( const char * envvar )
00080 {
00081   char * storage_space = strdup(envvar); // This causes a memory leak
00082                                          // but this can't be helped
00083   // Why copy the string? putenv is meant to take a const char* of the
00084   // form "name=value". Also, putenv is neither ANSI C nor ANSI C++, but
00085   // is often present in stdlib on most Unix-like systems. -- AGAP.
00086   // But on some platforms (Linux), putenv "captures" the string. -- AGAP.
00087 
00088   return putenv(storage_space);
00089 }
00090 
00091 int
00092 vpl_gethostname(char *name, size_t len)
00093 {
00094 #if VXL_UNISTD_HAS_GETHOSTNAME
00095   return gethostname(name, len);
00096 #else
00097   if (len) *name=0;
00098   return -1;
00099 #endif
00100 }