2 * Modifications for uClinux
3 * Copyright (C) 1998 Kenneth Albanowski <kjahds@kjahds.com>
6 * Copyright (c) 1993 by David I. Bell
7 * Permission is granted to use, distribute, or modify this source,
8 * provided that this copyright notice remains intact.
10 * Most simple built-in commands are here.
15 #include <sys/types.h>
16 #include <sys/mount.h>
40 fputs(*++argv
, stdout
);
53 if (getcwd(buf
, PATHLEN
) == NULL
) {
54 fprintf(stderr
, "Cannot get current directory\n");
71 path
= getenv("HOME");
73 fprintf(stderr
, "No HOME environment variable\n");
88 int state
= 0, mode
= -1;
92 if (strcmp(argv
[1], "-m") == 0)
94 else if (mkdir(argv
[1], 0777) < 0)
96 else if (mode
!= -1 && chmod(argv
[1], mode
) < 0)
98 } else if (state
== 1) {
99 mode
= strtol(argv
[1], NULL
, 8);
112 sleep(atoi(argv
[1]));
127 if (strcmp(argv
[2], "b") == 0)
129 else if (strcmp(argv
[2], "c") == 0)
132 fprintf(stderr
, "Bad device type\n");
138 while (isdecimal(*cp
))
139 major
= major
* 10 + *cp
++ - '0';
141 if (*cp
|| (major
< 0) || (major
> 255)) {
142 fprintf(stderr
, "Bad major number\n");
148 while (isdecimal(*cp
))
149 minor
= minor
* 10 + *cp
++ - '0';
151 if (*cp
|| (minor
< 0) || (minor
> 255)) {
152 fprintf(stderr
, "Bad minor number\n");
156 if (mknod(argv
[1], mode
, major
* 256 + minor
) < 0)
167 if (rmdir(argv
[1]) < 0)
189 if (unlink(argv
[1]) < 0)
207 mode
= mode
* 8 + (*cp
++ - '0');
210 fprintf(stderr
, "Mode must be octal\n");
217 if (chmod(argv
[1], mode
) < 0)
235 if (isdecimal(*cp
)) {
237 while (isdecimal(*cp
))
238 uid
= uid
* 10 + (*cp
++ - '0');
241 fprintf(stderr
, "Bad uid value\n");
247 fprintf(stderr
, "Unknown user name\n");
259 if ((stat(*argv
, &statbuf
) < 0) ||
260 (chown(*argv
, uid
, statbuf
.st_gid
) < 0))
277 if (isdecimal(*cp
)) {
279 while (isdecimal(*cp
))
280 gid
= gid
* 10 + (*cp
++ - '0');
283 fprintf(stderr
, "Bad gid value\n");
289 fprintf(stderr
, "Unknown group name\n");
301 if ((stat(*argv
, &statbuf
) < 0) ||
302 (chown(*argv
, statbuf
.st_uid
, gid
) < 0))
318 now
.modtime
= now
.actime
;
323 if (utime(name
, &now
) <0)
325 fd
= open(name
, O_CREAT
| O_WRONLY
| O_EXCL
, 0666);
347 lastarg
= argv
[argc
- 1];
349 dirflag
= isadir(lastarg
);
351 if ((argc
> 3) && !dirflag
) {
352 fprintf(stderr
, "%s: not a directory\n", lastarg
);
358 if (access(srcname
, 0) < 0) {
365 destname
= buildname(destname
, srcname
);
367 if (rename(srcname
, destname
) >= 0)
370 if (errno
!= EXDEV
) {
375 if (!copyfile(srcname
, destname
, TRUE
))
378 if (unlink(srcname
) < 0)
394 if (argv
[1][0] == '-') {
395 if (strcmp(argv
[1], "-s")) {
396 fprintf(stderr
, "Unknown option\n");
401 fprintf(stderr
, "Wrong number of arguments for symbolic link\n");
406 if (symlink(argv
[2], argv
[3]) < 0)
409 fprintf(stderr
, "Symbolic links are not allowed\n");
415 * Here for normal hard links.
417 lastarg
= argv
[argc
- 1];
418 dirflag
= isadir(lastarg
);
420 if ((argc
> 3) && !dirflag
) {
421 fprintf(stderr
, "%s: not a directory\n", lastarg
);
427 if (access(srcname
, 0) < 0) {
434 destname
= buildname(destname
, srcname
);
436 if (link(srcname
, destname
) < 0) {
454 lastarg
= argv
[argc
- 1];
456 dirflag
= isadir(lastarg
);
458 if ((argc
> 3) && !dirflag
) {
459 fprintf(stderr
, "%s: not a directory\n", lastarg
);
467 destname
= buildname(destname
, srcname
);
469 (void) copyfile(srcname
, destname
, FALSE
);
486 while ((argc
> 0) && (**argv
== '-')) {
490 while (*++str
) switch (*str
) {
492 if ((argc
<= 0) || (**argv
== '-')) {
493 fprintf(stderr
, "Missing file system type\n");
502 fprintf(stderr
, "Unknown option\n");
508 fprintf(stderr
, "Wrong number of arguments for mount\n");
512 if (mount(argv
[0], argv
[1], type
, 0, 0) < 0)
513 perror("mount failed");
518 do_umount(argc
, argv
)
522 if (umount(argv
[1]) < 0)
544 struct stat statbuf1
;
545 struct stat statbuf2
;
547 if (stat(argv
[1], &statbuf1
) < 0) {
552 if (stat(argv
[2], &statbuf2
) < 0) {
557 if ((statbuf1
.st_dev
== statbuf2
.st_dev
) &&
558 (statbuf1
.st_ino
== statbuf2
.st_ino
))
560 printf("Files are links to each other\n");
564 if (statbuf1
.st_size
!= statbuf2
.st_size
) {
565 printf("Files are different sizes\n");
569 fd1
= open(argv
[1], 0);
575 fd2
= open(argv
[2], 0);
582 buf1
= malloc(8192-16);
583 buf2
= malloc(8192-16);
590 cc1
= read(fd1
, buf1
, 8192-16);
596 cc2
= read(fd2
, buf2
, 8192-16);
602 if ((cc1
== 0) && (cc2
== 0)) {
603 printf("Files are identical\n");
608 printf("First file is shorter than second\n");
613 printf("Second file is shorter than first\n");
617 if (memcmp(buf1
, buf2
, cc1
) == 0) {
624 while (*bp1
++ == *bp2
++)
627 printf("Files differ at byte position %ld\n", pos
);
654 fp
= fopen(name
, "r");
660 printf("<< %s >>\n", name
);
664 while (fp
&& ((ch
= fgetc(fp
)) != EOF
)) {
676 col
= ((col
+ 1) | 0x07) + 1;
703 if (intflag
|| (read(0, buf
, sizeof(buf
)) < 0)) {
745 do_setenv(argc
, argv
)
749 setenv(argv
[1], argv
[2], 1);
754 do_printenv(argc
, argv
)
759 extern char **environ
;
766 printf("%s\n", *env
++);
770 len
= strlen(argv
[1]);
772 if ((strlen(*env
) > len
) && (env
[0][len
] == '=') &&
773 (memcmp(argv
[1], *env
, len
) == 0))
775 printf("%s\n", &env
[0][len
+1]);
794 printf("%03o\n", mask
);
801 mask
= mask
* 8 + *cp
++ - '0';
803 if (*cp
|| (mask
& ~0777)) {
804 fprintf(stderr
, "Bad umask value\n");
823 if (argv
[1][0] == '-') {
825 if (strcmp(cp
, "HUP") == 0)
827 else if (strcmp(cp
, "INT") == 0)
829 else if (strcmp(cp
, "QUIT") == 0)
831 else if (strcmp(cp
, "ILL") == 0)
833 else if (strcmp(cp
, "TRAP") == 0)
835 else if (strcmp(cp
, "ABRT") == 0)
837 else if (strcmp(cp
, "IOT") == 0)
839 else if (strcmp(cp
, "BUS") == 0)
841 else if (strcmp(cp
, "FPE") == 0)
843 else if (strcmp(cp
, "KILL") == 0)
845 else if (strcmp(cp
, "USR1") == 0)
847 else if (strcmp(cp
, "SEGV") == 0)
849 else if (strcmp(cp
, "USR2") == 0)
851 else if (strcmp(cp
, "PIPE") == 0)
853 else if (strcmp(cp
, "ALRM") == 0)
855 else if (strcmp(cp
, "TERM") == 0)
858 else if (strcmp(cp
, "STKFLT") == 0)
861 else if (strcmp(cp
, "CHLD") == 0)
863 else if (strcmp(cp
, "CONT") == 0)
865 else if (strcmp(cp
, "STOP") == 0)
867 else if (strcmp(cp
, "TSTP") == 0)
869 else if (strcmp(cp
, "TTIN") == 0)
871 else if (strcmp(cp
, "TTOU") == 0)
873 else if (strcmp(cp
, "URG") == 0)
875 else if (strcmp(cp
, "PWR") == 0)
879 while (isdecimal(*cp
))
880 sig
= sig
* 10 + *cp
++ - '0';
883 fprintf(stderr
, "Unknown signal\n");
895 while (isdecimal(*cp
))
896 pid
= pid
* 10 + *cp
++ - '0';
899 fprintf(stderr
, "Non-numeric pid\n");
904 if (kill(pid
, sig
) < 0) {