diff --git a/src/cutils.c b/src/cutils.c index 3002110..d2738b1 100644 --- a/src/cutils.c +++ b/src/cutils.c @@ -1,245 +1,243 @@ /** * @file cutils.c * * @brief Utilities C functions. * * @copyright * Copyright (©) 2021 EPFL (Ecole Polytechnique Fédérale de Lausanne) * SPC (Swiss Plasma Center) * * futils is free software: you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by the Free * Software Foundation, either version 3 of the License, or (at your option) * any later version. * * futils is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see . * * @authors * (in alphabetical order) * @author Stephan Brunner * @author Ben McMillan * @author Trach-Minh Tran */ #include #include #include #include #include #include #include #include #include double mem_(void) { // Return current memory usage in MB (linux x86 ony!) FILE *file; char proc[256]; int mm, rss; sprintf(proc,"/proc/%d/statm",(int)getpid()); if (!(file = fopen(proc,"r"))) { return -1; } fscanf(file,"%d %d",&mm,&rss); fclose(file); return ((double)rss) * ((double)getpagesize() /1024.0/1024.0); } double mem(void) { // Return current memory usage in MB (linux x86 ony!) return mem_(); } /* #include */ /* double mem_(void) */ /* { */ /* // Return current memory usage in MB */ /* struct rusage usage; */ /* double memkb; */ /* if (getrusage(RUSAGE_SELF, &usage) != 0) */ /* return -1; */ /* memkb = (double) usage.ru_maxrss; */ /* return ( memkb/1024.0 ); */ /* } */ double second_(void) { // Return elapsed wall time in s. struct timeval tp; struct timezone tzp; int i; i = gettimeofday(&tp,&tzp); return ( (double) tp.tv_sec + (double) tp.tv_usec * 1.e-6 ); } double second(void) { // Return elapsed wall time in s. return second_(); } double seconds_(void) { // Return elapsed wall time in s. struct timeval tp; struct timezone tzp; int i; i = gettimeofday(&tp,&tzp); return ( (double) tp.tv_sec + (double) tp.tv_usec * 1.e-6 ); } double seconds(void) { // Return elapsed wall time in s. return seconds_(); } int fsize_(const char *file, unsigned int l) { // Return size of file in bytes struct stat buf; stat( file, &buf); return (int)buf.st_size; } int fsize(const char *file, unsigned int l) { // Return size of file in bytes return fsize_(file, l); } void ftos_(const char *file, int *s, char *buf) { // Put the content of file to a string int fd, n; fd = open(file, O_RDONLY); n = read(fd, buf, (size_t)*s); close(fd); } void ftos(const char *file, int *s, char *buf) { // Put the content of file to a string ftos_(file, s, buf); } void stof_(const char *file, int *s, char *buf) { // Write string to file int fd, n; fd = creat(file, 0644); n = write(fd, buf, (size_t)*s); close(fd); } void stof(const char *file, int *s, char *buf) { // Write string to file stof_(file, s, buf); } void stostdout_(int *s, char *buf) { // Write string to stdout int n; n = write(1, buf, (size_t)*s); } void stostdout(int *s, char *buf) { // Write string to stdout stostdout_(s, buf); } // Read in 1kb chunks in copy_file, // also use as temporary for file names #define CHSIZE 1024 void copy_file_(char *infile, int *lengthin, char *outfile, int *lengthout) { // Copies a file from one location to another using fread, fwrite. char buf[CHSIZE]; int err, count=CHSIZE; memcpy(buf,infile,CHSIZE*sizeof(char)); buf[*lengthin