coreutils

General Software Utilities
git clone http://git.omkov.net/coreutils
Log | Tree | Refs | README | LICENCE | Download

AuthorJamozed <[email protected]>
Date2020-09-16 13:05:46
Commitfbdf0770ac269a8775cb359a4ca462dc05ad38cb
Parente65742c42d43f63419b5702c1fdb0b7756dafffa

basename: Add comments

Diffstat

M src/basename.c | 20 ++++++++++++++++----

1 files changed, 16 insertions, 4 deletions

diff --git a/src/basename.c b/src/basename.c
index 1b93e9c..77bd9be 100644
--- a/src/basename.c
+++ b/src/basename.c
@@ -57,23 +57,34 @@ int main(int ac, char *av[]) { A0 = av[0];
 	if (opt.ind == ac) { error(1, "missing operand"); }
 
 	register char *p = av[opt.ind];
+	
+	// If the string is empty, print "."
 	if (!*p) { fputc('.', stdout); fputc('\n', stdout); return 0; }
 
+	// From the end of the string, move left to the first non '/' character
 	for (++p; *p; ++p) {} for (--p; *p == '/'; --p);
+	
+	// If the string contains only '/' characters, print "/"
 	if (p + 1 == av[opt.ind]) { fputs("/\n", stdout); return 0; }
-	else { p[1] = 0; }
+	else { p[1] = 0; } // Otherwise remove the trailing '/' characters
 
-	if (av[opt.ind + 1]) {
+	if (av[opt.ind + 1]) { // If a suffix operand is provided
 		register char *s = av[opt.ind + 1]; for (; *s; ++s);
+		
+		// Move left through string and suffix as long as they are the same
 		for (--s; *p && *p != '/' && *s && *p == *s; --p, --s);
+		
+		// If the suffix is matched completely and characters remain in the
+		// string without it, remove the suffix from the string
 		if (!*s && *p && *p != '/') { p[1] = 0; }
 	}
 
-	for (; *p && *p != '/'; --p);
-	fputs(p + 1, stdout); fputc('\n', stdout);
+	// Move pointer left until the start of the string or '/' is found and print
+	for (; *p && *p != '/'; --p) {} fputs(p + 1, stdout); fputc('\n', stdout);
 	return 0;
 }
 
+/* Print help information */
 static void hlp(void) {
 	puts("basename - return the non-directory portion of a path\n");
 	puts("usage: basename string [suffix]\n");
@@ -83,6 +94,7 @@ static void hlp(void) {
 	return;
 }
 
+/* Print version information */
 static void ver(void) {
 	puts("OMKOV coreutils basename, version " VERSION);
 	puts("Copyright (C) 2020, Jakob Wakeling");