Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #3

Merged
merged 12 commits into from
Apr 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#.gitignore
*.swp
.deps

# automake
Makefile.in
Makefile

# autoconf
/aclocal.m4
/compile
/config/**
!/config/install-sh
/config.*
/configure
/configure.in
/depcomp
/install-sh
/missing
/stamp-h1

# m4
autom4te.cache

*.out
3 changes: 3 additions & 0 deletions AUTHORS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Authors

[Kurt L. Manion](//github.com/klmanion)
8 changes: 8 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
## Makefile.am

SUBDIRS = src
dist_doc_DATA = README.md AUTHORS.md

EXTRA_DIST = \
autogen.sh \
$(NULL)
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ This is a reimplementation of uuencoding done personally for educational reasons
The idea for this challenge came from r/dailyprogrammer.

This was actualy quite a fun project to undertake because it was my first multi-threaded program.

## Compilation
Run ./autogen.sh in your shell, and omit the install step.
7 changes: 7 additions & 0 deletions autogen.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/sh
#autogen.sh

mkdir -p 'm4' || exit 1
autoreconf --force --install || exit 1
printf 'autotools generated; now run:\n\t./configure\n\tmake\n\tmake install\n'
exit 0;
23 changes: 23 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
## configure.ac

AC_PREREQ([2.69])
AC_INIT([uuenc], [2.0.1], [klmanion@gmail.com])
AC_CONFIG_AUX_DIR([config])

AM_INIT_AUTOMAKE([-Wall -Werror foreign])

AC_CONFIG_HEADERS([config.h])
AC_CONFIG_MACRO_DIR([m4])

AC_PROG_CC
AC_LANG_C

AC_FUNC_MALLOC

AC_CONFIG_FILES([
Makefile
src/Makefile
])

AC_OUTPUT

9 changes: 9 additions & 0 deletions src/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#src/.gitignore

*.out
coded.txt
input.txt
*.gch

*.[oa]
uuenc
4 changes: 4 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## src/Makefile.am

bin_PROGRAMS = uuenc
uuenc_SOURCES = main.c encode.c decode.c enclist.c bitconv.c cat.c
Binary file removed src/a.out
Binary file not shown.
85 changes: 85 additions & 0 deletions src/bitconv.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
//bitconv.c
//

#include <stdio.h>
#include <string.h>

#include "bitconv.h"

/* calculates how many bits the array resulting from stobin will have
* makeing sure that it has padding bits to make it a multiple of 6 */
size_t
stobin_size(
const char *s)
{
size_t l;
l = (strlen(s))*8;
//add padding bits
if (l%6 != 0)
l += 6 - (l%6);
return l;
}

/* converts an array of chars to an array of 1s and 0s
* skips the ending newline
* returned value must be freed */
uint8_t*
stobin(
const char *s)
{
uint8_t *a;
size_t a_len;
static const int mask[] = { 0x1, 0x2, 0x4, 0x8,
0x10, 0x20, 0x40, 0x80 };
a_len = stobin_size(s);
a = (uint8_t *)malloc(a_len * sizeof(uint8_t));
memset(a, 0, a_len);
for(size_t i=0,j=0,len=strlen(s); i<len; ++i) {
//for each character to be encoded
for(int k=7; k>=0; --k,++j) {
//for each bit in that character
a[j] = (s[i] & mask[k]) >> k;
}
}
return a;
}

/* takes first 6 elements from uint8_t array and considering them as bits
* converts them to their integer equivalent */
uint8_t
bitstoc(
uint8_t *a)
{
uint8_t acc=0;
for(int i=0; i<6; ++i)
{
acc += a[i] << ((6-1)-i);
}
return acc;
}

/*
* reads at least 45 characters from the file
* seeking forward
* !!! this dose not capture the newline
* returns EOF on EOF
* returns 0 otherwise
*/
int
readline(
char **line,
FILE *fd)
{
char *x;
*line = (char *)malloc(47*sizeof(char));
(void)fgets(*line, 46, fd);
//strip newline
x = strchr(*line, '\n');
if (x != NULL)
*x = '\0';
if (feof(fd)) {
return EOF;
} else {
return 0;
}
}
19 changes: 19 additions & 0 deletions src/bitconv.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//bitconv.h
//

#ifndef __BITCONV_H__
#define __BITCONV_H__

#include <stdlib.h>

__BEGIN_DECLS
size_t stobin_size __P((const char *));
uint8_t* stobin __P((const char *));
uint8_t bitstoc __P((uint8_t *));

int readline __P((char **,FILE *));
__END_DECLS

#endif /* !__BITCONV_H__ */

/* vim: set ts=4 sw=4 noexpandtab tw=79: */
17 changes: 17 additions & 0 deletions src/cat.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//cat.c
//

#include <stdio.h>

#include "cat.h"

int
cat(
const char *file)
{
char cmd[80];
snprintf(cmd, 79, "cat %s", file);
return system(cmd);
}

/* vim: set ts=4 sw=4 noexpandtab tw=79: */
15 changes: 15 additions & 0 deletions src/cat.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//cat.h
//

#ifndef __CAT_H__
#define __CAT_H__

#include <stdlib.h>

__BEGIN_DECLS
int cat __P((const char *));
__END_DECLS

#endif /* !__CAT_H__ */

/* vim: set ts=4 sw=4 noexpandtab tw=79: */
11 changes: 0 additions & 11 deletions src/coded.txt

This file was deleted.

18 changes: 18 additions & 0 deletions src/decode.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//decode.c
//

#include <stdio.h>

#include "decode.h"
#include "bitconv.h"

uint8_t
decode(
const char *restrict ifile)
{
printf("%s\n", "decode does nothing as of yet");
return 0; //stand-in
}

/* vim: set ts=4 sw=4 noexpandtab tw=79: */

15 changes: 15 additions & 0 deletions src/decode.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//decode.h
//

#ifndef __DECODE_H__
#define __DECODE_H__

#include <stdint.h>

__BEGIN_DECLS
uint8_t decode __P((const char *restrict));
__END_DECLS

#endif /* !__DECODE_H__ */

/* vim: set ts=4 sw=4 noexpandtab tw=79: */
65 changes: 65 additions & 0 deletions src/enclist.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//enclist.c
//

#include <stdio.h>
#include <err.h>

#include "enclist.h"

/* debugging purposes only */
int __attribute__((unused))
enclist_length(
enclist_t *el)
{
if (el && el->cdr) {
return 1 + enclist_length(el->cdr);
} else if (el) {
return 1;
} else {
return 0;
}
}


pthread_t*
newthread(
enclist_t *el)
{
if (el && el->cdr != NULL) {
return newthread(el->cdr);
} else if (el) {
(*el).cdr = (enclist_t *)malloc(enclist_sz);
el->cdr->cdr = NULL;
return &el->cdr->thread;
} else {
warnx("%s\n", "newthread() called with null enclist_t");
return NULL;
}
}


void
writeenc(
enclist_t *el,
FILE * fd)
{
void *enc = NULL;
pthread_join(el->thread, &enc);
if (enc)
fprintf(fd, "%s", (char *)enc);
free(enc);
if (el->cdr)
writeenc(el->cdr, fd);
}

void*
freeenc(
enclist_t *el)
{
if (el->cdr)
el->cdr = freeenc(el->cdr);
free(el);
return el = NULL;
}

/* vim: set ts=4 sw=4 noexpandtab tw=79: */
28 changes: 28 additions & 0 deletions src/enclist.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//enclist.h
//

#ifndef __ENCLIST_H__
#define __ENCLIST_H__

#include <stdlib.h>
#include <pthread.h>

/*
* linked-list structure to dynamically hold encoded lines
*/
typedef
struct _enclist {
pthread_t thread;
struct _enclist *cdr;
} enclist_t;
#define enclist_sz (sizeof(enclist_t))

__BEGIN_DECLS
pthread_t* newthread __P((enclist_t *));
void writeenc __P((enclist_t *,FILE *));
void* freeenc __P((enclist_t *));
__END_DECLS

#endif /* !__ENCLIST_H__ */

/* vim: set ts=4 sw=4 noexpandtab tw=79: */
Loading