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