Path: blob/devel/elmergrid/src/metis-5.1.0/GKlib/fs.c
3206 views
/*!1\file fs.c2\brief Various file-system functions.34This file contains various functions that deal with interfacing with5the filesystem in a portable way.67\date Started 4/10/958\author George9\version\verbatim $Id: fs.c 10711 2011-08-31 22:23:04Z karypis $ \endverbatim10*/111213#include <GKlib.h>14151617/*************************************************************************18* This function checks if a file exists19**************************************************************************/20int gk_fexists(char *fname)21{22struct stat status;2324if (stat(fname, &status) == -1)25return 0;2627return S_ISREG(status.st_mode);28}293031/*************************************************************************32* This function checks if a directory exists33**************************************************************************/34int gk_dexists(char *dirname)35{36struct stat status;3738if (stat(dirname, &status) == -1)39return 0;4041return S_ISDIR(status.st_mode);42}434445/*************************************************************************/46/*! \brief Returns the size of the file in bytes4748This function returns the size of a file as a 64 bit integer. If there49were any errors in stat'ing the file, -1 is returned.50\note That due to the -1 return code, the maximum file size is limited to5163 bits (which I guess is okay for now).52*/53/**************************************************************************/54intmax_t gk_getfsize(char *filename)55{56struct stat status;5758if (stat(filename, &status) == -1)59return -1;6061return (intmax_t)(status.st_size);62}636465/*************************************************************************/66/*! This function gets some basic statistics about the file.67\param fname is the name of the file68\param r_nlines is the number of lines in the file. If it is NULL,69this information is not returned.70\param r_ntokens is the number of tokens in the file. If it is NULL,71this information is not returned.72\param r_max_nlntokens is the maximum number of tokens in any line73in the file. If it is NULL this information is not returned.74\param r_nbytes is the number of bytes in the file. If it is NULL,75this information is not returned.76*/77/*************************************************************************/78void gk_getfilestats(char *fname, size_t *r_nlines, size_t *r_ntokens,79size_t *r_max_nlntokens, size_t *r_nbytes)80{81size_t nlines=0, ntokens=0, max_nlntokens=0, nbytes=0, oldntokens=0, nread;82int intoken=0;83char buffer[2049], *cptr;84FILE *fpin;8586fpin = gk_fopen(fname, "r", "gk_GetFileStats");8788while (!feof(fpin)) {89nread = fread(buffer, sizeof(char), 2048, fpin);90nbytes += nread;9192buffer[nread] = '\0'; /* There is space for this one */93for (cptr=buffer; *cptr!='\0'; cptr++) {94if (*cptr == '\n') {95nlines++;96ntokens += intoken;97intoken = 0;98if (max_nlntokens < ntokens-oldntokens)99max_nlntokens = ntokens-oldntokens;100oldntokens = ntokens;101}102else if (*cptr == ' ' || *cptr == '\t') {103ntokens += intoken;104intoken = 0;105}106else {107intoken = 1;108}109}110}111ntokens += intoken;112if (max_nlntokens < ntokens-oldntokens)113max_nlntokens = ntokens-oldntokens;114115gk_fclose(fpin);116117if (r_nlines != NULL)118*r_nlines = nlines;119if (r_ntokens != NULL)120*r_ntokens = ntokens;121if (r_max_nlntokens != NULL)122*r_max_nlntokens = max_nlntokens;123if (r_nbytes != NULL)124*r_nbytes = nbytes;125}126127128/*************************************************************************129* This function takes in a potentially full path specification of a file130* and just returns a string containing just the basename of the file.131* The basename is derived from the actual filename by stripping the last132* .ext part.133**************************************************************************/134char *gk_getbasename(char *path)135{136char *startptr, *endptr;137char *basename;138139if ((startptr = strrchr(path, '/')) == NULL)140startptr = path;141else142startptr = startptr+1;143144basename = gk_strdup(startptr);145146if ((endptr = strrchr(basename, '.')) != NULL)147*endptr = '\0';148149return basename;150}151152/*************************************************************************153* This function takes in a potentially full path specification of a file154* and just returns a string corresponding to its file extension. The155* extension of a file is considered to be the string right after the156* last '.' character.157**************************************************************************/158char *gk_getextname(char *path)159{160char *startptr;161162if ((startptr = strrchr(path, '.')) == NULL)163return gk_strdup(path);164else165return gk_strdup(startptr+1);166}167168/*************************************************************************169* This function takes in a potentially full path specification of a file170* and just returns a string containing just the filename.171**************************************************************************/172char *gk_getfilename(char *path)173{174char *startptr;175176if ((startptr = strrchr(path, '/')) == NULL)177return gk_strdup(path);178else179return gk_strdup(startptr+1);180}181182/*************************************************************************183* This function takes in a potentially full path specification of a file184* and extracts the directory path component if it exists, otherwise it185* returns "./" as the path. The memory for it is dynamically allocated.186**************************************************************************/187char *getpathname(char *path)188{189char *endptr, *tmp;190191if ((endptr = strrchr(path, '/')) == NULL) {192return gk_strdup(".");193}194else {195tmp = gk_strdup(path);196*(strrchr(tmp, '/')) = '\0';197return tmp;198}199}200201202203/*************************************************************************204* This function creates a path205**************************************************************************/206int gk_mkpath(char *pathname)207{208char tmp[2048];209210sprintf(tmp, "mkdir -p %s", pathname);211return system(tmp);212}213214215/*************************************************************************216* This function deletes a directory tree and all of its contents217**************************************************************************/218int gk_rmpath(char *pathname)219{220char tmp[2048];221222sprintf(tmp, "rm -r %s", pathname);223return system(tmp);224}225226227