From pgilbert@bank-banque-canada.ca  Wed Jun 21 15:49:03 2000
From: pgilbert@bank-banque-canada.ca (Paul Gilbert)
Date: Wed, 21 Jun 2000 10:49:03 -0400
Subject: [Rd] OS calls
Message-ID: <3950D5DE.E3B7ED7D@bank-banque-canada.ca>

I've just been reviewing some functions in my syskern library. This library was
set up originally to provide me with a way to program around small R/S
differences  and to encapsulate some operating system requests in one place, so
that these problems would not be spread throughout my code.  Over the years the
need for many of the programs in this library has disappeared.

I have always worked in Unix but my hope was that other R users would implement
the operating system functions for other OSs. To some extent this has happened,
in the sense that many functions I once had in syskern are now handled in Rbase.
Most of the remaining functions are rather trivial or small variations on things
already done in Rbase, and I think it would be valuable for everyone if these
too could be added to Rbase. The most difficult of the functions is probably
mail, but that is already being done in bug.report(), so I am only suggesting
that it should be separated out of bug.report() and provided as a separate
function.

Below are my Unix implementations of remaining OS utilities.

Paul Gilbert
_______

sleep <- function(n) {system.call(paste("sleep ", n))} # pause for n seconds
present.working.directory <- function(){system.call("pwd")} #present directory
local.host.netname <- function() {system.call("uname -n")}
whoami <- function(){system.call("whoami")} # return user id (for mail)
   # There are problems with different shells returning different results for
whoami when
   #    a user does  su to another name. It is probably better to use a C call
for this.
file.copy <- function(from, to)system.call(paste("cp ", from, to)) #copy file

file.date.info <- function(file.name)
     {# This could be a lot better. It will fail for files older than a year.
      # Also, a returned format like date() below would be better.
      mo <- (1:12)[c("Jan","Feb","Mar","Apr","May", "Jun","Jul","Aug", "Sep",
         "Oct","Nov","Dec") ==
          substring(system.call(paste("ls -l ",file)),33,35)]
      day <- as.integer(substring(system.call(paste("ls -l ",file.name)),37,38))

      hr  <- as.integer(substring(system.call(paste("ls -l ",file.name)),40,41))

      sec <- as.integer(substring(system.call(paste("ls -l ",file.name)),43,44))

      c(mo,day,hr,sec)
     }

# Except for tz this could use the date() function in R, but that may not be the
best way.
date.parsed <- function()
        {d<-parse(text=strsplit(
              system.call("date \'+%Y %m %d %H %M %S\'")," ")[[1]])
         list(y=  eval(d[1]),
              m=eval(d[2]),
              d= eval(d[3]),
              H= eval(d[4]),
              M= eval(d[5]),
              S= eval(d[6]),
              tz=system.call("date '+%Z'"))
        }

mail <- function(to, subject="", text="")
     {# If to is null then mail is not sent (useful for testing).
      file <- tempfile()
      write(text, file=file)
      if(!is.null(to))
         system.call(paste("cat ",file, " | mail  -s '", subject, "' ", to))
      unlink(file)
      invisible()
     }



-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._


[Rd] OS calls
Paul Gilbert