Skip to main content

Posts

Showing posts with the label os command

Write a program to implement following UNIX commands.

Write a program to implement following UNIX commands. wc -c <filename> displays number of characters for the given file. wc -w <filename> display number of words for given file. wc -l <filename> display number of line for given file. Program: #include <stdio.h> #include <stdlib.h> #include <string.h> void main(){ setbuf(stdout,NULL); FILE *fp; char s[80],a[5],c[5],temp[100],f[50],p[50],ch; int n; while(1){ printf("\nAP:>"); gets(s); if(strcmp(s,"exit")==0){ exit(0); } sscanf(s,"%s%s%s",&c,&a,&f); fp=fopen(f,"r"); if(strcmp(c,"wc")!=0){ printf("Invalid Command."); }else if(fp==NULL){ printf("File cannot be found."); }else if(strcmp(a,"-c")==0){ n=0; while(!feof(fp)){ ch = fgetc(fp); n++; } printf("\nNumber of character present in file:%d",n); }else if(strcmp(a,"-l")==0){ ...

Write a program to implement following UNIX commands

Write a program to implement following UNIX commands mv <source file> <target file> it moves file into target file. cp <source file> <target file> it copies file into target file. rm <target file> it deletes the file. Program: #include <stdio.h> #include <stdlib.h> #include <string.h> void main(){ setbuf(stdout,NULL); FILE *fp1, *fp2; char s[50], cm[20], path1[20], path2[20], ch; while(1){ printf("\nAP:>"); gets(s); if(strcmp(s,"exit")==0){ exit(0); }else{ sscanf(s,"%s%s%s",&cm,&path1,&path2); if(strcmp(cm,"rm")==0){ if(remove(path1)==0){ printf("File Deleted."); }else{ printf("File not deleted."); } }else if(strcmp(cm,"mv")==0){ fp1 = fopen(path1,"r"); fp2 = fopen(path2,"w"); while(!feof(fp1)){ ch = fgetc(fp1); fputc(ch,fp2); } remove(path1); }...

Write a program to implement ls command of UNIX with following options on DOS.

Write a program to implement ls command of UNIX with following options on DOS. ls -l to display long listing of files rowwise. ls -Q to display the filename in double quotes. ls -c to sort the files on last modification time of the files Program: #include <stdio.h> #include <stdlib.h> #include <dirent.h> #include <string.h> void main(){ setbuf(stdout,NULL); DIR *dir; struct dirent *ent; char s[80], c[5], f[50], ar[2], ch[2]; while(1){ printf("\nAP:>"); gets(s); if(strcmp(s,"exit")==0){ exit(0); } sscanf(s,"%s %s %s",&c,&ch,&f); if(strcmp(c,"ls")!=0){ printf("Invalid Command"); }else if(strcmp(ch,"-l")==0){ if((dir = opendir(f))==NULL){ perror("Unable to open directory."); exit(1); } while((ent=readdir(dir))!=NULL){ printf("\n%s",ent->d_name); } if(closedir(dir)!=0){ perror("Unable to close dir...

Write a program to implement cat command of UNIX on DOS.

Write a program to implement cat command of UNIX on DOS. cat <filename> to display files on the screen. cat < filename it is same as type command. cat > filename it is same as copy command. Program: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <dirent.h> void main(){ setbuf(stdout,NULL); DIR *dir; struct dirent *ent; FILE *fp; char s[80],temp[80],c[5],f[50],a[2],p[2]; while(1){ printf("\nAP:>"); gets(s); if(strcmp(s,"exit")==0){ exit(0); } sscanf(s,"%s%s%s%s",c,a,f,p); printf("%s|%s|%s|%s",c,a,f,p); if(strcmp(c,"cat")!=0){ printf("invalid command"); }else if(strcmp(a,"<")==0 && strcmp(p,">")==0){ if((dir=opendir(f))==NULL){ perror("Unable to open direcotry."); exit(0); } while((ent= readdir(dir))!=NULL){ printf("%s\n",ent->d_name); } if(closedi...

Write a Program to implement "ls" command of UNIX with following options on DOS

Write a Program to implement "ls" command of UNIX with following options on DOS. "ls" to display files. "ls -a" to display all the hidden files. "ls -m" to display the files separated with commas. Program: #include<stdio.h> #include<stdlib.h> #include<string.h> #include<dirent.h> void ls(){ DIR *d; struct dirent *dn; char dr[20]; printf("Enter the Directory name:"); gets(dr); if((d=opendir(dr))==NULL){ printf("\nDirectory can't open"); } else { while((dn=readdir(d))!=NULL){ printf(" %s",dn->d_name); } } } void lsm(){ DIR *d; struct dirent *dn; char dr[20]; printf("Enter the Directory name:"); gets(dr); if((d=opendir(dr))==NULL){ printf("\nDirectory can't open"); } else{ while((dn=readdir(d))!=NULL){ printf("%s,",dn->d...