Initial commit but don't expect more commits

master
Barrington 2023-02-09 16:27:25 +01:00
commit 19a6e62712
Signed by: svragv
GPG Key ID: B39C0BCEE94A4A89
49 changed files with 1740 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
src/*.o
src/bin/

160
CHANGELOG Normal file
View File

@ -0,0 +1,160 @@
96d5493 Fixed stuff in rmdir
74047ab proto -l in ls
8a477a0 trying EBIN indentation style :-------DDDDDDDDDDDD
fbff968 wait i'm fucking stupid, lmao, well musl, good luck searching for a file called -<CHAR>, but if it makes you happy, go for it my dude
e4192c6 don't take - as stdin if it is a flag (solves musl getopt() bullshit)
dc61ade sorry, my emacs indentation was retarded
342a885 less long line
dd949ba Tabs instead of spaces in indentation lol
34a3f38 added contributing guideliness
609309e im an idiot
36f49ec cat: ignore -u and - can be used for stdin
8627b3b shit to cat.c read the diff lmao
997b2ed https://lainsafe.delegao.moe/files/160545969573108.png
cc03664 Cleanup code
c322cdd %c is better since gnu date gives that by default so take it
b0ca123 Added a probably non posix compliant date(1) but it works :DDD
8dc1039 Added the -p flag to ls
a43985b Holy cow, I added comments but I made another commit to k9core!
1d78c2c It seems that, somehow, i typed 1 instead of 0
8f16928 POSIX, I don't really get you sometimes...
f487111 Added tee!
8eed6cc added -h flag to chgrp
b244eb3 added chgrp
ea00641 wc prints the filename
21718a0 Modified the style
571b6c7 Is this ok, POSIX?
98f9bd9 Remove useless headers
016c4f6 Whatever this does lmao
20eb8d5 cleaned up chmod.c
0a5169b generic case for uname
8cde471 what the hell is -m flag posix is literally the same as -c
9713636 Finally fixed wc :D
4d96265 Improved uname.c
d8c6dd6 Modified the readme because this also has util-linux packes
7f38565 git you're a bit dumb
a8ae0f1 moved non coreutils programs to a nonuserland folder since they are not userland programs
b653b75 Better style and stuff to uname.c
2b3baec In GNU's uname(1) if you pass no arguments it prints the kernel name so mine does that too.
41fac48 Added uname, works but probably not as it should, initial commit for this and i'll fix it if needed later (probably i forgot some flags, programming is hard)
7f4a6e2 added chroot
dfa79c9 im stupid
015f021 added cat the posix -u flag which is useless
07c86b1 Improved makefile
30bc675 added a bunch of programs
d832f81 added groups
f0712c3 lmao
e5464f8 Added logname.c differences with whoami.c: * whoami prints who you are * logname prints the username you logged with meaning that if you run logname with doas(1) or sudo(8) you'll get the real username instead of root
cd73c58 tty(1), which prints the name of the teletypewriter you're using.
fdd3579 improved sleep by using usleep() instead of sleep() which allows more precise times
0d806ef add sync
cb32179 added ernno things
2d60914 im a bit dumb you know
32e3a82 lol
069bde6 i kinda hate posix because -n, but there it is i just have to add a for loop so it iterates every file passed by args
642d6f3 added head
61a4887 modified kill.c so doesn't use getopt (which in this case was a complete overkill). Also a warning if nothing given.
0d9b8f2 sleep
0f8f9dc exec
fecba9a Almost done wc only pick from stdin if no file given. but all flags are working!
dcf829a somebody pls fix flags and stdin
3a44055 Merge branch 'master' of gitlab.com:call-cc/k9core into master
8d1bf0e added flags to wc
1a37413 improve ls (make it look cleaner)
371c5f0 What the fuck is this
c0935a1 added errno to ln.c
0ee58eb idk whats this git told me isnt' staged so hre's your commit
a1f38d5 Edited some stuff in wc and mv
bb78561 Fixed shred
7bedf74 use stat() instead of a own function to count bytes.
ab00cba Added shred
801cf9d Added unlink, not really sure of that this do but well here it is
3823f62 less bad mv
9c856f4 yeah so now the thing closes the file descriptors
2996e21 cp now supports binary data and touch.c uses the saner creat() instead of open() because using open() there was simply stupid
1d8f3bf fprintf in mount.c
59d1ef6 Better errno
b574d18 added id.c.
43d55a3 -f flag for umount.c (now we use umount2 though)
636072c lol
69d0fae Added stuff to mount.c
8065fed Better whoami
7d414e2 Mount and whoami
7cb95b3 added umount
755222a I just do a prototype of mkdir -p that does not work because it does not work so is commented don't uncomment it unless you can fix it thank you
b339661 Merge branch 'master' of gitlab.com:call-cc/k9core
5238638 Added -l to kill.c
b263799 Fix to remove error in -Wall -Wextra
87c139e Fix ln slightly by removing instead of unlinking.
2401662 whoops
f317faa Aded cp and mv
61245b5 This will save a lot of time
e1e3f9f more readable cat.c
c037341 Cat prints binary data now
c904af6 Someone make this read binary shit
268a888 Rewritten cat so it's not literally 9base's
9d6db6f Improved wc
971e32a bare bones version of wc
e4c35b7 unistd.h was useless in ls.c
8ac5f0b Add -a flag for ls.
8c81a09 Do not print dot files in ls.c
a6bfb92 Added ls.c
530bb5c Added chown.c
f4dc088 Added variable guidelines
1c6f622 Improve echo, or fix -n flag
538adce printf -> fprintf in src/chmod.c
60b3510 Added chmod
d2be0fe Added false and true
aff3c77 Readme in org
a4d4d90 Fixed style in src/echo.c
82b36c8 Clean code in src/echo.c
73ab1c7 Sorry i haven't slept in 21 hours
2f5f39a goddammit
eba3830 I don't know I was really drunk at the time
e4cdd7d slight fix
2214494 Add -n flag to echo
79ec3ad License update
4ca78b4 Improve makefile and mark ln as complete
85f3c31 I don't know how to use gdb
8adf347 Switch statement
9c7fec7 Added kill
559fd48 Change STYLE.ORG file name because it trips up gitlab
c1115b3 Add to README
81244ae Add to TODO and migrate to the kill-9 public license
e27abc9 Improve yes
0670f46 Fix errors in style, plus more stuff about C standardization
02699d5 Add error alert to ln and another item to the todo
fbacc8d Style written in org
bc423b1 Add todo.org, fix pwd.c
8dcc572 Added new style thing
f4e746e Change errors in rmdir to warns
9486a50 Add -f flag to ln
f458b93 More makefile improvements
e4758c0 Remove note, improve makefile
66dbeda I hate git.
36adf54 Fixed makefile
fe104b8 Merge branch 'master' of https://gitlab.com/call-cc/k9core
c68d449 Add note to ln.c
bdbe212 Add to TODO and clean up ln
14e32f3 Improved makefile
3843efd Tried to add symlinks the worst way possible. Please fix
0575e4d Add ln
87681f9 Added yes
976d1ed idk why is this merge important tbh Merge branch 'master' of gitlab.com:call-cc/k9core
62ecbde Added src/mkdir.c, src/rm.c, some tips for commiters and close file descriptor in src/touch.c
30192ae Adhere to style guidelines
cb17679 added coding style guide
d4eb349 Make dirname more posix compliant
d2c5b8a Add dirname
83af9b4 Errors count to rmdir
9077404 Rewritten rmdir
5450b0c add multiple-argument deletion to rmdir
35c7b05 Add rmdir
7db446d Add makefile
ff4f7c1 Added touch
36b7302 Add README
ba25b37 Add pwd
152ad26 Very slight cat fix
602dd40 Fix echo, shorten file size
9301665 Add echo
0e055db Initial commit

24
LICENSE Normal file
View File

@ -0,0 +1,24 @@
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
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 AUTHORS 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 IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>

7
README.org Normal file
View File

@ -0,0 +1,7 @@
* k9core
Extremely minimalist coreutils. It also includes some util-linux
packages
** License
Licensed under the k9pl. See LICENSE for more information.

166
src/.clang-format Normal file
View File

@ -0,0 +1,166 @@
---
Language: Cpp
# BasedOnStyle: Mozilla
AccessModifierOffset: -2
AlignAfterOpenBracket: Align
AlignConsecutiveMacros: None
AlignConsecutiveAssignments: None
AlignConsecutiveBitFields: None
AlignConsecutiveDeclarations: None
AlignEscapedNewlines: Right
AlignOperands: Align
AlignTrailingComments: true
AllowAllArgumentsOnNextLine: true
AllowAllConstructorInitializersOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortEnumsOnASingleLine: true
AllowShortBlocksOnASingleLine: Never
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: Inline
AllowShortLambdasOnASingleLine: All
AllowShortIfStatementsOnASingleLine: Never
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: TopLevel
AlwaysBreakAfterReturnType: TopLevel
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: Yes
AttributeMacros:
- __capability
BinPackArguments: false
BinPackParameters: false
BraceWrapping:
AfterCaseLabel: false
AfterClass: true
AfterControlStatement: Never
AfterEnum: true
AfterFunction: true
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: true
AfterUnion: true
AfterExternBlock: true
BeforeCatch: false
BeforeElse: false
BeforeLambdaBody: false
BeforeWhile: false
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: false
SplitEmptyNamespace: true
BreakBeforeBinaryOperators: None
BreakBeforeConceptDeclarations: true
BreakBeforeBraces: Mozilla
BreakBeforeInheritanceComma: true
BreakInheritanceList: BeforeComma
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeComma
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit: 80
CommentPragmas: '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 5
ContinuationIndentWidth: 5
Cpp11BracedListStyle: false
DeriveLineEnding: true
DerivePointerAlignment: false
DisableFormat: false
EmptyLineBeforeAccessModifier: LogicalBlock
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: false
ForEachMacros:
- foreach
- Q_FOREACH
- BOOST_FOREACH
StatementAttributeLikeMacros:
- Q_EMIT
IncludeBlocks: Preserve
IncludeCategories:
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
Priority: 2
SortPriority: 0
CaseSensitive: false
- Regex: '^(<|"(gtest|gmock|isl|json)/)'
Priority: 3
SortPriority: 0
CaseSensitive: false
- Regex: '.*'
Priority: 1
SortPriority: 0
CaseSensitive: false
IncludeIsMainRegex: '(Test)?$'
IncludeIsMainSourceRegex: ''
IndentCaseLabels: true
IndentCaseBlocks: false
IndentGotoLabels: true
IndentPPDirectives: None
IndentExternBlock: AfterExternBlock
IndentRequires: false
IndentWidth: 5
IndentWrappedFunctionNames: false
InsertTrailingCommas: None
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: true
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBinPackProtocolList: Auto
ObjCBlockIndentWidth: 5
ObjCBreakBeforeNestedBlockParam: true
ObjCSpaceAfterProperty: true
ObjCSpaceBeforeProtocolList: false
PenaltyBreakAssignment: 5
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyBreakTemplateDeclaration: 10
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 200
PenaltyIndentedWhitespace: 0
PointerAlignment: Right
ReflowComments: true
SortIncludes: true
SortJavaStaticImport: Before
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeCaseColon: false
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: false
SpaceAroundPointerQualifiers: Default
SpaceBeforeRangeBasedForLoopColon: true
SpaceInEmptyBlock: false
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInConditionalStatement: false
SpacesInContainerLiterals: false
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
SpaceBeforeSquareBrackets: false
BitFieldColonSpacing: Both
Standard: Latest
StatementMacros:
- Q_UNUSED
- QT_REQUIRE_VERSION
TabWidth: 5
UseCRLF: false
UseTab: Always
WhitespaceSensitiveMacros:
- STRINGIZE
- PP_STRINGIZE
- BOOST_PP_STRINGIZE
- NS_SWIFT_NAME
- CF_SWIFT_NAME
...

53
src/Makefile Normal file
View File

@ -0,0 +1,53 @@
# This makefile is BSD compatiable: You can run it without gmake on most systems
CC = cc
PREFIX = /usr/local
compile:
mkdir -p bin
$(CC) -c *.c nouserland/*.c
$(CC) -o bin/cat cat.o
$(CC) -o bin/chmod chmod.o
$(CC) -o bin/chown chown.o
$(CC) -o bin/cp cp.o
$(CC) -o bin/dirname dirname.o
$(CC) -o bin/echo echo.o
$(CC) -o bin/exec exec.o
$(CC) -o bin/false false.o
$(CC) -o bin/groups groups.o
$(CC) -o bin/head head.o
$(CC) -o bin/hostid hostid.o
$(CC) -o bin/hostname hostname.o
$(CC) -o bin/id id.o
$(CC) -o bin/kill kill.o
$(CC) -o bin/ln ln.o
$(CC) -o bin/longname logname.o
$(CC) -o bin/ls ls.o
$(CC) -o bin/mkdir mkdir.o
$(CC) -o bin/mount mount.o
$(CC) -o bin/mv mv.o
$(CC) -o bin/nproc nproc.o
$(CC) -o bin/pwd pwd.o
$(CC) -o bin/rm rm.o
$(CC) -o bin/rmdir rmdir.o
$(CC) -o bin/shred shred.o
$(CC) -o bin/sleep sleep.o
$(CC) -o bin/sync sync.o
$(CC) -o bin/touch touch.o
$(CC) -o bin/true true.o
$(CC) -o bin/tty tty.o
$(CC) -o bin/umount umount.o
$(CC) -o bin/unlink unlink.o
$(CC) -o bin/wc wc.o
$(CC) -o bin/whoami whoami.o
$(CC) -o bin/yes yes.o
$(CC) -o bin/uname uname.o
clean:
rm -f *.o
rm -fr bin/
install:
mkdir -p $(DESTDIR)$(PREFIX)/k9core
cp bin/* /usr/local/k9core/
.PHONY:
clean objclean compile

42
src/cat.c Normal file
View File

@ -0,0 +1,42 @@
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int
cat(int fd, const char *filename)
{
int c;
char buf[8192];
if(filename[0] == '-' && filename[1] != 'u')
fd = 0;
if(fd != 0)
fd = open(filename, O_RDONLY);
if(fd == -1) {
fprintf(stderr, "error opening %s: %s\n", filename, strerror(errno));
return -1;
}
while((c = read(fd, buf, sizeof(buf))) > 0) {
if(c == -1)
return -1;
write(1, buf, c);
}
close(fd);
return 0;
}
int
main(int argc, char *argv[])
{
getopt(argc, argv, "u");
if(argc == optind)
cat(0, "-");
for(int i = optind; i < argc; i++)
cat(1, argv[i]);
return 0;
}

33
src/chgrp.c Normal file
View File

@ -0,0 +1,33 @@
#include <errno.h>
#include <grp.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int
main(int argc, char *argv[])
{
int c;
int follow_symlink;
while((c = getopt(argc, argv, "h")) != -1) {
switch(c) {
case 'h':
follow_symlink = 1;
break;
}
}
if(argc == 1 || argc == 2) {
fprintf(stderr, "usage: chgrp group file...\n");
return 1;
}
struct group *group_data = getgrnam(argv[optind]);
gid_t gid = group_data->gr_gid;
for(int i = optind + 1; i < argc; i++) {
if(follow_symlink) {
if(lchown(argv[i], gid, getuid()) == -1)
fprintf(stderr, "Error: %i = %s\n", errno, strerror(errno));
} else if(chown(argv[i], gid, getuid()) == -1) {
fprintf(stderr, "Error: %i = %s\n", errno, strerror(errno));
}
}
}

22
src/chmod.c Normal file
View File

@ -0,0 +1,22 @@
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
int
main(int argc, char *argv[])
{
mode_t mode = atoi(argv[1]);
for(int i = 2; i < argc; i++) {
int fd = chmod(argv[i], mode);
if(fd == -1) fprintf(stderr,
"Error setting %i on %s\n %i = %s",
mode,
argv[i],
errno,
strerror(errno));
}
return 0;
}

26
src/chown.c Normal file
View File

@ -0,0 +1,26 @@
#include <pwd.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
/* TODO: use strtok() to get the group */
int
main(int argc, char *argv[])
{
if(argc == 1) {
fprintf(stderr, "No command given\n");
return 1;
}
const char *user = argv[1];
struct passwd *data = getpwnam(user);
uid_t uid = data->pw_gid;
gid_t gid = data->pw_gid; /* Change this with the strtok() thing */
for(int i = 2; i <= argc; i++) {
chown(argv[i], uid, gid);
}
return 0;
}

14
src/chroot.c Normal file
View File

@ -0,0 +1,14 @@
#include <stdio.h>
#include <unistd.h>
/* UNTESTED */
int
main(int argc, char *argv[])
{
if(argc == 1) {
fprintf(stderr, "Missing operand\n");
return 1;
}
chroot(argv[1]);
}

45
src/cp.c Normal file
View File

@ -0,0 +1,45 @@
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int
copy(const char *src, const char *dst)
{
int source = open(src, O_RDONLY);
int destination = creat(dst, 0644);
if(destination == -1) {
fprintf(stderr,
"Error opening destination file: %i = %s\n",
errno,
strerror(errno));
return 1;
}
if(source == -1) {
fprintf(stderr,
"Error opening source file: %i = %s\n",
errno,
strerror(errno));
return 1;
}
int lines;
char buf[8912];
while((lines = read(source, buf, sizeof(buf))) > 0)
write(destination, buf, lines);
close(destination);
close(source);
return 0;
}
int
main(int argc, char *argv[])
{
int fd;
if(argc == 1) {
fprintf(stderr, "usage: cp source destination\n");
return 1;
} else
fd = copy(argv[1], argv[2]);
}

39
src/date.c Normal file
View File

@ -0,0 +1,39 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
int
main(int argc, char *argv[])
{
int c;
int u = 0;
char buffer[256];
time_t now;
now = time(NULL);
char FORMAT[256] = "%c";
struct tm *timeinfo;
while((c = getopt(argc, argv, "u")) != -1) {
switch(c) {
case 'u':
u = 1;
break;
}
}
timeinfo = localtime(&now);
if(u)
timeinfo = gmtime(&now);
if(argc > optind && argv[optind][0] == '+') {
argv[optind]++;
strcpy(FORMAT, argv[optind]);
}
strftime(buffer, 256, FORMAT, timeinfo);
puts(buffer);
return 0;
}

14
src/dirname.c Normal file
View File

@ -0,0 +1,14 @@
#include <libgen.h>
#include <stdio.h>
int
main(int argc, char *argv[])
{
char *dir = dirname(argv[1]);
if(dir == NULL) {
fprintf(stderr, "%s", argv[0]);
return 1;
}
printf("%s/\n", dir); /* Trailing slash because POSIX */
return 0;
}

32
src/du.c Normal file
View File

@ -0,0 +1,32 @@
#include <getopt.h>
#include <stdio.h>
#include <sys/stat.h>
int
main(int argc, char *argv[])
{
int c;
int human_readable = 0;
struct stat file_data;
while((c = getopt(argc, argv, "h")) != -1) {
switch(c) {
case 'h':
human_readable = 1;
break;
}
}
if(argc == optind) {
printf("no!\n");
return 1;
}
for(int i = optind; i < argc; i++) {
stat(argv[i], &file_data);
if(human_readable)
printf("%li\t %s", file_data.st_size * 1024, argv[i]);
else
printf("%li\t %s", file_data.st_size, argv[i]);
puts("");
}
return 0;
}

22
src/echo.c Normal file
View File

@ -0,0 +1,22 @@
#include <stdio.h>
#include <string.h>
int
main(int argc, char *argv[])
{
int nflag;
if(!strcmp(*++argv, "-n")) {
nflag = 1;
argv++;
}
while(*argv) {
(void)fputs(*argv, stdout); /* Print argv */
if(*++argv)
putchar(' '); /* If multiple things in argv, print a space
between them. */
}
if(!nflag)
putchar('\n');
return 0;
}

14
src/exec.c Normal file
View File

@ -0,0 +1,14 @@
#include <stdio.h>
#include <unistd.h>
int
main(int argc, char *argv[])
{
if(argc == 1) {
fprintf(stderr, "what do you want to do?\n");
return 1;
}
char *const argv2[1] = { argv[2] };
execv(argv[1], argv2);
return 0;
}

5
src/false.c Normal file
View File

@ -0,0 +1,5 @@
int
main(void)
{
return 1;
}

26
src/groups.c Normal file
View File

@ -0,0 +1,26 @@
#include <grp.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int
main(int argc, char *argv[])
{
struct passwd *pwd;
if(argc == 2)
pwd = getpwnam(argv[1]);
else
pwd = getpwuid(getuid());
int ngroups = 10;
uid_t *groups = malloc(ngroups);
getgrouplist(pwd->pw_name, pwd->pw_gid, groups, &ngroups);
struct group *gr;
for(int i = 0; i < ngroups; i++) {
gr = getgrgid(groups[i]);
printf("%s ", gr->gr_name);
}
printf("\n");
return 0;
}

49
src/head.c Normal file
View File

@ -0,0 +1,49 @@
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int
head(FILE *file, int lines)
{
if(file == NULL) {
fprintf(
stderr, "error opening file: %i = %s\n", errno, strerror(errno));
return 1;
}
int a;
int c = 0;
while((a = fgetc(file)) != EOF) {
if(a == '\n')
++c;
putchar(a);
if(c == lines)
return lines;
}
return lines;
}
int
main(int argc, char *argv[])
{
int lines = 10;
switch(argc) {
case 1:
head(stdin, lines);
break;
case 2:
head(fopen(argv[1], "r"), lines);
break;
case 3:
lines = abs(atoi(argv[1]));
head(fopen(argv[2], "r"), lines);
break;
default:
lines = atoi(argv[2]);
head(fopen(argv[3], "r"), lines);
break;
}
return 0;
}

11
src/hostid.c Normal file
View File

@ -0,0 +1,11 @@
#include <stdio.h>
#include <unistd.h>
int
main(void)
{
long id = gethostid();
printf("%lx\n", id);
return 0;
}

11
src/hostname.c Normal file
View File

@ -0,0 +1,11 @@
#include <stdio.h>
#include <unistd.h>
int
main(void)
{
char buf[64];
gethostname(buf, 64);
puts(buf);
return 0;
}

27
src/id.c Normal file
View File

@ -0,0 +1,27 @@
#include <getopt.h>
#include <pwd.h>
#include <stdio.h>
#include <unistd.h>
int
main(int argc, char *argv[])
{
int c = getopt(argc, argv, "Ggnru");
struct passwd *user_data = getpwnam(getlogin());
switch(c) {
case 'g':
case 'u':
printf("%u\n", user_data->pw_gid);
break;
case 'n':
printf("%s\n", user_data->pw_name);
break;
default:
printf("%u %s\n",
user_data->pw_gid,
user_data->pw_name); /* I know
* it does not work
* like this */
}
return 0;
}

63
src/kill.c Normal file
View File

@ -0,0 +1,63 @@
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
void
list_signals(void)
{
puts("1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) "
"SIGTRAP");
puts("6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) "
"SIGUSR1");
puts("11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) "
"SIGTERM");
puts("16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) "
"SIGTSTP");
puts("21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) "
"SIGXFSZ");
puts("26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR");
puts("31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) "
"SIGRTMIN+3");
puts("38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) "
"SIGRTMIN+8");
puts("43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) "
"SIGRTMIN+12 47) SIGRTMIN+13");
puts("48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) "
"SIGRTMAX-13 52) SIGRTMAX-12");
puts("53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) "
"SIGRTMAX-8 57) SIGRTMAX-7");
puts("58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) "
"SIGRTMAX-2");
puts("63) SIGRTMAX-1 64) SIGRTMAX");
}
int
main(int argc, char *argv[])
{
int sig = 0;
pid_t pid;
if(argc == 1) {
fprintf(stderr, "expected something\n");
return 1;
}
if(argv[1][1] == 'l') {
list_signals();
return 0;
}
switch(argc) {
case 2:
sig = 15;
pid = atoi(argv[1]);
break;
case 3:
sig = abs(atoi(argv[1]));
pid = abs(atoi(argv[2]));
break;
default:
fprintf(stderr, "Specify who to kill\n");
return 1;
}
kill(pid, sig);
return 0;
}

48
src/ln.c Normal file
View File

@ -0,0 +1,48 @@
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int
main(int argc, char *argv[])
{
if(argc == 1) {
printf("Usage: ln oldfile newfile\n");
return 1;
}
int opts, fd, fflag;
while((opts = getopt(argc, argv, "sf")) != -1) {
if(opts == 'f')
fflag = 1;
switch(opts) {
case 's':
if(fflag == 1 && (access(argv[3], F_OK) != 1)) {
if(remove(argv[3]) == -1)
rmdir(argv[3]);
}
int symstat = symlink(argv[2], argv[3]);
if(symstat == -1) {
fprintf(stderr,
"Symlink error: %i = %s",
errno,
strerror(errno));
return 1;
}
break;
case '?':
printf("-%c: Argument not found", optopt);
break;
default:
fd = link(argv[1], argv[2]);
if(fd == -1) {
fprintf(stderr,
"Error creating link: %i = %s\n",
errno,
strerror(errno));
return 1;
}
return 0;
}
}
}

8
src/logname.c Normal file
View File

@ -0,0 +1,8 @@
#include <stdio.h>
#include <unistd.h>
int
main(void)
{
printf("%s\n", getlogin());
}

86
src/ls.c Normal file
View File

@ -0,0 +1,86 @@
#include <dirent.h>
#include <errno.h>
#include <getopt.h>
#include <stdio.h>
#include <string.h>
int
recursive_list_dirs(const char *directory)
{
DIR *dir = opendir(directory);
if(dir == NULL) {
fprintf(stderr, "Error opening directory: %s\n", strerror(errno));
puts(directory);
return -1;
}
struct dirent *ent;
while((ent = readdir(dir)) != NULL) {
if(ent->d_type == DT_DIR) {
char path[1024];
if(!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."))
continue;
snprintf(path, sizeof(path), "%s/%s", directory, ent->d_name);
printf("%s/%s\n", path, ent->d_name);
recursive_list_dirs(path);
} else
printf("%s\n", ent->d_name);
}
closedir(dir);
return 0;
}
int
main(int argc, char *argv[])
{
int c;
int all, show_slash, show_line, recursive;
all = show_slash = show_line = recursive = 0;
while((c = getopt(argc, argv, "lapR")) != -1) {
switch(c) {
case 'a':
all = 1;
break;
case 'p':
show_slash = 1;
break;
case 'l':
show_line = 1;
break;
case 'R':
recursive = 1;
break;
}
}
char directory[256];
if(!argv[optind])
strcpy(directory, "./");
else
strcpy(directory, argv[optind]); /* Very dirty code, i'll fix it
* later */
DIR *dir = opendir(directory);
struct dirent *ent;
if(recursive) {
recursive_list_dirs(directory);
return 0;
}
if(dir != NULL) {
while((ent = readdir(dir)) != NULL) {
if(ent->d_name[0] == '.' && !all)
continue;
if(!show_line) {
if(ent->d_type == DT_DIR && show_slash)
printf("%s/ ", ent->d_name);
else
printf("%s ", ent->d_name);
} else
printf("%s\n", ent->d_name);
}
}
puts("");
closedir(dir);
return 0;
}

42
src/mkdir.c Normal file
View File

@ -0,0 +1,42 @@
#include <getopt.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
int
main(int argc, char *argv[])
{
int c = getopt(argc, argv, "p");
if(argc == 1) {
fprintf(stderr, "specify path(s) to make\n");
return 1;
}
/*
if(c == 'p')
{
const char tok[2] = "/";
char *token;
token = strtok(argv[2],tok);
char *directories = argv[2];
printf("%s\n",directories);
while(token != NULL)
{
mkdir(token,511);
token = strtok(NULL,"/");
printf("%s",token);
strcat(directories,"/");
printf("%s",directories);
}
return 0;
}
*/
for(int i = 1; i < argc; i++) {
int fd = mkdir(argv[i], 420);
if(fd == -1) {
fprintf(stderr, "Error creating dir %s\n", argv[i]);
return 1;
}
}
return 0;
}

38
src/mv.c Normal file
View File

@ -0,0 +1,38 @@
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
/*
int
move(const char *src, const char *dst)
{
int source = open(src,O_RDONLY);
int destination = creat(dst,0644);
if(destination == -1)
{
printf("Error opening destination file\n");
return 1;
}
if(source == -1)
{
printf("Error opening source file\n");
return 1;
}
int lines;
char buf[8912];
while((lines = read(source,buf,sizeof(buf))) > 0)
write(destination,buf,lines);
close(destination); close(source);
return 0;
}
*/
int
main(int argc, char *argv[])
{
if(argc == 1) {
fprintf(stderr, "usage: mv source destination\n");
return 1;
} else
rename(argv[1], argv[2]);
}

45
src/nouserland/mount.c Normal file
View File

@ -0,0 +1,45 @@
#include <stdio.h>
#include <sys/mount.h>
#include <unistd.h>
#include <getopt.h>
#include <errno.h>
#include <string.h>
/* Do NOT use this unironically for now, this only supports ext4, and
* you cannot specify another filesystem unless you change the source
* code. */
/* Update: now this supports filetype with -t flag. But I still don't
* recommend it to mounting something that it's not extx
*/
int
main(int argc, char *argv[]) {
int c = getopt(argc, argv,"t:");
if(argc < 2)
{
printf("./mount source destination\n");
return 1;
}
if(getuid() != 0)
{
fprintf(stderr,"Only root can run this\n");
return 1;
}
char filesystem[10] = "ext4";
int source = 1;
int destination = 2;
if(c == 't')
{
strcpy(filesystem,optarg);
source++;
destination++;
}
int fd = mount(argv[source],argv[destination],filesystem,0,NULL);
if(fd == -1)
{
fprintf(stderr,"error mounting: %i = %s\n",errno,strerror(errno));
}
return 0;
}

28
src/nouserland/umount.c Normal file
View File

@ -0,0 +1,28 @@
#include <stdio.h>
#include <sys/mount.h>
#include <errno.h>
#include <getopt.h>
#include <string.h>
int
main(int argc, char *argv[])
{
int c = getopt(argc, argv, "f");
int options = 0; /* No options by default */
int destination = 1;
if(c == 'f')
{
options = MNT_FORCE;
destination++;
}
if(argc == 1)
{
printf("give a directory\n");
return 1;
}
int fd = umount2(argv[destination],options);
if(fd == -1)
{
fprintf(stderr,"error umounting: %i = %s\n",errno,strerror(errno));
}
}

10
src/nproc.c Normal file
View File

@ -0,0 +1,10 @@
#include <stdio.h>
#include <sys/sysinfo.h>
int
main(void)
{
int cores = get_nprocs_conf();
printf("%i\n", cores);
return 0;
}

9
src/pwd.c Normal file
View File

@ -0,0 +1,9 @@
#include <stdio.h>
#include <unistd.h>
int
main()
{
puts(getcwd(NULL, 0));
return 0;
}

22
src/rm.c Normal file
View File

@ -0,0 +1,22 @@
#include <errno.h>
#include <stdio.h>
#include <string.h>
int
main(int argc, char *argv[])
{
if(argc == 1) {
printf("Specify files to remove.\n");
return 1;
}
for(int i = 1; i < argc; i++) {
int fd = remove(argv[i]);
if(fd == -1) {
fprintf(stderr,
"Error removing file: %i = %s",
errno,
strerror(errno));
}
}
return 0;
}

19
src/rmdir.c Normal file
View File

@ -0,0 +1,19 @@
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int
main(int argc, char *argv[])
{
for(int i = 1; i < argc; i++) {
if(rmdir(argv[i])) {
fprintf(stderr,
"rmdir: failed to remove '%s', %s\n",
argv[i],
strerror(errno));
}
}
return 0;
}

38
src/shred.c Normal file
View File

@ -0,0 +1,38 @@
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
int
fill_with_zeroes(const char *filename)
{
int fd = open(filename, O_WRONLY);
if(fd == -1) {
printf("Error reading file: %i = %s\n", errno, strerror(errno));
return 1;
}
struct stat stat_struct; /* What name should i use? */
stat(filename, &stat_struct);
long int bytes_to_write = stat_struct.st_size;
char *buf = NULL;
buf = malloc(bytes_to_write);
for(int i = 0; i < bytes_to_write; i++)
write(fd, "\0\0\0\0\0", bytes_to_write + 2048);
free(buf);
return 0;
}
int
main(int argc, char *argv[])
{
int c = getopt(argc, argv, "u"); /* TODO: add -f */
fill_with_zeroes(argv[1]);
if(c == 'u')
remove(argv[1]);
}

15
src/sleep.c Normal file
View File

@ -0,0 +1,15 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int
main(int argc, char *argv[])
{
if(argc == 1) {
fprintf(stderr, "missing opperand\n");
return 1;
}
usleep(atof(argv[1]) * 1000000);
return 0;
}

60
src/stat.c Normal file
View File

@ -0,0 +1,60 @@
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
int
main(int argc, char **argv)
{
if(argc == 1) {
fprintf(stderr, "usage: stat FILE...\n");
return 1;
}
char mod_date[64], acc_date[64], creat_date[64];
struct stat file_data;
for(int i = 1; i < argc; i++) {
if(stat(argv[i], &file_data) == -1) {
printf("Cannot stat '%s': %s\n", argv[i], strerror(errno));
continue;
}
/* About file size, location... */
printf("File: %s\nSize: %lu\t blocks: %li\t IO Block: %li\nDevice: "
"%lu\t",
argv[i],
file_data.st_size,
file_data.st_blocks,
file_data.st_blksize,
file_data.st_dev);
/* File permisions
* TODO: Display only the permissions (644) instead of the whole mode
* (100644)
*/
printf("Inode: %u\t Links: %lu\nAccess: %o\tUid: %u\tGid:%u\n",
file_data.st_gid,
file_data.st_nlink,
file_data.st_mode,
file_data.st_uid,
file_data.st_gid);
/* Access, creation and modification date */
struct tm *timeinfo;
/* Modification time */
timeinfo = localtime(&file_data.st_mtim.tv_sec);
strftime(mod_date, 64, "%F %H:%M:%S", timeinfo);
/* Creation time */
timeinfo = localtime(&file_data.st_ctim.tv_sec);
strftime(creat_date, 64, "%F %H:%M:%S", timeinfo);
/* Access time */
timeinfo = localtime(&file_data.st_atim.tv_sec);
strftime(acc_date, 64, "%F %H:%M:%S", timeinfo);
printf("Access: %s\nModify: %s\nCreation %s\n",
acc_date,
mod_date,
creat_date);
}
return 0;
}

8
src/sync.c Normal file
View File

@ -0,0 +1,8 @@
#include <unistd.h>
int
main(void)
{
sync();
return 0;
}

62
src/tee.c Normal file
View File

@ -0,0 +1,62 @@
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int
tee(int fd)
{
if(fd == -1) {
fprintf(stderr, "%s\n", strerror(errno));
return 1;
}
char buf[8192];
int read_bytes = 0;
while((read_bytes = read(0, buf, 8192)) > 0) {
write(fd, buf, read_bytes);
if(fd != 1)
write(1, buf, read_bytes);
}
return 0;
}
int
main(int argc, char *argv[])
{
int c;
int append = 0;
int ignore_signt = 0;
int fd = 0;
int FLAGS = O_WRONLY | O_CREAT; /* yeah, it will overwrite the thing if it
* can't read what's in the file, thanks
* POSIX! */
while((c = getopt(argc, argv, "ai")) != -1) {
switch(c) {
case 'a':
append = 1;
break;
case 'i':
ignore_signt = 1;
break;
}
}
if(argc == optind) {
if(ignore_signt)
signal(SIGINT, SIG_IGN);
tee(1);
} else {
if(argv[argc - 1][0] == '-')
fd = 1;