rev fix, nl add

This commit is contained in:
Your Name 2023-12-02 00:09:37 +03:00
parent 12a10f303d
commit 5a9b277de9
3 changed files with 75 additions and 3 deletions

66
coreutils/nl.c Normal file
View File

@ -0,0 +1,66 @@
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include "config.h"
unsigned int w_flag = 2;
unsigned int lines = 1;
int nl(const char *path) {
FILE *fp = stdin;
if (strcmp(path, "-"))
fp = fopen(path, "r");
if (fp == NULL) {
fprintf(stderr, "rev: %s: %s\n", path, strerror(errno));
return 1;
}
char buf[BUF_SIZE + 1];
while (fgets(buf, sizeof(buf), fp))
if (strlen(buf) > 1)
fprintf(stdout, "%*u\t%s", w_flag, lines++, buf);
putchar('\n');
if (strcmp(path, "-"))
fclose(fp);
return 0;
}
int main(int argc, char **argv) {
int opt;
while ((opt = getopt(argc, argv, "w:v:")) != -1) {
switch (opt) {
case 'w':
w_flag = atoi(optarg);
break;
case 'v':
lines = atoi(optarg);
break;
default:
printf("nl [file1 file2...]\n\t[-w N Width of line numbers]\n\t[-v N Start from N]\n");
return 0;
}
}
argv += optind;
argc -= optind;
if (argc == 0)
return nl("-");
else {
int ret = 0;
for (int i = 0; i < argc; i++)
if (nl(argv[i]))
ret = 1;
return ret;
}
}

6
sysutils/nologin.c Normal file
View File

@ -0,0 +1,6 @@
#include <stdio.h>
int main(void) {
printf("nologin: shell disabled\n");
return 0;
}

View File

@ -16,7 +16,7 @@ void strrev(char *str, off_t len) {
}
}
int cat(const char *path) {
int rev(const char *path) {
FILE *fp = stdin;
if (strcmp(path, "-"))
@ -51,8 +51,8 @@ int main(int argc, char **argv) {
argc -= optind;
if (argc == 0)
return cat("-");
return rev("-");
else
return cat(argv[0]);
return rev(argv[0]);
}