UP Assignment
UP Assignment
Activity report on
UNIX Programming
Submitted by:
Bhoomika P – 1JS19CS042
Mrs. Savitha
Assistant Professor
Department of Computer Science
JSS Academy of Technical Education, Bangalore-60
PROGRAM 14:
A) cat
B) mv
C) ls -l
A) cat
The cat (short for “concatenate“) command is one of the most frequently used
commands in Linux/Unix-like operating systems. cat command allows us to create
single or multiple files, view content of a file, concatenate files and redirect output
in terminal or files.
#include<sys/types.h>
#include<sys/stat.h>
#include<stdio.h>
#include<fcntl.h>
main( int argc,char *argv[3] )
{
int fd,i;
char buf[2];
fd=open(argv[1],O_RDONLY,0777);
if(fd==-argc)
{
printf("file open error");
}
else
{
while((i=read(fd,buf,1))>0)
{
printf("%c",buf[0]);
}
close(fd);
}
}
OUTPUT
B) ls
#include <sys/types.h>
#include <sys/dir.h>
#include <sys/param.h>
#include <stdio.h>
#define FALSE 0
#define TRUE 1
extern int alphasort();
char pathname[MAXPATHLEN];
main() {
int count,i;
struct dirent **files;
int file_select();
if (getwd(pathname) == NULL )
{
printf("Error getting pathn");
);
exit(0);
}
printf("Current
"Current Working Directory = %sn%sn",pathname);
count = scandir(pathname, &files, file_select, alphasort);
if (count <= 0)
{
printf("No
"No files in this directoryn
directoryn");
exit(0);
}
printf("Number
"Number of files = %dn
%dn",count);
for (i=1;i<count 1; i)
printf("%s \n",files[i-1]->d_name);
>d_name);
}
int file_select(struct direct *entry)
{
if ((strcmp(entry->d_name, ".") == 0) ||(strcmp(entry->d_name, "..")) == 0))
return (FALSE);
else
return (TRUE);
}
C) mv
mv is a Unix command that moves one or more files or directories from one
place to another.. If both filenames are on the same file system, this results in a
simple file rename; otherwise the file content is copi
copied
ed to the new location and the
old file is removed.
#include<sys/types.h>
#include<sys/stat.h>
#include<stdio.h>
#include<fcntl.h>
main( int argc,char *argv[] )
{
int i,fd1,fd2;
char *file1,*file2,buf[2];
file1=argv[1];
file2=argv[2];
printf("file1=%s file2=%s",file1,file2);
,file1,file2);
fd1=open(file1,O_RDONLY,0777);
fd2=creat(file2,0777);
while(i=read(fd1,buf,1)>0)
write(fd2,buf,1);
remove(file1);
close(fd1);
close(fd2);
}
PROGRAM 12:
The st_dev field describes the device on which this file resides.
The st_rdev field describes the device that this file (inode) represents.
The st_size field gives the size of the file (if it is a regular file or a symbolic link)
in bytes. The size of a symlink is the length of the pathname it contains, without a
trailing null byte.
The st_blocks field indicates the number of blocks allocated to the file, 512-byte
units. (This may be smaller than st_size/512, for example, when the file has holes.)
The st_blksize field gives the "preferred" blocksize for efficient file system I/O.
(Writing to a file in smaller chunks may cause an inefficient read-modify-rewrite.
struct stat {
dev_tst_dev; /* ID of device containing file */
ino_tst_ino; /* inode number */
mode_tst_mode; /* protection */
nlink_tst_nlink; /* number of hard links */
uid_tst_uid; /* user ID of owner */
gid_tst_gid; /* group ID of owner */
dev_tst_rdev; /* device ID (if special file) */
off_tst_size; /* total size, in bytes */
blksize_tst_blksize; /*blocksize for filesystem I/O*/
blkcnt_tst_blocks; /* number of blocks allocated */
time_tst_atime; /*time of last access*/
time_tst_mtime; /*time of last modification*/
time_tst_ctime; /*time of last status change*/
};
The fstat() function gets status information about the object specified by the open
descriptor descriptor and stores the information in the area of memory indicated by
the buffer argument. The status information is returned in a stat structure, as
defined in the <sys/stat.h> header file.
0 fstat() was successful. The information is returned in buffer.
-1 fstat() was not successful. The errnoglobal variable is set to indicate the
error.
/**
* C program to find file permission, size, creation and last modification date of * a
given file.
*/
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <time.h>
int main()
{
char path[100];
struct stat stats;
printf("Enter source file path: ");
fflush(stdout);
scanf("%s", path);
// stat() returns 0 on successful operation,
// otherwise returns -1 if unable to get file properties.
if (stat(path, &stats) == 0)
{
printFileProperties(stats);
}
else
{
printf("Unable to get file properties.\n");
fflush(stdout);
printf("Please check whether '%s' file exists.\n", path);
fflush(stdout);
}
return 0;
}
/**
* Function to print file properties.
*/
void printFileProperties(struct stat stats)
{
struct tm dt;
// File permissions
printf("\nFile access: ");
if (stats.st_mode& R_OK)
printf("read ");
if (stats.st_mode& W_OK)
printf("write ");
if (stats.st_mode& X_OK)
printf("execute");
// File size
printf("\nFile size: %ld", stats.st_size);
// Get file creation time in seconds and
// convert seconds to date and time format
dt = *(gmtime(&stats.st_ctime));
printf("\nCreated on: %d-%d-%d %d:%d:%d", dt.tm_mday, dt.tm_mon,
dt.tm_year + 1900, dt.tm_hour, dt.tm_min, dt.tm_sec);
// File modification time
dt = *(gmtime(&stats.st_mtime));
printf("\nModified on: %d-%d-%d %d:%d:%d", dt.tm_mday, dt.tm_mon,
dt.tm_year + 1900, dt.tm_hour, dt.tm_min, dt.tm_sec);
}
OUTPUT:
Soft Link: A soft link (also known as a Symbolic link) acts as a pointer or a reference to the file
name. It does not access the data available in the original file. If the earlier file is deleted, the soft
link will point to a file that does not exist anymore.
PROGRAM:
// C program to create an Soft Link
// to the existing file
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
// Driver Code
int main(int argc, char* argv[])
{
// Symlink function
int sl = symlink(argv[1], argv[2]);
// argv[1] is existing file name
// argv[2] is soft link file name if (sl == 0) {
printf("Soft Link created" " successfully");
}
return 0;
}
Hard Link: A hard link acts as a copy (mirrored) of the selected file. It accesses the data
available in the original file.
If the earlier selected file is deleted, the hard link to the file will still contain the data of that file.
PROGRAM:
// C program to create an Hard Link
// to the existing file
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
// Driver Code
int main(int argc, char* argv[])
{
// Link function
int l = link(argv[1], argv[2]);
// argv[1] is existing file name
// argv[2] is link file name if (l == 0) {
printf("Hard Link created" " successfully");
}
return 0;
}