Adding the new C files too.

This commit is contained in:
Christoph Lohmann 2013-06-09 15:20:55 +02:00
parent 2c162042b1
commit b0898c605d
7 changed files with 264 additions and 0 deletions

72
chgrp.c Normal file
View File

@ -0,0 +1,72 @@
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <ftw.h>
#include <errno.h>
#include <grp.h>
#include <sys/types.h>
#include "util.h"
int gid;
int failures = 0;
static void
usage(void)
{
eprintf("usage: chgrp [-R] groupname file...\n");
}
static int
chgrp(const char *path, const struct stat *st, int f)
{
(void)f;
if(chown(path, st->st_uid, gid) == -1) {
fprintf(stderr, "chgrp: '%s': %s\n", path, strerror(errno));
failures++;
}
return 0;
}
int
main(int argc, char **argv)
{
int rflag = 0;
struct group *gr;
struct stat st;
ARGBEGIN {
case 'R':
rflag = 1;
break;
default:
usage();
} ARGEND;
if(argc<2)
usage();
gr = getgrnam(argv[0]);
if(!gr)
eprintf("chgrp: '%s': No such group\n", argv[0]);
gid = gr->gr_gid;
if(rflag) {
while(*++argv)
ftw(*argv, chgrp, FOPEN_MAX);
return 0;
}
while(*++argv) {
if(stat(*argv, &st) == -1) {
fprintf(stderr, "chgrp: '%s': %s\n", *argv,
strerror(errno));
failures++;
continue;
}
chgrp(*argv, &st, 0);
}
return failures;
}

61
chvt.c Executable file
View File

@ -0,0 +1,61 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include "util.h"
enum {
/* from <linux/vt.h> */
VT_ACTIVATE = 0x5606,
VT_WAITACTIVE = 0x5607,
/* from <linux/kd.h> */
KDGKBTYPE = 0x4B33
};
char *vts[] = {
"/proc/self/fd/0",
"/dev/console",
"/dev/tty",
"/dev/tty0",
};
static void
usage(void)
{
eprintf("usage: chvt N\n");
}
int
main(int argc, char **argv)
{
int n, i, fd;
char c;
if(argc!=2 || strspn(argv[1], "1234567890") != strlen(argv[1]))
usage();
n = atoi(argv[1]);
for(i = 0; i < LEN(vts); i++) {
fd = open(vts[i], O_RDONLY);
if(fd < 1)
continue;
c = 0;
if(ioctl(fd, KDGKBTYPE, &c) == 0)
goto VTfound;
close(fd);
}
eprintf("chvt: couldn't find a console.\n");
VTfound:
if(ioctl(fd, VT_ACTIVATE, n) == -1)
eprintf("chvt: VT_ACTIVATE '%d':", n);
if(ioctl(fd, VT_WAITACTIVE, n) == -1)
eprintf("chvt: VT_WAITACTIVE '%d':", n);
close(fd);
return 0;
}

38
nice.c Normal file
View File

@ -0,0 +1,38 @@
/* See LICENSE file for copyright and license details. */
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include "util.h"
static void
usage(void)
{
eprintf("usage: nice [-n inc] command [options ...]\n");
}
int
main(int argc, char **argv)
{
int inc = 10;
ARGBEGIN {
case 'n':
inc = atoi(EARGF(usage()));
break;
default:
usage();
} ARGEND;
nice(inc); /* POSIX specifies the nice failure still invokes the command. */
if(!*argv)
usage();
execvp(*argv, argv);
eprintf("nice: '%s': %s\n", *argv, strerror(errno));
return EXIT_FAILURE;
}

24
printenv.c Normal file
View File

@ -0,0 +1,24 @@
#include <stdio.h>
#include <stdlib.h>
extern char **environ;
int
main(int argc, char **argv)
{
char *var;
if(argc == 1) {
while(*environ)
printf("%s\n", *environ++);
return 0;
}
while(*++argv) {
if((var = getenv(*argv)))
printf("%s\n", var);
}
return 0;
}

28
rmdir.c Normal file
View File

@ -0,0 +1,28 @@
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include "util.h"
static void
usage(void)
{
eprintf("usage: rmdir dir...\n");
}
int
main(int argc, char **argv)
{
argv++;;
if(!*argv)
usage();
while(*argv) {
if(rmdir(*argv++) == -1)
fprintf(stderr, "rmdir: '%s': %s\n",
argv[-1], strerror(errno));
}
return 0;
}

20
sync.c Normal file
View File

@ -0,0 +1,20 @@
#include <stdio.h>
#include <unistd.h>
#include "util.h"
static void
usage(void)
{
eprintf("usage: sync\n");
}
int
main(int argc, char **argv)
{
if(argc != 1)
usage();
sync();
return 0;
}

21
unlink.c Normal file
View File

@ -0,0 +1,21 @@
#include <unistd.h>
#include "util.h"
static void
usage(void)
{
eprintf("usage: unlink file\n");
}
int
main(int argc, char **argv)
{
if(argc != 2)
usage();
if(unlink(argv[1]) == -1)
eprintf("unlink: '%s':", argv[1]);
return 0;
}