Operating System/System Programming(Arif Butt)

Lec13) UNIX File Management

Tony Lim 2021. 6. 29. 12:40
728x90

lseek is used to move offset of where current fd(file dscirptor) is pointing to in system-wide-file-table.

 

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
extern int errno;
int  main(){
   int rv = lseek(0, 54, SEEK_SET);
   if (rv == -1){
      printf("Cannot seek\n");
      exit(errno);
   }
   else
      printf("seek OK\n");
   return 0;
}

we are trying handle fd0 which is stdin a keyboard.  we cannot lseek the keyboard so it will throw on error.

notice we are returning errno which is global variable in system.

notice it is about Illegal seek and it's name is ESPIPE.

inside man page of lseek we can see that ESPIPE is associated with a pipe~ . in our case keyboard.

 

 

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

int main(){
//create a file and check the value of fd
   remove("f1.txt");
   int fd = open("f1.txt",O_RDWR|O_CREAT|O_TRUNC,0622);
   printf("open() returned fd= %d\n", fd);

//check and print value of cfo
   int cfo = lseek(fd, 0, SEEK_CUR);
   printf("Location of CFO= %d\n", cfo);

//write and check the value of cfo
   write(fd, "abcde", 5);
   cfo = lseek(fd, 0, SEEK_CUR);
   printf("Location of CFO after writing ""abcde""= %d\n", cfo);

//move the cfo 100 bytes ahead the EOF
   lseek(fd, 100, SEEK_END);
   cfo = lseek(fd, 0, SEEK_CUR);
   printf("Location of CFO after lseek(fd, 100, SEEK_END)= %d\n", cfo);

//let us write and check the value of cfo
   write(fd, "ABCDE", 5);
   cfo = lseek(fd, 0, SEEK_CUR);
   printf("Location of CFO after writing ""ABCDE""= %d\n", cfo);
   return 0;
}

 

 

 

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

int main(int argc, char *argv[]){
   int holes = atoi(argv[1]);
	int fd = open("filewithholes",O_CREAT|O_WRONLY|O_TRUNC, 0644);
	write(fd, "PUCIT", 5);
	for(int i = 0; i < holes; i++){
		lseek(fd, 10, SEEK_CUR);
		write(fd, "PUCIT", 5);
	}
	close(fd);
   return 0;
}

with every loop we are moving 10 byte ahead.

od = Write  an  unambiguous  representation, octal bytes by default, of FILE to standard output.

 

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
//#include <fcntl.h> // if you are using constant O_RDONLY
//#include <sys/stat.h>
int main(int argc, char* argv[])
{
   if(argc != 2){
      fprintf(stderr, "Incorrect number of arguments\n");
      exit(1);
   }
   if (access(argv[1],F_OK) == 0)
      printf("This file exist\n");
   else{
      printf("This file do not exist\n");
      exit(1);
   }
   if (access(argv[1], R_OK) == 0)
      printf("You have read access to  this file\n");
   else
      printf("You do not have read access to this file\n");
   
   if (access(argv[1], W_OK) == 0)
      printf("You have write access to  this file\n");
   else
      printf("You do not have write access to this file\n");
   
   if (access(argv[1],X_OK) == 0)
      printf("You have execuete access to  this file\n");
   else
      printf("You do not have execute  access to this file\n");
   
   exit (0);
}

access() checks whether the calling process can access the file pathname.

 

 

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
int main(){
   int fd1, fd2, n;
   char buff[10];
   fd1 = open ("abc.txt", O_RDONLY);
   fd2 = dup(fd1);
   //reading via fd1
   n = read (fd1, buff, 5);
   write(1, buff, n);
   //reading via fd2
   n = read (fd2, buff, 5);
   write(1, buff, n);
   //reading via fd1
   n = read (fd1, buff, 5);
   write(1, buff, n);
   return 0;
}

dup() = get duplicate of paramter's fd

fd1 and fd2 are both are pointing to same SWFT

so we can read them continously.

but when we don't use dup() and just write  fd2 = open("abc.txt",O_RDONLY)

fd1 and fd2 will point to different SWFT. so fd2 will start from zero and read ABCDE again.

 

 

 

.listargs print out command line argument and total nubmer of arguments.

notice redirection of fd is not considered as command line arguement of listargs.c

shell doesn't pass redireciton sybombol and the file name to the command.

 

 

close will close descriptor zero.

dup will use create duplicate of given fd , using the lowest-numbered unused file descriptor for the new descriptor.

 

 

728x90