OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [uclinux/] [userland/] [sash/] [cmds.c] - Blame information for rev 1767

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 199 simons
/*
2
 * Modifications for uClinux
3
 * Copyright (C) 1998  Kenneth Albanowski <kjahds@kjahds.com>
4
 *
5
 * Original code
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.
9
 *
10
 * Most simple built-in commands are here.
11
 */
12
 
13
#include "sash.h"
14
 
15
#include <sys/types.h>
16
#include <sys/stat.h>
17
#include <sys/time.h>
18
#include <unistd.h>
19
#include <fcntl.h>
20
#include <signal.h>
21
#include <pwd.h>
22
#include <grp.h>
23
#include <utime.h>
24
#include <errno.h>
25
 
26
 
27
void
28
do_echo(argc, argv)
29
        char    **argv;
30
{
31
        BOOL    first;
32
 
33
        first = TRUE;
34
        while (argc-- > 1) {
35
                if (!first)
36
                        fputc(' ', stdout);
37
                first = FALSE;
38
                fputs(*++argv, stdout);
39
        }
40
        fputc('\n', stdout);
41
}
42
 
43
 
44
void
45
do_pwd(argc, argv)
46
        char    **argv;
47
{
48
        char    buf[PATHLEN];
49
 
50
        if (getcwd(buf, PATHLEN) == NULL) {
51
                fprintf(stderr, "Cannot get current directory\n");
52
                return;
53
        }
54
 
55
        printf("%s\n", buf);
56
}
57
 
58
void
59
do_time(argc, argv)
60
        char ** argv;
61
{
62
        struct timeval tv;
63
        gettimeofday(&tv, 0);
64
        printf("Time of day = %d.%6.6d seconds\n", tv.tv_sec, tv.tv_usec);
65
}
66
 
67
void
68
do_cd(argc, argv)
69
        char    **argv;
70
{
71
        char    *path;
72
 
73
        if (argc > 1)
74
                path = argv[1];
75
        else {
76
                path = getenv("HOME");
77
                if (path == NULL) {
78
                        fprintf(stderr, "No HOME environment variable\n");
79
                        return;
80
                }
81
        }
82
 
83
        if (chdir(path) < 0)
84
                perror(path);
85
}
86
 
87
 
88
void
89
do_mkdir(argc, argv)
90
        char    **argv;
91
{
92
        while (argc-- > 1) {
93
                if (mkdir(argv[1], 0777) < 0)
94
                        perror(argv[1]);
95
                argv++;
96
        }
97
}
98
 
99
void
100
do_sleep(argc, argv)
101
        char    **argv;
102
{
103
        if (argc > 1)
104
                sleep(atoi(argv[1]));
105
}
106
 
107
void
108
do_mknod(argc, argv)
109
        char    **argv;
110
{
111
        char    *cp;
112
        int     mode;
113
        int     major;
114
        int     minor;
115
 
116
        mode = 0666;
117
 
118
        if (strcmp(argv[2], "b") == 0)
119
                mode |= S_IFBLK;
120
        else if (strcmp(argv[2], "c") == 0)
121
                mode |= S_IFCHR;
122
        else {
123
                fprintf(stderr, "Bad device type\n");
124
                return;
125
        }
126
 
127
        major = 0;
128
        cp = argv[3];
129
        while (isdecimal(*cp))
130
                major = major * 10 + *cp++ - '0';
131
 
132
        if (*cp || (major < 0) || (major > 255)) {
133
                fprintf(stderr, "Bad major number\n");
134
                return;
135
        }
136
 
137
        minor = 0;
138
        cp = argv[4];
139
        while (isdecimal(*cp))
140
                minor = minor * 10 + *cp++ - '0';
141
 
142
        if (*cp || (minor < 0) || (minor > 255)) {
143
                fprintf(stderr, "Bad minor number\n");
144
                return;
145
        }
146
 
147
        if (mknod(argv[1], mode, major * 256 + minor) < 0)
148
                perror(argv[1]);
149
}
150
 
151
 
152
void
153
do_rmdir(argc, argv)
154
        char    **argv;
155
{
156
        while (argc-- > 1) {
157
                if (rmdir(argv[1]) < 0)
158
                        perror(argv[1]);
159
                argv++;
160
        }
161
}
162
 
163
 
164
void
165
do_sync(argc, argv)
166
        char    **argv;
167
{
168
#ifdef EMBED
169
#define FLATFSD_PID_FILE "/var/log/flatfsd.pid"
170
        pid_t pid;
171
        FILE *in;
172
 
173
        /* get the pid of flatfsd */
174
        if ((in = fopen(FLATFSD_PID_FILE, "r")) != NULL) {
175
                if (fread(&pid, sizeof(pid_t), 1, in) > 0) {
176
                        /* we read something.. hopefully the pid */
177
                        /* send that pid signal 10 */
178
                        kill(pid, 10);
179
                }
180
                fclose(in);
181
        }
182
#endif
183
        sync();
184
 
185
}
186
 
187
 
188
void
189
do_rm(argc, argv)
190
        char    **argv;
191
{
192
        while (argc-- > 1) {
193
                if (unlink(argv[1]) < 0)
194
                        perror(argv[1]);
195
                argv++;
196
        }
197
}
198
 
199
 
200
void
201
do_chmod(argc, argv)
202
        char    **argv;
203
{
204
        char    *cp;
205
        int     mode;
206
 
207
        mode = 0;
208
        cp = argv[1];
209
        while (isoctal(*cp))
210
                mode = mode * 8 + (*cp++ - '0');
211
 
212
        if (*cp) {
213
                fprintf(stderr, "Mode must be octal\n");
214
                return;
215
        }
216
        argc--;
217
        argv++;
218
 
219
        while (argc-- > 1) {
220
                if (chmod(argv[1], mode) < 0)
221
                        perror(argv[1]);
222
                argv++;
223
        }
224
}
225
 
226
 
227
void
228
do_chown(argc, argv)
229
        char    **argv;
230
{
231
        char            *cp;
232
        int             uid;
233
        struct passwd   *pwd;
234
        struct stat     statbuf;
235
 
236
        cp = argv[1];
237
        if (isdecimal(*cp)) {
238
                uid = 0;
239
                while (isdecimal(*cp))
240
                        uid = uid * 10 + (*cp++ - '0');
241
 
242
                if (*cp) {
243
                        fprintf(stderr, "Bad uid value\n");
244
                        return;
245
                }
246
        } else {
247
                pwd = getpwnam(cp);
248
                if (pwd == NULL) {
249
                        fprintf(stderr, "Unknown user name\n");
250
                        return;
251
                }
252
 
253
                uid = pwd->pw_uid;
254
        }
255
 
256
        argc--;
257
        argv++;
258
 
259
        while (argc-- > 1) {
260
                argv++;
261
                if ((stat(*argv, &statbuf) < 0) ||
262
                        (chown(*argv, uid, statbuf.st_gid) < 0))
263
                                perror(*argv);
264
        }
265
}
266
 
267
 
268
void
269
do_chgrp(argc, argv)
270
        char    **argv;
271
{
272
        char            *cp;
273
        int             gid;
274
        struct group    *grp;
275
        struct stat     statbuf;
276
 
277
        cp = argv[1];
278
        if (isdecimal(*cp)) {
279
                gid = 0;
280
                while (isdecimal(*cp))
281
                        gid = gid * 10 + (*cp++ - '0');
282
 
283
                if (*cp) {
284
                        fprintf(stderr, "Bad gid value\n");
285
                        return;
286
                }
287
        } else {
288
                grp = getgrnam(cp);
289
                if (grp == NULL) {
290
                        fprintf(stderr, "Unknown group name\n");
291
                        return;
292
                }
293
 
294
                gid = grp->gr_gid;
295
        }
296
 
297
        argc--;
298
        argv++;
299
 
300
        while (argc-- > 1) {
301
                argv++;
302
                if ((stat(*argv, &statbuf) < 0) ||
303
                        (chown(*argv, statbuf.st_uid, gid) < 0))
304
                                perror(*argv);
305
        }
306
}
307
 
308
 
309
void
310
do_touch(argc, argv)
311
        char    **argv;
312
{
313
        char            *name;
314
        int             fd;
315
        struct  utimbuf now;
316
 
317
        time(&now.actime);
318
        now.modtime = now.actime;
319
 
320
        while (argc-- > 1) {
321
                name = *(++argv);
322
 
323
                if (utime(name, &now) <0)
324
                {
325
                fd = open(name, O_CREAT | O_WRONLY | O_EXCL, 0666);
326
                if (fd >= 0)
327
                        {
328
                        close(fd);
329
                        continue;
330
                        }
331
                perror(name);
332
                }
333
        }
334
}
335
 
336
 
337
void
338
do_mv(argc, argv)
339
        char    **argv;
340
{
341
        int     dirflag;
342
        char    *srcname;
343
        char    *destname;
344
        char    *lastarg;
345
 
346
        lastarg = argv[argc - 1];
347
 
348
        dirflag = isadir(lastarg);
349
 
350
        if ((argc > 3) && !dirflag) {
351
                fprintf(stderr, "%s: not a directory\n", lastarg);
352
                return;
353
        }
354
 
355
        while (argc-- > 2) {
356
                srcname = *(++argv);
357
                if (access(srcname, 0) < 0) {
358
                        perror(srcname);
359
                        continue;
360
                }
361
 
362
                destname = lastarg;
363
                if (dirflag)
364
                        destname = buildname(destname, srcname);
365
 
366
                if (rename(srcname, destname) >= 0)
367
                        continue;
368
 
369
                if (errno != EXDEV) {
370
                        perror(destname);
371
                        continue;
372
                }
373
 
374
                if (!copyfile(srcname, destname, TRUE))
375
                        continue;
376
 
377
                if (unlink(srcname) < 0)
378
                        perror(srcname);
379
        }
380
}
381
 
382
 
383
void
384
do_ln(argc, argv)
385
        char    **argv;
386
{
387
        int     dirflag;
388
        char    *srcname;
389
        char    *destname;
390
        char    *lastarg;
391
 
392
        if (argv[1][0] == '-') {
393
                if (strcmp(argv[1], "-s")) {
394
                        fprintf(stderr, "Unknown option\n");
395
                        return;
396
                }
397
 
398
                if (argc != 4) {
399
                        fprintf(stderr, "Wrong number of arguments for symbolic link\n");
400
                        return;
401
                }
402
 
403
#ifdef  S_ISLNK
404
                if (symlink(argv[2], argv[3]) < 0)
405
                        perror(argv[3]);
406
#else
407
                fprintf(stderr, "Symbolic links are not allowed\n");
408
#endif
409
                return;
410
        }
411
 
412
        /*
413
         * Here for normal hard links.
414
         */
415
        lastarg = argv[argc - 1];
416
        dirflag = isadir(lastarg);
417
 
418
        if ((argc > 3) && !dirflag) {
419
                fprintf(stderr, "%s: not a directory\n", lastarg);
420
                return;
421
        }
422
 
423
        while (argc-- > 2) {
424
                srcname = *(++argv);
425
                if (access(srcname, 0) < 0) {
426
                        perror(srcname);
427
                        continue;
428
                }
429
 
430
                destname = lastarg;
431
                if (dirflag)
432
                        destname = buildname(destname, srcname);
433
 
434
                if (link(srcname, destname) < 0) {
435
                        perror(destname);
436
                        continue;
437
                }
438
        }
439
}
440
 
441
 
442
void
443
do_cp(argc, argv)
444
        char    **argv;
445
{
446
        BOOL    dirflag;
447
        char    *srcname;
448
        char    *destname;
449
        char    *lastarg;
450
 
451
        lastarg = argv[argc - 1];
452
 
453
        dirflag = isadir(lastarg);
454
 
455
        if ((argc > 3) && !dirflag) {
456
                fprintf(stderr, "%s: not a directory\n", lastarg);
457
                return;
458
        }
459
 
460
        while (argc-- > 2) {
461
                destname = lastarg;
462
                srcname = *++argv;
463
                if (dirflag)
464
                        destname = buildname(destname, srcname);
465
 
466
                (void) copyfile(srcname, destname, FALSE);
467
        }
468
}
469
 
470
 
471
void
472
do_mount(argc, argv)
473
        char    **argv;
474
{
475
        char    *str;
476
        char    *type;
477
 
478
        argc--;
479
        argv++;
480
        type = "minix";
481
 
482
        while ((argc > 0) && (**argv == '-')) {
483
                argc--;
484
                str = *argv++ ;
485
 
486
                while (*++str) switch (*str) {
487
                        case 't':
488
                                if ((argc <= 0) || (**argv == '-')) {
489
                                        fprintf(stderr, "Missing file system type\n");
490
                                        return;
491
                                }
492
 
493
                                type = *argv++;
494
                                argc--;
495
                                break;
496
 
497
                        default:
498
                                fprintf(stderr, "Unknown option\n");
499
                                return;
500
                }
501
        }
502
 
503
        if (argc != 2) {
504
                fprintf(stderr, "Wrong number of arguments for mount\n");
505
                return;
506
        }
507
 
508
        if (mount(argv[0], argv[1], type, 0) < 0)
509
                perror("mount failed");
510
}
511
 
512
 
513
void
514
do_umount(argc, argv)
515
        char    **argv;
516
{
517
        if (umount(argv[1]) < 0)
518
                perror(argv[1]);
519
}
520
 
521
 
522
void
523
do_cmp(argc, argv)
524
        char    **argv;
525
{
526
        int             fd1;
527
        int             fd2;
528
        int             cc1;
529
        int             cc2;
530
        long            pos;
531
        char            *srcname;
532
        char            *destname;
533
        char            *lastarg;
534
        char            *bp1;
535
        char            *bp2;
536
        char            *buf1;
537
        char            *buf2;
538
        struct  stat    statbuf1;
539
        struct  stat    statbuf2;
540
 
541
        if (stat(argv[1], &statbuf1) < 0) {
542
                perror(argv[1]);
543
                return;
544
        }
545
 
546
        if (stat(argv[2], &statbuf2) < 0) {
547
                perror(argv[2]);
548
                return;
549
        }
550
 
551
        if ((statbuf1.st_dev == statbuf2.st_dev) &&
552
                (statbuf1.st_ino == statbuf2.st_ino))
553
        {
554
                printf("Files are links to each other\n");
555
                return;
556
        }
557
 
558
        if (statbuf1.st_size != statbuf2.st_size) {
559
                printf("Files are different sizes\n");
560
                return;
561
        }
562
 
563
        fd1 = open(argv[1], 0);
564
        if (fd1 < 0) {
565
                perror(argv[1]);
566
                return;
567
        }
568
 
569
        fd2 = open(argv[2], 0);
570
        if (fd2 < 0) {
571
                perror(argv[2]);
572
                close(fd1);
573
                return;
574
        }
575
 
576
        buf1 = malloc(8192-16);
577
        buf2 = malloc(8192-16);
578
 
579
        pos = 0;
580
        while (TRUE) {
581
                if (intflag)
582
                        goto closefiles;
583
 
584
                cc1 = read(fd1, buf1, 8192-16);
585
                if (cc1 < 0) {
586
                        perror(argv[1]);
587
                        goto closefiles;
588
                }
589
 
590
                cc2 = read(fd2, buf2, 8192-16);
591
                if (cc2 < 0) {
592
                        perror(argv[2]);
593
                        goto closefiles;
594
                }
595
 
596
                if ((cc1 == 0) && (cc2 == 0)) {
597
                        printf("Files are identical\n");
598
                        goto closefiles;
599
                }
600
 
601
                if (cc1 < cc2) {
602
                        printf("First file is shorter than second\n");
603
                        goto closefiles;
604
                }
605
 
606
                if (cc1 > cc2) {
607
                        printf("Second file is shorter than first\n");
608
                        goto closefiles;
609
                }
610
 
611
                if (memcmp(buf1, buf2, cc1) == 0) {
612
                        pos += cc1;
613
                        continue;
614
                }
615
 
616
                bp1 = buf1;
617
                bp2 = buf2;
618
                while (*bp1++ == *bp2++)
619
                        pos++;
620
 
621
                printf("Files differ at byte position %ld\n", pos);
622
                goto closefiles;
623
        }
624
 
625
closefiles:
626
        close(fd1);
627
        close(fd2);
628
        free(buf1);
629
        free(buf2);
630
}
631
 
632
 
633
void
634
do_more(argc, argv)
635
        char    **argv;
636
{
637
        FILE    *fp;
638
        char    *name;
639
        int     ch;
640
        int     line;
641
        int     col;
642
        char    buf[80];
643
 
644
        while (argc-- > 1) {
645
                name = *(++argv);
646
 
647
                fp = fopen(name, "r");
648
                if (fp == NULL) {
649
                        perror(name);
650
                        return;
651
                }
652
 
653
                printf("<< %s >>\n", name);
654
                line = 1;
655
                col = 0;
656
 
657
                while (fp && ((ch = fgetc(fp)) != EOF)) {
658
                        switch (ch) {
659
                                case '\r':
660
                                        col = 0;
661
                                        break;
662
 
663
                                case '\n':
664
                                        line++;
665
                                        col = 0;
666
                                        break;
667
 
668
                                case '\t':
669
                                        col = ((col + 1) | 0x07) + 1;
670
                                        break;
671
 
672
                                case '\b':
673
                                        if (col > 0)
674
                                                col--;
675
                                        break;
676
 
677
                                default:
678
                                        col++;
679
                        }
680
 
681
                        putchar(ch);
682
                        if (col >= 80) {
683
                                col -= 80;
684
                                line++;
685
                        }
686
 
687
                        if (line < 24)
688
                                continue;
689
 
690
                        if (col > 0)
691
                                putchar('\n');
692
 
693
                        printf("--More--");
694
                        fflush(stdout);
695
 
696
                        if (intflag || (read(0, buf, sizeof(buf)) < 0)) {
697
                                if (fp)
698
                                        fclose(fp);
699
                                return;
700
                        }
701
 
702
                        ch = buf[0];
703
                        if (ch == ':')
704
                                ch = buf[1];
705
 
706
                        switch (ch) {
707
                                case 'N':
708
                                case 'n':
709
                                        fclose(fp);
710
                                        fp = NULL;
711
                                        break;
712
 
713
                                case 'Q':
714
                                case 'q':
715
                                        fclose(fp);
716
                                        return;
717
                        }
718
 
719
                        col = 0;
720
                        line = 1;
721
                }
722
                if (fp)
723
                        fclose(fp);
724
        }
725
}
726
 
727
 
728
void
729
do_exit(argc, argv)
730
        char    **argv;
731
{
732
        exit(0);
733
}
734
 
735
 
736
void
737
do_setenv(argc, argv)
738
        char    **argv;
739
{
740
        setenv(argv[1], argv[2], 1);
741
}
742
 
743
 
744
void
745
do_printenv(argc, argv)
746
        char    **argv;
747
{
748
        char            **env;
749
        extern char     **environ;
750
        int             len;
751
 
752
        env = environ;
753
 
754
        if (argc == 1) {
755
                while (*env)
756
                        printf("%s\n", *env++);
757
                return;
758
        }
759
 
760
        len = strlen(argv[1]);
761
        while (*env) {
762
                if ((strlen(*env) > len) && (env[0][len] == '=') &&
763
                        (memcmp(argv[1], *env, len) == 0))
764
                {
765
                        printf("%s\n", &env[0][len+1]);
766
                        return;
767
                }
768
                env++;
769
        }
770
}
771
 
772
 
773
void
774
do_umask(argc, argv)
775
        char    **argv;
776
{
777
        char    *cp;
778
        int     mask;
779
 
780
        if (argc <= 1) {
781
                mask = umask(0);
782
                umask(mask);
783
                printf("%03o\n", mask);
784
                return;
785
        }
786
 
787
        mask = 0;
788
        cp = argv[1];
789
        while (isoctal(*cp))
790
                mask = mask * 8 + *cp++ - '0';
791
 
792
        if (*cp || (mask & ~0777)) {
793
                fprintf(stderr, "Bad umask value\n");
794
                return;
795
        }
796
 
797
        umask(mask);
798
}
799
 
800
 
801
void
802
do_kill(argc, argv)
803
        char    **argv;
804
{
805
        char    *cp;
806
        int     sig;
807
        int     pid;
808
 
809
        sig = SIGTERM;
810
 
811
        if (argv[1][0] == '-') {
812
                cp = &argv[1][1];
813
                if (strcmp(cp, "HUP") == 0)
814
                        sig = SIGHUP;
815
                else if (strcmp(cp, "INT") == 0)
816
                        sig = SIGINT;
817
                else if (strcmp(cp, "QUIT") == 0)
818
                        sig = SIGQUIT;
819
                else if (strcmp(cp, "ILL") == 0)
820
                        sig = SIGILL;
821
                else if (strcmp(cp, "TRAP") == 0)
822
                        sig = SIGTRAP;
823
                else if (strcmp(cp, "ABRT") == 0)
824
                        sig = SIGABRT;
825
                else if (strcmp(cp, "IOT") == 0)
826
                        sig = SIGIOT;
827
                else if (strcmp(cp, "BUS") == 0)
828
                        sig = SIGBUS;
829
                else if (strcmp(cp, "FPE") == 0)
830
                        sig = SIGFPE;
831
                else if (strcmp(cp, "KILL") == 0)
832
                        sig = SIGKILL;
833
                else if (strcmp(cp, "USR1") == 0)
834
                        sig = SIGUSR1;
835
                else if (strcmp(cp, "SEGV") == 0)
836
                        sig = SIGSEGV;
837
                else if (strcmp(cp, "USR2") == 0)
838
                        sig = SIGUSR2;
839
                else if (strcmp(cp, "PIPE") == 0)
840
                        sig = SIGPIPE;
841
                else if (strcmp(cp, "ALRM") == 0)
842
                        sig = SIGALRM;
843
                else if (strcmp(cp, "TERM") == 0)
844
                        sig = SIGTERM;
845
                else if (strcmp(cp, "STKFLT") == 0)
846
                        sig = SIGSTKFLT;
847
                else if (strcmp(cp, "CHLD") == 0)
848
                        sig = SIGCHLD;
849
                else if (strcmp(cp, "CONT") == 0)
850
                        sig = SIGCONT;
851
                else if (strcmp(cp, "STOP") == 0)
852
                        sig = SIGSTOP;
853
                else if (strcmp(cp, "TSTP") == 0)
854
                        sig = SIGTSTP;
855
                else if (strcmp(cp, "TTIN") == 0)
856
                        sig = SIGTTIN;
857
                else if (strcmp(cp, "TTOU") == 0)
858
                        sig = SIGTTOU;
859
                else if (strcmp(cp, "URG") == 0)
860
                        sig = SIGURG;
861
                else if (strcmp(cp, "PWR") == 0)
862
                        sig = SIGPWR;
863
                else {
864
                        sig = 0;
865
                        while (isdecimal(*cp))
866
                                sig = sig * 10 + *cp++ - '0';
867
 
868
                        if (*cp) {
869
                                fprintf(stderr, "Unknown signal\n");
870
                                return;
871
                        }
872
                }
873
                argc--;
874
                argv++;
875
        }
876
 
877
        while (argc-- > 1) {
878
                cp = *++argv;
879
                pid = 0;
880
                while (isdecimal(*cp))
881
                        pid = pid * 10 + *cp++ - '0';
882
 
883
                if (*cp) {
884
                        fprintf(stderr, "Non-numeric pid\n");
885
                        return;
886
                }
887
 
888
                if (kill(pid, sig) < 0)
889
                        perror(*argv);
890
        }
891
}
892
 
893
/* END CODE */

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.