Operating systems short programs (C language)

profileVampass
Labfinal.pdf

Write a program in C which receives a file name on the command line and which states whether or not the user of the program is in the same group as the owner of the file

1)

Write a program in C which receives a file name on the command line and in the case the file is a directory, states whether or not it contains any visible sub-directories

2)

Write a program in C which receives a file name on the command line and displays the home directory of the owner of the file

3)

Write a program in C which creates two child processes. Each child process should read a character from the keyboard and then write it to a single pipe. The parent process should read the characters from the pipe and then state whether or not the characters are the same.

4)

Create and execute a shell scrip which displays the full path name of the user's current directory.5)

Lab Final Saturday, May 11, 2013 8:00 PM

Lab final Page 1

Write a program in C which receives a file name on the command line and which states whether or not the user of the program is in the same group as the owner of the file

#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <sys/stat.h>

main(int argc, char *argv[]) { struct stat buf;

stat(argv[1],&buf);

if(getgid() != buf.st_gid) printf("Not in same group!\n"); else printf("Same group\n");

Part 1 Tuesday, May 14, 2013 12:08 AM

Lab final Page 2

Write a program in C which receives a file name on the command line and in the case the file is a directory, states whether or not it contains any visible sub-directories

#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <dirent.h> #include <sys/stat.h> #include <string.h>

int main(int argc, char *argv[]) { struct dirent *dir_info; DIR *dir; struct stat buf; char *name = argv[1]; int true_flag = 0;

if((dir = opendir(name)) == NULL) { printf("\n%s is a file\n\n", name); } else { printf("\n%s is a directory..\n", name);

while((dir_info = readdir(dir)) != NULL) { char *temp = dir_info->d_name; stat(temp, &buf); if((buf.st_mode & S_IFDIR) //if(opendir(temp) != NULL && strcmp(temp, ".") != 0 && strcmp(temp, "..") != 0) { printf("%s has visible sub-directories..\n\n", name); return; } }

printf("%s has no visible directories\n\n",name);

} }

Part 2 Tuesday, May 14, 2013 12:08 AM

Lab final Page 3

Write a program in C which receives a file name on the command line and displays the home directory of the owner of the file

#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/stat.h> #include <pwd.h>

main(int argc, char *argv[]){ struct stat buf;

stat(argv[1], &buf);

struct passwd *user_info = getpwuid(buf.st_uid);

printf("Home directory for the owner of %s is:\n%s\n", argv[1], user_info->pw_dir);

}

Part 3 Monday, May 13, 2013 11:28 PM

Lab final Page 4

Write a program in C which creates two child processes. Each child process should read a character from the keyboard and then write it to a single pipe. The parent process should read the characters from the pipe and then state whether or not the characters are the same.

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

struct data { char value; }

main(int argc, char *argv[]) { struct data d; int p[2]; int i = 0;

if(pipe(p) == -1) printf("pipe failed");

for(i;i<2;i++) { if(fork() == 0) { char input; printf("\nType in the character for process %d:\n",i); scanf("%c", &input); printf("You enetered %c for process %d\n", input,i); d.value = input;

if(write(p[1],&d,sizeof(struct data)) == -1) printf("write failed"); return; } wait(); } int k = 0; struct data c; int compare[2];

for(k;k<2;k++) { if(read(p[0],&c, sizeof(struct data)) == -1) printf("read failed"); compare[k] = c.value; }

if(compare[0] == compare[1]) printf("\nCongratulations! Input characters match!\n"); else printf("\nSorry, input characters do not match. You lose.\n"); }

Part 4

Lab final Page 5

Create and execute a shell scrip which displays the full path name of the user's current directory.

Open pico

Put in "pwd"

Ctrl x

Chmod u = rwx file_name

Gcc part1.c -o part1 ./part1 part1.c

Part5 Tuesday, May 14, 2013 12:07 AM

Lab final Page 6