Social Icons

Wednesday, 1 May 2013

Machine Information Using Library Function getuid,getgid,gethostname


#include<stdio.h>
#include<sys/types.h>
#include<sys/utsname.h>
#include<unistd.h>
#include<stdlib.h>
#include<pwd.h>
int main()
{
uid_t uid;
gid_t gid;
char s[100];
struct passwd *pw;
struct utsname uts;

uid=getuid();
gid=getgid();

printf("\n userid : %d  , groupid : %d ",uid,gid);
printf("\n login name : %s ",getlogin());

  pw=getpwuid(uid);
printf("\n uid : %d , gid :  %d , name : %s",pw->pw_uid,pw->pw_gid,pw->pw_name);

gethostname(s,255);
printf("\n host name : %s",s);

uname(&uts);

printf("\n machine name :  %s , \n  sysname :  %s ,\n  version : %s ,\n Nodename : %s ,\n  ",uts.machine,uts.sysname,uts.version,uts.nodename);
return 0;
}

Sum And Product Using Getopt Function


#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
int main(int argc , char *argv[])
{

int n,i,sum=0, pr=1;
int a,b;
while((n=getopt(argc,argv,":if:::l::r"))!=-1)
{
 switch(n)
 {
 case 'i' :
 case 'r' : printf("\n option %c",n);
              break;
 case 'f'  :// printf("\n filename %s",optarg);
                a=optind;
               for(;optind<a+3;optind++)
                 {
                    i=atoi(argv[optind]);
                    sum=sum+i;
                  }
                    printf(" \n sum of the two no :  %d ",sum);
                 break;
 case 'l'  :     b=optind;
                 for(;optind<b+2;optind++)
                 {
                    i=atoi(argv[optind]);
                    pr=pr*i;
                  }
                 printf(" \n Product  of the two no :  %d ",pr);
                 break;

 case '?'  : printf("\n unknown option %c",optopt);
               break;
 case ':'  : printf("\n argument needed : ");
              break;
 }

 }

           for(;optind<argc;optind++)
    {
    printf(" \n arguments : %s ",argv[optind]);
    }


return 0;
}


             
             

Copying Files content Using Read & write system call


#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>

int main()
{
int nread;
char buf[150];
int  m,n,o;
m=open("kaha", O_RDONLY,S_IRUSR|S_IWUSR);
if(m==-1)
{
printf("\n Error Occured : ");
}

n=open("tu.txt",O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR);
if(n==-1)
                {

printf("\n Error Occured : ");

}

lseek(m,2,SEEK_SET);
lseek(n,5,SEEK_SET);

while(nread=read(m,buf,sizeof(buf)))
{
  write(n,buf,10);
}



return 0;
}

Reading Files Of a Directory


#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<dirent.h>
struct dirent *p;
int main()
{
DIR *q;
int  m;
q=opendir("1");
if(q==NULL)
{
printf("\n Error Occured : ");

}

else
{
printf("\n  File Name  |||||||     Inode No.\n");
printf("......................................");
while(p=readdir(q))
{

printf("\n %s",p->d_name);
m=p->d_ino;
printf("\t\t\t %d",m);
}
}
closedir(q);
return 0;
}


 

Sample Text

Sample text

 
Just Programming Cse DriveReputation Management