You are on page 1of 6

# HOMEWORK No.

Course :Linux Programming Code : CSE207

Submitted by: Yogendra singh


Roll_no: RC1912A26
Reg_no: 10905201

PART A

1. Write a C program to create a file at least of 2 MB with command


ls -R / >file1.txt and move contents of the file to another file using
a) System call(open, read and buffer of 10 KB)
b) Library functions
1. fgets, fputs
2. fgetc, fputc
c) System call(with buffer of 20 bytes)
Also calculate and compare the time taken by each of the above and
which is better.

#include<unistd.h>
#include<fstream.h>
#define A_MEGABYTE (1024 * 1024)
int main()
{
char *little_memory;
int megabyte = A_MEGABYTE;
int exit_code = EXIT_FAILURE;
some_memory = (char *)malloc(megabyte);
if (little_memory != NULL) {
sprintf(little_memory,”lovely professional university \n”);
int fputc(int c, FILE *stream);
int putc(int c, FILE *stream);
char *getcwd(char *buf, size_t 20);
printf(“%s”, little_memory);
exit_code = EXIT_SUCCESS;
}
exit( 0 );
}
2. WAP to read a ‘C’ program file and count the following in the program.
1. Total no. of statements
2. Total no. of included files
3. Total no. of blocks and brackets

#include<stdio.h>
#include<sys/stat.h>
#include<dirent.h>
#include<libgen.h>

public class NumberOfLine{

  public static void main(String[] args) {
 
    try{

      System.out.println("Getting line number of a paritcular file examp
le!");
      BufferedReader bf = new BufferedReader(new InputStreamReader(Syste
m.in));
      System.out.println("Please enter file name with extension:");
      
      String str = bf.readLine();
 
      File file = new File(str);

      if (file.exists()){

        FileReader fr = new FileReader(file);
   
     LineNumberReader ln = new LineNumberReader(fr);
  
      int count = 0;
 
        while (ln.readLine() != null){
 
          count++;
     
   }
 
       System.out.println("Total line no: " + count);
 
        ln.close();
 
      }
 
      else{
 
       System.out.println("File does not exists!");
 
     }
 
    }

    catch(IOException e){

      e.printStackTrace();

    }

  }

}
3. Write a C program which takes directory name as input and delete all
files, within that directory.

#include <stdio.h>
#include <sys/stat.h>
#include <dirent.h>
#include <libgen.h>

int recursiveDelete(char* dirname) {

  DIR *dp;
  struct dirent *ep;

  char abs_filename[FILENAME_MAX];

  dp = opendir (dirname);
  if (dp != NULL)
    {
      while (ep = readdir (dp)) {
        struct stat stFileInfo;

        snprintf(abs_filename, FILENAME_MAX, "%s/%s", dirname, ep-


>d_name);

        if (lstat(abs_filename, &stFileInfo) < 0)


          perror ( abs_filename );

        if(S_ISDIR(stFileInfo.st_mode)) {
          if(strcmp(ep->d_name, ".") &&
             strcmp(ep->d_name, "..")) {
            printf("%s directory\n",abs_filename);
            recursiveDelete(abs_filename);
          }
        } else {
          printf("%s file\n",abs_filename);
                  remove(abs_filename);
        }
          }
      (void) closedir (dp);
        }
  else
    perror ("Couldn't open the directory");

  remove(dirname);
  return 0;

}
2.
PART B

Q4. Illustrate through example in which scenario would you prefer using
perror ( ) and in which strerror ( ).
Ans:
The function strerror() is very similar to perror(), except it returns a pointer to
the error message string for a given value (you usually pass in the variable errno.)

"perror" prints out an error message based on the current value of the external
variable "errno". The value of "errno" is set by C library functions when error
conditions are encountered. Thus the message printed by "perror" reflects the last
error encountered during a call to a library function. The function "strerror" obtains
the text of the appropriate error message.

Syntax of strerror:
#include<string.h>
Char *strerroe(int errnum);

Syntax of perror:
#include<stdio.h>
Void perror(const char *s);

Example

/* PERROR.C: This program attempts to open a file named


* NOSUCHF.ILE. Because this file probably doesn't exist,
* an error message is displayed. The same message is
* created using perror, strerror, and _strerror.
*/

#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <io.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

void main( void )


{
  int  fh;

  if( (fh = _open( "NOSUCHF.ILE", _O_RDONLY )) == -1 )


  {
     /* Three ways to create error message: */
     perror( "perror says open failed" );
     printf( "strerror says open failed: %s\n", strerror( errno ) );
     printf( _strerror( "_strerror says open failed" ) );
  }
  else
  {
     printf( "open succeeded on input file\n" );
     _close( fh );
  }
}

Output

perror says open failed: No such file or directory

strerror says open failed: No such file or directory


_strerror says open failed: No such file or directory

Q5.Write a C program to input file name and change its permissions to


reading by all and writing by owner only.

#include<stdio.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdlib.h>
#include<unistd.h>
Int main()
{
Int m;
M=open(“ yogendra ”, O_WRONLY , S_IRUSR,S_IRGRP , S_IROTH );
Exit(0);
}

Q6 To create a following tree structure in current directory.

Your Rollno

Previous Rollno Next Rollno

Ans:

mkdir –p /your_rollno/previour_rollno
cd your_rollno
mkdir next_rollno
AND 2nd method is:

$ Mkdir your_rollno
$ cd your_rollno
Now home directory your_rollno will we set than we can make sub directory………….

[your_rollno] mkdir previous_rollno


[your_rollno] mkdor next_rollno

Now next_rollno and previous_rollno sub directory has make inside home directory
your_rollno……………………….

You might also like