File: loginshell.c

package info (click to toggle)
tinyssh 20250501-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 3,388 kB
  • sloc: ansic: 20,245; sh: 1,582; python: 1,449; makefile: 913
file content (41 lines) | stat: -rw-r--r-- 731 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*
20140429
20241208 - reformated using clang-format
Jan Mojzis
Public domain.
*/

#include "e.h"
#include "loginshell.h"

/*
The 'loginshell()' function converts shell path
into -shell name. For example:
'/bin/sh' -> '-sh'
'/usr/pkg/bin/bash' -> '-bash'
*/

int loginshell(char *out, long long outlen, const char *in) {

    long long len, pos = -1;

    if (!out || !in || outlen < 2) {
        errno = EINVAL;
        return 0;
    }

    for (len = 0; in[len]; ++len)
        if (in[len] == '/') pos = len;
    in += pos + 1;
    len -= pos + 1;
    if (len > outlen - 2) len = outlen - 2; /* truncate name */

    *out++ = '-';
    while (len > 0) {
        *out++ = *in++;
        --len;
    }
    *out = 0;

    return 1;
}