Question

6 Write a C program that asks the user to enter the fil he all its permissions in a readable format.) Also, the program prints the file contents to the standard output if the file is of a regular type. Use only Unix V/O functions. Do not use the O package nor C standard I/O functions. Save your source file as lab07a.c and include it in the report.

I need to rewrite this program using only RIO I/O functions on c

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <unistd.h>

#include<fcntl.h>

int main()

{

char fileName[20];

//declare a struct of type stat to fill in details of file

struct stat info;

int is_reg=0,fd;

char c;

  

//ask user to input file name

printf("Enter file name: ");

scanf("%s",fileName);

printf("FileName: %s\n",fileName);

//call stat function to populate struct info with details of file

stat(fileName,&info);

if(S_ISREG(info.st_mode)!=0)

{

is_reg=1;

printf("%s is a regular file\n",fileName);

}

if(S_ISDIR(info.st_mode)!=0)

{

printf("%s is a directory \n",fileName);

}

if(S_ISCHR(info.st_mode)!=0)

{

printf("%s is a character device \n",fileName);

}

if(S_ISBLK(info.st_mode)!=0)

{

printf("%s is a block device \n",fileName);

}

if(S_ISFIFO(info.st_mode)!=0)

{

printf("%s is a FIFO \n",fileName);

}

else if(S_ISLNK(info.st_mode)!=0)

{

printf("%s is a symbolic link \n",fileName);

}

/*else if(S_ISSOCK(info.st_mode)!=0)

{

printf("%s is a socket\n",fileName);

}*/

  

if(is_reg == 1)

{

//printf("%d\n",is_reg);

//open file for reading and display onto screen

fd = open(fileName, O_RDONLY);

if(fd < 0 )

{

perror("Read: ");

return -1;

}

while (read(fd, &c, 1) > 0)

{

printf("%c", c);

}

}

return 0;

}

---------------------------------------------------------------

//output

  

Enter file name: info.txt

FileName: info.txt   

info.txt is a regular file   

The field st_mtime is changed by file modifications, e.g. by mknod(2), truncate(2), utime(2) and write(2) (of more than zero bytes).   

Moreover, st_mtime of a directory is changed by the creation or deletion of files in that directory.   

The st_mtime field is not changed for changes in owner, group, hard link count, or mode.   

Add a comment
Know the answer?
Add Answer to:
I need to rewrite this program using only RIO I/O functions on c 6 Write a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hello! I need help setting my program up to initiate certain tasks! We are using a...

    Hello! I need help setting my program up to initiate certain tasks! We are using a Centos Server as a root user! Use the CentOS Server and as root user perform the following: Setup two ext4 partitions on /dev/sdc at 1 GB each Mount sdc1 to /var/www (create any directories as needed) Mount sdc2 to /var/data (create any directories as needed) Install the httpd package Create a file called final.txt in /var/www/html (in that put your name/date on the first...

  • Write a program in C using Unix system calls and functions that will change the permissions on a ...

    Write a program in C using Unix system calls and functions that will change the permissions on a file. The executable shall be called “mychmod” and will be executed by: mychmod -u rwx -g rwx -o rwx -U rwx -G rwx -O rwx file1 file2 ... The lowercase options will add permissions while the uppercase options will remove permissions. Each of the switches is optional and should be interpreted as the ones in the Unix command chmod(1), you can review...

  • You must write a C program that prompts the user for two numbers (no command line...

    You must write a C program that prompts the user for two numbers (no command line input) and multiplies them together using “a la russe” multiplication. The program must display your banner logo as part of a prompt to the user. The valid range of values is 0 to 6000. You may assume that the user will always enter numerical decimal format values. Your program should check this numerical range (including checking for negative numbers) and reprompt the user for...

  • //I NEED THE PROGRAM IN C LANGUAGE!// QUESTION: I need you to write a program which...

    //I NEED THE PROGRAM IN C LANGUAGE!// QUESTION: I need you to write a program which manipulates text from an input file using the string library. Your program will accept command line arguments for the input and output file names as well as a list of blacklisted words. There are two major features in this programming: 1. Given an input file with text and a list of words, find and replace every use of these blacklisted words with the string...

  • I need this written in the C format not C++ Name this program count.c-The program reads...

    I need this written in the C format not C++ Name this program count.c-The program reads an unknown number of words - strings that all 20 characters or less in length. It simply counts the number of words read. The end of input is signaled when the user enters control-d (end-of-file). Your program prints the number of words that the user entered. 4.

  • Writing Unix Utilities in C (not C++ or C#) my-cat The program my-cat is a simple...

    Writing Unix Utilities in C (not C++ or C#) my-cat The program my-cat is a simple program. Generally, it reads a file as specified by the user and prints its contents. A typical usage is as follows, in which the user wants to see the contents of my-cat.c, and thus types: prompt> ./my-cat my-cat.c #include <stdio.h> ... As shown, my-cat reads the file my-cat.c and prints out its contents. The "./" before the my-cat above is a UNIX thing; it...

  • Hello I am confused about this C++ program. I need to create a payroll C++ program...

    Hello I am confused about this C++ program. I need to create a payroll C++ program following these steps. Source file structure Your program will consist of three source files: main.cpp the main program Payroll.hppclass declaration for the Payroll class. Make sure you have an include guard. Payroll.cpp Payroll's member functions Class definition Create a Payroll class definition. The class has private members which originate from user input: number of hours worked hourly rate float float - and private members...

  • Please help!! I need help with C++ and im using the program Atom. Q6 Write a...

    Please help!! I need help with C++ and im using the program Atom. Q6 Write a complete C++ program that does the following. 1. It asks the user to enter positive integers a and b that are each at most 100. 2. The 3. The program determines and prints the maximum entry in each column of the table. 4. The program then prints the smallest value among these maximum entries. For example, the following represents one run of the program....

  • C++, data structure Write a program that plays a game called "guess the state, guess the...

    C++, data structure Write a program that plays a game called "guess the state, guess the capital". How do you play? The program randomly selects a state or a state capital. The program asks the player to guess the state's capital (or the capital's state). The program reads the user's guess; and, tells the user if its guess is right, or if the guess is wrong, tells the user that its guess is wrong and displays the correct answer. Before...

  • CSC 126 Arrays, Functions, & File 'O Part I Your program should prompt the user to...

    CSC 126 Arrays, Functions, & File 'O Part I Your program should prompt the user to calculate the total bill of a transaction at a Book store After the calculations have been performed, the program should output the result to the screen and save the output in a text file which can be later retrieved The program should work as follows: XYZ Book Store Sales Register This program calculates your total bill and generates a receipt for you. Please enter...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT