Annotation of solidite/utils/basename.c, revision 1.1

1.1     ! user        1: /* Copyright (c) 2026 The Solidite Developers
        !             2:  *
        !             3:  * This program is free software: you can redistribute it and/or modify it under
        !             4:  * the terms of the GNU Affero General Public License as published by the Free
        !             5:  * Software Foundation, either version 3 of the License, or (at your option)
        !             6:  * any later version.
        !             7:  *
        !             8:  * This program is distributed in the hope that it will be useful, but WITHOUT
        !             9:  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
        !            10:  * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
        !            11:  * details.
        !            12:  *
        !            13:  * You should have received a copy of the GNU Affero General Public License
        !            14:  * along with this program. If not, see <https://www.gnu.org/licenses/>. */
        !            15: 
        !            16: #include <locale.h>
        !            17: #include <stddef.h>
        !            18: #include <stdlib.h>
        !            19: #include <string.h>
        !            20: #include <unistd.h>
        !            21: 
        !            22: #include "include/text.h"
        !            23: 
        !            24: int main(int argc, char *argv[]) {
        !            25:   setlocale(LC_ALL, "");
        !            26: 
        !            27:   if (argc == 1) {
        !            28:     const char OUTPUT[] = ".\n";
        !            29:     (void)write(STDOUT_FILENO, OUTPUT, sizeof(OUTPUT) - 1);
        !            30:     return 0; // step 1
        !            31:   }
        !            32:   if (argv[1][0] == '\0')
        !            33:     return 0;
        !            34: 
        !            35:   int non_slash_char_found = 0;
        !            36:   for (size_t i = 0; i < strlen(argv[1]); ++i) {
        !            37:     if (argv[1][i] != '/') {
        !            38:       non_slash_char_found = 1;
        !            39:       break;
        !            40:     } else
        !            41:       continue;
        !            42:   }
        !            43:   if (non_slash_char_found == 0) {
        !            44:     const char OUTPUT[] = "/\n";
        !            45:     (void)write(STDOUT_FILENO, OUTPUT, sizeof(OUTPUT) - 1);
        !            46:     return 0;
        !            47:   } // step 3
        !            48: 
        !            49:   size_t real_strlen = strlen(argv[1]);
        !            50:   // count the actual strlen when null terms replaced
        !            51:   while (argv[1][real_strlen - 1] == '/') {
        !            52:     argv[1][real_strlen - 1] = '\0';
        !            53:     --real_strlen;
        !            54:   } // step 4
        !            55: 
        !            56:   char *final_slash = strrchr(argv[1], '/');
        !            57:   if (final_slash != NULL)
        !            58:     argv[1] = final_slash + 1;
        !            59:   // step 5
        !            60:   
        !            61:   if (argc == 3 && ends_with(argv[1], argv[2]) == 0)
        !            62:     argv[1][strlen(argv[1]) - strlen(argv[2])] = '\0'; 
        !            63: 
        !            64:   if (argv[1][0] == '\0')
        !            65:     return 0;
        !            66: 
        !            67:   size_t output_length = strlen(argv[1]);
        !            68:   argv[1][output_length] = '\n';
        !            69:   // argv[1] isn't valid C string anymore, be careful
        !            70:   (void)write(STDOUT_FILENO, argv[1], output_length + 1);
        !            71:   return 0;
        !            72: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>