[hackers] [sbase] update cmp, grep || Connor Lane Smith

From: <hg_AT_suckless.org>
Date: Sat, 18 Jun 2011 07:45:20 +0200 (CEST)

changeset: 85:f35314692343
user: Connor Lane Smith <cls_AT_lubutu.com>
date: Sat Jun 18 06:42:24 2011 +0100
files: cmp.c grep.1 grep.c
description:
update cmp, grep

diff -r 322aff19c976 -r f35314692343 cmp.c
--- a/cmp.c Sat Jun 18 06:41:28 2011 +0100
+++ b/cmp.c Sat Jun 18 06:42:24 2011 +0100
@@ -3,6 +3,9 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
+#include "util.h"
+
+enum { Same = 0, Diff = 1, Error = 2 };
 
 int
 main(int argc, char *argv[])
@@ -24,37 +27,31 @@
                         sflag = true;
                         break;
                 default:
- exit(2);
+ exit(Error);
                 }
- if(optind != argc-2) {
- fprintf(stderr, "usage: %s [-ls] file1 file2\n", argv[0]);
- exit(2);
- }
+ if(optind != argc-2)
+ enprintf(Error, "usage: %s [-ls] file1 file2\n", argv[0]);
         for(i = 0; i < 2; i++)
- if(!(fp[i] = fopen(argv[optind+i], "r"))) {
- fprintf(stderr, "fopen %s:", argv[optind+i]);
- exit(2);
- }
+ if(!(fp[i] = fopen(argv[optind+i], "r")))
+ enprintf(Error, "fopen %s:", argv[optind+i]);
         for(n = 1; ((b[0] = getc(fp[0])) != EOF) | ((b[1] = getc(fp[1])) != EOF); n++) {
                 if(b[0] == '\n')
                         line++;
                 if(b[0] == b[1])
                         continue;
                 for(i = 0; i < 2; i++)
- if(b[i] == EOF) {
- fprintf(stderr, "cmp: EOF on %s\n", argv[optind+i]);
- return 1;
- }
+ if(b[i] == EOF)
+ enprintf(Diff, "cmp: EOF on %s\n", argv[optind+i]);
                 if(!lflag) {
                         if(!sflag)
                                 printf("%s %s differ: char %ld, line %ld\n",
                                        argv[optind], argv[optind+1], n, line);
- return 1;
+ exit(Diff);
                 }
                 else {
                         printf("%4ld %3o %3o\n", n, b[0], b[1]);
                         same = false;
                 }
         }
- return same ? 0 : 1;
+ return same ? Same : Diff;
 }
diff -r 322aff19c976 -r f35314692343 grep.1
--- a/grep.1 Sat Jun 18 06:41:28 2011 +0100
+++ b/grep.1 Sat Jun 18 06:42:24 2011 +0100
@@ -8,7 +8,9 @@
 .RI [ file ...]
 .SH DESCRIPTION
 .B grep
-searches the input files for lines that match the pattern, a regular expression as defined in
+searches the input files for lines that match the
+.IR pattern ,
+a regular expression as defined in
 .IR regex (7).
 By default each matching line is printed to stdout. If no file is given, grep
 reads from stdin.
diff -r 322aff19c976 -r f35314692343 grep.c
--- a/grep.c Sat Jun 18 06:41:28 2011 +0100
+++ b/grep.c Sat Jun 18 06:42:24 2011 +0100
@@ -5,6 +5,9 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include "text.h"
+#include "util.h"
+
+enum { Match = 0, NoMatch = 1, Error = 2 };
 
 static void grep(FILE *, const char *, regex_t *);
 
@@ -39,27 +42,22 @@
                         vflag = true;
                         break;
                 default:
- exit(2);
+ exit(Error);
                 }
- if(optind == argc) {
- fprintf(stderr, "usage: %s [-Ecilnqv] pattern [files...]\n", argv[0]);
- exit(2);
- }
+ if(optind == argc)
+ enprintf(Error, "usage: %s [-Ecilnqv] pattern [files...]\n", argv[0]);
         regcomp(&preg, argv[optind++], flags);
 
         many = (argc > optind+1);
         if(optind == argc)
                 grep(stdin, "<stdin>", &preg);
         else for(; optind < argc; optind++) {
- if(!(fp = fopen(argv[optind], "r"))) {
- fprintf(stderr, "fopen %s: ", argv[optind]);
- perror(NULL);
- exit(2);
- }
+ if(!(fp = fopen(argv[optind], "r")))
+ enprintf(Error, "fopen %s:", argv[optind]);
                 grep(fp, argv[optind], &preg);
                 fclose(fp);
         }
- return match ? 0 : 1;
+ return match ? Match : NoMatch;
 }
 
 void
@@ -80,7 +78,7 @@
                         puts(str);
                         goto end;
                 case 'q':
- exit(0);
+ exit(Match);
                 default:
                         if(many)
                                 printf("%s:", str);
@@ -94,10 +92,7 @@
         if(mode == 'c')
                 printf("%ld\n", c);
 end:
- if(ferror(fp)) {
- fprintf(stderr, "%s: read error: ", str);
- perror(NULL);
- exit(2);
- }
+ if(ferror(fp))
+ enprintf(Error, "%s: read error:", str);
         free(buf);
 }
Received on Sat Jun 18 2011 - 07:45:20 CEST

This archive was generated by hypermail 2.2.0 : Sat Jun 18 2011 - 07:48:06 CEST