coreutils

General Software Utilities
git clone http://git.omkov.net/coreutils
Log | Tree | Refs | README | LICENCE | Download

AuthorJamozed <[email protected]>
Date2020-06-26 14:00:02
Commitd798a0963d9f91e165bf1b426cab6d9f3cc1ab9f
Parente2c696c1f1838c70ccebbe823e86277349a6736c

rand: Add rand utility

Diffstat

M CMakeLists.txt | 1 +
M README.md | 1 +
A man/rand.1 | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++
A src/rand.c | 138 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
M src/sleep.c | 1 -

5 files changed, 191 insertions, 1 deletions

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 5d26e01..aba9e71 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -21,6 +21,7 @@ ADD_EXECUTABLE(logname  ${CMAKE_SOURCE_DIR}/src/logname.c)
 ADD_EXECUTABLE(mkdir    ${CMAKE_SOURCE_DIR}/src/mkdir.c)
 ADD_EXECUTABLE(nice     ${CMAKE_SOURCE_DIR}/src/nice.c)
 ADD_EXECUTABLE(pwd      ${CMAKE_SOURCE_DIR}/src/pwd.c)
+ADD_EXECUTABLE(rand     ${CMAKE_SOURCE_DIR}/src/rand.c)
 ADD_EXECUTABLE(rmdir    ${CMAKE_SOURCE_DIR}/src/rmdir.c)
 ADD_EXECUTABLE(sleep    ${CMAKE_SOURCE_DIR}/src/sleep.c)
 ADD_EXECUTABLE(sync     ${CMAKE_SOURCE_DIR}/src/sync.c)
diff --git a/README.md b/README.md
index 86e304f..452027b 100644
--- a/README.md
+++ b/README.md
@@ -20,6 +20,7 @@ UNIX-like systems.
 | mkdir            | Make directories                         | POSIX    |
 | nice             | Execute with an altered nice value       | POSIX    |
 | pwd              | Print working directory                  | POSIX    |
+| rand             | Generate random string                   |          |
 | rmdir            | Remove directories                       | POSIX    |
 | sleep            | Suspend execution for an interval        | POSIX    |
 | sync             | Synchronise file system caches to disk   |          |
diff --git a/man/rand.1 b/man/rand.1
new file mode 100644
index 0000000..79e2c1f
--- /dev/null
+++ b/man/rand.1
@@ -0,0 +1,51 @@
+.TH RAND 1 2020-06-27 "OMKOV coreutils" "General Commands Manual"
+.SH NAME
+rand \(em generate random string
+.SH SYNOPSYS
+\fBrand\fR [-aAns] [\fIlength\fR]
+.SH DESCRIPTION
+Generate a random string of \fIlength\fR characters long. The string will
+contain the character types specified by the -a, -A, -n, and -s options, those
+being lowercase letters, uppercase letters, numbers and symbols respectively. If
+no \fIlength\fR is specified, a default of 8 will be used. If no character type
+options are specified, \fIrand\fR will generate a random string containing
+uppercase letters and numbers.
+.SH OPTIONS
+The following options are supported:
+.TP
+.B -a
+Enable lowercase letters in the string output.
+.TP
+.B -A
+Enable uppercase letters in the string output.
+.TP
+.B -n
+Enable numbers in the string output.
+.TP
+.B -s
+Enable symbols '!@#$%^&*' in the string output.
+.TP
+.B --help
+Display help information.
+.TP
+.B --version
+Display version information.
+.SH OPERANDS
+The following operand is supported:
+.TP
+.I length
+The length of the string output.
+.SH EXIT STATUS
+The following exit values will be returned:
+.TP
+\ 0
+Successful completion.
+.TP
+>0
+An error occurred.
+.SH COPYRIGHT
+.nf
+Copyright (C) 2020, Jakob Wakeling
+All rights reserved.
+OMKOV Permissive Licence (https://www.omkov.net/OLPE)
+.fi
diff --git a/src/rand.c b/src/rand.c
new file mode 100644
index 0000000..5b7747b
--- /dev/null
+++ b/src/rand.c
@@ -0,0 +1,138 @@
+// rand.c, version 1.0.0
+// OMKOV coreutils rand
+// Copyright (C) 2020, Jakob Wakeling
+// All rights reserved.
+
+/*
+OMKOV Permissive Licence, version 1.0
+
+Copyright (C) 2020, Jakob Wakeling
+All rights reserved.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal with
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software is furnished to do so,
+subject to the following conditions:
+
+* Redistributions of source code must retain the above copyright notice, this
+  list of conditions and the following disclaimers.
+* Redistributions in binary form must reproduce the above copyright notice, this
+  list of conditions and the following disclaimers in the documentation and/or
+  other materials provided with the distribution.
+* Neither the names of the copyright holders, nor the names of its contributors
+  may be used to endorse or promote products derived from this Software without
+  specific prior written permission.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT
+HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE.
+*/
+
+#include "error.h"
+#include "optget.h"
+
+#include <errno.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#define aSIZE (sizeof (aset) - 1)
+#define ASIZE (sizeof (Aset) - 1)
+#define nSIZE (sizeof (nset) - 1)
+#define sSIZE (sizeof (sset) - 1)
+
+static const char aset[] = "abcdefghijklmnopqrstuvwxyz";
+static const char Aset[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
+static const char nset[] = "0123456789";
+static const char sset[] = "!@#$%^&*";
+
+static int       mode;
+static uintmax_t len;
+
+static void help(void);
+static void version(void);
+
+int main(int argc, char *argv[]) {
+	lop_t lops[] = {
+		{ "help",    ARG_NUL, 256 },
+		{ "version", ARG_NUL, 257 },
+		{ NULL, 0, 0 }
+	};
+	
+	opt_t opt = OPTGET_INIT; opt.str = "aAns"; opt.lops = lops; int o;
+	while ((o = optget(&opt, argv, 1)) != -1) switch (o) {
+	case 'a': { mode |= 1; break; }
+	case 'A': { mode |= 2; break; }
+	case 'n': { mode |= 4; break; }
+	case 's': { mode |= 8; break; }
+	case 256: { help(); return 0; }
+	case 257: { version(); return 0; }
+	default: { return 1; }
+	}
+	
+	if (!mode) { mode = 6; }
+	if (opt.ind == argc) { len = 8; }
+	else {
+		register uintmax_t d; register char *p = argv[opt.ind];
+		for (; *p >= '0' && *p <= '9'; ++p) {
+			d = (uintmax_t)*p - '0';
+			if (len > (UINTMAX_MAX - d) / 10) { break; }
+			len = len * 10 + d;
+		}
+		
+		if (*p) { error(1, "%s: %s: invalid length", argv[0], argv[opt.ind]); }
+	}
+	
+	FILE *rand; char *set; unsigned setlen = 0, n;
+	if (!(rand = fopen("/dev/urandom", "r"))) {
+		error(1, "%s: %s: %s", argv[0], "/dev/urandom", serrno);
+	}
+	
+	if (mode & 1) { setlen += aSIZE; }
+	if (mode & 2) { setlen += ASIZE; }
+	if (mode & 4) { setlen += nSIZE; }
+	if (mode & 8) { setlen += sSIZE; }
+	
+	if (!(set = (char *)malloc(setlen + 1))) {
+		fclose(rand); error(1, "%s: %s", argv[0], serrno);
+	}
+	
+	if (mode & 1) { strcat(set, aset); }
+	if (mode & 2) { strcat(set, Aset); }
+	if (mode & 4) { strcat(set, nset); }
+	if (mode & 8) { strcat(set, sset); }
+	
+	for (uintmax_t i = 0; i < len; ++i) {
+		fread(&n, sizeof (unsigned long), 1, rand); n %= setlen;
+		fputc(set[n], stdout);
+	} fputc('\n', stdout);
+	
+	fclose(rand); free(set); return 0;
+}
+
+static void help(void) {
+	puts("rand - generate random string\n");
+	puts("usage: rand [-aAns] [length]\n");
+	puts("options:");
+	puts("  -a         Enable lowercase letters in the string output");
+	puts("  -A         Enable uppercase letters in the string output");
+	puts("  -n         Enable numbers in the string output");
+	puts("  -s         Enable symbols '!@#$%^&*' in the string output");
+	puts("  --help     Display help information");
+	puts("  --version  Display version information");
+	return;
+}
+
+static void version(void) {
+	puts("OMKOV coreutils rand, version 1.0.0");
+	puts("Copyright (C) 2020, Jakob Wakeling");
+	puts("All rights reserved.");
+	puts("OMKOV Permissive Licence (https://www.omkov.net/OLPE)");
+	return;
+}
diff --git a/src/sleep.c b/src/sleep.c
index 7bb5f9f..1d3c5d2 100644
--- a/src/sleep.c
+++ b/src/sleep.c
@@ -38,7 +38,6 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE.
 
 #include <unistd.h>
 
-#include <ctype.h>
 #include <errno.h>
 #include <stdio.h>
 #include <stdint.h>