Você está na página 1de 12

Poragramming

header.h
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include<dirent.h>
#include<fcntl.h>
#include<signal.h>
#include<sys/sem.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<sys/msg.h>
#include<sys/ipc.h>
#include<pthread.h>

1_my_ls
#include"header.h"
main(int argc,char **argv)
{
DIR *dp;
struct dirent *p;
struct stat v;
char *ch;
int c;
if(!(argc >= 1))
{
printf("Usage : ./my_ls [options]... [PATH]\n");
return;
}
if(argc == 1)
{
//-------------------------------------------------- $ ls-----------------------------------------dp = opendir(".");
if((int)dp == 0)
{
perror("opendir");
return;
}
while(p = readdir(dp))
{
if(p->d_name[0] != '.')
printf("%s\n",p->d_name);
}
}
else if(argc == 2)
{
//--------------------------------------------------$ ls
PATH--------------------------------------if(argv[1][0] != '-')
{
dp = opendir(argv[1]);
if((int)dp == 0)
{
perror("opendir");
return;
}
while(p = readdir(dp))
{
if(p->d_name[0] != '.')

printf("%s\n",p->d_name);
}
}
else
{
dp = opendir(".");
if((int)dp == 0)
{
perror("opendir");
return;
}
//----------------------------------------------------$ ls
-a---------------------------------------------if(argv[1][1] == 'a')
{
while(p = readdir(dp))
{
printf("%s\n",p->d_name);
}
}
//----------------------------------------------------$ ls
-i---------------------------------------------else if(argv[1][1] == 'i')
{
while(p = readdir(dp))
{
if(p->d_name[0] != '.')
printf("%s\t%d\n",p->d_name,(int)p->d_ino);
}
}
//---------------------------------------------------$ ls
-ls--------------------------------------------else if(argv[1][1] == 'l' && argv[1][2] == 's')
{
while(p = readdir(dp))
{
if(p->d_name[0] != '.')
{
stat(p->d_name,&v);
printf("%d ",(int)v.st_blocks/2);
printf("%c",v.st_mode&S_IRUSR?'r':'-');
printf("%c",v.st_mode&S_IWUSR?'w':'-');
printf("%c",v.st_mode&S_IXUSR?'x':'-');
printf("%c",v.st_mode&S_IRGRP?'r':'-');
printf("%c",v.st_mode&S_IWGRP?'w':'-');
printf("%c",v.st_mode&S_IXGRP?'x':'-');
printf("%c",v.st_mode&S_IROTH?'r':'-');

printf("%c",v.st_mode&S_IWOTH?'w':'-');
printf("%c ",v.st_mode&S_IXOTH?'x':'-');
printf("%d ",v.st_nlink);
printf("V14BE9U2 V14BE9U2 ");
printf("%d ",(int)v.st_size);
printf("%s ",p->d_name);
printf("%s ",ctime(&v.st_mtime));
}
}
}
else if(argv[1][1] == 'l')
{
while(p = readdir(dp))
{
if(p->d_name[0] != '.')
{
stat(p->d_name,&v);
printf("%c",v.st_mode&S_IRUSR?'r':'-');
printf("%c",v.st_mode&S_IWUSR?'w':'-');
printf("%c",v.st_mode&S_IXUSR?'x':'-');
printf("%c",v.st_mode&S_IRGRP?'r':'-');
printf("%c",v.st_mode&S_IWGRP?'w':'-');
printf("%c",v.st_mode&S_IXGRP?'x':'-');
printf("%c",v.st_mode&S_IROTH?'r':'-');
printf("%c",v.st_mode&S_IWOTH?'w':'-');
printf("%c ",v.st_mode&S_IXOTH?'x':'-');
printf("%d ",v.st_nlink);
printf("V14BE9U2 V14BE9U2 ");
printf("%d ",(int)v.st_size);
printf("%s ",p->d_name);
printf("%s",ctime(&v.st_mtime));
}
}
}
}
}
else if(argc == 3)
{
if(!strcmp(argv[1],"-l"))
{
dp = opendir(argv[2]);
if((int)dp == 0)
{
perror("opendir");
return;
}
while(p = readdir(dp))
{
if(p->d_name[0] != '.')
{

stat(p->d_name,&v);
printf("%c",v.st_mode&S_IRUSR?'r':'-');
printf("%c",v.st_mode&S_IWUSR?'w':'-');
printf("%c",v.st_mode&S_IXUSR?'x':'-');
printf("%c",v.st_mode&S_IRGRP?'r':'-');
printf("%c",v.st_mode&S_IWGRP?'w':'-');
printf("%c",v.st_mode&S_IXGRP?'x':'-');
printf("%c",v.st_mode&S_IROTH?'r':'-');
printf("%c",v.st_mode&S_IWOTH?'w':'-');
printf("%c ",v.st_mode&S_IXOTH?'x':'-');
printf("%d ",v.st_nlink);
printf("V14BE9U2 V14BE9U2 ");
printf("%d ",(int)v.st_size);
printf("%s ",p->d_name);
printf("%s",ctime(&v.st_mtime));
}
}
}
}
}

2 search
#include"header.h"
void search(char *,char *);
main(int argc,char **argv)
{
struct dirent *p;
struct stat v;
DIR *dp;
char *ch,path[100];
int c;
if(argc != 3)
{
printf("USAGE: ./search path filename\n");
return;
}
dp = opendir(argv[1]);
if(dp == 0)
{
perror("opendir");
return;
}
while(p = readdir(dp))
{
stat(p->d_name,&v);
if(S_ISDIR(v.st_mode))
{
printf("%s ",p->d_name);
if(!strcmp(p->d_name,argv[2]))
{
}
/*
//

//
//

strcpy(path,argv[1]);
strcat(path,p->d_name);
printf(".... %s \n",path);
search(path,file);*/
}
else if(S_ISREG(v.st_mode))
{
if(!strcmp(p->d_name,argv[2]))
printf("%s ",p->d_name);
}
}
printf("\n");

}
void search(char *path,char *file)
{
char *path2;
}

atoA
#include"header.h"
main()
{
int p[2],q;
int i;
q = pipe(p);
if(q<0)
{
perror("pipe");
return;
}
if(fork())
{
//parent
char a[20];
printf("Enter the data:\n");
scanf("%s",a);
write(p[1],a,strlen(a)+1);
sleep(1);
read(p[0],a,sizeof(a));
printf("In parent data = %s\n",a);
}
else
{
//child
char b[20];
printf("In child before read\n");
read(p[0],b,sizeof(b));
printf("In child data = %s\n",b);
for(i=0;b[i];i++)
{
if(b[i]>=97 && b[i]<=122)
{
b[i]=b[i]-32;
}
}
write(p[1],b,strlen(b)+1);
}
}

type of given file


/*WAP to find out type of a given file.*/
#include"header.h"
main(int argc,char **argv)
{
struct stat v,v1;
if(argc != 2)
{
printf("USAGE: ./a.out fname\n");
return;
}
if(stat(argv[1],&v) || lstat(argv[1],&v1))
{
perror("stat");
return;
}
/*

if(v.st_mode&1<<15)
{
if(v1.st_mode&1<<13)
{
printf("Link file\n");
}
else
{
printf("Regular file\n");
}
}
else if(v.st_mode&1<<14)
{
printf("Directory\n");
}*/
if(S_ISREG(v.st_mode))
{
printf("Regular file\n");
}
else if(S_ISDIR(v.st_mode))
{
printf("Directory file\n");
}

line_check
/*WAP to check whether the link is soft or hard link.*/
#include"header.h"
main(int argc,char **argv)
{
struct stat v1,v2,v3,v4;
if(argc != 3)
{
printf("USAGE: ./a.out fname1 fname2\n");
return;
}
if(stat(argv[1],&v1)<0 || stat(argv[2],&v2)<0)
{
perror("stat");
return;
}
lstat(argv[1],&v2);
printf("stat = %d\t lstat = %d\n",(int)v1.st_ino,(int)v2.st_ino);
lstat(argv[2],&v4);
stat(argv[2],&v3);
printf("stat = %d\t lstat = %d\n",(int)v3.st_ino,(int)v4.st_ino);
if(v1.st_ino == v3.st_ino && v2.st_ino == v4.st_ino)
{
printf("LINK TYPE: Hardlink\n");
}
else if(v1.st_ino == v3.st_ino && v2.st_ino != v4.st_ino)
{
printf("LINK TYPE: Softlink\n");
}
else
{
printf("NO LINK\n");
}
}

permission
#include"header.h"
main(int argc,char **argv)
{
struct stat v;
if(argc != 2)
{
printf("USAGE: ./a.out fname\n");
return;
}
if(lstat(argv[1],&v)<0)
{
perror("stat");
return;
}
printf("permission : %o\n",v.st_mode&0777);
}

pipe2
#include"header.h"
main()
{
char ch = 'a';
int p[2],c=0;
pipe2(p,O_NONBLOCK);
while(write(p[1],&ch,1)!= -1)
{
c++;
}
printf("c = %d\n",c);
}

print_dir
#include"header.h"
main(int argc, char **argv)
{
DIR *dp;
struct dirent *p;
struct stat v;
char *ch;
int c;
if(argc != 2)
{
printf("USAGE: ./a.out PATH\n");
return;
}
dp = opendir(argv[1]);
if((int)dp==0)
{
perror("opendir");
return;
}
while(p=readdir(dp))
{
if(p->d_name[0]!='.')
{
printf("%s ",p->d_name);
c = sizeof(argv[1])+sizeof(p->d_name);
ch = malloc(c);
strcpy(ch,argv[1]);

strcat(ch,p->d_name);
stat(ch,&v);
printf("%d\n",(int)v.st_ino);
}
}
printf("\n");
}

Você também pode gostar