Add shared binary

This commit is contained in:
Melon 2021-04-24 11:19:44 +01:00
parent 9b4e4f7b64
commit 1394cd7204
4 changed files with 207 additions and 0 deletions

View File

@ -0,0 +1,68 @@
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include "config_file.h"
/*
* Example usage
FILE *fcr;
fcr = fopen("./test-read.cfg", "r");
char *ckey;
char *cval;
while(config_read(fcr, &ckey, &cval)) {
fprintf(stderr, "key: %s\nval: %s\n\n",ckey,cval);
}
fclose(fcr);
*/
keyvaluepair_t config_read(FILE *fp) {
keyvaluepair_t result;
result.key = "";
result.value = "";
result.loop = false;
// If the input file doesn't exist then just exit
if (fp == NULL) {
fprintf(stderr, "Can't read from a non-existant file");
return result;
}
// Max line length is 250
int MAX_LEN = 250;
char buffer[MAX_LEN];
// Grab a line but if there aren't any return false
if(!fgets(buffer, MAX_LEN - 1, fp)) return result;
// Find the newline and remove it
buffer[strcspn(buffer, "\n")] = 0;
// Get the char at " = " and calculate the offset
char *midchar = strstr(buffer, " = ");
int midpoint = midchar - buffer;
// Make a char array big enough for the key and copy the key into it
char keytoken[midpoint+1];
strncpy(keytoken, buffer, midpoint);
keytoken[midpoint]=0;
// Make a char array big enough for the rest and copy from 3 chars after midchar
char valtoken[MAX_LEN-midpoint+1];
strcpy(valtoken, buffer+midpoint+3);
valtoken[MAX_LEN-midpoint]=0;
// Return them via the char array pointers
result.key = malloc(midpoint+1);
result.value = malloc(MAX_LEN-midpoint+1);
strcpy(result.key, keytoken);
strcpy(result.value, valtoken);
result.loop = true;
return result;
}

View File

@ -0,0 +1,46 @@
#include <stdio.h>
#include "config_file.h"
/*
* Example usage
FILE *fcw;
fcw = fopen("./test-write.cfg", "w");
int count = 5;
char *testkeys[] = {
"test1",
"bb",
"key",
"value",
"idk"
};
char *testvalues[] = {
"test2",
"dd",
"to",
"pair",
"maybe"
};
for(int i=0; i<count; i++) {
config_write(fcw, testkeys[i], testvalues[i]);
}
fclose(fcw);
*/
void config_write(FILE *fp, char *key, char *value) {
if (fp == NULL) {
fprintf(stderr, "Can't write to a non-existant file");
key = NULL;
value = NULL;
return;
}
fprintf(fp, "%s = %s\n", key, value);
fprintf(stderr, "wrote: %s = %s\n", key, value);
}

View File

@ -0,0 +1,36 @@
#include "unix_sockets.h"
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int unix_socket_client_start(char *sock_path) {
struct sockaddr_un addr;
// Create a new client socket with domain: AF_UNIX, type: SOCK_STREAM, protocol: 0
int sfd = socket(AF_UNIX, SOCK_STREAM, 0);
// Make sure socket's file descriptor is legit.
if (sfd == -1) {
fprintf(stderr,"ERROR: socket is not legit");
return -1;
}
//
// Construct server address, and make the connection.
//
memset(&addr, 0, sizeof(struct sockaddr_un));
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, sock_path, sizeof(addr.sun_path) - 1);
// Connects the active socket referred to be sfd to the listening socket
// whose address is specified by addr.
if (connect(sfd, (struct sockaddr *) &addr, sizeof(struct sockaddr_un)) == -1) {
fprintf(stderr,"Failed to connect");
return -1;
}
return sfd;
}

View File

@ -0,0 +1,57 @@
#include "unix_sockets.h"
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int unix_socket_server_start(char *sock_path) {
struct sockaddr_un addr;
// Create a new server socket with domain: AF_UNIX, type: SOCK_STREAM, protocol: 0
int sfd = socket(AF_UNIX, SOCK_STREAM, 0);
printf("Server socket fd = %d\n", sfd);
// Make sure socket's file descriptor is legit.
if (sfd == -1) {
fprintf(stderr,"ERROR: socket is not legit");
return -1;
}
// Make sure the address we're planning to use isn't too long.
if (strlen(sock_path) > sizeof(addr.sun_path) - 1) {
fprintf(stderr,"Server socket path too long: %s",sock_path);
return -1;
}
// Delete any file that already exists at the address. Make sure the deletion
// succeeds. If the error is just that the file/directory doesn't exist, it's fine.
if (remove(sock_path) == -1 && errno != ENOENT) {
fprintf(stderr,"remove-%s",sock_path);
return -1;
}
// Zero out the address, and set family and path.
memset(&addr, 0, sizeof(struct sockaddr_un));
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, sock_path, sizeof(addr.sun_path) - 1);
// Bind the socket to the address. Note that we're binding the server socket
// to a well-known address so that clients know where to connect.
if (bind(sfd, (struct sockaddr *) &addr, sizeof(struct sockaddr_un)) == -1) {
fprintf(stderr,"Failed to bind");
return -1;
}
// The listen call marks the socket as *passive*. The socket will subsequently
// be used to accept connections from *active* sockets.
// listen cannot be called on a connected socket (a socket on which a connect()
// has been succesfully performed or a socket returned by a call to accept()).
if (listen(sfd, BACKLOG) == -1) {
fprintf(stderr,"Failed to listen");
return -1;
}
return sfd;
}