3 \brief Various file-system functions.
5 This file contains various functions that deal with interfacing with
6 the filesystem in a portable way.
10 \version\verbatim $Id: fs.c 10711 2011-08-31 22:23:04Z karypis $ \endverbatim
18 /*************************************************************************
19 * This function checks if a file exists
20 **************************************************************************/
21 int gk_fexists(char *fname
)
25 if (stat(fname
, &status
) == -1)
28 return S_ISREG(status
.st_mode
);
32 /*************************************************************************
33 * This function checks if a directory exists
34 **************************************************************************/
35 int gk_dexists(char *dirname
)
39 if (stat(dirname
, &status
) == -1)
42 return S_ISDIR(status
.st_mode
);
46 /*************************************************************************/
47 /*! \brief Returns the size of the file in bytes
49 This function returns the size of a file as a 64 bit integer. If there
50 were any errors in stat'ing the file, -1 is returned.
51 \note That due to the -1 return code, the maximum file size is limited to
52 63 bits (which I guess is okay for now).
54 /**************************************************************************/
55 intmax_t gk_getfsize(char *filename
)
59 if (stat(filename
, &status
) == -1)
62 return (intmax_t)(status
.st_size
);
66 /*************************************************************************/
67 /*! This function gets some basic statistics about the file.
68 \param fname is the name of the file
69 \param r_nlines is the number of lines in the file. If it is NULL,
70 this information is not returned.
71 \param r_ntokens is the number of tokens in the file. If it is NULL,
72 this information is not returned.
73 \param r_max_nlntokens is the maximum number of tokens in any line
74 in the file. If it is NULL this information is not returned.
75 \param r_nbytes is the number of bytes in the file. If it is NULL,
76 this information is not returned.
78 /*************************************************************************/
79 void gk_getfilestats(char *fname
, size_t *r_nlines
, size_t *r_ntokens
,
80 size_t *r_max_nlntokens
, size_t *r_nbytes
)
82 size_t nlines
=0, ntokens
=0, max_nlntokens
=0, nbytes
=0, oldntokens
=0, nread
;
84 char buffer
[2049], *cptr
;
87 fpin
= gk_fopen(fname
, "r", "gk_GetFileStats");
90 nread
= fread(buffer
, sizeof(char), 2048, fpin
);
93 buffer
[nread
] = '\0'; /* There is space for this one */
94 for (cptr
=buffer
; *cptr
!='\0'; cptr
++) {
99 if (max_nlntokens
< ntokens
-oldntokens
)
100 max_nlntokens
= ntokens
-oldntokens
;
101 oldntokens
= ntokens
;
103 else if (*cptr
== ' ' || *cptr
== '\t') {
113 if (max_nlntokens
< ntokens
-oldntokens
)
114 max_nlntokens
= ntokens
-oldntokens
;
118 if (r_nlines
!= NULL
)
120 if (r_ntokens
!= NULL
)
121 *r_ntokens
= ntokens
;
122 if (r_max_nlntokens
!= NULL
)
123 *r_max_nlntokens
= max_nlntokens
;
124 if (r_nbytes
!= NULL
)
129 /*************************************************************************
130 * This function takes in a potentially full path specification of a file
131 * and just returns a string containing just the basename of the file.
132 * The basename is derived from the actual filename by stripping the last
134 **************************************************************************/
135 char *gk_getbasename(char *path
)
137 char *startptr
, *endptr
;
140 if ((startptr
= strrchr(path
, '/')) == NULL
)
143 startptr
= startptr
+1;
145 basename
= gk_strdup(startptr
);
147 if ((endptr
= strrchr(basename
, '.')) != NULL
)
153 /*************************************************************************
154 * This function takes in a potentially full path specification of a file
155 * and just returns a string corresponding to its file extension. The
156 * extension of a file is considered to be the string right after the
157 * last '.' character.
158 **************************************************************************/
159 char *gk_getextname(char *path
)
163 if ((startptr
= strrchr(path
, '.')) == NULL
)
164 return gk_strdup(path
);
166 return gk_strdup(startptr
+1);
169 /*************************************************************************
170 * This function takes in a potentially full path specification of a file
171 * and just returns a string containing just the filename.
172 **************************************************************************/
173 char *gk_getfilename(char *path
)
177 if ((startptr
= strrchr(path
, '/')) == NULL
)
178 return gk_strdup(path
);
180 return gk_strdup(startptr
+1);
183 /*************************************************************************
184 * This function takes in a potentially full path specification of a file
185 * and extracts the directory path component if it exists, otherwise it
186 * returns "./" as the path. The memory for it is dynamically allocated.
187 **************************************************************************/
188 char *getpathname(char *path
)
192 if ((endptr
= strrchr(path
, '/')) == NULL
) {
193 return gk_strdup(".");
196 tmp
= gk_strdup(path
);
197 *(strrchr(tmp
, '/')) = '\0';
204 /*************************************************************************
205 * This function creates a path
206 **************************************************************************/
207 int gk_mkpath(char *pathname
)
211 sprintf(tmp
, "mkdir -p %s", pathname
);
216 /*************************************************************************
217 * This function deletes a directory tree and all of its contents
218 **************************************************************************/
219 int gk_rmpath(char *pathname
)
223 sprintf(tmp
, "rm -r %s", pathname
);