| 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 | #include "extern.h" |
| 33 | |
| 34 | typedef struct se { |
| 35 | char *st_name; |
| 36 | uint32 st_nkey; |
| 37 | int st_count; |
| 38 | uint32 st_dkey; |
| 39 | uint32 st_key; |
| 40 | struct se *st_next; |
| 41 | } KSTATE, *KSTATEPTR; |
| 42 | |
| 43 | static KSTATEPTR _st_head = NIL(KSTATE)((KSTATE*)((void*)0)); |
| 44 | static KSTATEPTR _st_tail = NIL(KSTATE)((KSTATE*)((void*)0)); |
| 45 | static int _st_upd = FALSE0; |
| 46 | static char *_st_file = NIL(char)((char*)((void*)0)); |
| 47 | |
| 48 | static int _my_fgets ANSI((char *, int, FILE *))(char *, int, FILE *); |
| 49 | |
| 50 | PUBLIC void |
| 51 | Read_state() |
| 52 | { |
| 53 | char *buf; |
| 54 | char sizeb[20]; |
| 55 | FILE *fp; |
| 56 | KSTATEPTR sp; |
| 57 | |
| 58 | if( (fp = Search_file(".KEEP_STATE", &_st_file)) != NIL(FILE)((FILE*)((void*)0)) ) |
| |
| 59 | { |
| 60 | if( _my_fgets( sizeb, 20, fp ) ) |
| |
| 61 | { |
| 62 | int size = atol(sizeb); |
| 63 | buf = MALLOC(size+2, char)(char*) malloc((unsigned int)(size+2)*(size_t)sizeof(char)); |
| 64 | |
| 65 | while( _my_fgets(buf, size, fp) ) |
| 3 | | Loop condition is true. Entering loop body | |
|
| 9 | | Loop condition is true. Entering loop body | |
|
| 66 | { |
| 67 | TALLOC(sp, 1, KSTATE)if ((sp = (KSTATE*) calloc((unsigned int)(1), (size_t)sizeof( KSTATE))) == (KSTATE*)0) {No_ram();}; |
| 10 | | Within the expansion of the macro 'TALLOC':
| |
b | Assuming pointer value is null |
|
| 68 | sp->st_name = DmStrDup(buf); |
| 11 | | Access to field 'st_name' results in a dereference of a null pointer (loaded from variable 'sp') |
|
| 69 | (void) Hash(buf, &sp->st_nkey); |
| 70 | if( _my_fgets(buf, size, fp) ) |
| |
| 71 | sp->st_count = atoi(buf); |
| 72 | if( _my_fgets(buf, size, fp) ) |
| |
| 73 | sp->st_dkey = (uint32) atol(buf); |
| 74 | if( _my_fgets(buf, size, fp) ) |
| |
| 75 | sp->st_key = (uint32) atol(buf); |
| 76 | else { |
| 77 | FREE(sp)free((char*)(sp)); |
| 78 | break; |
| 79 | } |
| 80 | if( _st_head == NIL(KSTATE)((KSTATE*)((void*)0)) ) |
| 7 | | Assuming '_st_head' is not equal to null | |
|
| |
| 81 | _st_head = sp; |
| 82 | else |
| 83 | _st_tail->st_next = sp; |
| 84 | _st_tail = sp; |
| 85 | } |
| 86 | FREE(buf)free((char*)(buf)); |
| 87 | } |
| 88 | Closefile(); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | |
| 93 | PUBLIC void |
| 94 | Write_state() |
| 95 | { |
| 96 | static int in_write = 0; |
| 97 | register KSTATEPTR sp; |
| 98 | FILE *fp; |
| 99 | |
| 100 | if( !_st_upd || !_st_file || (_st_file && !*_st_file) || |
| 101 | Trace || in_write ) return; |
| 102 | |
| 103 | in_write++; |
| 104 | if( (fp = Openfile(_st_file, TRUE1, TRUE1)) != NIL(FILE)((FILE*)((void*)0)) ) { |
| 105 | int maxlen = 0; |
| 106 | int tmplen; |
| 107 | |
| 108 | for( sp = _st_head; sp; sp=sp->st_next ) |
| 109 | if( (tmplen = strlen(sp->st_name)+2) > maxlen ) |
| 110 | maxlen = tmplen; |
| 111 | |
| 112 | |
| 113 | if( maxlen < 20 ) maxlen = 20; |
| 114 | fprintf( fp, "%d\n", maxlen ); |
| 115 | |
| 116 | for( sp = _st_head; sp; sp=sp->st_next ) { |
| 117 | uint16 hv; |
| 118 | uint32 hk; |
| 119 | |
| 120 | if( Search_table(Defs, sp->st_name, &hv, &hk) ) { |
| 121 | fprintf( fp, "%s\n", sp->st_name ); |
| 122 | fprintf( fp, "%d\n", sp->st_count ); |
| 123 | |
| 124 | fprintf( fp, "%lu\n", (unsigned long)sp->st_dkey ); |
| 125 | fprintf( fp, "%lu\n", (unsigned long)sp->st_key ); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | Closefile(); |
| 130 | } |
| 131 | else |
| 132 | Fatal("Cannot open STATE file %s", _st_file); |
| 133 | |
| 134 | in_write = 0; |
| 135 | } |
| 136 | |
| 137 | |
| 138 | PUBLIC int |
| 139 | Check_state( cp, recipes, maxrcp ) |
| 140 | CELLPTR cp; |
| 141 | STRINGPTR *recipes; |
| 142 | int maxrcp; |
| 143 | { |
| 144 | KSTATEPTR st; |
| 145 | STRINGPTR sp; |
| 146 | int i; |
| 147 | uint32 thkey; |
| 148 | uint32 hkey; |
| 149 | uint32 nkey; |
| 150 | uint32 dkey; |
| 151 | int update = FALSE0; |
| 152 | |
| 153 | if( !_st_file || (_st_file && !*_st_file) || Trace ) |
| 154 | return(FALSE0); |
| 155 | |
| 156 | if( strcmp(cp->CE_NAMEce_name->ht_name,".REMOVE") == 0 |
| 157 | || (cp->ce_attr & (A_PHONY0x04000|A_NOSTATE0x08000)) ) |
| 158 | return(FALSE0); |
| 159 | |
| 160 | (void) Hash( cp->CE_NAMEce_name->ht_name, &nkey ); thkey = nkey + (uint32) cp->ce_count; |
| 161 | (void) Hash( Pwd, &dkey ); thkey += dkey; |
| 162 | |
| 163 | Suppress_temp_file = TRUE1; |
| 164 | for( i=0 ; i<maxrcp; i++ ) |
| 165 | for(sp=recipes[i]; sp != NIL(STRING)((STRING*)((void*)0)); sp=sp->st_next ) { |
| 166 | CELLPTR svct = Current_target; |
| 167 | char *cmnd; |
| 168 | t_attr silent = (Glob_attr & A_SILENT0x00002); |
| 169 | |
| 170 | Current_target = cp; |
| 171 | Glob_attr |= A_SILENT0x00002; |
| 172 | cmnd = Expand(sp->st_string); |
| 173 | Glob_attr = (Glob_attr & ~A_SILENT0x00002)|silent; |
| 174 | Current_target = svct; |
| 175 | |
| 176 | (void) Hash(cmnd, &hkey); thkey += hkey; |
| 177 | FREE(cmnd)free((char*)(cmnd)); |
| 178 | } |
| 179 | Suppress_temp_file = FALSE0; |
| 180 | |
| 181 | for( st=_st_head; st != NIL(KSTATE)((KSTATE*)((void*)0)); st=st->st_next ) { |
| 182 | if( st->st_nkey == nkey |
| 183 | && st->st_dkey == dkey |
| 184 | && st->st_count == cp->ce_count |
| 185 | && !strcmp(cp->CE_NAMEce_name->ht_name, st->st_name) ) |
| 186 | break; |
| 187 | } |
| 188 | |
| 189 | if( st == NIL(KSTATE)((KSTATE*)((void*)0)) ) { |
| 190 | KSTATEPTR nst; |
| 191 | |
| 192 | TALLOC(nst, 1, KSTATE)if ((nst = (KSTATE*) calloc((unsigned int)(1), (size_t)sizeof (KSTATE))) == (KSTATE*)0) {No_ram();}; |
| 193 | nst->st_name = cp->CE_NAMEce_name->ht_name; |
| 194 | nst->st_nkey = nkey; |
| 195 | nst->st_dkey = dkey; |
| 196 | nst->st_key = thkey; |
| 197 | nst->st_count = cp->ce_count; |
| 198 | |
| 199 | if( _st_head == NIL(KSTATE)((KSTATE*)((void*)0)) ) |
| 200 | _st_head = nst; |
| 201 | else |
| 202 | _st_tail->st_next = nst; |
| 203 | |
| 204 | _st_tail = nst; |
| 205 | _st_upd = TRUE1; |
| 206 | } |
| 207 | else if( st->st_key != thkey ) { |
| 208 | st->st_key = thkey; |
| 209 | _st_upd = update = TRUE1; |
| 210 | } |
| 211 | |
| 212 | return(st != NIL(KSTATE)((KSTATE*)((void*)0)) && update); |
| 213 | } |
| 214 | |
| 215 | |
| 216 | static int |
| 217 | _my_fgets(buf, size, fp) |
| 218 | char *buf; |
| 219 | int size; |
| 220 | FILE *fp; |
| 221 | { |
| 222 | char *p; |
| 223 | |
| 224 | if( fgets(buf, size, fp) == NULL((void*)0) ) return(0); |
| 225 | |
| 226 | if( (p=strrchr(buf,'\n')) != NIL(char)((char*)((void*)0)) ) *p='\0'; |
| 227 | if( (p=strrchr(buf,'\r')) != NIL(char)((char*)((void*)0)) ) *p='\0'; |
| 228 | return(1); |
| 229 | } |