0% found this document useful (0 votes)
2 views7 pages

Unix Lab 2

The document outlines an experiment for simulating UNIX commands through a C program at Sandip University. It includes the aim, required software, theoretical background on various UNIX commands, and a complete C program that implements functionalities such as creating and removing directories, printing the current working directory, and displaying user information. The document concludes with references and a space for the teacher's signature.

Uploaded by

selfgamer031
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views7 pages

Unix Lab 2

The document outlines an experiment for simulating UNIX commands through a C program at Sandip University. It includes the aim, required software, theoretical background on various UNIX commands, and a complete C program that implements functionalities such as creating and removing directories, printing the current working directory, and displaying user information. The document concludes with references and a space for the teacher's signature.

Uploaded by

selfgamer031
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

SANDIP UNIVERSITY, SIJOUL

School of Computer Science & Engineering

Name of Student:

Date of Performance: Date of Completion:

EXPERIMENT NO: 02

TITLE: A PROGRAM FOR SIMULATION OF UNIX COMMANDS.


AIM: WRITE A C PROGRAM FOR CREATE & REMOVE DIRECTORY, PRINT CURRENT
WORKING DIRECTORY, WORD COUNT OF FILE, PRINT DATE, AND CURRENT USER
NAME.

REQUIRED SOFTWARE: Ubuntu 12.4 or Extended.


THEORY:
1) mkdir - make directories
mkdir [OPTION] DIRECTORY
Options:-
Create the DIRECTORY, if they do not already exist.
Mandatory arguments to long options are mandatory for short options too.
-m, mode=MODE set permission mode (as in chmod), not rwxrwxrwx - umask
-p, parents no error if existing, make parent directories as needed
-v, verbose print a message for each created directory
-help display this help and exit
-version output version information and exit

2) pwd - print working directory

This command shows you the full path to the directory you are currently in. This is very handy to
use, especially when performing some of the other commands on this page

3) rmdir - Remove an existing directory

4) wc - Print byte, word, and line counts


5) who - Displays who is logged on to the system.
Syntax
who [ OPTION ]... [ FILE ] [ am i ]
Description: The who command prints information about all users who are currently logged in.

6) uname -Print information about the current system.


Syntax uname [OPTION]...
Description: Print certain system information. If no OPTION is specified, uname assumes the -s
option.

7) date -Prints or sets the system time and date.


Syntax
date [OPTION]... [+FORMAT]
date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
Program:-

//Program for Simulation of Unix Commands


#include<fcntl.h>
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<time.h>
#include<sys/utsname.h>
#include<utmp.h>
void Date();
void Who();
void Pwd();
void Uname();
void Mkdir();
void Rmdir();
void Wc();
char fname[10];
int ch,n,fd,sp,nl,bs;
time_t t;
struct tm *tm;
char c,buf[40],dname[20],oldname[20],newname[20];
struct utsname un;
struct utmp *u;
int main(void)
{
int ch;
do
{
printf("\n ----------MENU----------");
printf("\n 1: date");
printf("\n 2: who");
printf("\n 3: pwd");
printf("\n 4: uname");
printf("\n 5: mkdir");
printf("\n 6: rmdir");
printf("\n 7: wc");
printf("\n 8: Exit");
printf("\n Enter the choice");
scanf("%d",&ch);
switch(ch)
{
case 1: // comment for date
Date(); break;
case 2: // comment for who
Who();
break;
case 3: // comment for password
Pwd();
break;
case 4: // comment for user name
Uname();
break;
case 5: // comment for make directory
Mkdir();
break;
case 6: // comment for remove directory
Rmdir();
break;
case 7: // comment for wordcount
Wc();
break;
}
}while(ch!=8);
}
void Date()
{
tm = localtime(&t);
strftime(buf,40,"%a %b %d %X %Z %Y",tm);
printf("----------------date---------------\n\n");
printf("%s\n",buf);
printf("-----------------------------------\n\n");
}
void Who()
{
struct utmp *u;
utmpname("/var/run/utmp");
setutent();
while((u =getutent())!=NULL)
{
if(u->ut_type==7)
{
printf("\n tty line:=%d",u->ut_line);
printf("\n login name:=%s",u->ut_name);
printf("\n seconds since Epoch:=%d",u->ut_time);
}
}
endutent();
}
void Pwd()
{
char buf[100];
getwd(buf);
printf("%s",buf);
}
void Uname()
{
struct utsname un;
if(uname(&un)==0)
printf("\%s\n",[Link]);
}
void Mkdir()
{
printf("\n Enter the name directory\n");
scanf("%s",fname);
mkdir(fname,0777);
printf("\n Directory created successfully\n");
}
void Rmdir()
{
printf("\n Enter the name of directory which is to be remove\n");
scanf("%s",fname);
rmdir(fname);
printf("\n Directory successfully removed\n");
}
void Wc()
{
printf("\nEnter The File Name : ");
scanf("%s",dname);
fd = open(dname,O_RDONLY);
sp=0;nl=0;bs=0;
while(read(fd,&c,1))
{
if(c==' ')
sp++;
if(c=='\n')
nl++;
bs++;
}
printf("\n%d\t%d\t%d\t%s\n",nl,sp+1,bs,dname);
}
/Output:
student08@student:~$ gcc 2aup.c
student08@student:~$ ./[Link]
----------MENU----------
1: date
2: who
3: pwd
4: uname
5: mkdir
6: rmdir
7: wc
8: Exit
Enter the choice1
----------------date---------------
Thu Jan 01 05:30:00 IST 1970
-----------------------------------
----------MENU----------
1: date
2: who
3: pwd
4: uname
5: mkdir
6: rmdir
7: wc
8: Exit
Enter the choice2
tty line:=160301552
login name:=student08
seconds since Epoch:=1439308314
tty line:=160301552
login name:=student08
seconds since Epoch:=1439309129
----------MENU----------
1: date
2: who
3: pwd
4: uname
5: mkdir
6: rmdir
7: wc
8: Exit
Enter the choice3
/home/student08
----------MENU----------
1: date
2: who
3: pwd
4: uname
5: mkdir
6: rmdir
7: wc
8: Exit
Enter the choice4
Linux
----------MENU----------
1: date
2: who
3: pwd
4: uname
5: mkdir
6: rmdir
7: wc
8: Exit
Enter the choice5
Enter the name directory
ssbt
Directory created successfully
----------MENU----------
1: date
2: who
3: pwd
4: uname
5: mkdir
6: rmdir
7: wc
8: Exit
Enter the choice6
Enter the name of directory which is to be remove
ssbt
Directory successfully removed
----------MENU----------
1: date
2: who
3: pwd
4: uname
5: mkdir
6: rmdir
7: wc
8: Exit
Enter the choice7
Enter The File Name : information
0 1 10 information
----------MENU----------
1: date
2: who
3: pwd
4: uname
5: mkdir
6: rmdir
7: wc
8: Exit
Enter the choice8
student08@student:~$

REFRENCES:
Advanced Programming in the UNIX Environment, W Richard Stevens

CONCLUSION:

NAME OF TEACHER SIGN

You might also like