Reformat codebase
This commit is contained in:
parent
3354826dec
commit
a8757731b3
1 changed files with 253 additions and 266 deletions
39
netcalc.c
39
netcalc.c
|
@ -16,16 +16,14 @@ int SOCKFD;
|
|||
|
||||
int CLIENT;
|
||||
|
||||
void cleanup(int signum)
|
||||
{
|
||||
void cleanup(int signum) {
|
||||
if (close(SOCKFD) != 0) {
|
||||
perror("close");
|
||||
}
|
||||
exit(1);
|
||||
}
|
||||
|
||||
int prepaddr(char *host, char *port, struct addrinfo **ainfo, int afamily, int flags)
|
||||
{
|
||||
int prepaddr(char *host, char *port, struct addrinfo **ainfo, int afamily, int flags) {
|
||||
struct addrinfo hints; // hints to what we want
|
||||
memset(&hints, 0, sizeof hints);
|
||||
hints.ai_family = afamily; // AF_INET or AF_INET6 to force version
|
||||
|
@ -41,8 +39,7 @@ int prepaddr(char *host, char *port, struct addrinfo **ainfo, int afamily, int f
|
|||
return 0;
|
||||
}
|
||||
|
||||
int bindsocket(int sockd, struct addrinfo *ainfo)
|
||||
{
|
||||
int bindsocket(int sockd, struct addrinfo *ainfo) {
|
||||
if (bind(SOCKFD, ainfo->ai_addr, ainfo->ai_addrlen) != 0) {
|
||||
perror("bind");
|
||||
cleanup(0);
|
||||
|
@ -57,8 +54,7 @@ int bindsocket(int sockd, struct addrinfo *ainfo)
|
|||
return sockd;
|
||||
}
|
||||
|
||||
int prepsocket(struct addrinfo *ainfo)
|
||||
{
|
||||
int prepsocket(struct addrinfo *ainfo) {
|
||||
SOCKFD = socket(ainfo->ai_family, ainfo->ai_socktype, ainfo->ai_protocol);
|
||||
|
||||
if (SOCKFD == -1) {
|
||||
|
@ -76,8 +72,7 @@ int prepsocket(struct addrinfo *ainfo)
|
|||
return SOCKFD;
|
||||
}
|
||||
|
||||
int calc(unsigned int num1, unsigned int num2, char op, unsigned int *result)
|
||||
{
|
||||
int calc(unsigned int num1, unsigned int num2, char op, unsigned int *result) {
|
||||
switch (op) {
|
||||
case '+':
|
||||
if (__builtin_uadd_overflow(num1, num2, result)) {
|
||||
|
@ -106,8 +101,7 @@ int calc(unsigned int num1, unsigned int num2, char op, unsigned int *result)
|
|||
return 0;
|
||||
}
|
||||
|
||||
int base(char *numstr, unsigned int *num)
|
||||
{
|
||||
int base(char *numstr, unsigned int *num) {
|
||||
short base = 10;
|
||||
|
||||
size_t len = strlen(numstr);
|
||||
|
@ -126,8 +120,7 @@ int base(char *numstr, unsigned int *num)
|
|||
}
|
||||
}
|
||||
|
||||
int parse(char *buf, size_t buflen, unsigned int *first, unsigned int *second, char *op)
|
||||
{
|
||||
int parse(char *buf, size_t buflen, unsigned int *first, unsigned int *second, char *op) {
|
||||
char *num1str = NULL;
|
||||
char *num2str = NULL;
|
||||
char *opstr = NULL;
|
||||
|
@ -156,15 +149,13 @@ int parse(char *buf, size_t buflen, unsigned int *first, unsigned int *second, c
|
|||
return status;
|
||||
}
|
||||
|
||||
void report_error(char *buf, unsigned long buflen, char *msg)
|
||||
{
|
||||
void report_error(char *buf, unsigned long buflen, char *msg) {
|
||||
fprintf(stderr, "%s\n", msg);
|
||||
memset(buf, 0, buflen);
|
||||
sprintf(buf, "%s\n", msg);
|
||||
}
|
||||
|
||||
void bstr(unsigned int n, char **out)
|
||||
{
|
||||
void bstr(unsigned int n, char **out) {
|
||||
// voodoo
|
||||
int msb = 32 - __builtin_clz(n);
|
||||
(*out) = (char*) calloc(msb+1, sizeof (char));
|
||||
|
@ -175,8 +166,7 @@ void bstr(unsigned int n, char **out)
|
|||
}
|
||||
}
|
||||
|
||||
int server()
|
||||
{
|
||||
int server() {
|
||||
struct sockaddr_storage c_addr;
|
||||
socklen_t sin_size = sizeof c_addr;
|
||||
while (1) {
|
||||
|
@ -222,8 +212,7 @@ int server()
|
|||
}
|
||||
}
|
||||
|
||||
int connectclient(struct addrinfo *ainfo)
|
||||
{
|
||||
int connectclient(struct addrinfo *ainfo) {
|
||||
if (connect(SOCKFD, ainfo->ai_addr, ainfo->ai_addrlen) == -1) {
|
||||
perror("connect");
|
||||
return -1;
|
||||
|
@ -231,8 +220,7 @@ int connectclient(struct addrinfo *ainfo)
|
|||
return 0;
|
||||
}
|
||||
|
||||
int client()
|
||||
{
|
||||
int client() {
|
||||
char buf[BUFLEN];
|
||||
size_t nullsize = 0;
|
||||
char *line = NULL;
|
||||
|
@ -273,8 +261,7 @@ int client()
|
|||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int main(int argc, char *argv[]) {
|
||||
SOCKFD = 0;
|
||||
struct sigaction action;
|
||||
memset(&action, 0, sizeof(struct sigaction));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue