Q1. 1.Write a C Program to display a menu and perform respective tasks ? ***************************************** *** 1. Search User info by UID *** *** 2. Search User info by user Name *** *** 3. Display Info. Of all users *** *** 4. Quit *** ******************************************** Ans: #include <sys/types.h> #include <pwd.h> #include <stdio.h> #include <unistd.h> int main() { uid_t uid; gid_t gid; struct passwd *pw; uid = getuid(); gid = getgid(); printf("User is %s\n", getlogin()); printf("User IDs: uid=%d, gid=%d\n", uid, gid); pw = getpwuid(uid); printf("UID passwd entry:\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 = getpwnam("root"); printf("root passwd entry:\n"); printf("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); exit(0);

} 2. Write a C program to display a menu and do following tasks *************************************************** ** *** 1. Display System time (low level) *** *** 2. Display date in year-month – day format *** *** 3. Add current user & time in Log file *** *** 4. Display date & time without gmtime() *** *************************************************** ** Ans :with gm time #include<time.h> #include<stdio.h> #include<stdlib.h> #include<unistd.h> Int main() { Struct tm, *tm_ptr; Time_t t; (void) time(&t); Int i; Tm_ptr = gmtime(&tm); For(i=0;i<5;i++) { T= time((time_t *) 0); Printf(“the time in %d\n”,t); Sleep(2); Break; } Printf(“gmtime is %d\n%d\n%d”,tm_ptr->tm_mday,(tm_ptr>tm_mon+1),(tm_ptr->tm_year+1900)); Exit(0); }

Without gm time #include<time.h> #include<stdio.h> #include<stdlib.h> #include<unistd.h> Int main() { Time_t t; (void) time(&t); Printf(“raw time is %d”,t); Printf(“ctime is %s”,ctime(&t)); Exit(0); } 3. Write a C program to create a temporary file and display its name and time of creation and name of user who created it. Ans: #include <stdio.h> int main() { char tmpname[L_tmpnam]; char *filename; FILE *tmpfp; filename = tmpnam(tmpname); printf("Temporary file name is: %s\n", filename); tmpfp = tmpfile(); if(tmpfp) printf("Opened a temporary file OK\n"); else perror("tmpfile"); exit(0); } PART B 4. Write a C program to check if any key is hit and make change in the terminal settings as follows:

c- Switch to canonical mode. n- Switch to non canonical mode. s- Does not allow the signal keys like ctrl+D etc. o- Reset the original settings. q- Quit from program. And while waiting for any key to hit it should print the message “Please press any key”. Ans :#include<stdlib.h> #include<stdio.h> #include<unistd.h> #include<fcntl.h> #include<term.h> #include<curses.h> #include<termios.h> Int main() { Char ch; While () 5. Write a C program to maintain student record. It should also allocate memory dynamically for the following structure. Struct student { Char name [20]; Int Rollno; Float marks; }; The program menu should be: 1. Add new record. 2. Delete a record. 3. Display records. 4. Quit. Ans: #include <stdio.h> #include <unistd.h>

char *menu[] = { "a − add new record", "d − delete record", "q − quit", NULL, }; int getchoice(char *greet, char *choices[], FILE *in, FILE *out); int main() { int choice = 0; FILE *input; FILE *output; if(!isatty(fileno(stdout))) { fprintf(stderr,"You are not a terminal, OK.\n"); } input = fopen("/dev/tty", "r"); output = fopen("/dev/tty", "w"); if(!input || !output) { fprintf(stderr,"Unable to open /dev/tty\n"); exit(1); } do { choice = getchoice("Please select an action", menu, input, output); printf("You have chosen: %c\n", choice); } while(choice != 'q'); exit(0); } int getchoice(char *greet, char *choices[], FILE *in, FILE *out) { int chosen = 0; int selected; char **option; do { fprintf(out,"Choice: %s\n",greet); option = choices; while(*option) { fprintf(out,"%s\n",*option); option++;

} do { selected = fgetc(in); } while(selected == '\n'); option = choices; while(*option) { if(selected == *option[0]) { chosen = 1; break; } option++; } if(!chosen) { fprintf(out,"Incorrect choice, select again\n"); } } while(!chosen); return selected; } The program should de-allocate the memory occupied before exiting from the program.

Q6 WAP to create a file with name provided as command line argument and place a write lock on it ANS: Create a Write Lock with fcntl #include #include #include #include <fcntl.h> <stdio.h> <string.h> <unistd.h>

int main (int argc, char* argv[]) { char* file = argv[1]; int fd;

struct flock lock; printf ("opening %s\n", file); /* Open a file descriptor to the file. */ fd = open (file, O_WRONLY); printf ("locking\n"); /* Initialize the flock structure. */ memset (&lock, 0, sizeof(lock)); lock.l_type = F_WRLCK; /* Place a write lock on the file. */ fcntl (fd, F_SETLKW, &lock); printf ("locked; hit Enter to unlock... "); /* Wait for the user to hit Enter. */ getchar (); printf ("unlocking\n"); /* Release the lock. */ lock.l_type = F_UNLCK; fcntl (fd, F_SETLKW, &lock); close (fd); return 0; }

Sign up to vote on this title
UsefulNot useful