diff options
author | wosch <wosch@FreeBSD.org> | 1996-08-22 18:46:13 +0000 |
---|---|---|
committer | wosch <wosch@FreeBSD.org> | 1996-08-22 18:46:13 +0000 |
commit | 1647af0a74903fc3f24ba1c8dff768245ba03cae (patch) | |
tree | 72d248c17a7252aeec5a920535090abe27057f50 /usr.bin | |
parent | 654c394b55bf04b2f525ade4403d26976414c59c (diff) | |
download | FreeBSD-src-1647af0a74903fc3f24ba1c8dff768245ba03cae.zip FreeBSD-src-1647af0a74903fc3f24ba1c8dff768245ba03cae.tar.gz |
code cleanup
Diffstat (limited to 'usr.bin')
-rw-r--r-- | usr.bin/locate/bigram/locate.bigram.c | 49 | ||||
-rw-r--r-- | usr.bin/locate/code/locate.code.c | 19 |
2 files changed, 35 insertions, 33 deletions
diff --git a/usr.bin/locate/bigram/locate.bigram.c b/usr.bin/locate/bigram/locate.bigram.c index dc95399..5b83a3b 100644 --- a/usr.bin/locate/bigram/locate.bigram.c +++ b/usr.bin/locate/bigram/locate.bigram.c @@ -32,6 +32,8 @@ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. + * + * $Id$ */ #ifndef lint @@ -45,7 +47,8 @@ static char sccsid[] = "@(#)locate.bigram.c 8.1 (Berkeley) 6/6/93"; #endif /* not lint */ /* - * bigram < text > bigrams + * bigram < sorted_file_names | sort -nr | + * awk 'NR <= 128 { printf $2 }' > bigrams * * List bigrams for 'updatedb' script. * Use 'code' to encode a file using this output. @@ -58,21 +61,16 @@ static char sccsid[] = "@(#)locate.bigram.c 8.1 (Berkeley) 6/6/93"; u_char buf1[MAXPATHLEN] = " "; u_char buf2[MAXPATHLEN]; -unsigned int bigram[UCHAR_MAX][UCHAR_MAX]; - +u_int bigram[UCHAR_MAX][UCHAR_MAX]; -void main ( ) +int +main(void) { register u_char *cp; register u_char *oldpath = buf1, *path = buf2; - register int i, j; + register u_int i, j; - /* init bigram buffer */ - for (i = 0; i < UCHAR_MAX; i++) - for (j = 0; j < UCHAR_MAX; j++) - bigram[i][j] = 0; - - while ( fgets ( path, sizeof(buf2), stdin ) != NULL ) { + while (fgets(path, sizeof(buf2), stdin) != NULL) { /* skip empty lines */ if (*path == '\n') @@ -88,30 +86,29 @@ void main ( ) *cp = '?'; } - /* skip longest common prefix */ - for (cp = path; *cp == *oldpath && *cp; cp++, oldpath++); + for (cp = path; *cp == *oldpath && *cp != NULL; cp++, oldpath++); - /* - * output post-residue bigrams only - */ - - /* check later for boundary */ - while ( *cp != NULL && *(cp + 1) != NULL ) { + while (*cp != NULL && *(cp+1) != NULL) { bigram[*cp][*(cp+1)]++; cp += 2; } - if ( path == buf1 ) /* swap pointers */ - path = buf2, oldpath = buf1; - else - path = buf1, oldpath = buf2; + /* swap pointers */ + if (path == buf1) { + path = buf2; + oldpath = buf1; + } else { + path = buf1; + oldpath = buf2; + } } - /* output, boundary check */ + /* output, (paranoid) boundary check */ for (i = ASCII_MIN; i <= ASCII_MAX; i++) for (j = ASCII_MIN; j <= ASCII_MAX; j++) if (bigram[i][j] != 0) - fprintf(stdout, "%4d %c%c\n", - bigram[i][j], i, j); + printf("%4u %c%c\n", bigram[i][j], i, j); + + exit(0); } diff --git a/usr.bin/locate/code/locate.code.c b/usr.bin/locate/code/locate.code.c index 60be32a..4846c73 100644 --- a/usr.bin/locate/code/locate.code.c +++ b/usr.bin/locate/code/locate.code.c @@ -32,6 +32,8 @@ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. + * + * $Id$ */ #ifndef lint @@ -93,18 +95,19 @@ u_char buf1[MAXPATHLEN] = " "; u_char buf2[MAXPATHLEN]; char bigrams[BGBUFSIZE + 1] = { 0 }; -#define LOOKUP 1 +#define LOOKUP 1 /* use a lookup array instead a function, 3x faster */ + #ifdef LOOKUP #define BGINDEX(x) (big[(u_int)*x][(u_int)*(x+1)]) typedef u_char bg_t; bg_t big[UCHAR_MAX][UCHAR_MAX]; - #else #define BGINDEX(x) bgindex(x) typedef int bg_t; -#endif - int bgindex __P((char *)); +#endif /* LOOKUP */ + + void usage __P((void)); extern int optind; extern int optopt; @@ -135,6 +138,7 @@ main(argc, argv) /* First copy bigram array to stdout. */ (void)fgets(bigrams, BGBUFSIZE + 1, fp); + if (fwrite(bigrams, 1, BGBUFSIZE, stdout) != BGBUFSIZE) err(1, "stdout"); (void)fclose(fp); @@ -147,11 +151,12 @@ main(argc, argv) for (cp = bigrams, i = 0; *cp != NULL; i += 2, cp += 2) big[(int)*cp][(int)*(cp + 1)] = (bg_t)i; -#endif +#endif /* LOOKUP */ oldpath = buf1; path = buf2; oldcount = 0; + while (fgets(path, sizeof(buf2), stdin) != NULL) { /* skip empty lines */ @@ -169,7 +174,7 @@ main(argc, argv) } /* Skip longest common prefix. */ - for (cp = path; *cp == *oldpath && *cp; cp++, oldpath++); + for (cp = path; *cp == *oldpath && *cp != NULL; cp++, oldpath++); count = cp - path; diffcount = count - oldcount + OFFSET; @@ -225,7 +230,7 @@ bgindex(bg) /* Return location of bg in bigrams or -1. */ for (p = bigrams; *p != NULL; p++) if (*p++ == bg0 && *p == bg1) break; - return (*p == NULL ? -1 : --p - bigrams); + return (*p == NULL ? -1 : (--p - bigrams)); } #endif /* !LOOKUP */ |