File: files.c

package info (click to toggle)
scilab 2.6-4
  • links: PTS
  • area: non-free
  • in suites: woody
  • size: 54,632 kB
  • ctags: 40,267
  • sloc: ansic: 267,851; fortran: 166,549; sh: 10,005; makefile: 4,119; tcl: 1,070; cpp: 233; csh: 143; asm: 135; perl: 130; java: 39
file content (671 lines) | stat: -rw-r--r-- 17,396 bytes parent folder | download
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
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671

/*
 * From TclWinFCmd.c
 *
 *      This file implements the Windows specific portion of file manipulation 
 *      subcommands of the "file" command. 
 *
 * Copyright (c) 1996-1997 Sun Microsystems, Inc.
 *
 * See the file "license.terms" for information on usage and redistribution
 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
 *
 * SCCS: @(#) SciWinFCmd.c 1.19 97/08/05 15:23:47
 */

#include <windows.h>
#include "dstrings.h"
#include <errno.h>
#ifdef __ABSC__
#include "errno.h"
#endif

/*
 * The following constants specify the type of callback when
 * TraverseWinTree() calls the traverseProc()
 */

#define DOTREE_PRED   1		/* pre-order directory  */
#define DOTREE_POSTD  2		/* post-order directory */
#define DOTREE_F      3		/* regular file */

extern int SciWinGetPlatformId ();
extern void SciWinConvertError (DWORD errCode);
extern void sciprint (char *fmt,...);
/*
 * Prototype for the TraverseWinTree callback function.
 */

typedef int (TraversalProc) (char *src, char *dst, DWORD attr, int type,
			     Sci_DString * errorPtr);

/*
 * Declarations for local procedures defined in this file:
 */


static int TraversalDelete (char *src, char *dst, DWORD attr,
			    int type, Sci_DString * errorPtr);
static int TraverseWinTree (TraversalProc * traverseProc,
			    Sci_DString * sourcePtr, Sci_DString * destPtr,
			    Sci_DString * errorPtr);

static int ScipRemoveDirectory (
				 char *path,	/* Pathname of directory to be removed. */
				 int recursive,		/* If non-zero, removes directories that
							 * are nonempty.  Otherwise, will only remove
							 * empty directories. */
				 Sci_DString * errorPtr);	/* If non-NULL, initialized DString for
								 * error reporting. */


/*
 *---------------------------------------------------------------------------
 *
 * SciCreateDirectory --
 *
 *      Creates the specified directory.  All parent directories of the
 *      specified directory must already exist.  The directory is
 *      automatically created with permissions so that user can access
 *      the new directory and create new files or subdirectories in it.
 *
 * Results:
 *      If the directory was successfully created, returns SCI_OK.
 *      Otherwise the return value is SCI_ERROR and errno is set to
 *      indicate the error.  Some possible values for errno are:
 *
 *      EACCES:     a parent directory can't be read and/or written.
 *      EEXIST:     path already exists.
 *      ENOENT:     a parent directory doesn't exist.
 *
 * Side effects:
 *      A directory is created.
 *
 *---------------------------------------------------------------------------
 */

int
SciCreateDirectory (
		     char *path)	/* Pathname of directory to create */
{
  int error;

  if (CreateDirectory (path, NULL) == 0)
    {
      error = GetLastError ();
      if (SciWinGetPlatformId () == VER_PLATFORM_WIN32s)
	{
	  if ((error == ERROR_ACCESS_DENIED)
	      && (GetFileAttributes (path) != (DWORD) - 1))
	    {
	      error = ERROR_FILE_EXISTS;
	    }
	}
      SciWinConvertError (error);
      if (errno == EEXIST)
	{
	  return SCI_OK;
	}
      sciprint ("Cannot create directory %s\r\n", path);
      return SCI_ERROR;
    }
  return SCI_OK;
}



/*
 *----------------------------------------------------------------------
 *
 * SciRemoveDirectory -- 
 *
 *      Removes directory (and its contents, if the recursive flag is set).
 *
 * Results:
 *      If the directory was successfully removed, returns SCI_OK.
 *      Otherwise the return value is SCI_ERROR, errno is set to indicate
 *      the error, and the pathname of the file that caused the error
 *      is stored in errorPtr.  Some possible values for errno are:
 *
 *      EACCES:     path directory can't be read and/or written.
 *      EEXIST:     path is a non-empty directory.
 *      EINVAL:     path is root directory or current directory.
 *      ENOENT:     path doesn't exist or is "".
 *      ENOTDIR:    path is not a directory.
 *
 *      EACCES:     path is a char device (nul:, com1:, etc.) (95)
 *      EINVAL:     path is a char device (nul:, com1:, etc.) (NT)
 *
 * Side effects:
 *      Directory removed.  If an error occurs, the error will be returned
 *      immediately, and remaining files will not be deleted.
 *
 *----------------------------------------------------------------------
 */

int 
SciRemoveDirectory (
		     char *path)	/* Pathname of directory to be removed. */
{
  Sci_DString errorPtr;
  Sci_DStringInit (&errorPtr);
  if (ScipRemoveDirectory (path, 1, &errorPtr) == SCI_ERROR)
    {
      sciprint ("Cannot remove directory %s\r\n", errorPtr.string);
    }
  Sci_DStringFree (&errorPtr);
  return 0;
}



/*
 *---------------------------------------------------------------------------
 *
 * ScipDeleteFile --
 *
 *      Removes a single file (not a directory).
 *
 * Results:
 *      If the file was successfully deleted, returns SCI_OK.  Otherwise
 *      the return value is SCI_ERROR and errno is set to indicate the
 *      error.  Some possible values for errno are:
 *
 *      EACCES:     a parent directory can't be read and/or written.
 *      EISDIR:     path is a directory.
 *      ENOENT:     path doesn't exist or is "".
 *
 *      EACCES:     exists an open file already referring to path.
 *      EACCES:     path is a char device (nul:, com1:, etc.)
 *
 * Side effects:
 *      The file is deleted, even if it is read-only.
 *
 *---------------------------------------------------------------------------
 */

int
ScipDeleteFile (
		 char *path)	/* Pathname of file to be removed. */
{
  DWORD attr;

  if (DeleteFile (path) != FALSE)
    {
      return SCI_OK;
    }
  SciWinConvertError (GetLastError ());
  if (path[0] == '\0')
    {
      /*
       * Win32s thinks that "" is the same as "." and then reports EISDIR
       * instead of ENOENT.
       */

      errno = ENOENT;
    }
  else if (errno == EACCES)
    {
      attr = GetFileAttributes (path);
      if (attr != (DWORD) - 1)
	{
	  if (attr & FILE_ATTRIBUTE_DIRECTORY)
	    {
	      /*
	       * Windows NT reports removing a directory as EACCES instead
	       * of EISDIR.
	       */

	      errno = EISDIR;
	    }
	  else if (attr & FILE_ATTRIBUTE_READONLY)
	    {
	      SetFileAttributes (path, attr & ~FILE_ATTRIBUTE_READONLY);
	      if (DeleteFile (path) != FALSE)
		{
		  return SCI_OK;
		}
	      SciWinConvertError (GetLastError ());
	      SetFileAttributes (path, attr);
	    }
	}
    }
  else if (errno == ENOENT)
    {
      attr = GetFileAttributes (path);
      if (attr != (DWORD) - 1)
	{
	  if (attr & FILE_ATTRIBUTE_DIRECTORY)
	    {
	      /*
	       * Windows 95 reports removing a directory as ENOENT instead 
	       * of EISDIR. 
	       */

	      errno = EISDIR;
	    }
	}
    }
  else if (errno == EINVAL)
    {
      /*
       * Windows NT reports removing a char device as EINVAL instead of
       * EACCES.
       */

      errno = EACCES;
    }

  return SCI_ERROR;
}

/*
 *----------------------------------------------------------------------
 *
 * ScipRemoveDirectory -- 
 *
 *      Removes directory (and its contents, if the recursive flag is set).
 *
 * Results:
 *      If the directory was successfully removed, returns SCI_OK.
 *      Otherwise the return value is SCI_ERROR, errno is set to indicate
 *      the error, and the pathname of the file that caused the error
 *      is stored in errorPtr.  Some possible values for errno are:
 *
 *      EACCES:     path directory can't be read and/or written.
 *      EEXIST:     path is a non-empty directory.
 *      EINVAL:     path is root directory or current directory.
 *      ENOENT:     path doesn't exist or is "".
 *      ENOTDIR:    path is not a directory.
 *
 *      EACCES:     path is a char device (nul:, com1:, etc.) (95)
 *      EINVAL:     path is a char device (nul:, com1:, etc.) (NT)
 *
 * Side effects:
 *      Directory removed.  If an error occurs, the error will be returned
 *      immediately, and remaining files will not be deleted.
 *
 *----------------------------------------------------------------------
 */

int
ScipRemoveDirectory (
		      char *path,	/* Pathname of directory to be removed. */
		      int recursive,	/* If non-zero, removes directories that
					 * are nonempty.  Otherwise, will only remove
					 * empty directories. */
		      Sci_DString * errorPtr)	/* If non-NULL, initialized DString for
						 * error reporting. */
{
  int result;
  Sci_DString buffer;
  DWORD attr;

  if (RemoveDirectory (path) != FALSE)
    {
      return SCI_OK;
    }
  SciWinConvertError (GetLastError ());
  if (path[0] == '\0')
    {
      /*
       * Win32s thinks that "" is the same as "." and then reports EACCES
       * instead of ENOENT.
       */

      errno = ENOENT;
    }
  if (errno == EACCES)
    {
      attr = GetFileAttributes (path);
      if (attr != (DWORD) - 1)
	{
	  if ((attr & FILE_ATTRIBUTE_DIRECTORY) == 0)
	    {
	      /* 
	       * Windows 95 reports calling RemoveDirectory on a file as an 
	       * EACCES, not an ENOTDIR.
	       */

	      errno = ENOTDIR;
	      goto end;
	    }

	  if (attr & FILE_ATTRIBUTE_READONLY)
	    {
	      attr &= ~FILE_ATTRIBUTE_READONLY;
	      if (SetFileAttributes (path, attr) == FALSE)
		{
		  goto end;
		}
	      if (RemoveDirectory (path) != FALSE)
		{
		  return SCI_OK;
		}
	      SciWinConvertError (GetLastError ());
	      SetFileAttributes (path, attr | FILE_ATTRIBUTE_READONLY);
	    }

	  /* 
	   * Windows 95 and Win32s report removing a non-empty directory 
	   * as EACCES, not EEXIST.  If the directory is not empty,
	   * change errno so caller knows what's going on.
	   */

	  if (SciWinGetPlatformId () != VER_PLATFORM_WIN32_NT)
	    {
	      HANDLE handle;
	      WIN32_FIND_DATA data;
	      Sci_DString buffer;
	      char *find;
	      int len;

	      Sci_DStringInit (&buffer);
	      find = Sci_DStringAppend (&buffer, path, -1);
	      len = Sci_DStringLength (&buffer);
	      if ((len > 0) && (find[len - 1] != '\\'))
		{
		  Sci_DStringAppend (&buffer, "\\", 1);
		}
	      find = Sci_DStringAppend (&buffer, "*.*", 3);
	      handle = FindFirstFile (find, &data);
	      if (handle != INVALID_HANDLE_VALUE)
		{
		  while (1)
		    {
		      if ((strcmp (data.cFileName, ".") != 0)
			  && (strcmp (data.cFileName, "..") != 0))
			{
			  /*
			   * Found something in this directory.
			   */

			  errno = EEXIST;
			  break;
			}
		      if (FindNextFile (handle, &data) == FALSE)
			{
			  break;
			}
		    }
		  FindClose (handle);
		}
	      Sci_DStringFree (&buffer);
	    }
	}
    }
  if (errno == ENOTEMPTY)
    {
      /* 
       * The caller depends on EEXIST to signify that the directory is
       * not empty, not ENOTEMPTY. 
       */

      errno = EEXIST;
    }
  if ((recursive != 0) && (errno == EEXIST))
    {
      /*
       * The directory is nonempty, but the recursive flag has been
       * specified, so we recursively remove all the files in the directory.
       */

      Sci_DStringInit (&buffer);
      Sci_DStringAppend (&buffer, path, -1);
      result = TraverseWinTree (TraversalDelete, &buffer, NULL, errorPtr);
      Sci_DStringFree (&buffer);
      return result;
    }

end:
  if (errorPtr != NULL)
    {
      Sci_DStringAppend (errorPtr, path, -1);
    }
  return SCI_ERROR;
}

/*
 *---------------------------------------------------------------------------
 *
 * TraverseWinTree --
 *
 *      Traverse directory tree specified by sourcePtr, calling the function 
 *      traverseProc for each file and directory encountered.  If destPtr 
 *      is non-null, each of name in the sourcePtr directory is appended to 
 *      the directory specified by destPtr and passed as the second argument 
 *      to traverseProc() .
 *
 * Results:
 *      Standard Sci result.
 *
 * Side effects:
 *      None caused by TraverseWinTree, however the user specified 
 *      traverseProc() may change state.  If an error occurs, the error will
 *      be returned immediately, and remaining files will not be processed.
 *
 *---------------------------------------------------------------------------
 */

static int
TraverseWinTree (
		  TraversalProc * traverseProc,		/* Function to call for every file and
							 * directory in source hierarchy. */
		  Sci_DString * sourcePtr,	/* Pathname of source directory to be
						 * traversed. */
		  Sci_DString * targetPtr,	/* Pathname of directory to traverse in
						 * parallel with source directory. */
		  Sci_DString * errorPtr)	/* If non-NULL, an initialized DString for
						 * error reporting. */
{
  DWORD sourceAttr;
  char *source, *target, *errfile;
  int result, sourceLen, targetLen, sourceLenOriginal, targetLenOriginal;
  HANDLE handle;
  WIN32_FIND_DATA data;

  result = SCI_OK;
  source = Sci_DStringValue (sourcePtr);
  sourceLenOriginal = Sci_DStringLength (sourcePtr);
  if (targetPtr != NULL)
    {
      target = Sci_DStringValue (targetPtr);
      targetLenOriginal = Sci_DStringLength (targetPtr);
    }
  else
    {
      target = NULL;
      targetLenOriginal = 0;
    }

  errfile = NULL;

  sourceAttr = GetFileAttributes (source);
  if (sourceAttr == (DWORD) - 1)
    {
      errfile = source;
      goto end;
    }
  if ((sourceAttr & FILE_ATTRIBUTE_DIRECTORY) == 0)
    {
      /*
       * Process the regular file
       */

      return (*traverseProc) (source, target, sourceAttr, DOTREE_F, errorPtr);
    }

  /*
   * When given the pathname of the form "c:\" (one that already ends
   * with a backslash), must make sure not to add another "\" to the end
   * otherwise it will try to access a network drive.  
   */

  sourceLen = sourceLenOriginal;
  if ((sourceLen > 0) && (source[sourceLen - 1] != '\\'))
    {
      Sci_DStringAppend (sourcePtr, "\\", 1);
      sourceLen++;
    }
  source = Sci_DStringAppend (sourcePtr, "*.*", 3);
  handle = FindFirstFile (source, &data);
  Sci_DStringSetLength (sourcePtr, sourceLen);
  if (handle == INVALID_HANDLE_VALUE)
    {
      /* 
       * Can't read directory
       */

      SciWinConvertError (GetLastError ());
      errfile = source;
      goto end;
    }

  result = (*traverseProc) (source, target, sourceAttr, DOTREE_PRED, errorPtr);
  if (result != SCI_OK)
    {
      FindClose (handle);
      return result;
    }

  if (targetPtr != NULL)
    {
      targetLen = targetLenOriginal;
      if ((targetLen > 0) && (target[targetLen - 1] != '\\'))
	{
	  target = Sci_DStringAppend (targetPtr, "\\", 1);
	  targetLen++;
	}
    }

  while (1)
    {
      if ((strcmp (data.cFileName, ".") != 0)
	  && (strcmp (data.cFileName, "..") != 0))
	{
	  /* 
	   * Append name after slash, and recurse on the file. 
	   */

	  Sci_DStringAppend (sourcePtr, data.cFileName, -1);
	  if (targetPtr != NULL)
	    {
	      Sci_DStringAppend (targetPtr, data.cFileName, -1);
	    }
	  result = TraverseWinTree (traverseProc, sourcePtr, targetPtr,
				    errorPtr);
	  if (result != SCI_OK)
	    {
	      break;
	    }

	  /*
	   * Remove name after slash.
	   */

	  Sci_DStringSetLength (sourcePtr, sourceLen);
	  if (targetPtr != NULL)
	    {
	      Sci_DStringSetLength (targetPtr, targetLen);
	    }
	}
      if (FindNextFile (handle, &data) == FALSE)
	{
	  break;
	}
    }
  FindClose (handle);

  /*
   * Strip off the trailing slash we added
   */

  Sci_DStringSetLength (sourcePtr, sourceLenOriginal);
  source = Sci_DStringValue (sourcePtr);
  if (targetPtr != NULL)
    {
      Sci_DStringSetLength (targetPtr, targetLenOriginal);
      target = Sci_DStringValue (targetPtr);
    }

  if (result == SCI_OK)
    {
      /*
       * Call traverseProc() on a directory after visiting all the
       * files in that directory.
       */

      result = (*traverseProc) (source, target, sourceAttr,
				DOTREE_POSTD, errorPtr);
    }
end:
  if (errfile != NULL)
    {
      SciWinConvertError (GetLastError ());
      if (errorPtr != NULL)
	{
	  Sci_DStringAppend (errorPtr, errfile, -1);
	}
      result = SCI_ERROR;
    }

  return result;
}

/*
 *----------------------------------------------------------------------
 *
 * TraversalDelete --
 *
 *      Called by procedure TraverseWinTree for every file and
 *      directory that it encounters in a directory hierarchy. This
 *      procedure unlinks files, and removes directories after all the
 *      containing files have been processed.
 *
 * Results:
 *      Standard Sci result.
 *
 * Side effects:
 *      Files or directory specified by src will be deleted. If an
 *      error occurs, the windows error is converted to a Posix error
 *      and errno is set accordingly.
 *
 *----------------------------------------------------------------------
 */

static int
TraversalDelete (
		  char *src,	/* Source pathname. */
		  char *ignore,	/* Destination pathname (not used). */
		  DWORD srcAttr,	/* File attributes for src (not used). */
		  int type,	/* Reason for call - see TraverseWinTree(). */
		  Sci_DString * errorPtr)	/* If non-NULL, initialized DString for
						 * error return. */
{
  switch (type)
    {
    case DOTREE_F:
      if (ScipDeleteFile (src) == SCI_OK)
	{
	  return SCI_OK;
	}
      break;

    case DOTREE_PRED:
      return SCI_OK;

    case DOTREE_POSTD:
      if (ScipRemoveDirectory (src, 0, NULL) == SCI_OK)
	{
	  return SCI_OK;
	}
      break;

    }

  if (errorPtr != NULL)
    {
      Sci_DStringAppend (errorPtr, src, -1);
    }
  return SCI_ERROR;
}