Index: cvslatest.c =================================================================== RCS file: /cvsroot/src/usr.bin/cvslatest/cvslatest.c,v retrieving revision 1.6 diff -p -u -u -r1.6 cvslatest.c --- cvslatest.c 24 Sep 2017 09:43:27 -0000 1.6 +++ cvslatest.c 11 Mar 2018 01:27:37 -0000 @@ -1,11 +1,11 @@ /* $NetBSD: cvslatest.c,v 1.6 2017/09/24 09:43:27 joerg Exp $ */ /*- - * Copyright (c) 2016 The NetBSD Foundation, Inc. + * Copyright (c) 2016-2018 The NetBSD Foundation, Inc. * All rights reserved. * * This code is derived from software contributed to The NetBSD Foundation - * by Christos Zoulas. + * by Christos Zoulas and Matthew R. Green. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -55,6 +55,7 @@ __RCSID("$NetBSD: cvslatest.c,v 1.6 2017 #include #include #include +#include static int debug = 0; static int ignore = 0; @@ -166,6 +167,56 @@ cvsscan(char **pathv, const char *name, (void)fts_close(dh); } +static const char * +get_CVS_dir(void) +{ + const char *defname = "CVS", *name = NULL; + const char *home = getenv("HOME"); + char *line; + FILE *fp = NULL; + char cvsrc[PATH_MAX]; + static char token[PATH_MAX]; + enum { + find_cvs, find_D, find_path + } state = find_cvs; + + if (!home) + goto def; + if (snprintf(cvsrc, PATH_MAX, "%s/.cvsrc", home) >= PATH_MAX) + goto def; + fp = fopen(cvsrc, "r"); + if (fp == NULL) + goto def; + + /* Look for "cvs" "-D" "something". */ + while (fscanf(fp, "%1024s", token) == 1) { + if (state == find_cvs) { + if (strcmp(token, "cvs") != 0) + continue; + state = find_D; + } else if (state == find_D) { + if (*token != '-') + continue; + line = token + 1; + while (*line && *line != 'D') + line++; + if (!*line) + continue; + state = find_path; + } else if (state == find_path) { + name = token; + break; + } + } + +def: + if (fp) + fclose(fp); + if (name == NULL) + name = defname; + return name; +} + static __dead void usage(void) { @@ -178,7 +229,7 @@ int main(int argc, char *argv[]) { struct latest lat; - const char *name = "CVS"; + const char *name = NULL; int c; while ((c = getopt(argc, argv, "din:")) != -1) @@ -196,6 +247,9 @@ main(int argc, char *argv[]) usage(); } + if (name == NULL) + name = get_CVS_dir(); + if (argc == optind) usage();