fix style using clang-format

Closes: #2
This commit is contained in:
qorg11 2021-06-29 13:19:44 +02:00
parent 27a02780f6
commit 7a60fca519
No known key found for this signature in database
GPG Key ID: 343FC20A4ACA62B9
41 changed files with 887 additions and 746 deletions

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: false
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: Left
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: ControlStatements
SpaceAroundPointerQualifiers: Default
SpaceBeforeRangeBasedForLoopColon: true
SpaceInEmptyBlock: false
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInConditionalStatement: false
SpacesInContainerLiterals: true
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
...

View File

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

View File

@ -1,41 +1,33 @@
#include <stdio.h>
#include <unistd.h>
#include <grp.h>
#include <errno.h>
#include <grp.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int
main(int argc, char *argv[])
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));
}
}
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));
}
}
}

View File

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

View File

@ -1,28 +1,26 @@
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#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[])
main(int argc, char* argv[])
{
if(argc == 1)
{
fprintf(stderr,"No command given\n");
return 1;
}
const char *user = argv[1];
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;
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;
}

View File

@ -4,12 +4,11 @@
/* UNTESTED */
int
main(int argc, char *argv[])
main(int argc, char* argv[])
{
if(argc == 1)
{
fprintf(stderr,"Missing operand\n");
return 1;
}
chroot(argv[1]);
if (argc == 1) {
fprintf(stderr, "Missing operand\n");
return 1;
}
chroot(argv[1]);
}

View File

@ -1,43 +1,45 @@
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int
copy(const char *src, const char *dst)
copy(const char* src, const char* dst)
{
int source = open(src,O_RDONLY);
int destination = creat(dst,0644);
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;
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[])
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]);
int fd;
if (argc == 1) {
fprintf(stderr, "usage: cp source destination\n");
return 1;
} else
fd = copy(argv[1], argv[2]);
}

View File

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

View File

@ -1,14 +1,14 @@
#include <stdio.h>
#include <libgen.h>
#include <stdio.h>
int
main(int argc, char *argv[])
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;
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;
}

View File

@ -1,34 +1,32 @@
#include <getopt.h>
#include <stdio.h>
#include <sys/stat.h>
#include <getopt.h>
int
main(int argc, char *argv[])
main(int argc, char* argv[])
{
int c;
int human_readable = 0;
struct stat file_data;
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;
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;
}

View File

@ -2,20 +2,21 @@
#include <string.h>
int
main(int argc, char *argv[])
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;
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;
}

View File

@ -1,15 +1,14 @@
#include <unistd.h>
#include <stdio.h>
#include <unistd.h>
int
main(int argc, char *argv[])
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;
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;
}

View File

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

View File

@ -1,27 +1,26 @@
#include <stdio.h>
#include <pwd.h>
#include <unistd.h>
#include <grp.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int
main(int argc, char *argv[])
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;
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;
}

View File

@ -1,52 +1,49 @@
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
int
head(FILE *file, int lines)
head(FILE* file, int lines)
{
if(file == NULL)
{
fprintf(stderr,"error opening file: %i = %s\n",errno,strerror(errno));
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')
while ((a = fgetc(file)) != EOF) {
if (a == '\n')
++c;
putchar(a);
if(c == lines)
if (c == lines)
return lines;
}
return lines;
}
int
main(int argc, char *argv[])
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;
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;
}

View File

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

View File

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

View File

@ -1,26 +1,27 @@
#include <stdio.h>
#include <pwd.h>
#include <unistd.h>
#include <getopt.h>
#include <pwd.h>
#include <stdio.h>
#include <unistd.h>
int
main(int argc, char *argv[])
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 */
}
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;
return 0;
}

View File

@ -1,55 +1,63 @@
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.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");
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[])
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;
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;
}

View File

@ -1,46 +1,48 @@
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int
main(int argc, char *argv[])
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;
}
}
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;
}
}
}

View File

@ -4,5 +4,5 @@
int
main(void)
{
printf("%s\n",getlogin());
printf("%s\n", getlogin());
}

101
src/ls.c
View File

@ -1,87 +1,82 @@
#include <stdio.h>
#include <dirent.h>
#include <getopt.h>
#include <string.h>
#include <errno.h>
#include <getopt.h>
#include <stdio.h>
#include <string.h>
int
recursive_list_dirs(const char *directory)
recursive_list_dirs(const char* directory)
{
DIR *dir = opendir(directory);
if(dir == NULL) {
fprintf(stderr,"Error opening directory: %s\n",strerror(errno));
DIR* dir = opendir(directory);
if (dir == NULL) {
fprintf(stderr, "Error opening directory: %s\n", strerror(errno));
puts(directory);
return -1;
}
struct dirent *ent;
struct dirent* ent;
while((ent = readdir(dir)) != NULL) {
if(ent->d_type == DT_DIR) {
while ((ent = readdir(dir)) != NULL) {
if (ent->d_type == DT_DIR) {
char path[1024];
if (!strcmp(ent->d_name, ".")||!strcmp(ent->d_name, ".."))
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);
printf("%s/%s\n", path, ent->d_name);
recursive_list_dirs(path);
}
else printf("%s\n",ent->d_name);
} else
printf("%s\n", ent->d_name);
}
closedir(dir);
return 0;
}
int
main(int argc, char *argv[])
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;
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)
{
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)
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);
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("");

View File

@ -1,45 +1,42 @@
#include <stdio.h>
#include <sys/stat.h>
#include <getopt.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
int
main(int argc, char *argv[])
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 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;
int fd = mkdir(argv[i], 420);
if (fd == -1) {
fprintf(stderr, "Error creating dir %s\n", argv[i]);
return 1;
}
}
return 0;
}

View File

@ -1,7 +1,6 @@
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
/*
int
@ -12,13 +11,13 @@ move(const char *src, const char *dst)
if(destination == -1)
{
printf("Error opening destination file\n");
return 1;
printf("Error opening destination file\n");
return 1;
}
if(source == -1)
{
printf("Error opening source file\n");
return 1;
printf("Error opening source file\n");
return 1;
}
int lines;
char buf[8912];
@ -29,14 +28,11 @@ move(const char *src, const char *dst)
}
*/
int
main(int argc, char *argv[])
main(int argc, char* argv[])
{
if (argc == 1)
{
fprintf(stderr,"usage: mv source destination\n");
return 1;
}
else
rename(argv[1],argv[2]);
if (argc == 1) {
fprintf(stderr, "usage: mv source destination\n");
return 1;
} else
rename(argv[1], argv[2]);
}

View File

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

View File

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

View File

@ -1,23 +1,22 @@
#include <stdio.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
int
main(int argc, char *argv[])
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));
if (argc == 1) {
printf("Specify files to remove.\n");
return 1;
}
}
}
return 0;
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;
}

View File

@ -1,17 +1,18 @@
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int
main(int argc, char *argv[]) {
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));
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;

View File

@ -1,39 +1,38 @@
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <errno.h>
#include <sys/stat.h>
#include <unistd.h>
int
fill_with_zeroes(const char *filename)
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 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[])
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]);
int c = getopt(argc, argv, "u"); /* TODO: add -f */
fill_with_zeroes(argv[1]);
if (c == 'u')
remove(argv[1]);
}

View File

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

View File

@ -1,60 +1,60 @@
#include <stdio.h>
#include <sys/stat.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
int
main(int argc, char **argv)
main(int argc, char** argv)
{
if (argc == 1) {
fprintf(stderr,"usage: stat FILE...\n");
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));
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",
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)
* 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
);
file_data.st_gid);
/* Access, creation and modification date */
struct tm *timeinfo;
struct tm* timeinfo;
/* Modification time */
timeinfo = localtime(&file_data.st_mtim.tv_sec);
strftime(mod_date,64,"%F %H:%M:%S",timeinfo);
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);
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);
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;
}

View File

@ -3,6 +3,6 @@
int
main(void)
{
sync();
return 0;
sync();
return 0;
}

103
src/tee.c
View File

@ -1,65 +1,62 @@
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
#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;
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[])
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;
if(append)
FLAGS = O_RDWR|O_APPEND; /* Remember what I said? */
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;
if (append)
FLAGS = O_RDWR | O_APPEND; /* Remember what I said? */
fd = open(argv[argc-1],FLAGS);
if(ignore_signt)
signal(SIGINT,SIG_IGN);
if(tee(fd) == 1)
return -1;
}
return 0;
fd = open(argv[argc - 1], FLAGS);
if (ignore_signt)
signal(SIGINT, SIG_IGN);
if (tee(fd) == 1)
return -1;
}
return 0;
}

View File

@ -1,26 +1,27 @@
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
int
main(int argc, char *argv[])
main(int argc, char* argv[])
{
if(argc <=1)
{
fprintf(stderr,"Give a file\n");
return 1;
}
if (argc <= 1) {
fprintf(stderr, "Give a file\n");
return 1;
}
int fd = creat(argv[1], 0644);
int fd = creat(argv[1], 0644);
if(fd == -1)
{
fprintf(stderr,"Error creating file: %i = %s\n",errno,strerror(errno));
return 1;
}
close(fd);
return 0;
if (fd == -1) {
fprintf(stderr,
"Error creating file: %i = %s\n",
errno,
strerror(errno));
return 1;
}
close(fd);
return 0;
}

View File

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

View File

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

View File

@ -1,10 +1,10 @@
#include <stdio.h>
#include <sys/utsname.h>
#include <getopt.h>
#include <stdio.h>
#include <string.h>
#include <sys/utsname.h>
const char
*get_operating_system()
const char*
get_operating_system()
{
#ifdef __gnu_linux__
return "GNU/Linux";
@ -22,7 +22,7 @@ const char
}
int
main(int argc, char *argv[])
main(int argc, char* argv[])
{
int c;
int all = 0;
@ -34,25 +34,34 @@ main(int argc, char *argv[])
int nothing = 0;
struct utsname kernel_info;
if(argc == 1)
if (argc == 1)
nothing = 1;
while((c = getopt(argc, argv, "amnrsv")) != -1)
{
switch(c)
{
case 'a': all = 1; break;
case 'm': machine = 1; break;
case 'n': node_name = 1; break;
case 'r': kernel_release = 1; break;
case 's': kernel_name = 1; break;
case 'v': operating_system = 1; break;
while ((c = getopt(argc, argv, "amnrsv")) != -1) {
switch (c) {
case 'a':
all = 1;
break;
case 'm':
machine = 1;
break;
case 'n':
node_name = 1;
break;
case 'r':
kernel_release = 1;
break;
case 's':
kernel_name = 1;
break;
case 'v':
operating_system = 1;
break;
}
}
uname(&kernel_info);
if(all)
{
if (all) {
printf("%s %s %s %s %s %s\n",
kernel_info.sysname,
kernel_info.nodename,
@ -60,21 +69,18 @@ main(int argc, char *argv[])
kernel_info.version,
kernel_info.machine,
get_operating_system());
}
else
{
if(machine)
printf("%s ",kernel_info.machine);
if(node_name)
printf("%s ",kernel_info.nodename);
if(kernel_release)
printf("%s ",kernel_info.release);
if(kernel_name || nothing)
printf("%s ",kernel_info.sysname);
if(operating_system)
printf("%s",get_operating_system());
} else {
if (machine)
printf("%s ", kernel_info.machine);
if (node_name)
printf("%s ", kernel_info.nodename);
if (kernel_release)
printf("%s ", kernel_info.release);
if (kernel_name || nothing)
printf("%s ", kernel_info.sysname);
if (operating_system)
printf("%s", get_operating_system());
printf("\n");
}
return 0;
}

View File

@ -1,23 +1,19 @@
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int
main(int argc, char *argv[])
main(int argc, char* argv[])
{
if(argc == 1)
{
fprintf(stderr,"What do I unlink?\n");
return 1;
}
int fd = unlink(argv[1]);
if(fd == -1)
{
fprintf(stderr,"Error unlinking: %i = %s\n",
errno,strerror(errno));
}
return 0;
if (argc == 1) {
fprintf(stderr, "What do I unlink?\n");
return 1;
}
int fd = unlink(argv[1]);
if (fd == -1) {
fprintf(stderr, "Error unlinking: %i = %s\n", errno, strerror(errno));
}
return 0;
}

112
src/wc.c
View File

@ -1,8 +1,8 @@
#include <stdio.h>
#include <ctype.h>
#include <getopt.h>
#include <stdlib.h>
#include <errno.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int show_lines, show_words, show_bytes;
@ -16,31 +16,30 @@ struct wc_values
int total_lines;
int total_bytes;
int total_words;
};
int
wc(const char *filename, struct wc_values *data)
wc(const char* filename, struct wc_values* data)
{
FILE *file = fopen(filename,"r");
if(file == NULL) {
fprintf(stderr,"error opening file %s: %s\n",
filename,
strerror(errno));
FILE* file = fopen(filename, "r");
if (file == NULL) {
fprintf(stderr,
"error opening file %s: %s\n",
filename,
strerror(errno));
return -1;
}
size_t c;
char buf;
int newlines, spaces, bytes = 0;
newlines = spaces = bytes = 0;
while((c = fread(&buf,1,1, file)) > 0) {
if(!isascii(buf))
while ((c = fread(&buf, 1, 1, file)) > 0) {
if (!isascii(buf))
buf = toascii(buf);
bytes++;
if(buf == '\n')
if (buf == '\n')
newlines++;
if(isspace(buf))
if (isspace(buf))
spaces++;
}
data->bytes = bytes;
@ -50,32 +49,29 @@ wc(const char *filename, struct wc_values *data)
data->total_bytes += data->bytes;
data->total_lines += data->lines;
data->total_words += data->words;
fclose(file);
return 0;
}
void
print_values(const char *filename, struct wc_values data)
print_values(const char* filename, struct wc_values data)
{
if(show_bytes && show_lines && show_words)
printf("%i %i %i",
data.lines,
data.words,
data.bytes);
else {
if(!show_lines)
printf("%i ",data.lines);
if(!show_words)
printf("%i ",data.words);
if(!show_bytes)
printf("%i ",data.bytes);
if (show_bytes && show_lines && show_words)
printf("%i %i %i", data.lines, data.words, data.bytes);
else {
if (!show_lines)
printf("%i ", data.lines);
if (!show_words)
printf("%i ", data.words);
if (!show_bytes)
printf("%i ", data.bytes);
}
printf(" %s\n",filename);
printf(" %s\n", filename);
}
int
main(int argc, char *argv[])
main(int argc, char* argv[])
{
int c;
struct wc_values data;
@ -83,39 +79,37 @@ main(int argc, char *argv[])
int return_value = 0; /* Please let me know a better name */
show_lines = show_words = show_bytes = 1;
/* Process arguments */
while((c = getopt(argc,argv,"lwcm")) > 0){
switch(c) {
case 'l':
show_lines = 0;
break;
case 'w':
show_words = 0;
break;
case 'c':
case 'm':
show_bytes = 0;
break;
while ((c = getopt(argc, argv, "lwcm")) > 0) {
switch (c) {
case 'l':
show_lines = 0;
break;
case 'w':
show_words = 0;
break;
case 'c':
case 'm':
show_bytes = 0;
break;
}
}
if(optind == argc) {
wc("/dev/stdin",&data); /* lol */
print_values("stdin",data);
}
else
for(int i = optind; i<argc; i++) {
if(argv[i][0] == '-' && argv[i][1] == '\0')
return_value = wc("/dev/stdin",&data);
if (optind == argc) {
wc("/dev/stdin", &data); /* lol */
print_values("stdin", data);
} else
for (int i = optind; i < argc; i++) {
if (argv[i][0] == '-' && argv[i][1] == '\0')
return_value = wc("/dev/stdin", &data);
else
return_value = wc(argv[i],&data);
return_value = wc(argv[i], &data);
if (return_value == 0)
print_values(argv[i],data);
print_values(argv[i], data);
}
if(argc > optind+1)
printf("%i %i %i total\n",
data.total_lines,
data.total_words,
data.total_bytes);
if (argc > optind + 1)
printf("%i %i %i total\n",
data.total_lines,
data.total_words,
data.total_bytes);
return 0;
}

View File

@ -1,10 +1,10 @@
#include <stdio.h>
#include <pwd.h>
#include <stdio.h>
#include <unistd.h>
int
main(void)
{
struct passwd *user_data = getpwuid(getuid());
printf("%s\n",user_data->pw_name);
struct passwd* user_data = getpwuid(getuid());
printf("%s\n", user_data->pw_name);
}

View File

@ -1,11 +1,13 @@
#include <stdio.h>
int
main(int argc, char *argv[])
main(int argc, char* argv[])
{
if(argc > 1)
while(1) puts(argv[1]);
else
while(1) puts("y");
return 0;
if (argc > 1)
while (1)
puts(argv[1]);
else
while (1)
puts("y");
return 0;
}