-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell.c
executable file
·1494 lines (1123 loc) · 27 KB
/
shell.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
TITLE: OPERATING SYSTEMS LABS
AUTHOR 1: MARTIÑO RIVERA DOURADO
AUTHOR 2: CARMEN CORRALES CAMELLO
DATE: 13/12/2017
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <time.h>
#include <pwd.h>
#include <grp.h>
#include <errno.h>
#include <dirent.h>
#include <ctype.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/wait.h>
#include <sys/resource.h>
#include <signal.h>
//LISTS
#include "list.h"
#include "searchList.h"
#include "listproc.h"
// Constant values
#define CMD_LENGTH 70
#define NAME_LENGTH PATH_MAX
#define PATH_LENGTH PATH_MAX
#define REC_TAM 2048
#define LEERCOMPLETO ((ssize_t)-1)
// Global type definition
struct COMMAND {
char * name;
void (* function)(char **);
};
// Global variable for recursivity
int recursive_flag = 0; // Initialized as false
// Global variables for the lists
List allocList; // Allocated blocks list
procList backgList; // Background process list
// Global variable
int globalVar;
/******************************FUNCTIONS*************************************/
int isDirectory (char * file){
/*
* Objective: This function checks whether the file is a directory or not.
* */
struct stat s;
if (lstat (file, &s) == -1)
return 0;
return S_ISDIR (s.st_mode);
}
char TipoFichero (mode_t m){
/*
* Objective: This function returns a character depending on the type of file.
* */
switch (m&S_IFMT) { /*and bit a bit con los bits de formato,0170000 */
case S_IFSOCK: return 's'; /*socket */
case S_IFLNK: return 'l'; /*symbolic link*/
case S_IFREG: return '-'; /* fichero normal*/
case S_IFBLK: return 'b'; /*block device*/
case S_IFDIR: return 'd'; /*directorio */
case S_IFCHR: return 'c'; /*char device*/
case S_IFIFO: return 'p'; /*pipe*/
default: return '?'; /*desconocido, no deberia aparecer*/
}
}
char * getPerms (mode_t m){
/*
* Objective: This function returns an string meaning the permissions of a file.
* */
static char permisos[12];
strcpy (permisos,"---------- ");
permisos[0]=TipoFichero(m);
if (m&S_IRUSR) permisos[1]='r'; /*propietario*/
if (m&S_IWUSR) permisos[2]='w';
if (m&S_IXUSR) permisos[3]='x';
if (m&S_IRGRP) permisos[4]='r'; /*grupo*/
if (m&S_IWGRP) permisos[5]='w';
if (m&S_IXGRP) permisos[6]='x';
if (m&S_IROTH) permisos[7]='r'; /*resto*/
if (m&S_IWOTH) permisos[8]='w';
if (m&S_IXOTH) permisos[9]='x';
if (m&S_ISUID) permisos[3]='s'; /*setuid, setgid y stickybit*/
if (m&S_ISGID) permisos[6]='s';
if (m&S_ISVTX) permisos[9]='t';
return (permisos);
}
char * getUser (uid_t u){
/*
* Objective: This function RETURNS an array with the name of the user of the file .
* */
struct passwd * p;
p=getpwuid(u);
if (p==NULL)
return ("Unknown");
else
return (p-> pw_name);
}
char * getGroup (gid_t g){
/*
* Objective: This function RETURNS an array with the name of the group of the file .
* */
struct group * p;
p=getgrgid(g);
if (p==NULL)
return ("Unknown");
else
return (p-> gr_name);
}
char * getDate (struct timespec st){
/*
* Objective: This function RETURNS an array with the date of the last modification of the file .
* */
static char stringDate[16];
strftime(stringDate, 16, "%b %d %R", (localtime(&st.tv_sec) ));
return stringDate;
}
void do_info(char *file){
/*
* Objective: This function displays the info of a file .
* */
struct stat s;
int inoNumber;
char * perm;
int linkNumber;
char * user;
char * group;
int size;
char * date;
if (lstat(file, &s)==-1){
printf("Error: cannot access %s: %s\n",file, strerror(errno));
return;
}
inoNumber = (int) s.st_ino;
linkNumber = (int) s.st_nlink;
perm=getPerms(s.st_mode);
user=getUser(s.st_uid);
group=getGroup(s.st_gid);
size = (int) s.st_size;
date=getDate(s.st_atim);
printf ("%d %s %d %s %s %d %s %s", inoNumber, perm, linkNumber, user, group, size, date, file);
if (S_ISLNK(s.st_mode)){
char linkName[NAME_LENGTH];
realpath(file, linkName);
if (linkName != NULL){
printf(" -> %s\n", linkName );
}
else{
printf("\nError: cannot follow link %s: %s\n",file, strerror(errno));
}
}
else printf ("\n");
}
void info (char * args[]){
/*
* Objective: This function processes the command info .
* */
int i;
for(i=0; args[i]!=NULL; i++)
do_info(args[i]);
}
void recursive(char * args[]){
/*
* Objective: This function processes the command recursive, to change the recursive flag
* or display its state.
* */
if(args[0]==NULL){
if(recursive_flag)
printf("ON\n");
else
printf("OFF\n");
}
else if(!strcmp(args[0],"ON")){
recursive_flag = 1;
}
else if(!strcmp(args[0],"OFF")){
recursive_flag = 0;
}
else{
printf("Invalid arguments\n");
}
}
void do_list (char * file, int longListing, char * currentDir){
/*
* Objective: This function lists the info of a file. If LONGLISTING is true, the file will be listed
* like info does.
* If RECURSIVE_FLAG is true, a directory will be listed with all its contents recusively.
* */
if(!strcmp(file, currentDir)){
strcpy(file,".");
}
if(recursive_flag && isDirectory(file)){
DIR * d = opendir(file); // File descriptor of directory
struct dirent * dirEntry;
char absolutePath[PATH_LENGTH];
char * fileName;
if(d == NULL){
printf("Error: cannot access %s: %s\n",file, strerror(errno));
}
else{
while((dirEntry=readdir(d))!= NULL){
fileName = dirEntry->d_name;
if(strcmp(fileName, ".") && (strcmp(fileName, ".."))){
strcpy(absolutePath, file);
strcat(absolutePath,"/");
strcat(absolutePath, fileName);
do_list(absolutePath, longListing, currentDir);
}
}
}
closedir(d);
return;
}
if(isDirectory(file)){
DIR * d = opendir(file); // File descriptor of directory
struct dirent * dirEntry;
char absolutePath[PATH_LENGTH];
char * fileName;
if(d == NULL){
printf("Error: cannot access %s: %s\n",file, strerror(errno));
return;
}
while((dirEntry=readdir(d))!= NULL){
fileName = dirEntry->d_name;
if(strcmp(fileName, ".") && (strcmp(fileName, ".."))){
strcpy(absolutePath, file);
strcat(absolutePath,"/");
strcat(absolutePath, fileName);
if(longListing){
do_info(absolutePath);
}
else{
struct stat s;
if (lstat(absolutePath, &s)==-1){
printf("Error: cannot access %s: %s\n",absolutePath, strerror(errno));
}
else{
int size = (int) s.st_size;
printf("%d %s\n", size, fileName);
}
}
}
}
closedir(d);
return;
}
// If not directory
if(longListing){
do_info(file);
return;
}
struct stat s;
if (lstat(file, &s)==-1){
printf("Error: cannot access %s: %s\n",file, strerror(errno));
}
else{
int size = (int) s.st_size;
printf("%d %s\n", size, file);
}
}
void list (char * args[]){
/*
* Objective: This function processes the command list .
* */
int longListing = 0;
static char currentDir[NAME_LENGTH];
if(getcwd(currentDir,NAME_LENGTH)==NULL){
printf("\nError: cannot access current directory: %s\n", strerror(errno));
}
// Check first position
if(args[0]==NULL){
do_list(currentDir, longListing, currentDir);
}
else if(!strcmp(args[0], "-l")){
longListing = 1; // If parameter -l is given
args = args + 1;
if(args[0]==NULL){
do_list(currentDir, longListing, currentDir);
}
}
// Check next positions
int i;
for(i=0; args[i]!=NULL; i++){
do_list(args[i], longListing, currentDir);
}
}
void do_eliminate(char * file, int force){
/*
* Objective: This function eliminates a file or an empty directory. If FORCE is true,
* full directories
* */
int isDir = isDirectory(file);
if(force && isDir){
DIR * d = opendir(file);
struct dirent * dirEntry;
char absolutePath[PATH_LENGTH];
char * fileName;
while((dirEntry=readdir(d))!=NULL){
fileName = dirEntry->d_name;
if(strcmp(fileName, ".") && (strcmp(fileName, ".."))){
strcpy(absolutePath, file);
strcat(absolutePath,"/");
strcat(absolutePath, fileName);
do_eliminate(absolutePath, force);
}
}
closedir(d);
if((rmdir(file)==-1)){
printf("\nError: cannot eliminate the file %s: %s\n", file, strerror(errno));
}
}
else{
if(isDir){
if((rmdir(file)==-1)){
printf("\nError: cannot eliminate the file %s: %s\n", file, strerror(errno));
}
}
else{
if ((unlink(file))==-1) {
printf("\nError: cannot eliminate the file %s: %s\n", file, strerror(errno));
}
}
}
}
void eliminate(char * args[]){
/*
* Objective: This function processes the eliminate command .
* */
int force = 0;
if(args[0]!=NULL){
if(!strcmp(args[0], "-f")){
force = 1;
args = args + 1;
}
if(args[0]!=NULL){
do_eliminate(args[0], force);
}
}
}
int TrocearCadena(char * cadena, char * trozos[]){
// Provided function. It splits the input string (cadena) into args (trozos)
int i=1;
if ((trozos[0]=strtok(cadena," \n\t"))==NULL)
return 0;
while ((trozos[i]=strtok(NULL," \n\t"))!=NULL)
i++;
return i;
}
void logins(){
/*
* Objective: This function displays the logins.
* */
printf("\n\tcarmen.corralesc, martino.rivera.dourado\n");
}
void names(){
/*
* Objective: This function displays the names.
* */
printf("\n\tCarmen Corrales Camello, Martiño Rivera Dourado\n");
}
void pid(char * args[]){
/*
* Objective: This function gets the flags of the command and displays the process identifier.
* */
if(args[0]==NULL){
printf("PID of process executing the shell: %d\n", getpid());
}
else if(strcmp(args[0], "-p")==0){
if (getppid() == 0) printf("PID of the parent process in a different PID namespace:\n");
printf("PID of the parent process: %d\n", getppid());
}
else{
printf("Invalid arguments\n");
}
}
void autores(char * args[]){
/*
* Objective: This function works with the flags of a command and displays the authors information.
* */
if(args[0]==NULL){
names();
logins();
}
else if(strcmp(args[0],"-l")==0){
logins();
}
else if(strcmp(args[0], "-n")==0){
names();
}
else{
printf("Invalid arguments\n");
}
printf("\n");
}
void end (char * args[]){
/*
Objective: To end the shell
*/
exit(0);
}
void gettime(char * string){
/*
Objective: To get this instant time
*/
time_t now;
now = time(0);
strftime(string, 30, "%a %b %d %T %Y", localtime(&now));
}
int isNumber(char * str){
int answer = 0;
int i = 0;
if(str!=NULL){
answer = 1;
while(str[i]!='\n' && str[i]!='\0'){
answer = answer && (isdigit(str[i]) || (str[i]=='-')); // We check that all characters are digits
i++;
}
}
return answer;
}
void mallocDealloc (char *str){
/*
Objective: Do the deallocate part of the command malloc
*/
void * addr;
int tam;
if (str==NULL){
showList_method(MALLOC, allocList);
return;
}
if (!isNumber(str)){
printf("Invalid arguments\n");
return;
}
sscanf(str, "%d", &tam); // Cast to int from string
if ((addr=list_find_address_fromsize(tam, allocList))!=NULL){
printf("deallocated %d at %p\n", tam, addr);
free(addr);
list_remove_size(tam, allocList);
}
else{
showList_method(MALLOC, allocList);
}
}
void myMalloc (char * args[]){
/*
Objective: Execute the malloc command, allocate memory in the process space
*/
if (args[0]==NULL){
showList_method(MALLOC, allocList);
}
else if(isNumber(args[0])){
int inserted;
int tam;
char now[30];
sscanf(args[0], "%d", &tam); // Cast to int from string
void * address = malloc(tam);
gettime(now);
if(address==NULL){
printf("Error: cannot allocate %d: %s\n",tam, strerror(errno));
}
else{
printf("allocated %d at %p\n", tam, address);
elem newElem = list_createElement(MALLOC, address, tam, now, "", 0, 0);
inserted = list_insert(newElem, allocList); // Insertion in the list of allocation
if(!inserted){
printf("Error: cannot insert element in the list\n");
return;
}
}
}
else if(!strcmp(args[0], "-deallocate")){ // Call to deallocate
mallocDealloc(args[1]);
}
else{
printf("Invalid arguments\n");
}
}
void * mmapFile (char * file, int protection){
/*
Objective: To map a file in the shell's space
*/
int df, map=MAP_PRIVATE, mode=O_RDONLY, inserted;
struct stat s;
void *p;
char now[30];
if (protection & PROT_WRITE)
mode=O_RDWR;
if (stat(file,&s)==-1 || (df=open(file, mode))==-1)
return NULL;
if ((p=mmap (NULL,s.st_size, protection, map, df, 0))==MAP_FAILED)
return NULL;
// Insertion in the list
gettime(now);
elem newElem = list_createElement(MMAP, p, s.st_size, now, file, df, 0);
inserted = list_insert(newElem, allocList);
if(!inserted){
printf("Error: cannot insert element in the list\n");
return NULL;
}
return p;
}
void mmapDealloc (char *str){
/*
Objective: Do the deallocate part of mmap
*/
void * addr;
size_t size;
if (str==NULL){
showList_method(MMAP, allocList);
return;
}
if ((addr=list_find_address_fromname(&size, str, allocList))!=NULL){
printf ("file %s unmapped at %p\n", str, addr);
munmap(addr, size);
list_remove_name(str, allocList); // Removal from the list from the name
}
else{
showList_method(MMAP, allocList);
}
}
void myMmap (char * args[]){
/*
Objective: Process the mmap command with permissions rights
*/
char *perm;
void *p;
int protection=0;
if (args[0]==NULL){
showList_method(MMAP, allocList);
}
else if(!strcmp(args[0], "-deallocate")){
mmapDealloc(args[1]);
}
else{
// Process the permissions
if ((perm=args[1])!=NULL && strlen(perm)<4) {
if (strchr(perm,'r')!=NULL)
protection |= PROT_READ;
if (strchr(perm,'w')!=NULL)
protection |= PROT_WRITE;
if (strchr(perm,'x')!=NULL)
protection |= PROT_EXEC;
}
if ((p=mmapFile(args[0],protection))==NULL)
perror ("Imposible to map file");
else
printf ("file %s mapped at %p\n", args[0], p);
}
}
void * ObtainMemoryShmget (key_t key, off_t tam){
/*
Objective: To obtain the shared memory address
*/
void * p;
int aux,id,flags=0777;
struct shmid_ds s;
char now[30];
int inserted;
if (tam) /*si tam no es 0 la crea en modo exclusivo */
flags=flags | IPC_CREAT | IPC_EXCL;
/*si tam es 0 intenta acceder a una ya creada*/
if (key==IPC_PRIVATE){ /*no nos vale*/
errno=EINVAL;
return NULL;
}
if ((id=shmget(key, tam, flags))==-1)
return (NULL);
if ((p=shmat(id,NULL,0))==(void*) -1){
aux=errno; /*si se ha creado y no se puede mapear*/
if (tam) /*se borra */
shmctl(id,IPC_RMID,NULL);
errno=aux;
return (NULL);
}
shmctl (id,IPC_STAT,&s);
// Insertion in the list
gettime(now);
elem newElem = list_createElement(SHARED, p, s.shm_segsz, now, "", 0, key);
inserted = list_insert(newElem, allocList);
if(!inserted){
printf("Error: cannot insert element in the list\n");
return NULL;
}
return (p);
}
void sharednew (char * args[]){
/*
Objective: Process the command of sharednew memory
*/
key_t k;
off_t tam = 0;
void * p;
if (args[0]==NULL || args[1]==NULL){
showList_method(SHARED, allocList);
return;
}
k = (key_t) atoi(args[0]); // We get the key (cl)
if (args[1]!=NULL) // We get the size
tam=(off_t) atoll(args[1]);
if ((p=ObtainMemoryShmget(k,tam))==NULL) // If the address is not valid
perror ("Cannot allocate");
else
printf ("Allocated shared memory (key %d) at %p\n",k,p);
}
void sharedDealloc (char *str){
/*
Objective: Do the deallocate part of shared
*/
key_t k;
void * addr;
if (str==NULL){
showList_method(SHARED, allocList);
return;
}
k = (key_t) atoi(str);
if ((addr=list_find_address_fromkey(k,allocList))!=NULL){
shmdt (addr);
list_remove_key(k, allocList);
}
else{
printf ("No shared memory of key %d allocated \n", (int) k);
}
}
void shared (char * args[]){
/*
Objective: Process the shared command
*/
key_t k;
void * p;
if(args[0] == NULL){
showList_method(SHARED, allocList);
return;
}
if(!strcmp(args[0], "-deallocate")){
sharedDealloc (args[1]);
return;
}
k = (key_t) atoi(args[0]); // We get the key (cl)
if ((p=ObtainMemoryShmget(k,0))==NULL)
perror ("Cannot allocate");
else
printf ("Allocated shared memory (key %d) at %p\n",k,p);
}
void rmkey (char * args[]){
/*
Objective: Remove the shared memory key of the system
*/
key_t k;
int id;
char * key = args[0];
if (key==NULL || (k=(key_t) strtoul(key,NULL,10))==IPC_PRIVATE){
printf ("rmkey: invalid key\n");
return;
}
if ((id=shmget(k,0,0666))==-1){
perror ("shmget: impossible to obtain shared memory");
return;
}
if (shmctl(id,IPC_RMID,NULL)==-1)
perror ("shmctl: impossible to eliminate shared memory\n");
}
void allocation (char * args[]){
/*
Objective: Show the allocated space
*/
showList_method(MALLOC, allocList);
showList_method(MMAP, allocList);
showList_method(SHARED, allocList);
}
void deallocate (char * args[]){
/*
Objective: Deallocates a specified address from the allocated space
*/
void * addr;
pos p;
elem e;
if(args[0]==NULL){
allocation(args);
return;
}
sscanf(args[0], "%p", &addr);
if((p = list_find_address(addr, allocList))==-1){
allocation(args);
}
else{
list_getElement(&e, p, allocList);
switch(e.method){
case MALLOC:
free(e.address);
list_remove_size(e.size, allocList);
break;
case MMAP:
munmap(e.address, e.size);
list_remove_name(e.file_name, allocList);
break;
case SHARED:
shmdt (e.address);
list_remove_key(e.key, allocList);
break;
}
}
}
void mem (char * args[]){
/*
Objective: Prints the memory addresses required in the wording
*/
// Local variables
int var1;
char * var2;
char var3;
printf("Program functions:\n");
printf("\t autores: %p\n", autores);
printf("\t deallocate: %p\n", deallocate);
printf("\t pid: %p\n", pid);
printf("Global variables:\n");
printf("\t allocList: %p\n", &allocList);
printf("\t recursive_flag: %p\n", &recursive_flag);
printf("\t globalVar: %p\n", &globalVar);
printf("Local variables:\n");
printf("\t var1: %p\n", &var1);
printf("\t var2: %p\n", &var2);
printf("\t var3: %p\n", &var3);
}
void myMemdump (char * args[]){
/*
Objective: Shows the contents in ASCII and hexadecimal of a memory address in the shell's
space.
*/
int cont, limit, start, i;
char * address;
if(args[0]==NULL){
printf("Invalid arguments\n");
return;
}
else{
sscanf(args[0], "%p", &address);
}
if(args[1]==NULL){
cont = 25;
}
else{
sscanf(args[1], "%d", &cont);
}
start = 0;
while(cont>0){
if(cont >= 25){
limit = start + 25;
}
else{
limit = start + (cont % 25);
}
for(i = start; i<limit; i++){
if (isprint(*(address+i))){
printf(" %2c ", *(address+i));