Archive for Educational

System Software Lab Programs ( Lex and Yaac Programs )2a.l

// June 9th, 2009 // Comments // Educational, Engineering, Unix and System Software Lab

Subject : System Software Laboratory

Branch : Information Science & Engineering

Semester : 6

University : VTU

………………………………………………………………………………………………………………………….
PART – A

………………………………………………………………………………………………………………………….

2) a. Program to recognize a valid arithmetic expression and to recognize
the identifiers and operators present. Print them separately.

%{
#include<stdio.h>
#include<string.h>
#define max 20
int flag,i,j,k,top,b;
char stack[max],ident[max],oper[max],brac[max];
%}

%%
[a-zA-Z0-9] {j++;strcat(ident,yytext);}
[a-zA-Z0-9]+ {flag=1;}
“+” {oper[k++]=’+';}
“-” {oper[k++]=’-';}
“*” {oper[k++]=’*';}
“/” {oper[k++]=’*';}
“$” {oper[k++]=’$';}
“^” {oper[k++]=’^';}
“%” {oper[k++]=’%';}
“(” { stack[++top]=’(‘;brac[b++]=’(‘;}
“)” { if (stack[top]==’(‘ && top!=-1) top–;flag=0;brac[b++]=’)';}
%%

int  main()
{
int i=j=k=b=flag=0;
top=-1;

printf(“nEnter the Expression : “);
yylex();
printf(“nThe identifiers are :  “);

for(i=0;i<j;i++)
printf(“t%c”,ident[i]);

printf(“nNo.of identifiers are : %d”,j);
printf(“nThe operators are :n”);

for(i=0;i<k;i++)
printf(“t%c”,oper[i]);

printf(“nNo. of operators are :%d”,k);
if(flag==0 && top==-1 && j==k+1)
printf(“nValid expression”);
else
printf(“nInvalid expression”);

return 0;
}

System Software Lab Programs ( Lex and Yaac Programs )1b

// June 8th, 2009 // Comments // Educational, Engineering, Unix and System Software Lab

Subject : System Software Laboratory

Branch : Information Science & Engineering

Semester : 6

University : VTU

………………………………………………………………………………………………………………………….
PART – A

………………………………………………………………………………………………………………………….

1b. Program to count the numbers of comment lines in a given C program. Also eliminate them and copy the resulting program into separate file.

%{
int c=0,state=1;
%}

%%
“/*” { state=0;}
“*/” { c++; if (!state) state=1;}
{ if (state==1)
fprintf(yyout,”%s”,yytext);
}
%%

FILE * fp;
main(int argc,char ** argv)
{
if(argc<=1)
{
printf(“nNo file”);
exit(1);
}

fp=fopen(argv[1],”w”);

if(!fp)
{
printf(“nNo output file”);
exit(1);
}

yyout=fp;

fp=fopen(argv[1],”r”);

if(!fp)
{
printf(“nNo inpput file”);
exit(1);
}

yyin=fp;
yylex();
printf(“nNumber of comment lines : %d”,c);
}

yywrap()
{
if(state==0)
{
printf(“nUnterminated commennt”);
return 1;
}
}

System Software Lab Programs ( UNIX Programs ) – 7b

// June 7th, 2009 // Comments // Educational, Engineering, Unix and System Software Lab

Subject : System Software Laboratory

Branch : Information Science & Engineering

Semester : 6

University : VTU

[Click Here to get other programs by email ]

………………………………………………………………………………………………………………………….
PART -B

………………………………………………………………………………………………………………………….

7b) C program to prompt the user for the name of an environment variable and print its value if it is defined and a suitable message otherwise; and to repeat the process if user wants it.

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>

int main()
{
char env[10],*p;
int ch=1;

while(ch!=0)
{
printf(“n Enter name of evironment variable :”);
scanf(“%s”,env);

p=getenv(env);
printf(“%s”,p);
printf(“n Want to change ? 1 for yes & 0 for no :”);
scanf(“%d”,&ch);
}
return 0;
}

[Click Here to get other programs by email ]

System Software Lab Programs ( UNIX Programs ) – 7a

// June 7th, 2009 // Comments // Educational, Engineering, Unix and System Software Lab

Subject : System Software Laboratory

Branch : Information Science & Engineering

Semester : 6

University : VTU

[Click Here to get other programs by email ]

………………………………………………………………………………………………………………………….
PART -B

………………………………………………………………………………………………………………………….

7. a) Shell script to implement terminal locking. It should prompt the user for a password. After accepting the password entered by the user, it must prompt again for password confirmation (to retype the password). If a match occurs, it must lock the terminal and promp for the password. If the proper password is entered, the terminal must be unlocked. Note the script must be written to disregard BREAK, Control-D etc. No time limit need be implemented for the lock duration.

stty -echo
echo “Enter the password: ”
read pw
echo “Confirm password: ”
read cpw
if["$pw" = "$cpw"]
then
trap “.” 1 2 15 18 30
echo “Keyboard locked”
fi
echo “Enter password to resume
read npw
while["$pw" = "$npw"]
do
echo “Enter password”
read npw
done
echo “Terminal unlock”
fi
stty sane

[Click Here to get other programs by email ]

System Software Lab Programs ( UNIX Programs ) – 6b

// June 6th, 2009 // Comments // Educational, Engineering, Unix and System Software Lab

Subject : System Software Laboratory

Branch : Information Science & Engineering

Semester : 6

University : VTU

[Click Here to get other programs by email ]

………………………………………………………………………………………………………………………….
PART -B

………………………………………………………………………………………………………………………….

6b) C program that accepts a valid directory names as a command line argument and lists all the files in the given directory as well as all  the subsequent subdirectories. (The solution can be recursive or nonrecursive).

#include<stdio.h>
#include<ftw.h>
int recursive(const char *dir,const struct stat *name,int type)
{
if(type==FTW_D)
printf(“n Directory->%s”,dir);
else if(type==FTW_F)
printf(“nFile->%s”,dir);
return 0;
}

int main(int argc,char *argv[])
{
int depth=5;
ftw(argv[1],recursive,depth);
printf(“n”);
return 0;
}

[Click Here to get other programs by email ]

System Software Lab Programs ( UNIX Programs ) – 6a

// June 6th, 2009 // Comments // Educational, Engineering, Unix and System Software Lab

Subject : System Software Laboratory

Branch : Information Science & Engineering

Semester : 6

University : VTU

[Click Here to get other programs by email ]

………………………………………………………………………………………………………………………….
PART -B

………………………………………………………………………………………………………………………….

6. a) Shell script that accepts valid log-in names as arguments and prints their corresponding home directories. If no arguments are specified, print a suitable error message.

if [ $# -eq 0 ]
then
echo -e “No arguments”
else
for a in $*
do
grep $a /etc/passwd>list
if [ $? -eq 0 ]
then
echo “Home dir of $a is :”
cut -c 20-46 list
else
echo “Improper login name $a”
fi
done
fi

[Click Here to get other programs by email ]

System Software Lab Programs ( UNIX Programs ) – 5b

// June 5th, 2009 // Comments // Educational, Engineering, Unix and System Software Lab

Subject : System Software Laboratory

Branch : Information Science & Engineering

Semester : 6

University : VTU

[Click Here to get other programs by email ]

………………………………………………………………………………………………………………………….
PART -B

………………………………………………………………………………………………………………………….

5b) C program that accepts one command-line argument, executes the arguments as a shell command, determines the time taken by it and prints the time values, Use the “times”, function and the “tms” structure. The code need not include error checking.

#include<stdio.h>
#include<sys/times.h>
#include<unistd.h>

int main(int argc,char *argv[10])
{
char cmd[10];
struct tms obj1,obj2;
clock_t st,end;
float time;
st=times(&obj1);
system(“clear”);
printf(“%Sn”,argv[1]);
end=times(&obj2);
printf(“Real time taken = %fn”,st);
printf(“User time taken = %fn”,end);
time=(end-st)/3600;
printf(“nSystem time taken : %f”,time);
return 0;
}

[Click Here to get other programs by email ]

System Software Lab Programs ( UNIX Programs ) – 5a

// June 5th, 2009 // Comments // Educational, Unix and System Software Lab

Subject : System Software Laboratory

Branch : Information Science & Engineering

Semester : 6

University : VTU

[Click Here to get other programs by email ]

………………………………………………………………………………………………………………………….
PART -B

………………………………………………………………………………………………………………………….

5. a) Shell script that accepts path names and creates all the components in that pathnames as directories. For example, if the script name is
mpe, then the command mpe a/b/c/d should create directories a, a/b, a/b/c, and a/b/c/d.

v1=`echo $* |tr “/” ” “`
echo $v1
for i in $v1
do
mkdir $i
cd $i
done

[Click Here to get other programs by email ]

System Software Lab Programs ( UNIX Programs ) – 4b

// June 4th, 2009 // Comments // Educational, Engineering, Unix and System Software Lab

Subject : System Software Laboratory

Branch : Information Science & Engineering

Semester : 6

University : VTU

[Click Here to get other programs by email ]

………………………………………………………………………………………………………………………….
PART -B

………………………………………………………………………………………………………………………….

4b) C program to do the following: Using fork( ) create a child process. The child process prints its own process-id and id of its parent and then exits. The parent process waits for its child to finish (by executing the wait( )) and prints its own process-id and the id of its child process and then exits.

#include<stdio.h>
#include<sys/types.h>

int main()
{
int status,ppid,mpid,pid;
pid=fork();

if(pid<0)
{
printf(“Error in forking a child..!”);
exit(0);
}

if(pid==0)
{
ppid=getppid();

printf(“nChild is executing and it’s Parent ID is  : %d”,ppid);
mpid=getpid();
printf(“nChild ID is  : %d”,mpid);
kill();
exit(0);
}
//pid=wait(pid,&status,0);
mpid=getpid();

printf(“nParent ID is : %d  and it’s child ID is : %dn”,mpid,pid);
}

[Click Here to get other programs by email ]

System Software Lab Programs ( UNIX Programs ) – 4a

// June 3rd, 2009 // Comments // Educational, Engineering, Unix and System Software Lab

Subject : System Software Laboratory

Branch : Information Science & Engineering

Semester : 6

University : VTU

[Click Here to get other programs by email ]

………………………………………………………………………………………………………………………….
PART -B

………………………………………………………………………………………………………………………….

4. a) Shell script that accepts file names specified as arguments and creates a shell script that contains this file as well as the code to
recreate these files. Thus if the script generated by your script is executed, it would recreate the original files(This is same as the “bundle” script described by Brain W. Kernighan and Rob Pike in “ The Unix Programming Environment”, Prentice – Hall India).

#!/bin/bash

echo “# to unbundle,bash this file n:”
for i
do
echo “echo $ i1>i2″
echo “cat >$i <<’END of $i’”
cat $i
echo “END OF $i”
echo -e
done

[Click Here to get other programs by email ]