File: files.c

package info (click to toggle)
malaga 7.12-7
  • links: PTS
  • area: main
  • in suites: buster, stretch
  • size: 3,040 kB
  • ctags: 2,283
  • sloc: ansic: 21,457; sh: 8,168; lisp: 504; makefile: 270
file content (598 lines) | stat: -rw-r--r-- 16,958 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
/* Copyright (C) 1995 Bjoern Beutel. */

/* Description. =============================================================*/

/* Operations for files and file names. */

/* Includes. ================================================================*/

#define _POSIX_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <setjmp.h>
#include <glib.h>
#include "basic.h"
#include "files.h"
#ifdef POSIX
#include <unistd.h>
#include <pwd.h>
#include <sys/mman.h>
#include <fcntl.h>
#endif
#ifdef WIN32
#include <windows.h>
#endif

/* Constants. ===============================================================*/

enum {MAX_PATH_SIZE = 200}; /* Maximum path size in characters. */

/* Macros. ==================================================================*/

#define IS_LETTER(c) (((c) >= 'A' && (c) <= 'Z') || ((c) >= 'a' && (c) <= 'z'))

/* File operations. =========================================================*/

bool_t 
file_exists( string_t file_name )
/* Return TRUE iff file FILE_NAME exists and can be read. */
{ 
  FILE *stream;

  stream = fopen( file_name, "r" );
  if (stream == NULL) 
    return FALSE;
  fclose( stream );
  return TRUE;
}

/*---------------------------------------------------------------------------*/

FILE *
open_stream( string_t file_name, string_t stream_mode )
/* Open file FILE_NAME and create a stream from/to it in mode STREAM_MODE.
 * Works like "fopen", but calls "error" if it doesn't work. */
{ 
  FILE *stream;
 
  stream = fopen( file_name, stream_mode );
  if (stream == NULL) 
    complain( "Can't open \"%s\": %s.", file_name, strerror( errno ) );
  return stream;
} 

/*---------------------------------------------------------------------------*/

void 
close_stream( FILE **stream_p, string_t file_name )
/* Close the stream *STREAM_P which is connected to the file FILE_NAME
 * and set *STREAM_P to NULL. Don't do anything if *STREAM_P == NULL.
 * Works like "fclose", but calls "error" if FILE_NAME != NULL and an error
 * occurs during closing. */
{ 
  FILE *stream = *stream_p;

  *stream_p = NULL;
  if (stream != NULL && fclose( stream ) != 0 && file_name != NULL) 
    complain( "Can't close \"%s\": %s.", file_name, strerror( errno ) );
}

/*---------------------------------------------------------------------------*/

void 
write_vector( const void *address, int_t item_size, int_t item_count, 
              FILE *stream, string_t file_name )
/* Write ITEM_COUNT items, of size ITEM_SIZE each, stored at *ADDRESS,
 * to STREAM, which is connected to file FILE_NAME.
 * Works like "fwrite", but calls "error" if it doesn't work. */
{ 
  if (fwrite( address, (size_t) item_size, (size_t) item_count, stream ) 
      < (size_t) item_count) 
  {
    complain( "Can't write to \"%s\": %s.", file_name, strerror( errno ) );
  }
}

/*---------------------------------------------------------------------------*/

void 
read_vector( void *address, int_t item_size, int_t item_count, 
             FILE *stream, string_t file_name )
/* Read ITEM_COUNT items, of size ITEM_SIZE each, from STREAM,
 * which is connected to file FILE_NAME, and store them at *ADDRESS.
 * Works like "fread", but calls "error" if it doesn't work. */
{ 
  if (fread( address, (size_t) item_size, (size_t) item_count, stream ) 
      < (size_t) item_count) 
  {
    complain( "Can't read from \"%s\": %s.", file_name, strerror( errno ) );
  }
}

/*---------------------------------------------------------------------------*/

void *
read_new_vector( int_t item_size, int_t item_count, 
                 FILE *stream, string_t file_name )
/* Read ITEM_COUNT items, of size ITEM_SIZE each, from STREAM,
 * which is connected to file FILE_NAME, into allocated memory block,
 * and return a pointer to that block.
 * The block must be freed after use. */
{ 
  void *block;

  block = new_vector( item_size, item_count );
  read_vector( block, item_size, item_count, stream, file_name );
  return block;
}

/*---------------------------------------------------------------------------*/

void 
malaga_map_file( string_t file_name, void **address, int_t *length )
/* Map file "file_name" into the memory. It will be available in the 
 * memory region starting at *ADDRESS and will occupy LENGTH bytes.
 * After usage, return the memory region via "malaga_unmap_file". */
{ 
#ifdef POSIX
  int file_descriptor;

  /* Get a file descriptor. */
  file_descriptor = open( file_name, O_RDONLY );
  if (file_descriptor == -1) 
    complain( "Can't open \"%s\": %s.", file_name, strerror( errno ) );

  /* Get file length. */
  *length = lseek( file_descriptor, 0, SEEK_END );
  if (*length == -1) 
  {
    complain( "Can't get length of \"%s\": %s.", 
	      file_name, strerror( errno ) );
  }

  *address = mmap( NULL, *length, PROT_READ, MAP_SHARED, file_descriptor, 0 );
  if (*address == (void *) -1) 
    complain( "Can't read \"%s\": %s.", file_name, strerror( errno ) );

  /* The file descriptor is no longer needed. */
  close( file_descriptor );
#endif

#ifdef WIN32
  HANDLE file_handle, map_handle;

  file_handle = CreateFile( file_name, GENERIC_READ, FILE_SHARE_READ, NULL, 
			    OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
  if (file_handle == INVALID_HANDLE_VALUE) 
    complain( "Can't open \"%s\".", file_name );

  map_handle = CreateFileMapping( file_handle, NULL, PAGE_READONLY, 
				  0, 0, NULL );
  if (map_handle == NULL) 
    complain( "Can't map \"%s\".", file_name );

  *address = MapViewOfFile( map_handle, FILE_MAP_READ, 0, 0, 0 );
  if (*address == NULL) 
    complain( "Can't map \"%s\".", file_name );
  *length = GetFileSize( file_handle, NULL );

  CloseHandle( map_handle );
  CloseHandle( file_handle );
#endif
}

/*---------------------------------------------------------------------------*/

void 
malaga_unmap_file( void **address, int_t length )
/* Return the memory region that has been allocated by "malaga_map_file".
 * The region starts at *ADDRESS and occupies LENGTH bytes. */
{ 
#ifdef POSIX
  munmap( *address, length );
#endif
#ifdef WIN32
  UnmapViewOfFile( *address );
#endif
  *address = NULL;
}

/* File name operations. ====================================================*/

string_t 
name_in_path( string_t path_name )
/* Return the file name in PATH_NAME, 
 * i.e. the name after the last separator. */
{ 
  string_t name;

  for (name = path_name + strlen( path_name ); name > path_name; name--)
  {
    if (name[-1] == '/') 
      break;
#ifdef WIN32
    if (name[-1] == '\\'
        || (name == path_name + 2 && IS_LETTER( name[-2] ) && name[-1] == ':'))
    {
      break;
    }
#endif
  }
  return name;
}

/*---------------------------------------------------------------------------*/

static string_t 
get_env( string_t name )
/* Get the content of the environment variable NAME.
 * Emit an error if it is not defined. */
{ 
  string_t content;

  content = getenv( name );
  if (content == NULL) 
    complain( "Can't read environment variable \"%s\".", name );
  return content;
}

/*---------------------------------------------------------------------------*/

static void 
tidy_path( char_t *path )
/* Remove all superfluous "..", "." and "/" in PATH.
 * PATH must be absolute. */
{ 
  char_t *src_p;
  char_t *dest_p;

#ifdef POSIX
  dest_p = src_p = path;
  while (*src_p != EOS) 
  { 
    while (*src_p == '/') 
      src_p++;
    *dest_p++ = '/';
    if (src_p[0] == '.' && src_p[1] == '.' 
	&& (src_p[2] == '/' || src_p[2] == EOS)) 
    { 
      /* Walk up only if we are not on root level. */
      src_p += 2;
      if (dest_p > path + 1) 
	dest_p -= 2;
      while (*dest_p != '/') 
	dest_p--;
    } 
    else if (src_p[0] == '.' && (src_p[1] == '/' || src_p[1] == EOS)) 
    { 
      src_p++;
      dest_p--;
    } 
    else 
    { 
      while (*src_p != '/' && *src_p != EOS) 
	*dest_p++ = *src_p++;
    }
  }
  if (dest_p > path + 1 && dest_p[-1] == '/') 
    dest_p--;
  *dest_p = EOS;
#endif

#ifdef WIN32
  /* The first two chars is the drive specification. */
  if (! IS_LETTER( path[0] ) || path[1] != ':') 
    complain( "Missing drive name." );
  dest_p = src_p = path + 2;

  while (*src_p != EOS) 
  { 
    while (*src_p == '\\' || *src_p == '/') 
      src_p++;
    *dest_p++ = '\\';
    if (src_p[0] == '.' && src_p[1] == '.' 
        && (src_p[2] == '\\' || src_p[2] == '/' || src_p[2] == EOS)) 
    { 
      /* Walk up only if we are not on root level. */
      src_p += 2;
      if (dest_p > path + 3) 
	dest_p -= 2;
      while (*dest_p != '\\') 
	dest_p--;
    } 
    else if (src_p[0] == '.' 
	     && (src_p[1] == '\\' || src_p[1] == '/' || src_p[1] == EOS)) 
    { 
      src_p++;
      dest_p--;
    } 
    else 
    { 
      while (*src_p != '\\' && *src_p != '/' && *src_p != EOS) 
	*dest_p++ = *src_p++;
    }
  }
  if (dest_p > path + 3 && dest_p[-1] == '\\') 
    dest_p--;
  *dest_p = EOS;
#endif
}

/*---------------------------------------------------------------------------*/

char_t * 
absolute_path( string_t src_path, string_t relative_to )
/* Return the absolute path name which is equivalent to SRC_PATH.
 * If SRC_PATH starts with "~", it's replaced by the home directory of the
 * user whose login name is following (current user if no login name).
 * If RELATIVE_TO is not NULL, SRC_NAME is relative to that path name.
 * RELATIVE_TO must be an absolute path name (a directory or a file).
 * The returned path must be freed after use. */
{ 
#ifdef POSIX
  text_t *path;
  string_t src_path_p, login, login_p;
  char_t *dest_path;
  struct passwd *password;
  string_t relative_end, relative_dir;
  char_t current_dir[ MAX_PATH_SIZE ];

  path = new_text();

  /* Put a home directory in front. */
  src_path_p = src_path;
  if (*src_path_p == '~') 
  { 
    /* Put a users home directory in front. */
    src_path_p++;
    login_p = src_path_p;
    while (*src_path_p != '/' && *src_path_p != EOS) 
      src_path_p++;
    if (src_path_p == login_p) 
      add_to_text( path, get_env( "HOME" ) );
    else 
    { 
      /* Put home directory of user LOGIN in front. */
      login = new_string( login_p, src_path_p );
      password = getpwnam( login );
      if (password == NULL) 
	complain( "Can't find user \"%s\".", login );
      add_to_text( path, password->pw_dir );
      free_mem( &login );
    }
  } 
  else if (*src_path_p != '/') 
  { 
    if (relative_to != NULL) 
    { 
      /* Put RELATIVE_TO ahead (strip last name). */
      relative_end = relative_to + strlen( relative_to );
      while (relative_end[-1] != '/') 
	relative_end--;
      relative_dir = new_string( relative_to, relative_end );
      add_to_text( path, relative_dir );
      free_mem( &relative_dir );
    } 
    else 
    { 
      /* Put current directory in front. */
      getcwd( current_dir, MAX_PATH_SIZE );
      add_to_text( path, current_dir );
    }
  }
  
  /* Copy rest of DEST_PATH, clean it up and return it. */
  add_char_to_text( path, '/' );
  add_to_text( path, src_path_p );

  dest_path = text_to_string( &path );
  if (*dest_path != '/') 
    complain( "Path \"%s\" must be absolute.", src_path );
  tidy_path( dest_path );
  return dest_path;
#endif

#ifdef WIN32
  text_t *path;
  string_t src_path_p, dest_path;		
  string_t relative_end, relative_dir;
  char_t current_dir[ MAX_PATH_SIZE ];

  path = new_text();
  src_path_p = src_path;
  if (src_path_p[0] == '~' && (src_path_p[1] == '\\' || src_path_p[1] == '/'))
  {
    /* Put the users home directory in front. */
    src_path_p += 2;
    relative_to = getenv( "USERPROFILE" );
    if (relative_to == NULL) 
      relative_to = get_env( "SYSTEMDRIVE" );
    add_to_text( path, relative_to );
    add_char_to_text( path, '\\' );
  }
  else if (IS_LETTER( src_path_p[0] ) && src_path_p[1] == ':')
  {
    /* The path is already complete. */
    if (src_path_p[2] != '\\' && src_path_p[2] != '/')
      complain( "A drive name needs an absolute path." );
  }
  else if (src_path_p[0] == '\\' || src_path_p[0] == '/')
  {
    /* Put the current drive in front. */
    src_path_p++;
    if (relative_to != NULL)
    {
      add_char_to_text( path, relative_to[0] );
      add_char_to_text( path, relative_to[1] );
    }
    else
    {
      GetCurrentDirectory( MAX_PATH_SIZE, current_dir );
      add_char_to_text( path, current_dir[0] );
      add_char_to_text( path, current_dir[1] );
    }  
    add_char_to_text( path, '\\' );
  }
  else if (relative_to != NULL) 
  { 
    /* Put RELATIVE_TO ahead (strip last name). */
    relative_end = relative_to + strlen( relative_to );
    while (relative_end[-1] != '\\') 
      relative_end--;
    relative_dir = new_string( relative_to, relative_end );
    add_to_text( path, relative_dir );
    add_char_to_text( path, '\\' );
    free_mem( &relative_dir );
  }
  else 
  { 
    /* Put current directory in front. */
    GetCurrentDirectory( MAX_PATH_SIZE, current_dir );
    add_to_text( path, current_dir );
    add_char_to_text( path, '\\' );
  }
  
  add_to_text( path, src_path_p );
  dest_path = text_to_string( &path );
  tidy_path( dest_path );
  return dest_path;
#endif
}

/*---------------------------------------------------------------------------*/

char_t * 
replace_vars_in_string( string_t string )
/* Replace environment variables of form "${X}" in STRING.
 * Return the resulting string. It must be freed after use. */
{ 
  text_t *text;
  string_t string_p, variable_p, variable;

  text = new_text();
  for (string_p = string; *string_p != EOS; string_p++) 
  { 
    if (string_p[0] == '$' && string_p[1] == '{') 
    { 
      string_p += 2;
      variable_p = string_p;
      while (*string_p != '}') 
      { 
	if (*string_p == EOS) 
	  complain( "Missing \"}\" in environment variable name." );
	string_p++;
      }
      variable = new_string( variable_p, string_p );
      add_to_text( text, get_env( variable ) );
      free_mem( &variable );
    } 
    else 
      add_char_to_text( text, *string_p );
  }
  return text_to_string( &text );
}

/*---------------------------------------------------------------------------*/

static string_t extension_start( string_t name )
/* Return a pointer to the start (the dot) of the extension in NAME,
 * or to the end of the string if there is no extension. */
{
  string_t s, t;
  
  s = NULL;
  for (t = name; *t != EOS; t++)
  {
    if (*t == '/') 
      s = NULL;
#ifdef WIN32
    else if (*t == '\\') 
      s = NULL;
#endif
    else if (*t == '.') 
      s = t;
  }
  return (s != NULL ? s : t);
}

/*---------------------------------------------------------------------------*/

bool_t 
has_extension( string_t file_name, string_t extension )
/* Test if FILE_NAME has extension EXTENSION. */
{
  string_t ext; /* The real extension of FILE_NAME (including "."). */

  ext = extension_start( file_name );
  return (*ext != EOS && strcmp( ext + 1, extension ) == 0);
}

/*---------------------------------------------------------------------------*/

char_t *
replace_extension( string_t file_name, string_t extension )
/* Return a new string that contains FILE_NAME with new EXTENSION. 
 * The string must be freed after use. */
{
  string_t base;
  char_t *s;

  base = new_string( file_name, extension_start( file_name ) );
  s = concat_strings( base, ".", extension, NULL );
  free_mem( &base );
  return s;
}

/*---------------------------------------------------------------------------*/

void
set_file_name( string_t *file_name_p, string_t file_name )
/* Set *FILE_NAME_P to absolute path FILE_NAME, relative to current dir.
 * Print an error if *FILE_NAME_P is already set.
 * The created file name must be freed after use. */
{ 
  if (*file_name_p != NULL) 
    complain( "File \"%s\" is redundant.", file_name );
  *file_name_p = absolute_path( file_name, NULL );
}

/*---------------------------------------------------------------------------*/

void
set_binary_file_name( string_t *file_name_p, string_t file_name )
/* Set *FILE_NAME_P to
 * FILE_NAME plus "_l" for little endian, "_b" for big endian, "_c" else,
 * converted to absolute path. 
 * Print an error if *FILE_NAME_P is already set.
 * The created file name must be freed after use. */
{
  union { char_t chars[4]; int_t integer; } format;
  string_t suffix, binary_file_name;

  if (*file_name_p != NULL) 
    complain( "File \"%s\" is redundant.", file_name );
  
  format.integer = 0x12345678;
  if (sizeof( int_t ) != 4) 
    suffix = "_c";
  else if (format.chars[0] == 0x12 && format.chars[1] == 0x34
	   && format.chars[2] == 0x56 && format.chars[3] == 0x78)
  {
    suffix = "_b";
  }
  else if (format.chars[0] == 0x78 && format.chars[1] == 0x56
	   && format.chars[2] == 0x34 && format.chars[3] == 0x12)
  {
     suffix = "_l";
  }
  else 
    suffix = "_c";

  binary_file_name = concat_strings( file_name, suffix, NULL );
  *file_name_p = absolute_path( binary_file_name, NULL );
  free_mem( &binary_file_name );
}

/* End of file. =============================================================*/