Skip to content

Commit

Permalink
Consistent code formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacob Garby committed Oct 3, 2024
1 parent 1cb3ded commit 10014d6
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 66 deletions.
1 change: 1 addition & 0 deletions c/.clang-format
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
---
BasedOnStyle: Google
IndentWidth: 4
AlignArrayOfStructures: Left
23 changes: 12 additions & 11 deletions c/client/arg.c
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
// vim: fdm=marker
#include "client.h"
#include <argp.h>
#include <stdlib.h>
#include <string.h>

void print_subcmds(char *prog) {/*{{{*/
#include "client.h"

void print_subcmds(char *prog) { /*{{{*/
fprintf(stderr, "Usage: %s [subcommand] <options>\n", prog);
fprintf(stderr, "Valid subcommands: [r]un, [t]ime, [q]ueue\n");
}/*}}}*/
} /*}}}*/

static unsigned int parse_duration(const char *s) {/*{{{*/
static unsigned int parse_duration(const char *s) { /*{{{*/
unsigned int secs = 0;
unsigned long acc;
char *endptr;
Expand Down Expand Up @@ -53,9 +54,9 @@ static unsigned int parse_duration(const char *s) {/*{{{*/
}

return secs;
}/*}}}*/
} /*}}}*/

error_t parser_run(int key, char *arg, struct argp_state *state){/*{{{*/
error_t parser_run(int key, char *arg, struct argp_state *state) { /*{{{*/
struct args_run *args = (struct args_run *)state->input;

switch (key) {
Expand Down Expand Up @@ -85,9 +86,9 @@ error_t parser_run(int key, char *arg, struct argp_state *state){/*{{{*/
}

return 0;
}/*}}}*/
} /*}}}*/

error_t parser_time(int key, char *arg, struct argp_state *state){/*{{{*/
error_t parser_time(int key, char *arg, struct argp_state *state) { /*{{{*/
struct args_time *args = (struct args_time *)state->input;

switch (key) {
Expand Down Expand Up @@ -117,8 +118,8 @@ error_t parser_time(int key, char *arg, struct argp_state *state){/*{{{*/
}

return 0;
}/*}}}*/
} /*}}}*/

error_t parser_queue(int key, char *arg, struct argp_state *state){/*{{{*/
error_t parser_queue(int key, char *arg, struct argp_state *state) { /*{{{*/
return 0;
}/*}}}*/
} /*}}}*/
34 changes: 12 additions & 22 deletions c/client/client.c
Original file line number Diff line number Diff line change
@@ -1,47 +1,38 @@
// vim: fdm=marker
#include "client.h"

#include <argp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <stdlib.h>
#include <unistd.h>
#include <argp.h>

int main(int argc, char **argv) {/*{{{*/
int main(int argc, char **argv) { /*{{{*/
if (argc < 2) {
print_subcmds(argv[0]);
return EXIT_FAILURE;
}

if (!strcmp(argv[1], "run") || !strcmp(argv[1], "r"))
{
if (!strcmp(argv[1], "run") || !strcmp(argv[1], "r")) {
return subcmd_run(argc, argv);
}
else if (!strcmp(argv[1], "time") || !strcmp(argv[1], "t"))
{
} else if (!strcmp(argv[1], "time") || !strcmp(argv[1], "t")) {
return subcmd_time(argc, argv);
}
else if (!strcmp(argv[1], "queue") || !strcmp(argv[1], "q"))
{
} else if (!strcmp(argv[1], "queue") || !strcmp(argv[1], "q")) {
return subcmd_queue(argc, argv);
}
else if (!strcmp(argv[1], "version") || !strcmp(argv[1], "v"))
{
printf("Resman client version "CLIENT_VER_STRING".\n");
}
else
{
} else if (!strcmp(argv[1], "version") || !strcmp(argv[1], "v")) {
printf("Resman client version " CLIENT_VER_STRING ".\n");
} else {
print_subcmds(argv[0]);
return EXIT_FAILURE;
}

return 0;
}/*}}}*/
} /*}}}*/

int connect_to_server(const char *addr) {/*{{{*/
int connect_to_server(const char *addr) { /*{{{*/
int soc;
struct sockaddr_un sa_server;
unsigned int sa_len;
Expand All @@ -59,5 +50,4 @@ int connect_to_server(const char *addr) {/*{{{*/
}

return soc;
}/*}}}*/

} /*}}}*/
22 changes: 10 additions & 12 deletions c/common/common.c
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
#include "resman.h"

#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

#include "resman.h"

const char *socket_addr = "/tmp/resman.sock";

void free_job_descriptor(job_descriptor *job) {
free(job->msg);
}
void free_job_descriptor(job_descriptor *job) { free(job->msg); }

int ser32(char *b, uint32_t x) {
for (int j = 3; j >= 0; j--) {
Expand Down Expand Up @@ -92,14 +90,14 @@ int deserialise_job(const char *buf, size_t len, job_descriptor *job) {
job->req_type = (enum job_type)buf[i++];

i += deser32(&buf[i], &job->uid);
i += deser64(&buf[i], (uint64_t*)&job->t_submitted);
i += deser64(&buf[i], (uint64_t*)&job->t_started);
i += deser64(&buf[i], (uint64_t*)&job->t_ended);
i += deser64(&buf[i], (uint64_t *)&job->t_submitted);
i += deser64(&buf[i], (uint64_t *)&job->t_started);
i += deser64(&buf[i], (uint64_t *)&job->t_ended);

if (job->req_type == JOB_CMD) {
i += deser32(&buf[i], (uint32_t*)&job->cmd.pid);
i += deser32(&buf[i], (uint32_t *)&job->cmd.pid);
} else if (job->req_type == JOB_TIMESLOT) {
i += deser32(&buf[i], (uint32_t*)&job->timeslot.secs);
i += deser32(&buf[i], (uint32_t *)&job->timeslot.secs);
} else {
return -1;
}
Expand Down
14 changes: 7 additions & 7 deletions c/include/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,24 @@ struct args_queue {
static struct argp_option options_run[] = {
{"msg", 'm', "MESSAGE", 0, "Description of your job.", 0},
{"verbose", 'V', 0, 0, "Give verbose output.", 0},
{0},
{0, 0, 0, 0, 0, 0},
};

static struct argp argp_run = {
options_run, &parser_run, "COMMAND", "Submits a job to resmand.",
NULL, NULL, NULL
};
NULL, NULL, NULL};

static struct argp_option options_time[] = {
{"msg", 'm', "MESSAGE", 0, "Explanation for your reservation.", 0},
{"verbose", 'V', 0, 0, "Give verbose output.", 0},
{0},
{0, 0, 0, 0, 0, 0},
};

static struct argp argp_time = {
options_time, &parser_time, "DURATION", "Reserves the server for some time.",
NULL, NULL, NULL
};
options_time, &parser_time,
"DURATION", "Reserves the server for some time.",
NULL, NULL,
NULL};

/*}}}*/

Expand Down
4 changes: 2 additions & 2 deletions c/include/resman.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
#ifndef RESMAN_H
#define RESMAN_H

#include <stdio.h> /* fopen(), printf() */
#include <sys/types.h> /* uid_t, pid_t */
#include <time.h> /* time() */
#include <stdio.h> /* fopen(), printf() */
#include <time.h> /* time() */

#define UNUSED __attribute__((unused))

Expand Down
22 changes: 10 additions & 12 deletions c/server/server.c
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
// vim: fdm=marker
#include "resman.h"

#include <signal.h> /* signal() */
#include <stdbool.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>

#include <unistd.h>
#include <stdlib.h>
#include <stdbool.h>

#include <signal.h> /* signal() */
#include "resman.h"

#define LISTEN_QUEUE 8

void sigint_handler(int sig UNUSED) {/*{{{*/
void sigint_handler(int sig UNUSED) { /*{{{*/
printf("Caught SIGINT: exiting.\n");
exit(EXIT_SUCCESS);
}/*}}}*/
} /*}}}*/

int main(void) {/*{{{*/
int main(void) { /*{{{*/
int soc_listen, soc_client;
struct sockaddr_un sa_local = {0};
struct sockaddr_un sa_client = {0};
Expand Down Expand Up @@ -61,13 +59,13 @@ int main(void) {/*{{{*/
printf("[info] Client connected.\n");

int bytes_read;
if ((bytes_read = recv(soc_client, read_buf,
JOB_SER_MAXLEN, 0)) == -1) {
if ((bytes_read = recv(soc_client, read_buf, JOB_SER_MAXLEN, 0)) ==
-1) {
fprintf(stderr, "Failed to read from client.\n");
}

printf("Read %d bytes: %s", bytes_read, read_buf);

close(soc_client);
}
}/*}}}*/
} /*}}}*/

0 comments on commit 10014d6

Please sign in to comment.