File: link.c

package info (click to toggle)
cpbk 2.0-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 248 kB
  • ctags: 168
  • sloc: ansic: 2,193; makefile: 114; sh: 35
file content (179 lines) | stat: -rw-r--r-- 4,246 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
/*****************************************************************************
 *  Backup Copy                                                              *
 *  Programmed by: Kevin Lindsay                                             *
 *  Copyright (c) 1998 NetNation Communications Inc                          *
 * ***************************************************************************/ 

#include <stdio.h>
#include <stdlib.h>

#include "link.h"
#include "statmalloc.h"

/* Initialize 2D List */

dir_item *
init_2D_list()
{
   dir_item *listptr;
   
   if ((listptr = (dir_item *)statmalloc(sizeof(dir_item))) == NULL) {
      fprintf(stderr,"Could not allocate memory for init_2D_list!\n");
   }
   
   listptr->name = NULL;
   listptr->up = NULL;
   listptr->down = NULL;
   listptr->left = NULL;
   listptr->right = NULL;
   listptr->dir_info = NULL;
   listptr->empty = 0;
   listptr->mod_time = 0;
   listptr->fsize = 0;
   
   return(listptr);
}

/* Initialize Dest Item List */

dest_item *
init_dest_list()
{
   dest_item *destptr;
   
   if ((destptr = (dest_item *)statmalloc(sizeof(dest_item))) == NULL) {
      fprintf(stderr,"Could not allocate memory for init_dest_list!\n");
   }
   
   destptr->name = NULL;
   destptr->prev = NULL;
   destptr->next = NULL;
   destptr->name_len = 0;
   destptr->type = 0;
   destptr->delete = 1;
   destptr->mod_time = 0;
   destptr->fsize = 0;
   
   return(destptr);
}

/* Add Next to Dest List */

dest_item *
add_next(dest_item *destptr)
{
   
   if ((destptr->next = (dest_item *)statmalloc(sizeof(dest_item))) == NULL) {
      fprintf(stderr,"Could not allocate memory for Add Next!\n");
   }
   
   destptr->next->prev = destptr;
   destptr->next->next = NULL;
   destptr->next->name = NULL;
   destptr->next->name_len = 0;
   destptr->next->type = 0;
   destptr->next->delete = 1;
   destptr->next->mod_time = 0;
   destptr->next->fsize = 0;   
   
   return(destptr);
}

/* Add Right to 2D List */

dir_item *
add_right(dir_item *listptr)
{
   
   if ((listptr->right = (dir_item *)statmalloc(sizeof(dir_item))) == NULL) {
      fprintf(stderr,"Could not allocate memory for add_right!\n");
   }
   
   listptr->right->left = listptr;   
   listptr->right->up = NULL;
   listptr->right->down = NULL;
   listptr->right->right = NULL;
   listptr->right->name = NULL;
   listptr->right->dir_info = NULL;
   listptr->right->empty=0;
   listptr->right->name_len = 0;
   listptr->right->type = 0;
   listptr->right->mod_time = 0;
   listptr->right->fsize = 0;   
   
   return(listptr);
}

/* Add Down to 2D List */

dir_item *
add_down(dir_item *listptr)
{
   if ((listptr->down = (dir_item *)statmalloc(sizeof(dir_item))) == NULL) {
      fprintf(stderr,"Could not allocate memory for add_down!\n");      
   }
   
   listptr->down->up = listptr;
   listptr->down->left = listptr->left;
   listptr->down->down = NULL;
   listptr->down->right = NULL;
   listptr->down->name = NULL;   
   listptr->down->dir_info = NULL;
   listptr->down->empty = 0;
   listptr->down->name_len = 0;
   listptr->down->type = 0;   
   listptr->down->mod_time = 0;
   listptr->down->fsize = 0;   
   
   return(listptr);   
}

/* Free Dest Item List */
dir_item *
free_dirinfo(dir_item *curptr)
{
   dest_item *destptr;
   
   destptr = curptr->dir_info;
   
   while (destptr->next != NULL)
       destptr = destptr->next;
   
   while (destptr->prev != NULL) {
      destptr = destptr->prev;
      freemalloc(destptr->next->name);
      freemalloc(destptr->next);
   }
   freemalloc(destptr->name);
   freemalloc(destptr);
   
   return(curptr);
}

/* Free 2D List Right */

dir_item *
free_right(dir_item *listptr)
{
   if (listptr->right != NULL) {
      listptr = listptr->right;
      
      while (listptr->down != NULL)
	listptr = listptr->down;
      
      while (listptr->up != NULL) {
         listptr = listptr->up;
         if (listptr->down->dir_info != NULL) {
            listptr->down = free_dirinfo(listptr->down);
         }
         freemalloc(listptr->down->name);         
         freemalloc(listptr->down);
      }
      listptr = listptr->left;
      freemalloc(listptr->right->name);
      freemalloc(listptr->right);
      listptr->right = NULL;
   }
   return(listptr);
}