Sei sulla pagina 1di 11

//to kwno type of afile

#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<dirent.h>
int main(int ac,char *arv[])
{
struct stat st2;
lstat(arv[1],&st2);
//
printf("%d",st2.st_mode);
if(S_ISREG(st2.st_mode))
printf("%s is a REGULAR FILE");
if(S_ISDIR(st2.st_mode))
printf("%s is a DIRECTORY FILE");
if(S_ISCHR(st2.st_mode))
printf("%s is a CHARACTER DEVICE");
if(S_ISBLK(st2.st_mode))
printf("%s is a BLOCK DEVICE");
if(S_ISFIFO(st2.st_mode))
printf("%s is a FIFO FILE");
if(S_ISLNK(st2.st_mode))
printf("%s is a LINK FILE");
if(S_ISSOCK(st2.st_mode))
printf("%s is a SOCKET FILE");
}
#include<stdio.h>
#include<unistd.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<fcntl.h>
#include<dirent.h>
#define BUFF 1024
int main(int argc, char * argv[])
{
int sf,tf,n;
char buf[BUFF];
if(argc!=3)
{
printf("USAGE:$s src-file tar-file",argv[0]);
return 0;
}
sf=open(argv[1],O_RDONLY);
if(sf==-1)
{
printf("source file %s can not be opened",argv[1]);
return 0;
}
tf=open(argv[2],O_RDWR|O_CREAT|O_EXCL,S_IRWXU);
if(tf==-1)
{
printf("targer file %s can not be opened",argv[2]);
return 0;
}
while( (read(sf,buf,1))!=0)
write(tf,buf,1);
printf("copy done!!");
return 1;
}

# to simulat mv command
#include<stdio.h>
#include<unistd.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<fcntl.h>
#include<dirent.h>
#define BUFF 1024
int main(int argc, char * argv[])
{
int sf,tf,n;
char buf[BUFF];
if(argc!=3)
{
printf("USAGE:$s src-file tar-file",argv[0]);
return 0;
}
sf=open(argv[1],O_RDONLY);
if(sf==-1)
{
printf("source file %s can not be opened",argv[1]);
return 0;
}
tf=open(argv[2],O_RDWR|O_CREAT|O_EXCL,S_IRWXU);
if(tf==-1)
{
printf("targer file %s can not be opened",argv[2]);
return 0;
}
while( (read(sf,buf,1))!=0)
write(tf,buf,1);
printf("copy done!!");
return 1;
}
include<stdio.h>
#include<errno.h>
//to print all errors in system
int main()
{
int i=0;
printf("%d\n",sys_nerr);
for(i=0;i<sys_nerr;i++)
printf("%d. %s\n",i,strerror(i));
}
//to demo perror
#include<stdio.h>
#include<errno.h>
#include<fcntl.h>
int main()
{
int i;
i=open("pppp",O_RDONLY);
if(i==-1)
{
printf("error no=%d",errno);
perror("ERROR");
}
return 1;

}
//demo stat,lstat,fstat funtions
#include<stdio.h>
#include<unistd.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<fcntl.h>
#include<dirent.h>
int main(int argc, char * argv[])
{
int i=-1,fd,ch;
char *time;
struct stat buf;
while(1)
{
printf("1.stat\t2.lstat 3.fstat");
scanf("%d",&ch);
switch(ch)
{
case 1: i-stat(argv[1],&buf);
break;
case 2: i=lstat(argv[1],&buf);
break;
case 3: fd-open(argv[1],O_RDONLY);
i-fstat(fd,&buf);
break;
case 4: return 0;
default: printf("invalid choice");
break;
}
if(i>=0)
{
printf("Device :%d\n",buf.st_dev);
printf("Device Type:%d\n",buf.st_rdev);
printf("Inode number :%d\n",buf.st_ino);
printf("protection :%o\n",buf.st_mode);
printf("number of hard links :%d\n",buf.st_nlink);
printf("user-id :%d\n",buf.st_uid);
printf("group-id :%d\n",buf.st_gid);
printf("total size :%d\n",buf.st_size);
printf("block-size :%d\n",buf.st_blksize);
printf("number of blocks allocated :%d\n",buf.st_blocks);
time=(char *)ctime(&buf.st_atime);
printf("access time :%s\n",time);
time=(char *)ctime(&buf.st_ctime);
printf("time of last change :%s\n",time);
time=(char *)ctime(&buf.st_mtime);
printf("time of modicication :%s\n",time);
}
else
printf("file NOT existed");
}
return 1;
}

//to demo tp print error mesg


#include<stdio.h>
#include<errno.h>
int main()
{
int i;
printf("enter anY error number");
scanf("%d",&i);
printf("%d:\t%s",i,strerror(i));
return 1;
}
//to count types of file sina given dir
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<dirent.h>
int main(int ac,char *arv[])
{
DIR *open;
struct dirent *st1;
struct stat st2;
int count[]={0,0,0, 0,0,0,0};
open=opendir(arv[1]);
while((st1=readdir(open))!=0)
{
lstat(st1->d_name,&st2);
if(S_ISREG(st2.st_mode))
count[0]++;
if(S_ISDIR(st2.st_mode))
count[1]++;
if(S_ISCHR(st2.st_mode))
count[2]++;
if(S_ISBLK(st2.st_mode))
count[3]++;
if(S_ISFIFO(st2.st_mode))
count[4]++;
if(S_ISLNK(st2.st_mode))
count[5]++;
if(S_ISSOCK(st2.st_mode))
count[6]++;
}
printf("reg files=%d\ndir files=%d\nchr-dev files=%d\nblk-dev files=
%d\nFIFO files=%d\n link files=%d\nsocket files=
%d\n",count[0],count[1],count[2],count[3],count[4],count[5],count[6]);
}

// to demo redirection
#include<stdio.h>
#include<unistd.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<fcntl.h>
#include<dirent.h>

int main(int argc, char * argv[])


{
int bytes;
char buf[20];
int fd=open(argv[1],O_RDWR|O_CREAT);
while((bytes=read(STDOUT_FILENO,buf,20))>0)
write(fd,buf,bytes);
}
//demo directory related sys calls
#include<stdio.h>
#include<unistd.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<fcntl.h>
#include<dirent.h>
int main(int argc, char * argv[])
{
int i=-1,fd,ch;
char *time;
struct stat buf;
while(1)
{
printf("1.stat\t2.lstat 3.fstat");
scanf("%d",&ch);
switch(ch)
{
case 1: i-stat(argv[1],&buf);
break;
case 2: i=lstat(argv[1],&buf);
break;
case 3: fd-open(argv[1],O_RDONLY);
i-fstat(fd,&buf);
break;
case 4: return 0;
default: printf("invalid choice");
break;
}
if(i>=0)
{
printf("Device :%d\n",buf.st_dev);
printf("Device Type:%d\n",buf.st_rdev);
printf("Inode number :%d\n",buf.st_ino);
printf("protection :%o\n",buf.st_mode);
printf("number of hard links :%d\n",buf.st_nlink);
printf("user-id :%d\n",buf.st_uid);
printf("group-id :%d\n",buf.st_gid);
printf("total size :%d\n",buf.st_size);
printf("block-size :%d\n",buf.st_blksize);
printf("number of blocks allocated :%d\n",buf.st_blocks);
time=(char *)ctime(&buf.st_atime);
printf("access time :%s\n",time);
time=(char *)ctime(&buf.st_ctime);
printf("time of last change :%s\n",time);
time=(char *)ctime(&buf.st_mtime);

}
else

printf("time of modicication

:%s\n",time);

//to create alink file


#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
int main(int ac,char *arv[])
{
int i;
i=link(arv[1], arv[2]);
if(i==0)
printf("link done");
else
printf("link not done");
return 0;
}
//to change permissions of a file
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
int main(int ac,char *arv[])
{
int i=chmod(arv[1],S_IRWXU);
if(i!=-1)
printf("PERMS CHANGED");
else
printf("PERMS NOT CHANGED");
return 0;
}
//to demo directory navigation
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
int main(int ac,char *arv[])
{
int i;
char *buf,bu[20];
buf=get_current_dir_name();
printf("current working directory is::%s\n",buf);
i=chdir(arv[1]);
if(i==0)
printf("after change working directory is::
%s\n",get_current_dir_name());
else
printf("current working directory is::%s\n",buf);
i=chdir(buf);
if(i==0)
printf("current working directory after rechanging is ::
%s\n",getcwd(bu,20));

else
printf("current dire is: %s\n",get_current_dir_name());
return 0;
}
//to remove link fo a given file
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
int main(int ac,char *arv[])
{
int i=unlink(arv[1]);
i=unlink(arv[1]);
printf("i=%d\n",i);
if(i==-1)
printf("link removed");
else

printf("link not removed");

return 0;
}
//to contrast stat and lstat
#include<stdio.h>
#include<unistd.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<fcntl.h>
#include<dirent.h>
#define BUFF 1024
int main(int argc, char * argv[])
{
struct stat st2;
int ch;
while(1)
{
printf("1.stat 2.lstat 3.exit");
scanf("%d", &ch);
switch(ch)
{
case 1:stat(argv[1],&st2);break;
case 2:lstat(argv[1],&st2);break;
case 3:return 0;break;
default:printf("invalid option");
}
if(S_ISREG(st2.st_mode))
printf("REGULAR");
if(S_ISLNK(st2.st_mode))
printf("SYMBOLIC LINK");
}
}
//to simulate ls
#include<stdio.h>

#include<unistd.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<fcntl.h>
#include<dirent.h>
int main(int argc, char * argv[])
{
DIR *open;
struct dirent *st1;
open=opendir(argv[1]);
while((st1=readdir(open))!=0)
{
printf("%s\n",st1->d_name);
}
}
//to demo truncate leaving modified dates in tact
#include<stdio.h>
#include<unistd.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<fcntl.h>
#include<utime.h>
int main(int argc, char * argv[])
{
int i,j;
struct stat t;
struct utimbuf ut;
i=stat(argv[1],&t);
printf("i=%d",i);
if(i==0)
{
j=truncate(argv[1],0);
if(j!=-1)
{
ut.actime=t.st_atime;
ut.modtime=t.st_mtime;
utime(argv[1],&ut);
printf("file successfully truncated");
}
else
perror("file not truncated!!");
}
else
perror("file not found!!!");
}
//to demo copy times and files
#include<stdio.h>
#include<unistd.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<fcntl.h>

#include<utime.h>
int main(int argc, char * argv[])
{
int i,j;
struct stat t;
struct utimbuf ut;
i=stat(argv[1],&t);
printf("i=%d",i);
if(i==0)
{
j=creat(argv[2],S_IRWXU);
if(j>0)
{
ut.actime=t.st_atime;
ut.modtime=t.st_mtime;
utime(argv[2],&ut);
printf("file successfully cretaed with duplicate time");
}
else
perror("file not created!");
}
else
perror("file not found!!!");
}
// to demo three dir related sys calls
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<dirent.h>
int main(int arc,char *arv[])
{
DIR *open;
int i;
struct dirent *st1;
open=opendir(arv[1]);
while( (st1=readdir(open))!=0)
{
printf("%s\n",st1->d_name);
//seekdir(open,0);
i=telldir(open);
printf("current offset =%d",i);
}
}
// to creat a dir & put afile in to it
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<dirent.h>
int main(int arc,char *arv[])

int i=mkdir(arv[1],0777);
if(i==0)
{
printf("directory %s created\n",arv[1]);
char *buf=get_current_dir_name();
chdir(arv[1]);
int j=creat(arv[2],0777);
if(j>0)
{
printf("file %s created\n",arv[2]);
remove(arv[2]);
printf("file removed");
}
else
perror("file not created");
chdir(buf);
printf("directory changed back");
int k=rmdir(arv[1]);
if(k==0)
printf("dir removed");
else
perror("dir not removed");
}
else
perror("dir not created");

}
//to find ausers login shell and home dir
#include<stdio.h>
main()
{
printf("HOME DIR=: %s",getenv("HOME"));
printf("LOGIN SHELL=: %s",getenv("SHELL"));
printf("MATCH TYPE=: %s",getenv("MACHTYPE"));
printf("HISTORY FILE NAME=: %s",getenv("HIST_FILE"));
}
// to check if a file exits or not if exists check permissions
#include<stdio.h>
#include<unistd.h>
int main(int arc,char *arv[])
{
int i,j;
i=access(arv[1],F_OK);
printf("i=%d",i);
if(i==0)
{
printf("file exists");
j=access(arv[1],W_OK);
printf("j=%d",j);
if(j==0)
printf("file has write permission");
else
printf("file has NO write permission");
}
else

printf("file NOT existing");

Potrebbero piacerti anche