Showing posts with label cycle1. Show all posts
Showing posts with label cycle1. Show all posts

Tuesday, 8 November 2011

LS


#include<dirent.h>
#include<stdio.h>
main()
{
const char buffer[50];
struct dirent **collect;
int n;
printf("\n\nEnter the directory:");
scanf("%s",&buffer);
n=scandir(buffer,&collect,0,alphasort);
if(n<0)
perror("Error");
else
{
while(n--)
{
printf("\n %s",collect[n]->d_name);
free(collect[n]);
}
free(collect);
}
printf("\n\n End");
}

OUTPUT

./a.out
Enter Directory
.
tist
f1
f2
file.c
cat.c
copy.c

FILE TYPE


#include<stdio.h>
#include<sys/stat.h>
#include<sys/types.h>
main(int argc,char *argv[])
{
 int i;
 struct stat statbuf;
char *ptr;
for(i=0;i<argc;i++)
{
 printf("the file%s is",argv[i]);
if(stat(argv[i],&statbuf)<0)
{
printf("f staterror");
exit(1);
}
switch(statbuf.st_mode &S_IFMT)
{
case S_IFDIR:ptr="directory";
             break;
case S_IFCHR:ptr="character special";
             break;

case S_IFREG:ptr="regular";
                  break;
case S_IFBLK:ptr="block special";
              break;
case S_IFIFO:ptr="fifo";
            break;
default:ptr="unknown mode";
        break;
}
printf("%s \n",ptr);
}
exit(0);
}

OUTPUT
./a.out f1 file.c
file f1 is REGULAR
file file.c is REGULAR

CAT COMMAND


#include<sys/stat.h>
#include<sys/types.h>
#include<fcntl.h>
#include<unistd.h>
#include<stdio.h>
main(int argc,char *argv[])
{
char buf[2];
int f=O_RDONLY,src,dest,i=1,j;
dest=creat(argv[argc-1],S_IRWXU);
if(dest==-1)
{
printf("Destination file creation error");
exit(1);
}
for(j=1;j<(argc-1);j++)
{
src=open(argv[j],f);
if(src==-1)
{
printf("Source file open error");
exit(1);
}
while((i=read(src,buf,sizeof(buf)))>0)
write(dest,buf,sizeof(buf));
}
printf("\n\n File copying successful.....\n\n");
}


OUTPUT
./a.out f1 f2 f3
concatenated successfully

(should create files f1 and f2 with anything inside it)

copy.c

#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
main(int argc,char *argv[])
{
const char reader[500];
int d,i,f,c;
f=O_RDONLY;
d=open(argv[1],f);
if (d==-1)
{
printf("File open error");
}
else
{
i=read(d,reader,sizeof(reader));
c=creat(argv[2],S_IRWXU);
write(c,reader,i);
if (c==-1)
printf("Output file creation error");
else
printf("File %s copied sucessfully to %s",argv[1],argv[2]);
}
}

OUTPUT
./a.out f1 f2
file f1 copied successfully to f2