branch update for HEAD-2003021201
[reactos.git] / lib / msvcrt / stdio / rmtmp.c
1 /*
2  * COPYRIGHT:       See COPYING in the top level directory
3  * PROJECT:         ReactOS system libraries
4  * FILE:            lib/msvcrt/stdio/rmtmp.c
5  * PURPOSE:         remove temporary files in current directory
6  * PROGRAMMER:      Boudewijn ( ariadne@xs4all.nl)
7  * UPDATE HISTORY:
8  *                  Created 19/01/99
9  * NOTE             Not tested.
10  */
11
12 #include <msvcrt/stdio.h>
13 #include <msvcrt/string.h>
14 #include <msvcrt/internal/file.h>
15
16 #ifndef F_OK
17  #define F_OK   0x01
18 #endif
19 #ifndef R_OK
20  #define R_OK   0x02
21 #endif
22 #ifndef W_OK
23  #define W_OK   0x04
24 #endif
25 #ifndef X_OK
26  #define X_OK   0x08
27 #endif
28 #ifndef D_OK
29  #define D_OK   0x10
30 #endif
31
32 // should be replace by a closure of the tmp files
33 extern __file_rec *__file_rec_list;
34
35 int _rmtmp( void )
36 {
37 /*
38 loop files and check for name_to_remove 
39 */
40   __file_rec *fr = __file_rec_list;
41   __file_rec **last_fr = &__file_rec_list;
42   
43   int total_closed = 0;
44   int i = 0;
45   char temp_name[260];
46
47   /* Try to find an empty slot */
48   while (fr)
49   {
50     last_fr = &(fr->next);
51
52     /* If one of the existing slots is available, return it */
53     for (i=0; i<fr->count; i++) {
54       if (fr->files[i]->_name_to_remove != NULL) {
55                 if ( _access(fr->files[i]->_name_to_remove,W_OK) ) {
56                         strcpy(temp_name,fr->files[i]->_name_to_remove);
57                         fclose(fr->files[i]);
58                         remove(temp_name);
59                         total_closed++;
60                 }
61           }
62     }
63
64     /* If this one is full, go to the next */
65     if (fr->count == __FILE_REC_MAX)
66       fr = fr->next;
67     else
68       /* it isn't full, we can add to it */
69       break;
70   }
71   return total_closed; 
72 }