Saturday, 17 November 2018

I felt so happy as I have listened the knowledgeable  lecture delivered by Our Encyclopedia in CSE Dr.N.B.Venkateswarulu sir on 16-11-18 at our college.


I learnt the following topics from his great knowledge sharing lecture.

1.Brainfuck language-Turing compatibility language
2.About GIT-HUB account 
3.Go language-Google
4.PCA
5.T-SNE algorithm-Feature selection
6.showcodelens need-gvpceew.in/nbv/(What happened behind the program execution? Visualization tool)
7.pythontutor.com-good platform for learning c,c++,python,Java languages(Visualization tool)
8.typescript(standalone)-usage over the Java script(only at web browsers)
9.Knowledge flow creation in  WEKA 3.9 
10.Difficulty  in multi dimensional arrays

Definitely I will share all these things with my students.


Thank you so much sir.We are fortunate to have you here.

Wednesday, 11 January 2017

FOSS Exp:10

[student@localhost ~]$ vi 10exp.c

/*This program takes one or more file/directory names as command  
line input and reports the following information on the file:          
(a) File type (b) Number of links (c) Time of last access          
(d) Read, write, and execute permissions                              
Syntax: ./10filestat <file|dir> [<file|dir>]                          
Arguments: Expects atleast one file name or directory name as argument,  
and accepts more than 1 argument also as file names or directory names                                                        
Returns: File statistics which include the type of file, number of links  
to the file, time the file was last accessed, and read, write, and execute
permissions of the file (or directory) */
#include<unistd.h> /* F_OK, STDIN_FILENO, STDERR_FILENO, etc. */
#include<sys/stat.h> /* struct stat */
#include<sys/types.h> /* S_IFMT */
#include<stdio.h> /* fprintf, printf, sprintf, etc. */
#include<stdlib.h> /* exit, etc. */
#include<time.h> /* ctime */
/* Define all the error messages */
char *error_msg[]={"\nUsage: ./10filestat <file|dir> [<file|dir>]\n\n","\nFile does not exist !!\n\n","\nError doing 'stat' on file\n\n"};
/* Declare the prototype for function 'print_error' */
void print_error(int msg_num,int exit_code,int exit_flag);
int main(int argc,char *argv[])
{
  int i;/* A counter */
  mode_t file_perm; /* File permissions */
  struct stat file_details;/* Detailed file info */
  char success_msg[]="\nCommand executed successfully\n\n";
/* Check if correct number of arguments are supplied */
   if(argc<2)
      print_error(0,2,1);
  /* Loop through all the filenames */
   for(i=1;i<argc;i++)
    {
       /* Display the filename */
       printf("\n%s\n%s\n%s\n","----------------",argv[i],"----------------");
   
        /* Check if the file exists */
       if(access(argv[i],F_OK)==-1)
         {
            print_error(1,3,0);
            continue;
          }
     
      /*Retrieve the file details*/
       if(lstat(argv[i],&file_details)<0)
         {
           print_error(2,4,0);
           continue;
         }

     /* Get the file type */
      if(S_ISREG(file_details.st_mode))
printf("File type : Regular\n");
      else if(S_ISDIR(file_details.st_mode))
    printf("File type : Directory\n");
 else if(S_ISLNK(file_details.st_mode))
      printf("File type : Symbolic link\n");
     else
       printf("File type:Other");
/* Get the number of links */
printf("Number of links : %d\n",(int)file_details.st_nlink);
/* Get the time of last access of the file */
printf("Time of last access : %s",ctime(&file_details.st_atime));
/* Get the file permissions */
printf("File Permissions:\n");
file_perm=file_details.st_mode&~S_IFMT;
printf("\tUser : ");
if(file_perm&S_IRUSR)
  printf("Readable, ");
else
    printf("Not readable,");
if(file_perm&S_IWUSR)
  printf("Writable, ");
else
  printf("Not writable, ");
if(file_perm&S_IXUSR)
  printf("Executable\n");
else
   printf("Not executable\n");

       printf("\tGroup : ");
if(file_perm&S_IRGRP)
  printf("Readable, ");
else
  printf("Not readable, ");
if(file_perm&S_IWGRP)
 printf("Writable, ");
else
 printf("Not writable, ");
if(file_perm&S_IXGRP)
 printf("Executable\n");
else
 printf("Not executable\n");

printf("\tOthers : ");
if(file_perm&S_IROTH)
  printf("Readable, ");
else
  printf("Not readable, ");
if(file_perm&S_IWOTH)
  printf("Writable, ");
else
  printf("Not writable, ");
if(file_perm&S_IXOTH)
  printf("Executable\n");
else
          printf("Not executable\n");
       }
printf("%s",success_msg);
   return(1);
}
void print_error(int error_index,int exit_code,int exit_flag)
{
   fprintf(stderr,"%s\n",error_msg[error_index]);
     if(exit_flag)
        exit(exit_code);
}

[student@localhost ~]$ cc 10exp.c
[student@localhost ~]$ ./a.out
Usage: ./10filestat <file|dir> [<file|dir>]

[student@localhost ~]$ cc -o 10filestat 10exp.c
[student@localhost ~]$ ./10filestat Documents

----------------
Documents
----------------
File type : Directory
Number of links : 3
Time of last access : Thu Jan  5 17:33:52 2017
File Permissions:
        User : Readable, Writable, Executable
        Group : Readable, Not writable, Executable
        Others : Readable, Not writable, Executable

Command executed successfully

[student@localhost ~]$ ./10filestat cse1

----------------
cse1
----------------
File type : Regular
Number of links : 1
Time of last access : Thu Jan  5 18:21:43 2017
File Permissions:
        User : Readable, Writable, Not executable
        Group : Readable, Not writable, Not executable
        Others : Readable, Not writable, Not executable

Command executed successfully
[student@localhost ~]$ chmod u=rwx,g=rx,o=r cse1
[student@localhost ~]$ ./10filestat cse1

----------------
cse1
----------------
File type : Regular
Number of links : 1
Time of last access : Thu Jan  5 18:33:41 2017
File Permissions:
  User : Readable, Writable, Executable
        Group : Readable, Not writable, Executable
        Others : Readable, Not writable, Not executable

Command executed successfully 

Thursday, 29 December 2016

FOSS Exp:11,12,13.9.13.10



11. Write C programs that simulate the following unix commands:
a)mv b)cp (Use system calls)
A) Program to Implement Linux “mv” command

Vi 11a.c

#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<stdio.h>
#include<sys/stat.h>
int main(int argc,char **argv)
{
 if(argc>3 || argc<3)
  printf("please provide 2 arguments.\n");
 else
 {
 int r,fd1,fd2;
    if(access(argv[1],F_OK)<0)
       {
          printf("%s not found.\n",argv[1]);
       }
        fd1=open(argv[1],O_RDONLY);
        fd2=creat(argv[2],S_IWUSR);
        r=rename(argv[1],argv[2]);
        if(r==0)
         {
          printf("%s is moved or renamed to %s successfully. \n",argv[1],argv[2]);
         }
        unlink(argv[1]);
 }
        return(0);
}

[student@localhost ~]$ vi 11a.c
[student@localhost ~]$ cc 11a.c
[student@localhost ~]$ cat >fi
C PROGRAM to demonstrate mv command
[student@localhost ~]$ cat fi
C PROGRAM to demonstrate mv command

[student@localhost ~]$ ./a.out
please provide 2 arguments.

[student@localhost ~]$ ./a.out of nf
of not found.

[student@localhost ~]$ ./a.out fi nf
fi is moved or renamed to nf successfully.

[student@localhost ~]$ cat fi
cat: fi: No such file or directory

[student@localhost ~]$ cat nf
C PROGRAM to demonstrate mv command

or

another solution


#include<fcntl.h>
#include<stdio.h>
#include<sys/stat.h>
#include<sys/types.h>
int main(int argc,char **argv)
{
        int fd1,fd2,i;
        char *file1,*file2,buf[2];
         file1=argv[1];
         file2=argv[2];
         printf("file1=%s file2=%s",file1,file2);
        fd1=open(file1,O_RDONLY,0777);
        fd2=creat(file2,0777);
        while((i=read(fd1,buf,1)>0))
        {
                write(fd2,buf,1);
        }
    remove(file1);
  close(fd1);
  close(fd2);
}


b) Program to Implement Linux “CP” copy command
This C program is a simulation of the Linux “cp” copy command. The copy command can be used to make a copy of your files and directories, but here in this implementation only the basic functionality i.e., copy the content from one file to another is handled.
The program for “cp” command takes in two arguments namely the source file and the destination file.
./cpcmd sourcefile destinationfile
1) First check if both source & the destination files are received from the command line argument and exit if argc counter is not equal to 3.
There is also a check to handle “–help” option to print the usage of cpcmd.
2) Open the source file with read only flag set.
3) Open the destination file with the respective flags & modes.
O_WRONLY -> Open the file in write only mode
O_TRUNC -> Truncates the contents of the existing file
O_CREAT -> Creates a new file if it doesn’t exist
S_IXUSR are file permissions for the current user (‘X’ can be R for read & W for write)
S_IXGRP are file permissions for the groups (‘X’ can be R for read & W for write)
S_IXOTH are file permissions for the groups (‘X’ can be R for read & W for write)
4) Start data transfer from source file to destination file till it reaches EOF (nbread == 0).
guest-nUtd1q@hadoop54-ThinkCentre-E73:~$ gedit 11b.c

Source Code:
#include<stdio.h>

#include<stdlib.h>

#include<fcntl.h>

#include<errno.h>

#define BUFF_SIZE 1024

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

{

int srcFD,destFD,nbread,nbwrite;

char *buff[BUFF_SIZE];

/*Check if both src & dest files are received or --help is received to get usage*/

if(argc!=3||argv[1]=="--help")

{

printf("\nUsage: cpcmd source_file destination_file\n");

exit(EXIT_FAILURE);

}

/*Open source file*/

srcFD=open(argv[1],O_RDONLY);

if(srcFD==-1)

{

printf("\nError opening file %s errno = %d\n",argv[1],errno);

exit(EXIT_FAILURE);

}

/*Open destination file with respective flags & modes
 O_CREAT & O_TRUNC is to truncate existing file or create a new file
 S_IXXXX are file permissions for the user,groups & others*/

destFD=open(argv[2],O_WRONLY|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);

if(destFD==-1)

{

printf("\nError opening file %s errno = %d\n",argv[2],errno);

exit(EXIT_FAILURE);

}

/*Start data transfer from src file to dest file till it reaches EOF*/

while((nbread=read(srcFD,buff,BUFF_SIZE))>0)

{

if(write(destFD,buff,nbread)!=nbread)

printf("\nError in writing data to %s\n",argv[2]);

}

if(nbread==-1)

printf("\nError in reading data from %s\n",argv[1]);

if(close(srcFD)==-1)

printf("\nError in closing file %s\n",argv[1]);

if(close(destFD)==-1)

printf("\nError in closing file %s\n",argv[2]);

exit(EXIT_SUCCESS);

}

guest-nUtd1q@hadoop54-ThinkCentre-E73:~$ cat >f1

this cp command simulation

It is 11b exercise


guest-nUtd1q@hadoop54-ThinkCentre-E73:~$ cat f1

this cp command simulation

It is 11b exercise


guest-nUtd1q@hadoop54-ThinkCentre-E73:~$ cp f1 f2


guest-nUtd1q@hadoop54-ThinkCentre-E73:~$ cat f2

this cp command simulation

It is 11b exercise


guest-nUtd1q@hadoop54-ThinkCentre-E73:~$ cat f1

this cp command simulation

It is 11b exercise


guest-nUtd1q@hadoop54-ThinkCentre-E73:~$ gcc 11b.c

guest-nUtd1q@hadoop54-ThinkCentre-E73:~$ ./a.out



Usage: cpcmd source_file destination_file


guest-nUtd1q@hadoop54-ThinkCentre-E73:~$ ./a.out f2 f3


guest-nUtd1q@hadoop54-ThinkCentre-E73:~$ cat f3

this cp command simulation

It is 11b exercise


guest-nUtd1q@hadoop54-ThinkCentre-E73:~$ cat f2

this cp command simulation

It is 11b exercise


guest-nUtd1q@hadoop54-ThinkCentre-E73:~$ gcc 11b.c -o 11b


guest-nUtd1q@hadoop54-ThinkCentre-E73:~$ ./11b f2 f


guest-nUtd1q@hadoop54-ThinkCentre-E73:~$ cat f

this cp command simulation

It is 11b exercise


guest-nUtd1q@hadoop54-ThinkCentre-E73:~$ clear


12. Write a C program that simulates ls Command
guest-nUtd1q@hadoop54-ThinkCentre-E73:~$ gedit 12exp.c



#include<fcntl.h>/* System calls: open, read, etc. O_RDONLY, etc */

#include<unistd.h>/* STDIN_FILENO, STDERR_FILENO, etc. */

#include<dirent.h>/* DIR, dirent, etc. */

#include<string.h>/* String functions (strcat,strlen) */

#include<stdio.h>

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

{

DIR *dir_to_read;/* Handle to directory to be read */

struct dirent *dir_info;/* Details of directory entries */

char *cur_work_dir=NULL;/* String to hold current working directory name */

char success_msg[] = "\nCommand executed successfully\n\n";

/* Get the current working directory */

if((cur_work_dir=((char*)get_current_dir_name()))==NULL)

{

printf("\nCould not retrieve the current working directory\n");

}

/* Open the current working directory */

if((dir_to_read=opendir(cur_work_dir))==NULL)

{

printf("\nCould not open the current working directory\n\n");

}

/* Loop through current working directory and display each file name

till all the entries are exhausted */

while((dir_info=readdir(dir_to_read))!=NULL)

{

write(STDOUT_FILENO,dir_info->d_name,strlen(dir_info->d_name));

write(STDOUT_FILENO,"\n",strlen("\n"));

}

write(STDOUT_FILENO,success_msg,strlen(success_msg));

return 1;

}
output:
guest-nUtd1q@hadoop54-ThinkCentre-E73:~$ gcc 12exp.c

guest-nUtd1q@hadoop54-ThinkCentre-E73:~$ ./a.out

.

..

a.out

12exp.c

12exp.c~

11b.c

11b.c~

f

11b

f3

f2

f1

.mozilla

.ICEauthority

.local

.gconf

Videos

Pictures

Music

Documents

Public

Templates

Downloads

Desktop

.xsession-errors

.Xauthority

.kde

.cache

.config

.bashrc

.bash_logout

.profile

examples.desktop



Command executed successfully



guest-nUtd1q@hadoop54-ThinkCentre-E73:~$

13.9  Write a shell script to accept student number, name, marks in 5 subjects. Find total, average andgrade. Display the result of student and store in a file called stu.dat Rules: avg>=80 then grade A Avg<80&&Avg>=70 then grade B Avg<70&&Avg>=60 then grade C Avg<60&&Avg>=50 then grade D Avg<50&&Avg>=40 then grade E Else grade F

guest-nUtd1q@hadoop54-ThinkCentre-E73:~$ gedit 13_9exp.sh


echo "Do you want to enter student data:"

read a

while [ $a = y -o $a = Y ]

do

echo "enter number"

read sno

echo "enter name"

read name

echo "enter 5 subject marks"

read m1

read m2

read m3

read m4

read m5

total=`expr $m1 + $m2 + $m3 + $m4 + $m5`

echo total is: $total

avg=`expr $total / 5`

echo average is: $avg

if [ $avg -ge 80 ]

then

grade="A"

elif [ $avg -lt 80 -a $avg -ge 70 ]

then

grade="B"

elif [ $avg -lt 70 -a $avg -ge 60 ]

then

grade="C"

elif [ $avg -lt 60 -a $avg -ge 50 ]

then

grade="D"

elif [ $avg -lt 50 -a $avg -ge 40 ]

then

grade="E"

else

grade="F"

fi

echo "enter filename"

read f

echo "$sno       $name  $total   $avg     $grade" >> $f

echo "Do you want to continue"

read a

done    

First create the file with name stu.dat in the following format.

cat > stu.dat

Student Information
 is:
            SNO
            NAME            
            TOTAL
            AVERAGE
            GRADE
Press ctrl –d



guest-nUtd1q@hadoop54-ThinkCentre-E73:~$ cat >stu.dat

Student Information is:

SNO     NAME    TOTAL AVERAGE   GRADE

guest-nUtd1q@hadoop54-ThinkCentre-E73:~$ cat stu.dat

Student Information is:

SNO     NAME TOTAL AVERAGE     GRADE

guest-nUtd1q@hadoop54-ThinkCentre-E73:~$ sh 13_9exp.sh

Do you want to enter student data:

y

enter number

12

enter name

tanu

enter 5 subject marks

89

79

78

89

90

total is: 425

average is: 85

enter filename

stu.dat

Do you want to continue

y

enter number

23

enter name

rani

enter 5 subject marks

45

34

23

23

21

total is: 146

average is: 29

enter filename

stu.dat

Do you want to continue

n

guest-nUtd1q@hadoop54-ThinkCentre-E73:~$ cat stu.dat

Student Information is:

SNO     NAME TOTAL AVERAGE     GRADE

12        tanu     425      85        A

23        rani      146      29        F

guest-nUtd1q@hadoop54-ThinkCentre-E73:~$




1.      13.10 Write a shell script to accept empno,empname, and basic. Find DA, HRA, TA, PF using following rules.Display empno, empname, basic, DA,HRA,PF,TA,GROSS SAL and NETSAL. Also store all details ina file called emp.dat Rules: HRA is 18% of basic if basic > 5000 otherwise 550 DA is 35% of basic PF is 13% of basic IT is 14% of basic TA is 10% of basic



guest-nUtd1q@hadoop54-ThinkCentre-E73:~$ gedit 13_10exp.sh

echo "Do you want to enter employee data"
read a
while [ $a = y -o $a = Y ]
do
echo "enter employee number:"
read sno
echo "enter employee name:"
read name
echo "enter basic salary:"
read bsal
if [ $bsal -gt 5000 ]
then
k=`expr 18 \* $bsal`
hra=`expr $k / 100`
else
hra=550
fi
k=`expr 15 \* $bsal`
da=`expr $k / 100`
k=`expr 13 \* $bsal`
pf=`expr $k / 100`
k=`expr 10 \* $bsal`
ta=`expr $k / 100`
k=`expr 14 \* $bsal`
it=`expr $k / 100`
gsal=`expr $hra + $da + $ta + $it + $bsal + $pf`
nsal=`expr $hra + $da + $ta + $bsal`

echo "enter file name:"
read f
echo "EMPLOYEE DETAILS ARE:">> $f
echo "EMPNO is: $sno" >> $f
echo "EMPLOYEE NAME IS: $name">> $f
echo "BASIC SALARY IS: $bsal">>$f
echo "GROSS SALARY IS : $gsal">> $f
echo "NETSALARY IS: $nsal">>$f
echo "Do you want to continue"
read a
done


guest-nUtd1q@hadoop54-ThinkCentre-E73:~$ cat emp.dat
Employee Information is:
Empno    EmpName   Basic     DA     HRA       PF       TA   GROSS SAL     NETSAL

guest-nUtd1q@hadoop54-ThinkCentre-E73:~$ sh 13_10exp.sh
Do you want to enter employee data
y
enter employee number:
333
enter employee name:
yogi
enter basic salary:
8000
enter file name:
emp.dat
Do you want to continue
n
guest-nUtd1q@hadoop54-ThinkCentre-E73:~$ cat emp.dat
EMPLOYEE DETAILS ARE:
EMPNO is: 333
EMPLOYEE NAME IS: yogi
BASIC SALARY IS: 8000
GROSS SALARY IS : 13600
NETSALARY IS: 11440