Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F80499949
cutils.c
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Sun, Sep 1, 01:56
Size
5 KB
Mime Type
text/x-c
Expires
Tue, Sep 3, 01:56 (1 d, 23 h)
Engine
blob
Format
Raw Data
Handle
20311993
Attached To
rFUTILS futils
cutils.c
View Options
/**
* @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 <https://www.gnu.org/licenses/>.
*
* @authors
* (in alphabetical order)
* @author Stephan Brunner <stephan.brunner@epfl.ch>
* @author Ben McMillan <B.F.McMillan@warwick.ac.uk>
* @author Trach-Minh Tran <trach-minh.tran@epfl.ch>
*/
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/uio.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
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 <sys/resource.h> */
/* 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 <CHSIZE ? *lengthin : CHSIZE-1] = '\0';
FILE *in = fopen(buf, "rb");
if (in == NULL) {
printf("could not open infile\n");
return;
}
memcpy(buf,outfile,CHSIZE*sizeof(char));
buf[*lengthout<CHSIZE ? *lengthout : CHSIZE-1] = '\0';
FILE *out = fopen(buf,"wb");
if (out == NULL) {
fclose(in);
printf("could not open outfile\n");
return;
}
while (1) {
err = fread(buf,sizeof(char),count,in);
fwrite(buf,sizeof(char),err,out);
if (err!=count) break;
}
fclose(in);
fclose(out);
}
void copy_file(char *infile, int *lengthin, char *outfile, int *lengthout) {
// Copies a file from one location to another using fread, fwrite.
copy_file_(infile, lengthin, outfile, lengthout);
}
void move_file_(char *infile, int *lengthin, char *outfile, int *lengthout) {
// Moves a file from one location to another using copy_file_ and
// unlink (man 2 unlink)
char buf[CHSIZE];
copy_file_(infile, lengthin, outfile, lengthout);
memcpy(buf, infile,CHSIZE*sizeof(char));
buf[*lengthin <CHSIZE ? *lengthin : CHSIZE-1] = '\0';
if( unlink(buf) == -1 ){
fprintf(stderr, "%s: removing %s\n", strerror(errno), buf);
}
}
void move_file(char *infile, int *lengthin, char *outfile, int *lengthout) {
// Moves a file from one location to another using copy_file_ and
// unlink (man 2 unlink)
move_file_(infile, lengthin, outfile, lengthout);
}
Event Timeline
Log In to Comment