Загрузить файлы в «src»

This commit is contained in:
8nlight 2023-10-04 22:03:34 +03:00
parent cd73acea6b
commit 15ed4ce642
3 changed files with 64 additions and 0 deletions

33
src/env.c Normal file
View File

@ -0,0 +1,33 @@
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(const int argc, char **argv, const char **envp) {
int i;
for (i = 1; i < argc; i++) {
char *val = strchr(argv[i], '=');
if (!val)
break;
val[0] = '\0';
if (setenv(argv[i], val + 1, 1)) {
fprintf(stderr, "env: %s\n", strerror(errno));
return 1;
}
}
/* Print env */
if (i == argc) {
while (*envp)
puts(*envp++);
return 0;
}
execvp(argv[i], argv + i);
fprintf(stderr, "env: %s\n", strerror(errno));
return 1;
}

13
src/tty.c Normal file
View File

@ -0,0 +1,13 @@
#include <unistd.h>
#include <stdio.h>
int main(void) {
char *tty = ttyname(STDIN_FILENO);
if (tty)
puts(tty);
else
puts("not a tty");
return tty == NULL;
}

18
src/whoami.c Normal file
View File

@ -0,0 +1,18 @@
#include <sys/types.h>
#include <unistd.h>
#include <pwd.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
int main(void) {
struct passwd *pw = getpwuid(getuid());
if (!pw) {
fprintf(stderr, "whoami: %s\n", strerror(errno));
return 1;
}
puts(pw->pw_name);
return 0;
}