[hackers] [sbase] Initial commit of the uudecode tool and man page || dsp

From: <git_AT_suckless.org>
Date: Sun, 02 Feb 2014 21:50:35 +0100

commit 7008d751b23c80bdfa0d8f5695fea2c2a8612c1f
Author: dsp <dsp_AT_2f30.org>
Date: Sun Feb 2 19:08:22 2014 +0000

    Initial commit of the uudecode tool and man page
    
    Currently it operates only on regular files and does not
    support Base64.

diff --git a/Makefile b/Makefile
index f8469df..762d345 100644
--- a/Makefile
+++ b/Makefile
_AT_@ -85,6 +85,7 @@ SRC = \
         tr.c \
         true.c \
         tty.c \
+ uudecode.c \
         uuencode.c \
         uname.c \
         uniq.c \
diff --git a/uudecode.1 b/uudecode.1
new file mode 100644
index 0000000..62aff99
--- /dev/null
+++ b/uudecode.1
_AT_@ -0,0 +1,19 @@
+.TH UUDECODE 1 sbase\-VERSION
+.SH NAME
+uudecode \- decode a uuencoded file
+.SH SYNOPSIS
+.B uudecode
+.RI [file]
+.SH DESCRIPTION
+.B uudecode
+reads file (or by default, the standard input) and writes a decoded
+version to the file specified in the uuencoded header. In case that
+the file already exists, it is truncated. Otherwise a new file is
+created. After the operation the permissions of the created/accessed
+are changed to reflect the mode in the header.
+.SH NOTES
+This version of uudecode does not currently support the base64
+encoding algorithm.
+For safety currently uudecode operates only on regular files and
+stdout. Trying to uudecode to a link, directory, or special file
+yields an error.
diff --git a/uudecode.c b/uudecode.c
new file mode 100644
index 0000000..1644abe
--- /dev/null
+++ b/uudecode.c
_AT_@ -0,0 +1,245 @@
+/* See LICENSE file for copyright and license details. */
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <errno.h>
+#include <limits.h>
+#include "util.h"
+#include "text.h"
+
+static int uudecode(FILE *, FILE*);
+static int checkheader(FILE *, const char *, mode_t *, char **, char **);
+static int checkmode(const char *, mode_t *);
+static FILE *checkfile(const char *);
+
+static void
+usage(void)
+{
+ eprintf("usage: %s [file]
", argv0);
+}
+
+int
+main(int argc, char *argv[])
+{
+ FILE *fp, *nfp;
+ char *fname, *headerr;
+ mode_t mode = 0;
+ int chmodtest;
+
+ ARGBEGIN {
+ case 'm':
+ eprintf("-m not implemented
");
+ default:
+ usage();
+ } ARGEND;
+
+ if (argc > 1)
+ usage();
+
+ if (argc == 0) {
+ if (checkheader(stdin, "begin ", &mode, &fname, &headerr) < 0)
+ eprintf("%s
",headerr);
+
+ nfp = checkfile(fname);
+ if (nfp == NULL)
+ eprintf("fopen %s:", fname);
+
+ if (uudecode(stdin, nfp) < 0)
+ goto fail;
+
+ } else {
+ if (!(fp = fopen(argv[0], "r")))
+ eprintf("fopen %s:", argv[0]);
+
+ if (checkheader(fp, "begin ", &mode, &fname, &headerr) < 0) {
+ fclose(fp);
+ eprintf("%s
",headerr);
+ }
+ nfp = checkfile(fname);
+ if (nfp == NULL) {
+ fclose(fp);
+ eprintf("fopen %s:", fname);
+ }
+ if (uudecode(fp, nfp) < 0) {
+ fclose(fp);
+ goto fail;
+ }
+ }
+ chmodtest=fchmod(fileno(nfp),mode);
+ fclose(fp);
+ fclose(nfp);
+ if (chmodtest == -1)
+ eprintf("chmod %s:%s",fname,strerror(errno));
+
+ return EXIT_SUCCESS;
+fail:
+ fclose(nfp);
+ return EXIT_FAILURE;
+}
+
+static FILE *
+checkfile(const char *fname)
+{
+ struct stat st;
+ int ret;
+ if (strcmp(fname,"/dev/stdout") == 0)
+ return stdout;
+
+ ret = lstat(fname, &st);
+ if (ret < 0 && errno == ENOENT)
+ goto tropen; /* new file, try to open it */
+
+ if (ret < 0) {
+ weprintf("stat: %s:", fname);
+ return NULL;
+ }
+
+ if (!S_ISREG(st.st_mode)) {
+ weprintf("for safety uudecode operates only on regular files and /dev/stdout
");
+ return NULL;
+ }
+tropen:
+ return fopen(fname,"w");
+}
+
+static int
+checkheader(FILE *fp, const char *header, mode_t *mode, char **fname, char **headerr)
+{
+ char bufs[PATH_MAX + 11]; /* len header + mode + maxname */
+ char *p, *q, *bufp = NULL;
+ size_t n;
+ fgets(bufs, sizeof(bufs), fp);
+ if (bufp == NULL || bufp == '
Received on Sun Feb 02 2014 - 21:50:35 CET

This archive was generated by hypermail 2.3.0 : Sun Feb 02 2014 - 22:00:08 CET