-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.c
More file actions
115 lines (99 loc) · 2.74 KB
/
utils.c
File metadata and controls
115 lines (99 loc) · 2.74 KB
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
// File: utils.c
// Created April 7, 2014
// Michael Baptist - mbaptist@ucsc.edu
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdarg.h>
#include <dirent.h>
#include <sys/stat.h>
// comment this out to turn on debug prints.
//#define NDEBUG NDEBUG
#include "utils.h"
#define SUCCESS 0
#define FAILURE 1
void check_socket(int fd) {
if (fd < 0) {
perror("Error: Socket Corrupt ");
exit(errno);
} else {
DEBUGF("Client Socket: %d In Use\n", fd);
}
}
void check_connection(int x) {
if (x < 0) {
perror("Error: Connection Failed ");
exit(errno);
} else {
DEBUGF("Connection Established\n");
DEBUGF("Server Socket %d Connected\n", x);
}
}
FILE *retrieve_file(const char *restrict filename, const char* restrict mode) {
DIR *d = NULL;
struct dirent *dir = NULL;
d = opendir(".");
if (d) {
while ((dir = readdir(d)) != NULL) {
if (strcmp(dir->d_name, filename) == 0) {
FILE *file = fopen(filename, mode);
closedir(d);
return file;
}
}
fprintf(stderr, "Error: no file matching %s exists in current directory.\n", filename);
return NULL;
}
fprintf(stderr, "Error: no directory matching . exists.\n");
return NULL;
}
int get_file_size(FILE *restrict file) {
fseek(file, 0, SEEK_END); // seek to end of file
int size = (int)ftell(file); // get current file pointer
fseek(file, 0, SEEK_SET); // seek back to beginning of file
return size;
}
unsigned char *serialize_int(unsigned char *buffer, unsigned int val) {
unsigned int size = sizeof(unsigned int);
for (unsigned int i = 0; i < size; ++i) {
buffer[i] = val >> (8*(size-i-1));
}
return buffer + size;
}
unsigned char *serialize_data(unsigned char *buffer, char buf[], int len) {
for (int i = 0; i < len; ++i) {
buffer[i] = (unsigned char)buf[i];
}
return buffer + len;
}
unsigned char *deserialize_int(unsigned char *buffer, unsigned int *val) {
unsigned int size = sizeof(unsigned int);
*val = 0;
for (unsigned int i = 0; i < size; ++i) {
unsigned int x = (unsigned int)buffer[i];
*val +=(x << (8*(size-i-1)));
}
return buffer + size;
}
unsigned char *deserialize_data(unsigned char *buffer, char buf[], int len) {
for (int i = 0; i < len; ++i) {
buf[i] = (char)buffer[i];
}
return buffer + len;
}
void null_array(char *array[], int len) {
for (int i = 0; i < len; ++i) {
array[i] = NULL;
}
}
void debugprintf(char *format, ...) {
va_list args;
fflush (NULL);
va_start (args, format);
fprintf (stdout, "DEBUG: ");
vfprintf (stdout, format, args);
va_end (args);
fflush (NULL);
}