Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 static char *source_file = __FILE__;
00038
00039 #include <sys/types.h>
00040 #include <sys/stat.h>
00041 #include <unistd.h>
00042 #include <sys/param.h>
00043 #include <stdlib.h>
00044 #include <stdio.h>
00045 #include <cmplrs/rcodes.h>
00046
00047 #include "defs.h"
00048 #include "file_util.h"
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059 BOOL
00060 Is_File ( const char *fname )
00061 {
00062 struct stat desc;
00063
00064 if (fname == NULL)
00065 return FALSE;
00066
00067 if ( stat ( fname, &desc ) != 0 )
00068 return FALSE;
00069 return ( (desc.st_mode & S_IFREG) != 0 );
00070 }
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084 BOOL
00085 Same_File ( file1, file2 )
00086 FILE *file1, *file2;
00087 {
00088 struct stat d1, d2;
00089
00090 if ( file1 == NULL || file2 == NULL ) return FALSE;
00091 if ( fstat ( fileno(file1), &d1 ) == -1 ) return FALSE;
00092 if ( fstat ( fileno(file2), &d2 ) == -1 ) return FALSE;
00093 return ( d1.st_ino == d2.st_ino ) && ( d1.st_dev == d2.st_dev );
00094 }
00095
00096 #ifndef MONGOOSE_BE
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111 BOOL
00112 Find_File ( name, ext )
00113 char *name;
00114 char *ext;
00115 {
00116 INT16 len;
00117
00118
00119 if ( Is_File(name) ) return TRUE;
00120
00121
00122 if ( ! Has_Extension ( name, ext ) ) {
00123 len = strlen(name);
00124 strcat (name, ext);
00125 if ( Is_File(name) ) return TRUE;
00126 name[len] = 0;
00127 }
00128
00129
00130 return FALSE;
00131 }
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142 BOOL
00143 Has_Extension ( name, ext )
00144 char *name;
00145 char *ext;
00146 {
00147 INT16 nlen = strlen(name);
00148 INT16 elen = strlen(ext);
00149
00150
00151 if ( elen > nlen ) return FALSE;
00152
00153
00154 return ( strcmp ( &name[nlen-elen], ext ) == 0 );
00155 }
00156 #endif
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171 char *
00172 New_Extension ( const char *name, const char *ext )
00173 {
00174 char *new;
00175 INT16 len, i;
00176
00177
00178 len = strlen(name);
00179 new = (char *) malloc ( len + strlen(ext) + 1 );
00180 strcpy ( new, name );
00181 for ( i=len-1; i>=0; i-- ) {
00182 if ( new[i] == '/' ) break;
00183 if ( new[i] == '.' ) {
00184 new[i] = 0;
00185 break;
00186 }
00187 }
00188 strcat ( new, ext );
00189 return new;
00190 }
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203 char *
00204 Remove_Extension ( name )
00205 char *name;
00206 {
00207 char *new;
00208 INT16 len, i;
00209
00210 len = strlen(name);
00211 new = (char *) malloc ( len );
00212 strcpy ( new, name );
00213 for ( i=len-1; i>=0; i-- ) {
00214 if ( new[i] == '.' ) {
00215 new[i] = 0;
00216 break;
00217 }
00218 }
00219 return new;
00220 }
00221
00222
00223 #ifndef MONGOOSE_BE
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234 char *
00235 Make_Temp_File ( tmp, prefix )
00236 char *tmp;
00237 char *prefix;
00238 {
00239 INT16 len = strlen(tmp);
00240 char *name;
00241
00242 name = (char *) malloc (len + 20);
00243 if ( len > 0 ) {
00244 strcpy ( name, tmp );
00245 name[len++] = '/';
00246 }
00247 strcpy ( &name[len], prefix );
00248 len += strlen(prefix);
00249 sprintf ( &name[len], "%d", getpid() );
00250 return name;
00251 }
00252
00253
00254 static char *cwd = NULL;
00255 static INT cwd_size;
00256
00257 char *
00258 Get_Current_Working_Directory (void)
00259 {
00260 char *cwd;
00261 cwd = getcwd((char *) NULL, MAXPATHLEN);
00262 if (cwd == NULL) {
00263 cwd = getenv("PWD");
00264 if (cwd == NULL) {
00265
00266 cwd = ".";
00267 }
00268 }
00269 return cwd;
00270 }
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281 char *
00282 Full_Path_Name ( base, path, pathlen )
00283 char *base;
00284 char *path;
00285 INT pathlen;
00286 {
00287 INT baselen;
00288
00289
00290 if ( base == NULL ) return NULL;
00291 baselen = strlen (base);
00292 if ( baselen > pathlen ) exit(RC_SYSTEM_ERROR);
00293
00294
00295 if (base[0] == '/') {
00296 strcpy (path, base);
00297 return NULL;
00298 }
00299
00300
00301 if ( cwd == NULL ) {
00302 cwd = Get_Current_Working_Directory();
00303 cwd_size = strlen (cwd);
00304 }
00305 if ( baselen + cwd_size > pathlen ) exit(RC_SYSTEM_ERROR);
00306 strcpy (path, cwd);
00307 strcat (path, "/");
00308 strcat (path, base);
00309 return path;
00310 }
00311 #endif
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328 char *
00329 Last_Pathname_Component ( char *pname )
00330 {
00331 char *cp = pname + strlen(pname);
00332
00333 while (cp != pname) {
00334 if (*cp == '/') return cp+1;
00335 --cp;
00336 }
00337 if (*cp == '/') return cp+1;
00338 return cp;
00339
00340 }
00341
00342
00343 #ifndef MONGOOSE_BE
00344
00345
00346
00347
00348
00349
00350 static char *
00351 normalize_path(char * path)
00352 {
00353 char * inp = path, *outp = path, *tmp;
00354
00355 while (inp != NULL && *inp != '\0') {
00356 if (inp[0] == '/') {
00357 if (inp[1] == '/')
00358 inp+= 1;
00359 else if (inp[1] == '.' && inp[2] == '/')
00360 inp += 2;
00361 else if (inp[1] == '.' && inp[2] == '.' && inp[3] == '/') {
00362
00363 for (tmp = outp-1;
00364 tmp >= path && *tmp != '/';
00365 tmp -= 1);
00366
00367 if (tmp >= path && tmp[0] == '/' &&
00368 (tmp[1] != '.' || tmp[2] != '.'))
00369 outp = tmp;
00370 else {
00371 *outp++ = '/';
00372 *outp++ = '.';
00373 *outp++ = '.';
00374 }
00375 inp+= 3;
00376 }
00377 else
00378 *outp++ = *inp++;
00379 }
00380 else
00381 *outp++ = *inp++;
00382 }
00383
00384 *outp = '\0';
00385 return path;
00386 }
00387
00388 #define is_absolute_file_name(file_name) ((file_name)[0] == '/')
00389
00390
00391
00392
00393 extern char *
00394 Make_Absolute_Path (char *filename)
00395 {
00396 char *normalized;
00397 INT64 cwd_length;
00398
00399 if ( cwd == NULL ) {
00400 cwd = Get_Current_Working_Directory();
00401 cwd_size = strlen (cwd);
00402 }
00403 normalized = (char *)malloc(cwd_size+strlen(filename)+2);
00404 if (normalized == NULL) {
00405 perror("malloc");
00406 exit(RC_SYSTEM_ERROR);
00407 }
00408 if (is_absolute_file_name(filename))
00409 (void)strcpy(normalized, filename);
00410 else
00411 {
00412 cwd_length = cwd_size;
00413 strcpy(normalized, cwd);
00414 if (cwd[cwd_length - 1] != '/')
00415 {
00416 normalized[cwd_length++] = '/';
00417 normalized[cwd_length] = '\0';
00418 }
00419 (void)strcpy(&normalized[cwd_length], filename);
00420 }
00421 (void)normalize_path(normalized);
00422 return normalized;
00423 }
00424 #endif