aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFerass El Hafidi <vitali64pmemail@protonmail.com>2023-01-11 17:57:42 +0100
committerFerass El Hafidi <vitali64pmemail@protonmail.com>2023-01-11 17:57:42 +0100
commitd889bb84d54568f9bdb6e032d1269f30e84df8e5 (patch)
treed84c415130a480d086576d3b8783d4e926e9e536
parent413a53bed6a0afa74a2111b48312c7614baaee4d (diff)
downloadfases-d889bb84d54568f9bdb6e032d1269f30e84df8e5.tar.gz
fases-d889bb84d54568f9bdb6e032d1269f30e84df8e5.zip
core/: Properly use getopt()
Signed-off-by: Ferass El Hafidi <vitali64pmemail@protonmail.com>
-rw-r--r--core/chmod.c20
-rw-r--r--core/chown.c7
-rw-r--r--core/dirname.c2
-rw-r--r--core/head.c4
-rw-r--r--core/ln.c28
-rw-r--r--core/ls.c21
-rw-r--r--core/mkdir.c9
-rw-r--r--core/more.c20
-rw-r--r--core/mv.c17
-rw-r--r--core/rm.c11
-rw-r--r--core/tail.c41
-rw-r--r--core/test.c2
-rw-r--r--core/uname.c9
-rw-r--r--core/unlink.c2
14 files changed, 86 insertions, 107 deletions
diff --git a/core/chmod.c b/core/chmod.c
index 04cf04f..a1898a1 100644
--- a/core/chmod.c
+++ b/core/chmod.c
@@ -30,6 +30,7 @@
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#define REQ_PRINT_USAGE /* Require print_usage() from common.h */
#define DESCRIPTION "Change file modes."
@@ -39,6 +40,7 @@
int main(int argc, char *const argv[]) {
int argument, i = 0;
mode_t owner_modes, group_modes, other_modes;
+ char *argv0 = strdup(argv[0]);
if (argc == 1) {
print_usage(argv[0], DESCRIPTION, OPERANDS, VERSION);
return 1;
@@ -48,12 +50,12 @@ int main(int argc, char *const argv[]) {
print_usage(argv[0], DESCRIPTION, OPERANDS, VERSION);
return 1;
}
- }
+ } argc -= optind; argv += optind;
/* I know there's a better way of doing it, please let me know if you
* know a better way of doing it
*/
- if (argv[1][i] == '0') i++;
- switch (argv[1][i]) {
+ if (argv[0][i] == '0') i++;
+ switch (argv[0][i]) {
/* Owner modes */
case '7':
owner_modes = S_IRWXU;
@@ -68,11 +70,11 @@ int main(int argc, char *const argv[]) {
owner_modes = S_IXUSR;
break;
default:
- print_usage(argv[0], DESCRIPTION, OPERANDS, VERSION);
+ print_usage(argv0, DESCRIPTION, OPERANDS, VERSION);
return 1;
}
i++;
- switch (argv[1][i]) {
+ switch (argv[0][i]) {
/* Group modes */
case '7':
group_modes = S_IRWXG;
@@ -87,11 +89,11 @@ int main(int argc, char *const argv[]) {
group_modes = S_IXGRP;
break;
default:
- print_usage(argv[0], DESCRIPTION, OPERANDS, VERSION);
+ print_usage(argv0, DESCRIPTION, OPERANDS, VERSION);
return 1;
}
i++;
- switch (argv[1][i]) {
+ switch (argv[0][i]) {
/* Other modes */
case '7':
other_modes = S_IRWXO;
@@ -106,10 +108,10 @@ int main(int argc, char *const argv[]) {
other_modes = S_IXOTH;
break;
default:
- print_usage(argv[0], DESCRIPTION, OPERANDS, VERSION);
+ print_usage(argv0, DESCRIPTION, OPERANDS, VERSION);
return 1;
}
- chmod(argv[2], owner_modes | group_modes | other_modes);
+ chmod(argv[1], owner_modes | group_modes | other_modes);
return 0;
}
diff --git a/core/chown.c b/core/chown.c
index 09796a0..8ced12e 100644
--- a/core/chown.c
+++ b/core/chown.c
@@ -40,19 +40,12 @@
#include "common.h"
int main(int argc, char *const argv[]) {
- int argument;
struct passwd *user;
char *argv0 = strdup(argv[0]);
if (argc == 1) {
print_usage(argv[0], DESCRIPTION, OPERANDS, VERSION);
return 1;
}
- while ((argument = getopt(argc, argv, "")) != -1) {
- if (argument == '?') {
- print_usage(argv[0], DESCRIPTION, OPERANDS, VERSION);
- return 1;
- }
- } argc -= optind; argv += optind;
if ((user = getpwnam(argv[0])) == NULL && (user = getpwuid(strtol(argv[0], NULL, 10))) == NULL)
return errprint(argv0, argv[0], errno); /* User doesn't exist */
/* User found! */
diff --git a/core/dirname.c b/core/dirname.c
index a6a6594..bc2c1e3 100644
--- a/core/dirname.c
+++ b/core/dirname.c
@@ -37,8 +37,6 @@
#define OPERANDS "string"
#include "common.h"
-int getopt(int argc, char *const argv[], const char *optstring);
-
int main(int argc, char *argv[]) {
char *dirnamestr;
diff --git a/core/head.c b/core/head.c
index 6b1ba93..4c981e8 100644
--- a/core/head.c
+++ b/core/head.c
@@ -41,7 +41,7 @@
#include "common.h"
int main(int argc, char *const argv[]) {
- int argument, i = 1, lines, lines_printed;
+ int argument, i, lines, lines_printed;
FILE *file;
char s[4096], *argv0 = strdup(argv[0]);
@@ -62,7 +62,7 @@ int main(int argc, char *const argv[]) {
printf("%s", s);
}
if (!lines) lines = 10;
- for (i = 1; i != argc; i++) {
+ for (i = 0; i != argc; i++) {
if (strcmp(argv[i], "-")) file = fopen(argv[i], "r");
else while (read(STDIN_FILENO, s, 4096) > 0) printf("%s", s);
if (file == NULL)
diff --git a/core/ln.c b/core/ln.c
index 1750543..28a17ca 100644
--- a/core/ln.c
+++ b/core/ln.c
@@ -53,24 +53,22 @@ int main(int argc, char *const argv[]) {
return 1;
}
param[argument] = argument;
- }
+ } argc -= optind; argv += optind;
- for (int i = 1; i < argc - 1; i++) {
- if (argv[argc - 1][0] == '-') argc--;
- if (argv[i][0] != '-') {
- if (param['f']) remove(argv[argc - 1]);
- if (param['s']) symlink(argv[i], argv[argc - 1]);
- /* The -P option is the default behavior (at least on musl),
- * so no if statement.
- */
- else if (param['L']) {
- readlink(argv[i], buffer, strlen(buffer)); /* Read the link */
- if (errno) return errprint(argv0, argv[i], errno);
- link(buffer, argv[argc - 1]);
- }
- else link(argv[i], argv[argc - 1]);
+ for (int i = 0; i < argc; i++) {
+ if (param['f']) remove(argv[argc - 1]);
+ if (param['s']) symlink(argv[i], argv[argc - 1]);
+ /* The -P option is the default behavior (at least on musl),
+ * so no if statement.
+ */
+ else if (param['L']) {
+ readlink(argv[i], buffer, strlen(buffer)); /* Read the link */
if (errno) return errprint(argv0, argv[i], errno);
+ link(buffer, argv[argc - 1]);
}
+ else link(argv[i], argv[argc - 1]);
+ if (errno) return errprint(argv0, argv[i], errno);
+
}
return 0;
}
diff --git a/core/ls.c b/core/ls.c
index e7a21d2..7076243 100644
--- a/core/ls.c
+++ b/core/ls.c
@@ -42,13 +42,14 @@
#include <sys/ioctl.h>
#define REQ_PRINT_USAGE /* Require print_usage() from common.h */
+#define REQ_ERRPRINT /* Require errprint() from common.h */
#define DESCRIPTION "Print <directory>'s contents to standard output.\
If no directory is specified, print the current directory's contents."
#define OPERANDS "[-1aACRimlpgno] [directory]"
#include "common.h"
+char *argv0;
char param[256];
-int getopt(int argc, char *const argv[], const char *optstring);
int ls(char *path);
void printUsage(char *params);
@@ -57,8 +58,8 @@ int main(int argc, char *argv[]) {
int success = 0;
int argument, i;
char* params = "aACR1imlpgno";
-
- for(i=0; i<256; i++) {
+ argv0 = strdup(argv[0]);
+ for (i=0; i<256; i++) {
param[i]=0;
}
@@ -84,7 +85,7 @@ int main(int argc, char *argv[]) {
if (argument=='o' || argument=='n' || argument=='g') {
param['l'] = 'l';
}
- }
+ } argc -= optind; argv += optind;
if (status) {
if(!param['1']) printf("\n");
return status;
@@ -99,7 +100,7 @@ int main(int argc, char *argv[]) {
param['1'] = '1';
}
- for (i = 1; i < argc; i++) {
+ for (i = 0; i < argc; i++) {
if ((success |= (argv[i][0] != '-' ? 1 : 0))) {
if (!strcmp(argv[i],".")) status |= ls("./");
else status |= ls(argv[i]);
@@ -107,7 +108,7 @@ int main(int argc, char *argv[]) {
}
i = success ? status : ls("./");
- if(!param['1'])
+ if (!param['1'])
printf("\n");
return i;
@@ -131,10 +132,10 @@ int ls(char *path) {
if (directory == NULL) {
file = open(path, O_RDONLY);
- if (file == -1) return errno;
+ if (file == -1) return errprint(argv0, path, errno);
printf("%s\n", path);
- close(file);
- return errno;
+ if (close(file) == -1)
+ return errprint(argv0, path, errno);
}
if (param['R']) {
@@ -245,5 +246,5 @@ int ls(char *path) {
}
closedir(directory);
- return errno;
+ return errprint(argv0, path, errno);
}
diff --git a/core/mkdir.c b/core/mkdir.c
index 5fb7260..8eb75c2 100644
--- a/core/mkdir.c
+++ b/core/mkdir.c
@@ -30,6 +30,7 @@
#include <stdio.h>
#include <sys/stat.h>
#include <errno.h>
+#include <string.h>
#define REQ_PRINT_USAGE /* Require print_usage() from common.h */
#define REQ_ERRPRINT /* Require errprint() from common.h */
@@ -38,8 +39,8 @@
#include "common.h"
int main(int argc, char *const argv[]) {
- int success, argument, i = 1;
-
+ int success, argument, i = 0;
+ char *argv0 = strdup(argv[0]);
if (argc == 1) {
print_usage(argv[0], DESCRIPTION, OPERANDS, VERSION);
return 1;
@@ -50,13 +51,13 @@ int main(int argc, char *const argv[]) {
print_usage(argv[0], DESCRIPTION, OPERANDS, VERSION);
return 0;
}
- }
+ } argc -= optind; argv += optind;
for (; i < argc; i++) {
if (argv[i][0] != '-')
success = !mkdir(argv[i], S_IRWXU | S_IRWXG | S_IRWXO) ? 0 : 1;
if (success == 1) {
- return errprint(argv[0], argv[i], errno);
+ return errprint(argv0, argv[i], errno);
}
}
diff --git a/core/more.c b/core/more.c
index d4dc39c..2a7eb30 100644
--- a/core/more.c
+++ b/core/more.c
@@ -40,12 +40,10 @@
#define OPERANDS "file ..."
#include "common.h"
-int getopt(int argc, char *const argv[], const char *optstring);
-
int main(int argc, char *const argv[]) {
int i = 0, argument, success, read_file;
long int /* columns, */ lines;
- char buffer[4096], cmd;
+ char buffer[4096], cmd, *argv0 = strdup(argv[0]);
struct winsize w;
FILE *file;
struct termios oldattr, newattr;
@@ -60,12 +58,12 @@ int main(int argc, char *const argv[]) {
print_usage(argv[0], DESCRIPTION, OPERANDS, VERSION);
return 1;
}
+ } argc -= optind; argv += optind;
+ if (argc != 1) {
+ print_usage(argv0, DESCRIPTION, OPERANDS, VERSION); return 1;
}
- if (argc != 2) {
- print_usage(argv[0], DESCRIPTION, OPERANDS, VERSION); return 1;
- }
- file = fopen(argv[1], "r");
- if (errno) return errprint(argv[0], argv[1], errno);
+ file = fopen(argv[0], "r");
+ if (errno) return errprint(argv0, argv[0], errno);
success = 0;
while (!success) {
if (!read_file) read_file = 1; /* 1 => read on a page-by-page basis
@@ -76,7 +74,7 @@ int main(int argc, char *const argv[]) {
if (fgets(buffer, 4096, file) != NULL) {
printf("%s", buffer);
}
- else if (errno) return errprint(argv[0], "fgets()", errno);
+ else if (errno) return errprint(argv0, "fgets()", errno);
else {
success = 1;
break;
@@ -95,12 +93,12 @@ int main(int argc, char *const argv[]) {
cmd = getchar();
tcsetattr(0, TCSANOW, &oldattr); /* Restore old parameters */
- if (errno) return errprint(argv[0], NULL, errno);
+ if (errno) return errprint(argv0, NULL, errno);
switch (cmd) {
case 'q':
return 0;
case 'h':
- print_usage(argv[0], DESCRIPTION, OPERANDS, VERSION);
+ print_usage(argv0, DESCRIPTION, OPERANDS, VERSION);
read_file = 0;
break;
case ' ':
diff --git a/core/mv.c b/core/mv.c
index df3059a..a2eaab4 100644
--- a/core/mv.c
+++ b/core/mv.c
@@ -30,6 +30,7 @@
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
+#include <string.h>
#define REQ_PRINT_USAGE /* Require print_usage() from common.h */
#define REQ_ERRPRINT /* Require errprint() from common.h */
@@ -37,11 +38,9 @@
#define OPERANDS "[-if] source dest"
#include "common.h"
-int getopt(int argc, char *const argv[], const char *optstring);
-
int main(int argc, char *const argv[]) {
int argument, file;
- char cmd, param[256];
+ char cmd, param[256], *argv0 = strdup(argv[0]);
setvbuf(stdout, NULL, _IONBF, 0);
for (int i = 0; i < 256; i++) param[i] = 0; /* Initialise param,
* very important. */
@@ -53,13 +52,13 @@ int main(int argc, char *const argv[]) {
param[argument] = argument;
if (argument == 'f') param['i'] = 0;
if (argument == 'i') param['f'] = 0;
- }
+ } argc -= optind; argv += optind;
if (!param['f']) param['i'] = 'i';
- if (argc < 3) {
- print_usage(argv[0], DESCRIPTION, OPERANDS, VERSION);
+ if (argc < 2) {
+ print_usage(argv0, DESCRIPTION, OPERANDS, VERSION);
return 1;
}
- if ((file = open(argv[2], O_RDONLY)) != -1 && param['i']) {
+ if ((file = open(argv[1], O_RDONLY)) != -1 && param['i']) {
printf("File exists, override it? (y/n) ");
read(STDIN_FILENO, &cmd, 1);
if (cmd == 'n' || cmd == 'N') {
@@ -69,8 +68,8 @@ int main(int argc, char *const argv[]) {
}
close(file); /* In case it hasn't been closed */
- if (rename(argv[1], argv[2]))
+ if (rename(argv[0], argv[1]))
/* Technically, moving files == renaming files */
- return errprint(argv[0], NULL, errno);
+ return errprint(argv0, NULL, errno);
return 0;
}
diff --git a/core/rm.c b/core/rm.c
index 0793f00..d1a4e2e 100644
--- a/core/rm.c
+++ b/core/rm.c
@@ -29,6 +29,7 @@
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
+#include <string.h>
#define REQ_PRINT_USAGE /* Require print_usage() from common.h */
#define REQ_ERRPRINT /* Require errprint() from common.h */
@@ -37,8 +38,8 @@
#include "common.h"
int main(int argc, char *const argv[]) {
- int argument, i = 1;
- char param[256];
+ int argument, i = 0;
+ char param[256], *argv0 = strdup(argv[0]);
while ((argument = getopt(argc, argv, "Rr")) != -1) {
if (argument == '?') {
@@ -46,8 +47,8 @@ int main(int argc, char *const argv[]) {
return 1;
}
param[argument] = argument;
- }
- if (argc <= 1) {
+ } argc -= optind; argv += optind;
+ if (argc < 1) {
print_usage(argv[0], DESCRIPTION, OPERANDS, VERSION);
return 1;
}
@@ -57,7 +58,7 @@ int main(int argc, char *const argv[]) {
else remove(argv[i]); /* TODO: Actually
* recursively remove
* the directory */
- if (errno) return errprint(argv[0], argv[i], errno);
+ if (errno) return errprint(argv0, argv[i], errno);
}
return 0;
diff --git a/core/tail.c b/core/tail.c
index e0e5881..075eb00 100644
--- a/core/tail.c
+++ b/core/tail.c
@@ -40,15 +40,11 @@
#define OPERANDS "[-n number] [file] ..."
#include "common.h"
-/* Functions Prototypes & Variables */
-extern char *optarg;
-int getopt(int argc, char *const argv[], const char *optstring);
-
int main(int argc, char *const argv[]) {
int argument, i = 1, lines, file_lines;
FILE *file;
- char s[4096];
+ char s[4096], *argv0 = strdup(argv[0]);
while ((argument = getopt(argc, argv, "n:")) != -1) {
if (argument == '?' || argument == ':') {
@@ -61,30 +57,27 @@ int main(int argc, char *const argv[]) {
}
else
lines = 10;
- }
- if (argc < 2) {
+ } argc -= optind; argv += optind;
+ if (argc < 1) {
while (read(STDIN_FILENO, s, 4096) > 0)
printf("%s", s);
}
if (!lines) lines = 10;
- for (i = 1; i != argc; i++) {
- if (strcmp(argv[i], "-n")) {
- if (strcmp(argv[i], "-")) file = fopen(argv[i], "r");
- else while (read(STDIN_FILENO, s, 4096) > 0) printf("%s", s);
- if (file == NULL)
- return errprint(argv[0], argv[i], errno); /* Something went wrong */
- while (fgets(s, 4096, file) != NULL)
- file_lines++; /* Get number of lines */
- fclose(file);
- file_lines = file_lines - lines;
- if (strcmp(argv[i], "-")) file = fopen(argv[i], "r");
- while (fgets(s, 4096, file) != NULL) {
- if (errno) return errprint(argv[0], argv[i], errno);
- if (file_lines == 0) printf("%s", s);
- else file_lines--;
- }
+ for (i = 0; i != argc; i++) {
+ if (strcmp(argv[i], "-")) file = fopen(argv[i], "r");
+ else while (read(STDIN_FILENO, s, 4096) > 0) printf("%s", s);
+ if (file == NULL)
+ return errprint(argv[0], argv[i], errno); /* Something went wrong */
+ while (fgets(s, 4096, file) != NULL)
+ file_lines++; /* Get number of lines */
+ fclose(file);
+ file_lines = file_lines - lines;
+ if (strcmp(argv[i], "-")) file = fopen(argv[i], "r");
+ while (fgets(s, 4096, file) != NULL) {
+ if (errno) return errprint(argv0, argv[i], errno);
+ if (file_lines == 0) printf("%s", s);
+ else file_lines--;
}
- else i++;
}
return 0;
diff --git a/core/test.c b/core/test.c
index 6d6f280..1655ff8 100644
--- a/core/test.c
+++ b/core/test.c
@@ -36,8 +36,6 @@
#include <string.h>
#include <stdlib.h>
-int getopt(int argc, char *const argv[], const char *optstring);
-
/* For readability reasons, define true, false */
int true = 0;
int false = 1;
diff --git a/core/uname.c b/core/uname.c
index beec94a..f4881c6 100644
--- a/core/uname.c
+++ b/core/uname.c
@@ -30,6 +30,7 @@
#include <stdio.h>
#include <sys/utsname.h>
#include <errno.h>
+#include <string.h>
#define REQ_PRINT_USAGE /* Require print_usage() from common.h */
#define REQ_ERRPRINT /* Require errprint() from common.h */
@@ -37,14 +38,12 @@
#define OPERANDS "[-amnrsv]"
#include "common.h"
-int getopt(int argc, char *const argv[], const char *optstring);
-
int main(int argc, char *const argv[]) {
int argument;
struct utsname name;
char param[256];
for (int i = 0; i < 256; i++) param[i] = 0;
-
+ char *argv0 = strdup(argv[0]);
while ((argument = getopt(argc, argv, "amnrsv")) != -1) {
if (argument == '?') {
print_usage(argv[0], DESCRIPTION, OPERANDS, VERSION);
@@ -58,10 +57,10 @@ int main(int argc, char *const argv[]) {
param['v'] = 'v';
param['m'] = 'm';
}
- }
+ } argc -= optind; argv += optind;
uname(&name);
- if (errno) return errprint(argv[0], NULL, errno);
+ if (errno) return errprint(argv0, NULL, errno);
if (argc > 1) {
if (param['s']) printf("%s ", name.sysname);
if (param['n']) printf("%s ", name.nodename);
diff --git a/core/unlink.c b/core/unlink.c
index 0960808..f237c3c 100644
--- a/core/unlink.c
+++ b/core/unlink.c
@@ -36,8 +36,6 @@
#define OPERANDS "file"
#include "common.h"
-int getopt(int argc, char *const argv[], const char *optstring);
-
int main(int argc, char *const argv[]) {
if (argc != 2) {
print_usage(argv[0], DESCRIPTION, OPERANDS, VERSION);