Sei sulla pagina 1di 8

# HOMEWORK No.

Course :Linux Programming Code : CSE207

Date of Assignment: Date of Submission :

PART A

Answer:1:

#include <sys/types.h>    // defines special types


#include <pwd.h>     // defines the passwd structure
#include <stdio.h>    // standard I/O
int main ()
{
Int i;
    uid_t uid = getuid();
gid_t gid=getgid();
struct passwd *user_info;
Printf(“1.Search user info by UID \n 2.search user info by user
name”);
Printf(“3.display info. Of all users \n 4. Quit \n”);
Printf(“enter your choice:”);
scanf(“%d”,&i);
Switch(i)
{
Case 1://case for user info user id
    user_info = getpwuid(uid);
    printf("Hello, %s!\n", user_info->pw_gecos);
    printf("Your home directory is %s.\n", user_info->pw_dir);
    return 0;
break;

Case 2: //user info by user name


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

struct passwd *buffer;


struct utmp *naveen;
{
while((buffer=getpwent()!=NULL))
printf("%s", buffer->pw_gecos);
printf("%s", naveen->ut_user);
}

Break;
Case 3: //display info of all user
Printf(“user is: %s \n”,getlogin());
Printf(“user ids: uid=%d, gid=%d \n’,uid,gid);
Pw=getpwuid(uid);
Printf(“UID passwd:\n name=%s,uid=%d,gid=%d,home=%s,shell=
%s\n”,pw->pw_name, pw->pw_uid, pw->pw_gid, pw->pw_dir, pw-
>pw_shell);

Pw=getpwname(“root”);
Printf(“root password entry:”);
Printf(“name: %s, uid:%d, gid:%d,home=%s,shell=%s”, pw->pw_name,
pw->pw_uid, pw->pw_gid, pw->pw_dir, pw->pw_shell);
Exit(0);
Break;
Case 4:// case for quite
Return (0);
Break;
Defsult:
Printff(“wrong choice”);
} //closing of switch case portion
getch();
return (0);
}
===========================================================
Ans: 2
#inlcude<stdlib.h>
#include<unistd.h>
#inlcude<stdio.h>
#inlcude<time.h>
int main()
{
int i,choice;
time_t the_time,timeval;
struct tm *tm_ptr;
printf(“1. Display system time \n 2. Display date in year-month-day format”);
printf(“3. Add current user & time in log file \n 4. Display date & time without
gmtime”);
printf(“make your choice:”);
scanf(“%d”,&choice);
switch(choice)
{
case 1:
for(i=1;i<5;i++)
{
the_time=time((time_t *)0);
printf(“the system time is %ld”, the_time);
sleep(1);
}
break;
case 2:
(void)time(&the_time);
tm_ptr=gmtime(&the_time);
printf(“date is: %02d:%02d:%02d”,tm_ptr->tm_year,tm_ptr->tm_mon+1, tm_ptr-
>tm_mday);
break;
case 3:
FILE *f;
F=open(“not_here”,”r”);
If(!f)
Syslog(LOG_ERR|LOG_USER,”oops - %m \n”);
exit(o);
break;
case 4:
(void)time(&timeval);
printf(“the date and time is: %s”,ctime(&timeval));
exit(0);
break;
default:
printf(“wrong choice”);
}
return (0);
}
===========================================================
Ans:3

#include<stdio.h>
#include<stdlib.h>
Int main()
{
char tmpname[L_tmpname];
char *filename;
FILE *tmpfp;
filename = tmpname(tmpname);
printf(“temporary file name is : %s”, filename);
tmpfp=tmpfile();
if(tmpfp)
{
printf(“opened a temporary file ok \n”);
}
else
{
perror(“tmpfile”);
}
exit(0);
}
========================================================
PART B

Ans: 4
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <stdio.h>

#define BAUDRATE B38400


#define MODEMDEVICE "/dev/ttyS1"
#define _POSIX_SOURCE 1 #define FALSE 0
#define TRUE 1

volatile int STOP=FALSE;

main()
{
int fd,c, res;
struct termios oldtio,newtio;
char buf[255];

fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY );


if (fd <0) {perror(MODEMDEVICE); exit(-1); }

tcgetattr(fd,&oldtio);
bzero(&newtio, sizeof(newtio));
newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
newtio.c_iflag = IGNPAR;
newtio.c_oflag = 0;
newtio.c_lflag = 0;
newtio.c_cc[VTIME] = 0;
newtio.c_cc[VMIN] = 5;
tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&newtio);

while (STOP==FALSE)
{
res = read(fd,buf,255);
buf[res]=0;
printf(":%s:%d\n", buf, res);
if (buf[0]=='z') STOP=TRUE;
}
tcsetattr(fd,TCSANOW,&oldtio);
}

||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Ans 5:
#include <stdio.h>    
#include <stdlib.h>    
#include <unistd.h>  
#include<string.h>  
struct student
{
Char name[20];
Int rollno;
Float marks;
}
int main()
{
Struct student arr[100];
int i,choice,no;
printf("\t 1.ADD NEW RECORD \n 2.DELETION”);
printf("3.DISPLAY RECORDS \n 4.QUIT);

printf(“enter your choice:”);


scanf(“%d”,&choice);
case(choice)
{
case 1: //case for add new record
Printf(“enter the name of %d student”,i);
Scanf(“%c”,&arr[i].name);
Printf(“enter the rollno of %d student”,i);
Scanf(“%d”,&arr[i].rollno);
Printf(“enter the marks of %d student”,i);
Scanf(“%f”,&arr[i].marks);
break;
case 2:
int k; // local variable valid only for case 2
printf("enter rollno of student which you want to delete:");
scanf(“%d”,&k);
for(i=0;i<no;i++)
{
if(k==arr[i].code) //it will find value of i
{
for(j=i;j<10;j++) //I assign to j
{
strcpy(arr[j].name,arr[j+1].name);
arr[j].rollno = arr[j+1].rollno;
arr[j].marks = arr[j+1].marks;

cout<<"item has been deleted from list:"<<endl;


break;
case 3:
printf(“student records are:”)
for(i=0;i<no;i++)
{
Printf(“ name of %d student is:”,i);
Scanf(“%c \n”,arr[i].name);
Printf(: rollno of %d student”,i);
Scanf(“%d”,arr[i].rollno);
Printf(“ marks of %d student”,i);
Scanf(“%f”,arr[i].marks);
printf(“\n\n”);
}
Break;
case 4:
return (0);
default:
printf(“wrong choice:”);
}
getch();
Return (0);
}

|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

Ans: 6

#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<fcntl.h>
#include<errno.h>
int main()
{
int file_desc;
int save_errno;
file_desc = open(“Yogendra.txt”,O_RDWR|O_CREAT|O_EXCL,0444);
If (file_desc == -1)
{
save_errno = errno;
printf(“open failed with error %d \n”, save_errno);
}
else
{
printf(“file successes open \n”);
}
exit(EXIT_SUCCESS);
}
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

Potrebbero piacerti anche