Q:

Get substring of argv[0] between n and the length of argv

I'm trying to get a substring of argv[0] and output it to another string. I've already got the positions I want, I just can't figure out how to do it from other answers. Here's what I've got so far:
#include "echo.h"
#include "whoami.h"
#include <string.h>

int main(int argc, char *argv[])
{
    // applet_length: Length of first arg filename excluding things like "./"
    int applet_length = sizeof(argv[0]);
    // real_length: Full length of first argument
    int real_length = 0;
    while ((argv[0])[real_length])
        real_length++;
    int start = real_length - applet_length;
}

start is the point I want to start the substring from, and real_length is the overall length of argv[0]. How would I go about doing this?
I'm doing this for the purposes of making a coreutils executable like BusyBox where you can run an applet by invoking a symlink with its name.
E.g. argv[0] is "test1234", start is 4, real_length is 8, output is "1234"

I ended up with:
#include "echo.h"
#include "whoami.h"
// etc.
#include <string.h>
#include <libgen.h>

int main(int argc, char *argv[])
{
    char *applet = basename(argv[0]);
    if (!strcmp(applet, "echo"))
        echo(argc, argv);
    else if (!strcmp(applet, "whoami"))
        whoami(argc, argv);
    // etc.
    return 0;
}

A:

Here is a program with comments explaining how you can do what you want.  Hopefully it will help you learn how to solve such problems.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* Use if strdup() is unavailable.  Caller must free the memory returned. */
char *dup_str(const char *s)
{
    size_t n = strlen(s) + 1;
    char *r;
    if ((r = malloc(n)) == NULL) {
        return NULL;
    }
    strcpy(r, s);
    return r;
}

/* Use if POSIX basename() is unavailable */
char *base_name(char *s)
{
    char *start;

    /* Find the last '/', and move past it if there is one.  Otherwise return
       a copy of the whole string. */
    /* strrchr() finds the last place where the given character is in a given
       string.  Returns NULL if not found. */
    if ((start = strrchr(s, '/')) == NULL) {
        start = s;
    } else {
        ++start;
    }
    /* If you don't want to do anything interesting with the returned value,
       i.e., if you just want to print it for example, you can just return
       'start' here (and then you don't need dup_str(), or to free
       the result). */
    return dup_str(start);
}

/* test */
int main(int argc, char *argv[])
{
    char *b = base_name(argv[0]);

    if (b) {
        printf("%s\n", b);
    }
    /* Don't free if you removed dup_str() call above */
    free(b);

    return EXIT_SUCCESS;
}

