new package: zstd (#1786)
[termux-packages] / packages / command-not-found / command-not-found.c
index 42e4e48..5a609f5 100644 (file)
@@ -4,11 +4,11 @@
 
 #include "commands.h"
 
-inline int termux_min3(unsigned int a, unsigned int b, unsigned int c) {
+static inline int termux_min3(unsigned int a, unsigned int b, unsigned int c) {
        return (a < b ? (a < c ? a : c) : (b < c ? b : c));
 }
 
-int termux_levenshtein_distance(char const* restrict s1, char const* restrict s2) {
+static int termux_levenshtein_distance(char const* restrict s1, char const* restrict s2) {
        unsigned int s1len = strlen(s1);
        unsigned int s2len = strlen(s2);
        unsigned int matrix[s2len+1][s1len+1];
@@ -42,8 +42,13 @@ int main(int argc, char** argv) {
                        char const* binary_name = current_line + 1;
                        int distance = termux_levenshtein_distance(command_not_found, binary_name);
                        if (distance == 0 && strcmp(command_not_found, binary_name) == 0) {
-                               printf("The program '%s' is not installed. Install it by executing:\n apt install %s\n", binary_name, current_package);
-                               return 0;
+                               if (best_distance == 0) {
+                                       fprintf(stderr, "or\n");
+                               } else {
+                                       fprintf(stderr, "The program '%s' is not installed. Install it by executing:\n", binary_name);
+                               }
+                               fprintf(stderr, " pkg install %s\n", current_package);
+                               best_distance = 0;
                        } else if (best_distance == distance) {
                                guesses_at_best_distance++;
                        } else if (best_distance == -1 || best_distance > distance) {
@@ -57,13 +62,15 @@ int main(int argc, char** argv) {
                }
        }
 
+       if (best_distance == 0) return 127;
+
        if (best_distance == -1 || best_distance > 3) {
-               printf("%s: command not found\n", command_not_found);
+               fprintf(stderr, "%s: command not found\n", command_not_found);
        } else {
-               printf("No command '%s' found, did you mean:\n", command_not_found);
+               fprintf(stderr, "No command '%s' found, did you mean:\n", command_not_found);
                if (guesses_at_best_distance == 1) {
                        // Only one suggestion - show it:
-                       printf(" Command '%s' from package '%s'\n", best_command_guess, best_package_guess);
+                       fprintf(stderr, " Command '%s' from package '%s'\n", best_command_guess, best_package_guess);
                } else {
                        // Multiple suggestions at the same distance - show them all:
                        for (int i = 0; i < num_commands; i++) {
@@ -72,7 +79,7 @@ int main(int argc, char** argv) {
                                        char const* binary_name = current_line + 1;
                                        int distance = termux_levenshtein_distance(command_not_found, binary_name);
                                        if (best_distance == distance) {
-                                               printf(" Command '%s' from package '%s'\n", binary_name, current_package);
+                                               fprintf(stderr, " Command '%s' from package '%s'\n", binary_name, current_package);
                                        }
                                } else { // Package
                                        strncpy(current_package, current_line, sizeof(current_package));
@@ -80,5 +87,6 @@ int main(int argc, char** argv) {
                        }
                }
        }
+       return 127;
 }