Relicense Sortix to the ISC license.
I hereby relicense all my work on Sortix under the ISC license as below.
All Sortix contributions by other people are already under this license,
are not substantial enough to be copyrightable, or have been removed.
All imported code from other projects is compatible with this license.
All GPL licensed code from other projects had previously been removed.
Copyright 2011-2016 Jonas 'Sortie' Termansen and contributors.
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2016-03-02 17:38:16 -05:00
|
|
|
/*
|
2016-09-23 18:26:07 -04:00
|
|
|
* Copyright (c) 2013, 2015, 2016 Jonas 'Sortie' Termansen.
|
2021-10-03 07:19:48 -04:00
|
|
|
* Copyright (c) 2021 Juhani 'nortti' Krekelä.
|
Relicense Sortix to the ISC license.
I hereby relicense all my work on Sortix under the ISC license as below.
All Sortix contributions by other people are already under this license,
are not substantial enough to be copyrightable, or have been removed.
All imported code from other projects is compatible with this license.
All GPL licensed code from other projects had previously been removed.
Copyright 2011-2016 Jonas 'Sortie' Termansen and contributors.
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2016-03-02 17:38:16 -05:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*
|
|
|
|
* command-not-found.c
|
2021-10-03 07:19:48 -04:00
|
|
|
* Writes a notice that the attempted command wasn't found and suggests
|
|
|
|
* possible altetnatives.
|
Relicense Sortix to the ISC license.
I hereby relicense all my work on Sortix under the ISC license as below.
All Sortix contributions by other people are already under this license,
are not substantial enough to be copyrightable, or have been removed.
All imported code from other projects is compatible with this license.
All GPL licensed code from other projects had previously been removed.
Copyright 2011-2016 Jonas 'Sortie' Termansen and contributors.
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2016-03-02 17:38:16 -05:00
|
|
|
*/
|
2013-06-09 07:44:34 -04:00
|
|
|
|
2016-12-23 01:37:18 -05:00
|
|
|
#include <fcntl.h>
|
2013-06-09 07:44:34 -04:00
|
|
|
#include <stdio.h>
|
2015-12-24 21:24:51 -05:00
|
|
|
#include <stdlib.h>
|
2013-06-09 07:44:34 -04:00
|
|
|
#include <string.h>
|
2016-09-23 18:26:07 -04:00
|
|
|
#include <unistd.h>
|
2013-06-09 07:44:34 -04:00
|
|
|
|
2021-10-03 07:19:48 -04:00
|
|
|
#define ARRAY_LENGTH(array) (sizeof(array) / sizeof((array)[0]))
|
|
|
|
|
|
|
|
static const char* tty_name(void)
|
2016-12-23 01:37:18 -05:00
|
|
|
{
|
|
|
|
int tty_fd = open("/dev/tty", O_RDONLY);
|
|
|
|
const char* result = NULL;
|
|
|
|
if ( 0 <= tty_fd )
|
|
|
|
{
|
|
|
|
result = ttyname(tty_fd);
|
|
|
|
close(tty_fd);
|
|
|
|
}
|
|
|
|
return result ? result : "/dev/tty";
|
|
|
|
}
|
|
|
|
|
2021-10-03 07:19:48 -04:00
|
|
|
static void suggest_logout(void)
|
2015-12-24 21:24:51 -05:00
|
|
|
{
|
|
|
|
fprintf(stderr, " Exiting your shell normally to logout.\n");
|
|
|
|
}
|
|
|
|
|
2021-10-03 07:19:48 -04:00
|
|
|
static void suggest_poweroff(void)
|
2015-12-24 21:24:51 -05:00
|
|
|
{
|
2016-12-23 01:37:18 -05:00
|
|
|
if ( strcmp(tty_name(), "/dev/tty1") != 0 )
|
|
|
|
fprintf(stderr, " Powering off on /dev/tty1.\n");
|
|
|
|
else if ( getenv("LOGIN_PID") )
|
2015-12-24 21:24:51 -05:00
|
|
|
{
|
|
|
|
fprintf(stderr, " Exiting your shell normally to logout.\n");
|
|
|
|
fprintf(stderr, " Login as user 'poweroff' to power off computer.\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
fprintf(stderr, " Exiting your shell normally to poweroff.\n");
|
|
|
|
}
|
|
|
|
|
2021-10-03 07:19:48 -04:00
|
|
|
static void suggest_reboot(void)
|
2015-12-24 21:24:51 -05:00
|
|
|
{
|
2016-12-23 01:37:18 -05:00
|
|
|
if ( strcmp(tty_name(), "/dev/tty1") != 0 )
|
|
|
|
fprintf(stderr, " Rebooting on /dev/tty1.\n");
|
|
|
|
else if ( getenv("LOGIN_PID") )
|
2015-12-24 21:24:51 -05:00
|
|
|
{
|
|
|
|
fprintf(stderr, " Exiting your shell normally to logout.\n");
|
|
|
|
fprintf(stderr, " Login as user 'reboot' to reboot computer.\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
fprintf(stderr, " Exiting your shell with 'exit 1' to reboot.\n");
|
|
|
|
}
|
|
|
|
|
2021-10-03 07:19:48 -04:00
|
|
|
enum category
|
2018-03-30 17:44:08 -04:00
|
|
|
{
|
2021-10-03 07:19:48 -04:00
|
|
|
NONE,
|
|
|
|
BROWSER,
|
|
|
|
EDITOR,
|
|
|
|
LOGOUT,
|
|
|
|
MOUNT,
|
|
|
|
PAGER,
|
|
|
|
POWEROFF,
|
|
|
|
REBOOT,
|
|
|
|
RW,
|
|
|
|
SHELL,
|
|
|
|
UNMOUNT,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct command
|
|
|
|
{
|
|
|
|
enum category category;
|
|
|
|
const char* command;
|
|
|
|
const char* package;
|
|
|
|
void (*suggest)(void);
|
|
|
|
};
|
|
|
|
|
|
|
|
struct command commands[] =
|
|
|
|
{
|
|
|
|
{BROWSER, "chromium", NULL, NULL},
|
|
|
|
{BROWSER, "chromium-browser", NULL, NULL},
|
|
|
|
{BROWSER, "elinks", NULL, NULL},
|
|
|
|
{BROWSER, "firefox", NULL, NULL},
|
|
|
|
{BROWSER, "links", "links", NULL},
|
|
|
|
{BROWSER, "lynx", NULL, NULL},
|
|
|
|
{BROWSER, "w3m", NULL, NULL},
|
|
|
|
{BROWSER, "www-browser", NULL, NULL},
|
|
|
|
{BROWSER, "x-www-browser", NULL, NULL},
|
|
|
|
|
|
|
|
{EDITOR, "ed", "ed", NULL},
|
|
|
|
{EDITOR, "editor", "system", NULL},
|
|
|
|
{EDITOR, "emacs", "emacs", NULL},
|
|
|
|
{EDITOR, "nano", "nano", NULL},
|
|
|
|
{EDITOR, "vim", "vim", NULL},
|
|
|
|
{EDITOR, "vi", NULL, NULL},
|
|
|
|
|
|
|
|
{LOGOUT, "logoff", NULL, NULL},
|
|
|
|
{LOGOUT, "logout", NULL, suggest_logout},
|
|
|
|
|
|
|
|
{MOUNT, "extfs", "system", NULL},
|
|
|
|
{MOUNT, "mount", NULL, NULL},
|
|
|
|
|
|
|
|
{PAGER, "less", NULL, NULL},
|
|
|
|
{PAGER, "more", NULL, NULL},
|
|
|
|
{PAGER, "pager", "system", NULL},
|
|
|
|
|
|
|
|
{POWEROFF, "halt", NULL, NULL},
|
|
|
|
{POWEROFF, "poweroff", NULL, suggest_poweroff},
|
|
|
|
{POWEROFF, "shutdown", NULL, NULL},
|
|
|
|
|
|
|
|
{REBOOT, "reboot", NULL, suggest_reboot},
|
|
|
|
|
|
|
|
{RW, "dd", NULL, NULL},
|
|
|
|
{RW, "rw", "system", NULL},
|
|
|
|
|
|
|
|
{SHELL, "bash", NULL, NULL},
|
|
|
|
{SHELL, "dash", "dash", NULL},
|
|
|
|
{SHELL, "ksh", NULL, NULL},
|
|
|
|
{SHELL, "sh", "system", NULL},
|
|
|
|
{SHELL, "zsh", NULL, NULL},
|
|
|
|
|
|
|
|
{UNMOUNT, "umount", NULL, NULL},
|
|
|
|
{UNMOUNT, "unmount", "system", NULL},
|
|
|
|
};
|
|
|
|
|
|
|
|
static enum category find_category(const char* filename)
|
|
|
|
{
|
|
|
|
for ( size_t i = 0; i < ARRAY_LENGTH(commands); i++ )
|
|
|
|
{
|
|
|
|
if ( commands[i].command && !strcmp(filename, commands[i].command) )
|
|
|
|
return commands[i].category;
|
|
|
|
}
|
|
|
|
return NONE;
|
2018-03-30 17:44:08 -04:00
|
|
|
}
|
|
|
|
|
2013-06-09 07:44:34 -04:00
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
const char* filename = 2 <= argc ? argv[1] : argv[0];
|
2021-10-03 07:19:48 -04:00
|
|
|
|
|
|
|
enum category category = find_category(filename);
|
|
|
|
if ( category != NONE )
|
|
|
|
{
|
|
|
|
fprintf(stderr, "No command '%s' found, did you mean:\n", filename);
|
|
|
|
|
|
|
|
for ( size_t i = 0; i < ARRAY_LENGTH(commands); i++ )
|
|
|
|
{
|
|
|
|
if ( !commands[i].package && !commands[i].suggest )
|
|
|
|
continue;
|
|
|
|
else if ( commands[i].category == category )
|
|
|
|
{
|
|
|
|
if ( commands[i].suggest )
|
|
|
|
commands[i].suggest();
|
|
|
|
else if ( !strcmp(commands[i].package, "system") )
|
|
|
|
fprintf(stderr, " Command '%s' from the base system\n",
|
|
|
|
commands[i].command);
|
|
|
|
else
|
|
|
|
fprintf(stderr, " Command '%s' from the package '%s'\n",
|
|
|
|
commands[i].command, commands[i].package);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-09 07:44:34 -04:00
|
|
|
fprintf(stderr, "%s: command not found\n", filename);
|
|
|
|
return 127;
|
|
|
|
}
|