/*************************************************************************** os_mod.cpp - description ------------------- begin : Thu Feb 10 21:38:15 2005 copyright : (C) 2002 by Cavalli Andrea email : cavalli@bioc.unizh.ch **************************************************************************/ /*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * ***************************************************************************/ #include #include #include #include #include #include #include #include using namespace Almost; #define BUFFSIZE 4048 string getcwd_alm() { char buff[BUFFSIZE]; if(getcwd(buff,BUFFSIZE)!=NULL) return string(buff); else throw "getcwd: cwd path to long!"; return string(); } string ctermid_alm(){ return string(ctermid(NULL)); } string getlogin_alm(){ char buff[BUFFSIZE]; getlogin_r(buff,BUFFSIZE); return buff; } int alm_fork(){ return fork(); } pid_t alm_waitpid(int pid){ return waitpid(pid,0,WNOHANG); } int alm_system(string s){ return system(s.c_str()); } class Pipe { FILE * p; public: Pipe(string comm, string type){ p = popen(comm.c_str(),type.c_str()); } void close(){ if(p!=NULL){ pclose(p); p = NULL; } } bool eof(){ char c = fgetc(p); if(c==EOF){ return true;} else { ungetc(c,p); } return false; //return feof(p); } string getc(){ char c; c = fgetc(p); string s; s += c; return s; } }; int unlink_alm(string f){ return unlink(f.c_str()); } template<> inline string to_string(const Pipe & p){ return "pipe"; } extern "C" { void init_os(){ //declarations here Module mod = Module("os","Operating system functions"); mod.def_function("chdir","chdir changes the current directory to that specified in path.",chdir) .def_function("getcwd","The getcwd() returns the path of the current working directory",getcwd_alm) .def_function("ctermid","ctermid() returns a string which is the pathname for the current controlling terminal for this process.",ctermid_alm) .def_function("getegid"," Return the effective group id of the current process. This corresponds to the `set id' bit on the file being executed in the current process.",getegid) .def_function("geteuid","getuid returns the real user ID of the current process.",getuid) .def_function("getgid","getgid returns the real group ID of the current process.",getgid) .def_function("getlogin","Return the name of the user logged in on the controlling terminal of the process.",getlogin_alm) .def_function("unlink","",unlink_alm) ; mod .def_function("fork","",alm_fork) .def_function("waitpid","",alm_waitpid) .def_function("system","",alm_system); ; // To be added //.def_function("getgroups"," Return list of supplemental group ids associated with the current process.",getgroups_alm) // getpgid getpgrp getpid getppid getuid getenv putenv setegid // seteuid setgid setgroups setpgrp setpgid setreuid setregid setsid setuid // strerror umask uname // fdopen popen tmpfile popen2 popen3 popen4 // close dup dup2 fdatasync fpathconf fstat fstatvfs fsync ftruncate isatty // lseek open openpty pipe read tcgetpgrp tcsetpgrp ttyname write //O_RDONLY //O_WRONLY //O_RDWR //O_NDELAY //O_NONBLOCK //O_APPEND //O_DSYNC //O_RSYNC //O_SYNC //O_NOCTTY //O_CREAT //O_EXCL //O_TRUNC //O_BINARY //O_NOINHERIT //O_SHORT_LIVED //O_TEMPORARY //O_RANDOM //O_SEQUENTIAL //O_TEXT //access chroot chmod chown lchown link listdir lstat mkfifo mknod major minor makedev mkdir makedirs pathconf pathconf_names readlink remove removedirs rename renames rmdir stat stat_float_times statvfs symlink tempnam tmpnam TMP_MAX unlink utime walk //abort execl execle ... _exit //all const //fork forkpty kill killpg nice plock spawnl ... //system //times wait waitpid .... //confstr confstr_names getloadavg sysconf sysconf_names //abspath basename commonprefix dirname exists expanduser expandvars //getatime getmtime getctime getsize isabs isfile isdir islink ismount //join normcase normpath realpath samefile sameopenfile samestat split //splitdrive splitext walk supports_unicode_filenames //... others ... Class >(mod.self(),"pipe") .def_method("eof",&Pipe::eof) .def_method("close",&Pipe::close) .def_method("getc",&Pipe::getc) ; } }