-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.c
191 lines (178 loc) · 4.72 KB
/
utilities.c
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
/*
* File: utilities.c
* Author: Jesus Wahrman 15-11540 , Neil Villamizar 15-11523
* Description: file that contains the implementation of some useful
* functions used in tar
* Date: 29 / 10 / 19
*/
#include "tar.h"
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
/*
* Function : int_to_char
* -----------------------------------------------------------
* stores the first byte of the int to the first position of the
* array, then right shifts the int bits by 8 and repeats the
* process now with the second position of the array, then for
* the third and finally for the fourth
*
* x : int to store
* ret : pointer to the array of chars
*/
void int_to_char(int x, char * ret){
int i;
for(i=0; i<4; i++){
ret[i] = (char) (x)&255;
x >>= 8;
}
}
/*
* Function : write_aux
* -----------------------------------------------------------
* makes calls to syscall read until all len is read or an
* error occurs
*
* fd : file descriptor of the file to write
* len : ammount of chars to write from buf
* buf : array of chars to write from
*
* returns 0 in case of success or -1 in case of failure
*/
int write_aux(int fd, int len, char * buf){
int e , len2 = 0;
while(len2 < len){
e = write(fd, buf + len2, len-len2);
if ( e <= 0 ) return -1;
len2 += e;
}
return 0;
}
/*
* Function : rotate_char
* -----------------------------------------------------------
* uses bit magic to rotate the bits easily to the left
*
* a : char to rotate the bits from
* desp : number of times to rotate
*
* returns a char with the rotated bits
*/
char rotate_char(char a, int desp){
a = ( (unsigned char)a << desp ) | ( (unsigned char)a >> 8 - desp );
return a;
}
/*
* Function : encode_string
* -----------------------------------------------------------
* makes calls to rotate_char for all the chars in the string,
* uses 8 - ( desp % 8 ) because its the same rotate to the left
* desp times than rotate to the right 8 - ( desp % 8 )
*
* s : array of chars to rotate
* len : size of the array
* desp : number of times to rotate
*/
void encode_string(char * s, int len, int desp){
int i;
for(i=0; i<len; i++) s[i] = rotate_char(s[i], 8 -(desp%8));
}
/*
* Function : decode_string
* -----------------------------------------------------------
* makes calls to rotate_char for all the chars in the string
*
* s : array of chars to rotate
* len : size of the array
* desp : number of times to rotate
*/
void decode_string(char * s, int len, int desp){
int i;
for(i=0; i<len; i++) s[i] = rotate_char(s[i], desp%8);
}
/*
* Function : make_path
* -----------------------------------------------------------
* given two arrays of chars, uses string.h basic functions to
* create a new string with the format "path/name"
*
* path : pointer to the path name
* name : pointer to the file name
* returns an array of chars of the form path/name
*/
char* make_path( char* path , char* name ){
char *ret =
(char *)malloc( strlen(path) + strlen(name) + 2);
if ( strlen(path) == 0 ){
strcpy( ret , name );
}
else{
strcpy( ret , path );
ret[strlen(path)] = '/';
strcpy(ret + strlen(path)+1, name);
}
return ret;
}
/*
* Function : read_aux
* -----------------------------------------------------------
* makes calls to syscall read untile l chars are read and
* stored in buf or an error occurs
*
* fd : file descriptor of the file to read from
* len : ammount of chars to read
* buf : array of chars to write
*
* returns 0 in case of success or -1 in case of failure
*/
int read_aux( int fd , char* buf , int l ){
int l2, e;
l2 = 0;
while ( l2 < l ){
e = read( fd , buf + l2 , l - l2 );
if ( e <= 0 ) return -1;
l2 = l2 + e;
}
return 0;
}
/*
* Function : str_to_int
* -----------------------------------------------------------
* saves the bits from each character in the int, to do so
* it saves the bits from the fourth char, then left shifts 8 bits
* and saves the bits from the third, then left shifts 8 bits and
* repeats for the second and first char
*
* c : pointer to the array of chars
*
* returns an int that has the bits of the array
*/
int str_to_int( char* c ){
int x, i;
x = 0;
for( i = 3 ; i >= 0 ; i-- ){
x = x << 8;
x = x | (unsigned char)(c[i]);
}
return x;
}
/*
* Function : str_cmp
* -----------------------------------------------------------
* function that compares the last name in the given path
* with the given name, using a string compare function
*
* path : char array that represents the path
* name : char array that represents the name
*
* returns 0 if the last name in the path is the same as name
*/
int str_cmp ( char * path , char * name ){
int i, j;
i = 0;
j = 0;
for( i = 0 ; i < strlen(path) ; i++){
if ( path[i] == '/' ) j = i;
}
return strcmp( path + j +1 , name );
}