slip 1 a
/Program to create hole in file
#include<stdio.h>
#include<fcntl.h>
#include"string.h"
int main()
{
//create a new file by named as file.txt
int n=creat("file.txt","w");
char ch[16]="hello world how are";
char str[20]="od -c file.txt";
//change permission of file.txt with maximum access
system("chmod 777 file.txt");
//write "helloworld string in file.txt
write(n,ch,16);
// to move cursor from begging to 48th position
lseek(n,48,SEEK_SET);
//write "helloworld string in file.txt
write(n,ch,16);
// to prompt command in command prompt
system(str);
return(0);
}
slip 2b
/To handle the two-way communication between parent and child using pipe.
#include<stdio.h>
#include<unistd.h>
int main()
{
int pipefds1[2], pipefds2[2];
int returnstatus1, returnstatus2;
int pid;
char pipe1writemessage[20] = "Hi";
char pipe2writemessage[20] = "Hello";
char readmessage[20];
returnstatus1 = pipe(pipefds1);
if (returnstatus1 == -1) {
printf("Unable to create pipe 1 \n");
return 1;
}
returnstatus2 = pipe(pipefds2);
if (returnstatus2 == -1) {
printf("Unable to create pipe 2 \n");
return 1;
}
pid = fork();
if (pid != 0) // Parent process {
close(pipefds1[0]); // Close the unwanted pipe1 read side
close(pipefds2[1]); // Close the unwanted pipe2 write side
printf("In Parent: Writing to pipe 1 – Message is %s\n", pipe1writemessage);
write(pipefds1[1], pipe1writemessage, sizeof(pipe1writemessage));
read(pipefds2[0], readmessage, sizeof(readmessage));
printf("In Parent: Reading from pipe 2 – Message is %s\n", readmessage);
} else { //child process
close(pipefds1[1]); // Close the unwanted pipe1 write side
close(pipefds2[0]); // Close the unwanted pipe2 read side
read(pipefds1[0], readmessage, sizeof(readmessage));
printf("In Child: Reading from pipe 1 – Message is %s\n", readmessage);
printf("In Child: Writing to pipe 2 – Message is %s\n", pipe2writemessage);
write(pipefds2[1], pipe2writemessage, sizeof(pipe2writemessage));
}
return 0;
}
slip 3 q2#include<stdio.h>
#include<sys/types.h>
#include<dirent.h>
#include<sys/stat.h>
int main(int argc,char *argv[])
{
int i;
struct stat st;
for(i=0;i<argc;i++)
{
stat(argv[i],&st);
if(S_ISDIR(st.st_mode))
printf("\n type of the file %s is directory",argv[i]);
else if(S_ISREG(st.st_mode))
printf("\n type of the file is %s regular file",argv[i]);
else
printf(" other than directory or regular file");
}
return 0;
}
slip 6and 3 q1
/atexit()
#include <stdio.h>
#include <stdlib.h>
void functionA ()
{
printf("This is functionA\n");
}
int main ()
{
/* register the termination function */
atexit(functionA );
printf("Starting main program...\n");
printf("Exiting main program...\n");
return(0);
}
slip 5 q1
#include<stdio.h>
#include<sys/stat.h>
#include<string.h>
int main()
{
char fname[20];
struct stat buff;
printf("\n enter file to check size");
scanf("%s",fname);
stat(fname,&buff);
printf("\nsize of %s file is %ld byte",fname,buff.st_size);
return 0;
}
slip 6 q2
include<stdio.h>
#include<dirent.h>
#include<unistd.h>
int main()
{
DIR *dp;
int cnt=0;
dp=opendir(".");
struct dirent *dent;
while((dent=readdir(dp))!=NULL)
{
printf("\nfile=%s",dent->d_name);
cnt++;
}
printf("\n total number files is %d",cnt);
}
slip 1 q 2
#include<stdio.h>
#include<sys/stat.h>
int main(int argc,char *argv[])
{
struct stat buff;
for(int i=1;i<argc;i++)
{
stat(argv[i],&buff);
printf("\n file=%s inode=%ld",argv[i],buff.st_ino);
}
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_COMMAND_LENGTH 100
#define MAX_ARGUMENTS 10
void executeCommand(char* command) {
int i = 0;
char* args[MAX_ARGUMENTS];
char* token = strtok(command, " \t\n");
while (token != NULL) {
args[i++] = token;
token = strtok(NULL, " \t\n");
}
args[i] = NULL;
if (strcmp(args[0], "typeline") == 0) {
if (args[1] == NULL) {
printf("Error: Insufficient arguments for typeline command.\n");
return;
}
FILE* file = fopen(args[2], "r");
if (file == NULL) {
printf("Error: Failed to open the file '%s'.\n", args[2]);
return;
}
char line[256];
int lineCount = 0;
if (strcmp(args[1], "+") == 0) {
if (args[2] == NULL) {
printf("Error: Insufficient arguments for typeline command.\n");
fclose(file);
return;
}
int numLines = atoi(args[2]);
while (fgets(line, sizeof(line), file) != NULL && lineCount < numLines) {
printf("%s", line);
lineCount++;
}
} else if (strcmp(args[1], "-") == 0) {
if (args[2] == NULL) {
printf("Error: Insufficient arguments for typeline command.\n");
fclose(file);
return;
}
int numLines = atoi(args[2]);
int totalLines = 0;
while (fgets(line, sizeof(line), file) != NULL) {
totalLines++;
}
fseek(file, 0, SEEK_SET);
int startLine = totalLines - numLines;
lineCount = 0;
while (fgets(line, sizeof(line), file) != NULL && lineCount < totalLines) {
if (lineCount >= startLine) {
printf("%s", line);
}
lineCount++;
}
} else if (strcmp(args[1], "a") == 0) {
while (fgets(line, sizeof(line), file) != NULL) {
printf("%s", line);
}
} else {
printf("Error: Invalid typeline command.\n");
}
fclose(file);
} else {
pid_t pid = fork();
if (pid < 0) {
printf("Error: Failed to fork.\n");
return;
} else if (pid == 0) {
// Child process
if (execvp(args[0], args) == -1) {
printf("Error: Failed to execute the command '%s'.\n", args[0]);
}
exit(0);
} else {
// Parent process
wait(NULL);
}
}
}
int main() {
char command[MAX_COMMAND_LENGTH];
while (1) {
printf("NewShell$ ");
if (fgets(command, sizeof(command), stdin) == NULL) {
printf("\n");
break;
}
executeCommand(command);
}
return 0;
}
count
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#define MAX_COMMAND_LENGTH 100
void executeCommand(char *command) {
// Tokenize the command
char *token = strtok(command, " \t\n");
// Check for special commands
if (strcmp(token, "count") == 0) {
token = strtok(NULL, " \t\n");
if (token == NULL) {
printf("Missing file name for 'count' command\n");
return;
}
FILE *file = fopen(token, "r");
if (file == NULL) {
printf("File not found: %s\n", token);
return;
}
char ch;
int charCount = 0, wordCount = 0, lineCount = 0;
int insideWord = 0;
while ((ch = fgetc(file)) != EOF) {
charCount++;
if (ch == ' ' || ch == '\t' || ch == '\n') {
insideWord = 0;
} else if (insideWord == 0) {
insideWord = 1;
wordCount++;
}
if (ch == '\n') {
lineCount++;
}
}
fclose(file);
token = strtok(NULL, " \t\n");
if (token != NULL) {
if (strcmp(token, "c") == 0) {
printf("Character count: %d\n", charCount);
} else if (strcmp(token, "w") == 0) {
printf("Word count: %d\n", wordCount);
} else if (strcmp(token, "l") == 0) {
printf("Line count: %d\n", lineCount);
} else {
printf("Invalid count option: %s\n", token);
}
} else {
printf("Missing count option for 'count' command\n");
}
} else {
pid_t pid = fork();
if (pid == -1) {
printf("Fork failed\n");
return;
} else if (pid == 0) {
// Child process
execlp(command, command, NULL);
printf("Command not found: %s\n", command);
exit(1);
} else {
// Parent process
wait(NULL);
}
}
}
int main() {
char command[MAX_COMMAND_LENGTH];
while (1) {
printf("NewShell$ ");
fgets(command, MAX_COMMAND_LENGTH, stdin);
// Remove newline character
command[strcspn(command, "\n")] = '\0';
if (strcmp(command, "exit") == 0) {
break;
}
executeCommand(command);
}
return 0;
}
list
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#define MAX_COMMAND_LENGTH 100
void executeCommand(char *command) {
// Tokenize the command
char *token = strtok(command, " \t\n");
// Check for special commands
if (token != NULL && strcmp(token, "list") == 0) {
token = strtok(NULL, " \t\n");
if (token == NULL) {
printf("Missing option for 'list' command\n");
return;
}
if (strcmp(token, "f") == 0) {
// Print name of all files in the directory
DIR *dir;
struct dirent *entry;
dir = opendir(".");
if (dir == NULL) {
printf("Unable to open directory\n");
return;
}
while ((entry = readdir(dir)) != NULL) {
if (entry->d_type == DT_REG) {
printf("%s\n", entry->d_name);
}
}
closedir(dir);
} else if (strcmp(token, "n") == 0) {
// Print number of all entries in the directory
DIR *dir;
struct dirent *entry;
int count = 0;
dir = opendir(".");
if (dir == NULL) {
printf("Unable to open directory\n");
return;
}
while ((entry = readdir(dir)) != NULL) {
count++;
}
closedir(dir);
printf("Number of entries: %d\n", count);
} else if (strcmp(token, "i") == 0) {
// Print name and inode of all files in the directory
DIR *dir;
struct dirent *entry;
struct stat fileStat;
dir = opendir(".");
if (dir == NULL) {
printf("Unable to open directory\n");
return;
}
while ((entry = readdir(dir)) != NULL) {
if (entry->d_type == DT_REG) {
if (stat(entry->d_name, &fileStat) == 0) {
printf("Name: %s, Inode: %ld\n", entry->d_name, fileStat.st_ino);
}
}
}
closedir(dir);
} else {
printf("Invalid option for 'list' command\n");
}
} else {
pid_t pid = fork();
if (pid == -1) {
printf("Fork failed\n");
return;
} else if (pid == 0) {
// Child process
execlp(command, command, NULL);
printf("Command not found: %s\n", command);
exit(1);
} else {
// Parent process
wait(NULL);
}
}
}
int main() {
char command[MAX_COMMAND_LENGTH];
while (1) {
printf("NewShell$ ");
fgets(command, MAX_COMMAND_LENGTH, stdin);
// Remove newline character
command[strcspn(command, "\n")] = '\0';
if (strcmp(command, "exit") == 0) {
break;
}
executeCommand(command);
}
return 0;
}
Comments
Post a Comment