pg(1): make it fit the project better

This commit is contained in:
dtb 2024-04-21 06:30:01 -06:00
parent e26d8e0e1b
commit ea8b58554e
Signed by: trinity
GPG Key ID: 31FF85CCB6DC7641
2 changed files with 36 additions and 11 deletions

View File

@ -1,4 +1,9 @@
.TH P 1 .\" Copyright (c) 2024 DTB <trinity@trinity.moe>
.\"
.\" This work is licensed under CC BY-SA 4.0. To see a copy of this license,
.\" visit <http://creativecommons.org/licenses/by-sa/4.0/>.
.TH PG 1
.SH NAME .SH NAME

View File

@ -1,8 +1,28 @@
#include <stdio.h> /*
#include <stdlib.h> * Copyright (c) 2024 DTB <trinity@trinity.moe>
#include <string.h> * SPDX-License-Identifier: AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
* details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*/
#if !defined EX_USAGE || !defined EX_SOFTWARE #include <stdio.h> /* fclose(3), feof(3), fgetc(3), fgets(3), fopen(3),
* fprintf(3), fputc(3), stderr, stdin, stdout, EOF, FILE,
* NULL */
#include <stdlib.h> /* size_t */
#include <string.h> /* strchr */
#if !defined EX_OK || !defined EX_SOFTWARE || !defined EX_USAGE
# include <sysexits.h> # include <sysexits.h>
#endif #endif
@ -14,13 +34,13 @@
static size_t l = 22; /* plan9 default */ static size_t l = 22; /* plan9 default */
static char *program_name = "p"; static char *program_name = "pg";
static int print_lines(FILE *f, size_t l){ static int print_lines(FILE *f, size_t l){
int c; int c;
while((c = getc(f)) != EOF) while((c = fgetc(f)) != EOF)
if((c = putc(c, stdout)) == EOF || (l -= (c == '\n')) == 0) if((c = fputc(c, stdout)) == EOF || (l -= (c == '\n')) == 0)
break; break;
return c; return c;
@ -45,12 +65,12 @@ int main(int argc, char *argv[]){
} }
while(print_lines(stdin, l) != EOF){ while(print_lines(stdin, l) != EOF){
fgets(cmd, (sizeof cmd) / (sizeof *cmd), t); fgets((char *)cmd, (sizeof cmd) / (sizeof *cmd), t);
for(p = cmd; *p != '\0' && strchr(" \t", *p) != NULL; ++p); for(p = (char *)cmd; *p != '\0' && strchr(" \t", *p) != NULL; ++p);
if(feof(t) || (*p | 32) == 'q') if(feof(t) || (*p | 32) == 'q')
break; break;
} }
exit: fclose(t); exit: fclose(t);
return 0; return EX_OK;
} }