You are on page 1of 32

1.GREP.

C---------------------------------------------------------------------------------------------
MYGREP
Grep - print lines matching a pattern

SYNOPSIS
grep PATTERN [FILE...]
grep [-e PATTERN ] [FILE...]

DESCRIPTION
Grep searches the named input FILEs (or standard input if no files are
named, or the file name - is given) for lines containing a match to the
given PATTERN. By default, grep prints the matching lines.
OPTIONS
-n : prints the lines that are matched with pattern along with the line numbers
-v : prints the lines which are not matched with pattern
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(int argc, char *argv[])
{
char line[80];
int i=0,k=1;
FILE *p;
if(argc==3)
{
if((p=fopen(argv[2],"r"))==NULL)
{
printf("The file %s doesnt exists ",argv[2]);
exit(0);
}
while((fgets(line, 80, p ))> 0)
{
i=0;
while (i <= strlen(line))
{
if(strncmp(&line[i], argv[1],strlen(argv[1])) == 0)
{
printf("%s", line);
break;
}
i++;
}
}
}
else if(argc==4)
{
if((p=fopen(argv[3],"r"))==NULL)
{
printf("The file %s doesnt exists ",argv[3]);
exit(0);
}
if((strcmp(argv[1],"-n")) == 0)
{
while((fgets(line, 80, p ))> 0)
{
i=0;
while (i <= strlen(line))
{
if(strncmp(&line[i], argv[2],strlen(argv[2])) == 0)
{
printf("line %d:%s",k,line);
break;
}
i++;
}
k++;
}
}
else if(strcmp(argv[1],"-v")==0)

{
while((fgets(line, 80, p ))> 0)
{
i=0;
while (i <= strlen(line))
{
if(strncmp(&line[i], argv[2],strlen(argv[2])) != 0)
{
printf("line %d:%s",k,line);
break;
}
i++;
}
k++;
}
}

}
}
--2.WC.C--------------------------------------------------------------------------------
MYWC
wc - print the number of newlines, words, and bytes in files

SYNOPSIS
wc [OPTION]... [FILE]...

OPTIONS:
-c, --bytes
print the byte counts

-l, --lines
print the newline counts

-w, --words
print the word counts

-s, --spaces
print the space counts
:#include<stdio.h>
#include<stdlib.h>
int main(int argc, char *argv[])
{
char f[15],ch;
FILE *p;
int c=0,w=0,l=0,s=0,a=0,b=0,j=0;
if(argc==3)
{
if((p=fopen(argv[2],"r"))==NULL)
{
printf("The file %s doesnt exists ",argv[2]);
exit(0);
}
if (strcmp(argv[1], "-s") == 0)
{
while((ch=fgetc(p))!=EOF)
if (ch==' ')
s++;
}

if(strcmp(argv[1],"-w") == 0)
{
while((ch=fgetc(p))!=EOF)
if (ch==' '|| ch=='\n')
w++;
}
else if(strcmp(argv[1],"-l") == 0)
{
rewind(p);
while((ch=fgetc(p))!=EOF)
if(ch=='\n')
l++;
}
else if(strcmp(argv[1],"-c")==0)
{
rewind(p);
while((ch=fgetc(p))!=EOF)
c++;
}
}
else if(argc==2)
{
if((p=fopen(argv[1],"r"))==NULL)
{
printf("The file %s doesnt exists ",argv[1]);
exit(0);
rewind(p);
}
while((ch=fgetc(p))!=EOF)
{
if (ch == ' ')
s++;
if (ch==' '|| ch=='\n')
a++;
if(ch=='\n')
b++;
j++;
}
printf("\nLINES : %d \nWORDS : %d\nCHARACTERS : %d \nSPACES : %d",b,a,j,s);

}
if(strcmp(argv[1],"-w")==0)
printf("%d",w);
if(strcmp(argv[1],"-l")==0)
printf("%d",l);
if(strcmp(argv[1],"-c")==0)
printf("%d",c);
if(strcmp(argv[1],"-s")==0)
printf("%d",s);
printf("\n");
}
3.SORT.C-----------------------------------------------------------------------------------------------

NAME
sort - sort lines of text files

SYNOPSIS
sort [OPTION]... [FILE]...

DESCRIPTION
Write sorted concatenation of all FILE(s) to standard output.

#include<stdlib.h>
#include<string.h>
#include<malloc.h>
int main(int argc, char *argv[])
{
char line[80],*ch[80],temp[80];
int i=0,k,j;
FILE *p;
if(argc==2)
{
if((p=fopen(argv[1],"r"))==NULL)
{
printf("The file %s doesnt exists ",argv[1]);
exit(0);
}
while((fgets(line,80,p)) > 0 )
{
ch[i] = (char *)malloc(80);
strcpy(ch[i],line);
// puts(*ch);
i++;

}
k=i;
for(k=0;k<i;k++)
{
for(j=k+1;j<i;j++)
{
if((strcmp(ch[k],ch[j])) > 0)
{
strcpy(temp,ch[k]);
strcpy(ch[k],ch[j]);
strcpy(ch[j],temp);
}
}
}
for(i=0;i<k;i++)
printf("%s",ch[i]);
}
}
4.CAT.C------------------------------------------------------------------------------------------
NAME
cat - concatenate files and print on the standard output

SYNOPSIS
cat [OPTION] [FILE]...

DESCRIPTION
Concatenate FILE(s), or standard input, to standard output.

-A, --show-all
equivalent to -vET

-b, --number-nonblank
number nonblank output lines

-e equivalent to -vE

-E, --show-ends
display $ at end of each line

-n, --number
number all output lines

-s, --squeeze-blank
never more than one single blank line

-t equivalent to -vT

-T, --show-tabs
display TAB characters as ^I

-u (ignored)

-v, --show-nonprinting
use ^ and M- notation, except for LFD and TAB

--help display this help and exit

--version
output version information and exit
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>
int main(int argc, char *argv[])
{
printf("\n hi please give space b/w arguments \n ");
char c,line[80],*ch[80],temp[80];
int i=0,k,j;
FILE *p;
printf ("\nArguments are %d ", argc);
if(argc==2)
{
if((p=fopen(argv[1],"r"))==NULL)
{
printf("The file %s doesnt exists ",argv[1]);
exit(0);
}
while((fgets(line,80,p)) > 0 )
{
ch[i]=line;
puts(ch);
i++;

}
}
else if(argc==1)
{
printf("Enter the text");
while(3)
{
gets(temp);
puts(temp);
}
}
else if(argc==3)
{
if((p=fopen(argv[2],"w"))==NULL)
{
printf("The file %s doesnt exists ",argv[2]);
exit(0);
}
while((c=getchar())!= 11)
{
fputc(c,p);
}

}
}
5.LOGNAME.C----------------------------------------------------------------------------------------

NAME
logname - print user´s login name

SYNOPSIS
logname [OPTION]

DESCRIPTION
Print the name of the current user.

--help display this help and exit

#include<stdio.h>
#include<stdlib.h>
#include<locale.h>
#include<unistd.h>
#include<err.h>
int main(int argc,char *argv[])
{
char *p;
if(argc==2 && strcmp(argv[1],"--help")==0)
{
printf("\n-----------HELP FOR LOGNAME-----------");
printf("\n IT PRINTS THE USER NAME OF THE CURRENT USER\n");
}
else
{
if((p=getlogin()) == NULL)
err(1,NULL);
printf("%s\n",p);
}
}
6.HEAD.C---------------------------------------------------------------------------------------------
NAME
head - output the first part of files

SYNOPSIS
head [OPTION]... [FILE]...

DESCRIPTION
Print the first 10 lines of each FILE to standard output. With more than one FILE,
precede each with a
header giving the file name. With no FILE, or when FILE is -, read standard input.
#include<stdio.h>
#include<stdlib.h>
int main(int argc,char *argv[])
{
char ch;
FILE *p;
int i=0;
if(argc==2)
{
if((p=fopen(argv[1],"r"))==NULL)
{
printf("The file %s doesnt exists ",argv[1]);
exit(0);
}
while((ch=fgetc(p))!=EOF)
{
if(ch=='\n')
{
printf("\n");
i++;
}
printf("%c",ch);
if(i==10)
break;

}
}
else
printf("\nToo many arguments");
}
7. environemt variables--------------------------------------------------------------------------
NAME
env - run a program in a modified environment

SYNOPSIS
env [OPTION]... [-] [NAME=VALUE]... [COMMAND [ARG]...]

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

int main(int argc,char *argv[])


{
extern char**environ;
extern int optind;
char **ep;

if(argc==2 && strcmp(argv[1],"--help")==0)


{
printf("\n help for env ");
printf("\n prints the environmental variable \n");
}
else
{
argc-=optind;
argv+=optind;
for(ep=environ;*ep;ep++)
printf("%s\n",*ep);
}
}
8.HOSTNAME.c--------------------------------------------------------------------------------------
NAME
hostname - show or set the systemâs host name

DESCRIPTION
Hostname is the program that is used to either set or display the current host, domain
or node name of
the system. These names are used by many of the networking programs to identify
the machine. The domain
name is also used by NIS/YP.

GET NAME
When called without any arguments, the program displays the current names:

hostname will print the name of the system as returned by the gethostname(2)
function.
#include<sys/param.h>
#include<err.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>

extern char *progname;


void usage(void);
int main(int argc, char *argv[])
{
int ch,sflag;
char *p,hostname[MAXHOSTNAMELEN];
sflag=0;
if(argc==2 && strcmp(argv[1],"--help")==0)
{
printf("\n help for hostname ");
printf("\n prints the hostname of the system \n");
}
else
{
while((ch=getopt(argc,argv,"s"))!= -1)
switch(ch)
{
case 's':
sflag=1;
break;
default:
usage();
}
argc-=optind;
argv+=optind;

if(argc>1)
usage();
if(*argv)
{
if(sethostname(*argv,strlen(*argv)))
err(1,"sethostname");
}
else
{
if(gethostname(hostname,sizeof(hostname)))
err(1,"gethostname");
if(sflag && (p=strchr(hostname,'.')))
*p="\0";
(void) printf("%s \n",hostname);
}
exit(0);
}

void
usage(void)
{
(void)fprintf(stderr,"usage:%s[-s][name-of-host]\n", *progname);
exit(1);
}
[user18@localhost khan4]$ hostname
localhost.localdomain
[user18@localhost khan4]$
9.BASENAME.C------------------------------------------------------------------------------------
[user19@localhost ~]$ basename /usr/lib/file/gdhgh
gdhgh
NAME
basename - strip directory and suffix from filenames

SYNOPSIS
basename NAME [SUFFIX]
basename OPTION

DESCRIPTION
Print NAME with any leading directory components removed. If specified, also
remove a trailing SUFFIX.

--help display this help and exit

--version
output version information and exit

EXAMPLES
basename /usr/bin/sort
Output "sort".

basename include/stdio.h .h
Output "stdio".

#include<err.h>
#include<libgen.h>
#include<stdio.h>
#include<stdlib.h>
#include<locale.h>
#include<unistd.h>
void help(void);
void usage(void);
int main(int argc,char *argv[])
{
int ch;
char *p;
setlocale(LC_ALL,"");
if(argc==2)
{
if(strcmp(argv[1],"--help")==0)
help();
}

while((ch = getopt(argc,argv,"")) != -1)


{
switch(ch)
{
default: usage();
}
}
argc-=optind;
argv+=optind;
if(argc != 1 && argc !=2)
usage();
if(**argv == '\0')
{
(void)puts("");
exit(0);
}
p=basename(*argv);
if(p==NULL)
err(1,"%s",*argv);
if(*++argv)
{
size_t suffixlen,stringlen,off;
suffixlen=strlen(*argv);
stringlen=strlen(p);
if(suffixlen<stringlen)
{
off=stringlen-suffixlen;
if(!strcmp(p + off,*argv))
p[off]='\0';
}
}
(void)puts(p);
exit(0);
}
extern char *_progname;
void usage(void)
{
(void)fprintf(stderr,"HELP usage: %s string [suffix]\n",_progname);
exit(1);
}
void help()
{
printf("----------string directory and suffix from file names-----");
}
10.TTY.C--------------------------------------------------------------------------------------
NAME
tty - print the file name of the terminal connected to standard input
SYNOPSIS
tty [OPTION]...

DESCRIPTION
Print the file name of the terminal connected to standard input.

-s, --silent, --quiet


print nothing, only return an exit status

--help display this help and exit

--version
output version information and exit

#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
char *ttyname();
int main(argc,argv)
char **argv;
{
register char *p;
p=ttyname(0);
if((argc == 2) && strcmp(argv[1],"-help")==0)
{
printf("\t\nHELP FILE\n");
printf("\n THis command shows the terminal you are currently logged into \t\n");
}
else
{
if((argc == 2) && !strcmp(argv[1],"-s"))
;
else
{
printf("%s\n",(p)?p:"not a terminal");
exit(p?0:1);
}
}
}
11.UTIME.C----------------------------------------------------------------------

NAME
utime, utimes - change access and/or modification times of an inode

SYNOPSIS
#include <sys/types.h>
#include <utime.h>

int utime(const char *filename, const struct utimbuf *buf);

#include <sys/time.h>

int utimes(const char *filename, const struct timeval times[2]);

DESCRIPTION
utime() changes the access and modification times of the inode specified by
filename to the actime and
modtime fields of buf respectively.

If buf is NULL, then the access and modification times of the file are set to the
current time.

Changing time stamps is permitted when: either the process has appropriate
privileges (Linux: has the
CAP_FOWNER capability), or the effective user ID equals the user ID of the file, or
buf must is NULL and
the process has write permission to the file.

The utimbuf structure is:

struct utimbuf {
time_t actime; /* access time */
time_t modtime; /* modification time */
};

The function utime() allows specification of time stamps with a resolution of 1


second. The function
utimes() is similar, but allows a resolution of 1 microsecond. Here times[0] refers to
access time, and
times[1] to modification time.

The timeval structure is:

struct timeval {
long tv_sec; /* seconds */
long tv_usec; /* microseconds */
};

:
#include<stdio.h>
#include<sys/time.h>
#include<sys/types.h>
#include<utime.h>
#include<time.h>
struct utimbuff
{
time_t actime;
time_t modtime;
};
int main(int argc,char *argv[])
{
struct utimbuf times;
int offset,i;
if(argc==2 && strcmp(argv[1],"-help")==0)
{
printf("\t\nHELPFILE\n");
printf("\tThis command changes the modification and accesstime of the file\t\n");
}
else
{
if(argc<3 || sscanf(argv[2],"%d",&offset)!=1)
{
return 1;
}
printf("\n Coming here");
times.actime=times.actime+offset;
if(utime(argv[1],&times))
perror("utime");
}
return 0;
}
12.REV…………………………………………
NAME
rev - reverse lines of a file

SYNOPSIS
rev [file]

DESCRIPTION
The rev utility copies the specified files to the standard output, reversing the order of
characters in
every line. If no files are specified, the standard input is read.
#include<stdio.h>
#include<stdlib.h>
#define N 256
char line[N];
FILE *input;
main(argc,argv)
{
char **argv;
{

register i,c;
input = stdin;
if(argc==2 && strcmp(arg[1],"-help")==0)
{
printf("\n\t HELP FILE\n");
printf("\tThis command reverses the contents of the file\n");
printf("\tThis command also reverses the given string if no file name is specified\n");
}
else
{
do
{
if(argc>1)
{
if((input=fopen(arg[1],"r"))==NULL)
{
fprintf(stderr,"rev:cannot open %s \n",argv[1]);
}
}
for(;;)
{
for(i=0;i<N;N++)
{
line[i]=c=getc(input);
switch(c)
{
case EOF:
goto eof;
default:
continue;
case '\n':

break;
}
}
}
while(--i>=0)
}
}
}
}
[user21@localhost ~]$ cat file
hi i am there
[user21@localhost ~]$ rev file
ereht ma i ih

13.DOMAINNAME-----------------------------------------------------------------------------------
NAME
domainname - show or set the systemâs NIS/YP domain name

DESCRIPTION
Hostname is the program that is used to either set or display the current host, domain
or node name of
the system. These names are used by many of the networking programs to identify
the machine. The domain
name is also used by NIS/YP.

GET NAME

domainname, nisdomainname, ypdomainname will print the name of the system as


returned by the getdomain-
name(2) function. This is also known as the YP/NIS domain name of the system.

#include<sys/param.h>
#include<err.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>

extern char *__progname;

void usage(void);

int main(int argc,char *argv[])


{
int ch;
char domainname[MAXHOSTNAMELEN];
if(argc==2)
{
if((strcmp(argc[1],"--help")==0))
{
printf("\n\n\n\n domainname gives the domain name of the network\n\n\n");
exit(1);
}
}
while((ch=getopt(argc,argv,""))!=-1)
{
switch(ch)
{
default:
usage();
}
}

while((ch=getopt(argc,argv,""))!=-1)
{
switch(ch)
{
default:
usage();
}
}
argc-=optind;
argv+=optind;
if(argc>1)
usage();
if(*argv)
{
if(setdomainname(*argv,strlen(*argv)))
err(1,"setdomainname");
}
else
{
if(getdomainname(domainname,sizeof(domainname)))
err(1,"getdomainname");
(void)print("%s\n",domainname);
}
exit(0);
}
void usage(void)
{
(void)fprint("\nstderr, "usage:%s[name-of-domain]\n ",__progname");
exit(1);
}
age(void)
{
(void)fprintf("stderr, "usage:%s[name-of-domain]\n",__progname");
exit(1);
}
}
[user21@localhost ~]$ domainname -a
localhost
14.CUT.c---------------------------------------------------------------------------------
NAME
cut - remove sections from each line of files

SYNOPSIS
cut [OPTION]... [FILE]...

DESCRIPTION
Print selected parts of lines from each FILE to standard output.

Mandatory arguments to long options are mandatory for short options too.

-b, --bytes=LIST
select only these bytes

-c, --characters=LIST
select only these characters

-d, --delimiter=DELIM
use DELIM instead of TAB for field delimiter

-f, --fields=LIST
select only these fields; also print any line that contains no delimiter character,
unless the -s
option is specified

-n with -b: donât split multibyte characters

--complement
complement the set of selected bytes, characters or fields.

-s, --only-delimited
do not print lines not containing delimiters

--output-delimiter=STRING
use STRING as the output delimiter the default is to use the input delimiter

--help display this help and exit

--version
output version information and exit

Use one, and only one of -b, -c or -f. Each LIST is made up of one range, or many
ranges separated by
:
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<malloc.h>
void cut(int ,char *);
//void comp(char *);
void words(int ,char *);
void delimit(int ,char *,char *);
void menu();
int main(int argc,char *argv[])
{
int c=0;
char a;
if(argc==2)
{
if(strcmp(argv[1],"--help")==0)
{
menu();
exit(0);
}
}
else if(argc==3)
{
c=atoi(argv[1]);
cut(c,argv[2]);
}
else if(argc==4)
{
if(strcmp(argv[2],"-w")==0)
c=atoi(argv[1]);
words(c,argv[3]);
}
else if(argc==5)
{
if(strcmp(argv[2],"-d")==0)
{
c=atoi(argv[1]);
printf("hi dude \n ");
delimit(c,argv[3],argv[4]);
}
}
}
void cut(int b,char *fname)
{
FILE *ptr;
char ch;
int a=0,fl;
ptr=fopen(fname,"r");
while(1)
{
ch=fgetc(ptr);
a++;
if(ch==EOF)
break;
if(ch=='\n')
{
fl=0;a=0;
}
if(a==b)
{
fputc(ch,stdout);
printf("\n");
}
}
}

void words(int a,char *fname)


{
FILE *ptr;
char w;
int wd=1;
ptr=fopen(fname,"r");
while(1)
{
w=fgetc(ptr);
if(w=='\n')
{
wd=1;printf("\n");
}
if(w == ' ')
wd++;
if(w==EOF)
break;
if(a==wd)
{
fputc(w,stdout);
if(a==wd)
{
fputc(w,stdout);
}
}
}
void delimit(int r,char *de,char *fn)
{
FILE *ptr;
char d;
int cnt=1;
ptr=fopen(fn,"r");
while(1)
{
d=fgetc(ptr);
if(d=='\n')
cnt=1;
if(de[0]==d)
cnt++;
if(cnt==r)
{
if(d==de[0])
{
printf("\n");
}
else
fputc(d,stdout);
}
if(d==EOF)
break;

}
}
void menu()
{
printf("\n\n----------- cut-------- ------\n\n ");
printf("\n cut options----------------------- :");
printf("\n -d to view user defined delimiter ");
printf("\n -w to view words ");

}
15.TAIL.----------------------------------------------------------------------------------------------
NAME
tail - output the last part of files

SYNOPSIS
tail [OPTION]... [FILE]...

DESCRIPTION
Print the last 10 lines of each FILE to standard output. With more than one FILE,
precede each with a
header giving the file name. With no FILE, or when FILE is -, read standard input.

Mandatory arguments to long options are mandatory for short options too.

--retry
keep trying to open a file even if it is inaccessible when tail starts or if it
becomes inaccessi-
ble later; useful when following by name, i.e., with --follow=name

-c, --bytes=N
output the last N bytes

-f, --follow[={name|descriptor}]
output appended data as the file grows; -f, --follow, and --follow=descriptor are
equivalent

-F same as --follow=name --retry

-n, --lines=N
output the last N lines, instead of the last 10

--max-unchanged-stats=N
with --follow=name, reopen a FILE which has not changed size after N (default
5) iterations to see
if it has been unlinked or renamed (this is the usual case of rotated log files)

--pid=PID
with -f, terminate after process ID, PID dies

-q, --quiet, --silent


never output headers giving file names

-s, --sleep-interval=S
with -f, sleep for approximately S seconds (default 1.0) between iterations.

-v, --verbose
:
#include<stdio.h>
#include<stdlib.h>
int main(int argc,char *argv[])
{
char ch;
FILE *p;
int i=1,j=1,k;
if(argc==2)
{
if((p=fopen(argv[1],"r"))==NULL)
{
printf("The file %s doesnt exists ",argv[1]);
exit(0);
}
while((ch=fgetc(p))!=EOF)
if(ch=='\n')
i++;
k=(i-10);
rewind(p);
while((ch=fgetc(p))!=EOF)
{
if(ch=='\n')
j++;
if(j==k)
break;
}
while((ch=fgetc(p))!=EOF)
{
if(ch=='\n')
{
printf("\n");
j++;
}
printf("%c",ch);
}

else
printf("\nToo many arguments");
}
16.FILE.C-----------------------------------------------------------------------------------------------
----
NAME
file - determine file type

SYNOPSIS
file [ -bchikLnNprsvz ] [ -f namefile ] [ -F separator ] [ -m magicfiles ] file ...
file -C [ -m magicfile ]

DESCRIPTION
This manual page documents version 4.17 of the file command.

File tests each argument in an attempt to classify it. There are three sets of tests,
performed in this
order: filesystem tests, magic number tests, and language tests. The first test that
succeeds causes the
file type to be printed.

The type printed will usually contain one of the words text (the file contains only
printing characters
and a few common control characters and is probably safe to read on an ASCII
terminal), executable (the
file contains the result of compiling a program in a form understandable to some
UNIX kernel or another),
or data meaning anything else (data is usually âbinaryâ or non-printable).
Exceptions are well-known
file formats (core files, tar archives) that are known to contain binary data. When
modifying the file
/usr/share/file/magic or the program itself, preserve these keywords . People depend
on knowing that all
the readable files in a directory have the word ââtextââ printed. Donât do as
Berkeley did and change
ââshell commands textââ to ââshell scriptââ. Note that the file /usr/share/file/magic
is built mechani-
cally from a large number of small files in the subdirectory Magdir in the source
distribution of this
program.

The filesystem tests are based on examining the return from a stat(2) system call.
The program checks to
see if the file is empty, or if itâs some sort of special file. Any known file types
appropriate to the
system you are running on (sockets, symbolic links, or named pipes (FIFOs) on those
systems that imple-
ment them) are intuited if they are defined in the system header file <sys/stat.h>.

The magic number tests are used to check for files with data in particular fixed
formats. The canonical
example of this is a binary executable (compiled program) a.out file, whose format is
defined in a.out.h
and possibly exec.h in the standard include directory. These files have a âmagic
numberâ stored in a
particular place near the beginning of the file that tells the UNIX operating system
that the file is a
binary executable, and which of several types thereof. The concept of âmagic
numberâ has been applied by
extension to data files. Any file with some invariant identifier at a small fixed offset
into the file
can usually be described in this way. The information identifying these files is read
from the compiled
magic file /usr/share/file/magic.mgc , or /usr/share/file/magic if the compile file does
not exist. In
addition file will look in $HOME/.magic.mgc , or $HOME/.magic for magic entries.

If a file does not match any of the entries in the magic file, it is examined to see if it
seems to be a
:#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<stdlib.h>
int main(a,b)
int a;
char *b[];
{
int i;
struct stat statbuff;
char *ptr;
if(a<2)
{
printf("\n number of arguments are not enough ");
exit(0);
}
for(i=1;i<a;i++)
{
printf("\n%s",b[i]);
if((stat(b[i], &statbuff)) < 0)
{
printf("\n enter in stat system call ");
exit(0);
}
switch(statbuff.st_mode & S_IFMT)
{
case S_IFDIR :
ptr="Directory";
break;
case S_IFCHR :
ptr="character special file";
break;
case S_IFBLK :
ptr="Block special file";
break;
case S_IFREG :
ptr="Regular file";
break;
case S_IFIFO :
ptr="Fifo file";
break;
default :
ptr="unknownfile";
break;
}
}
printf ("\nFile Type is : %s\n ", ptr);
}
17.MYDIR.C---------------------------------------------------------------------------------
NAME
mkdir - make directories

SYNOPSIS
mkdir [OPTION] DIRECTORY...

DESCRIPTION
Create the DIRECTORY(ies), if they do not already exist.

Mandatory arguments to long options are mandatory for short options too.

-Z, --context=CONTEXT (SELinux) set security context to CONTEXT

-m, --mode=MODE
set permission mode (as in chmod), not rwxrwxrwx - umask

-p, --parents
no error if existing, make parent directories as needed

-v, --verbose
print a message for each created directory

--help display this help and exit

--version
output version information and exit

AUTHOR
Written by David MacKenzie.

REPORTING BUGS
Report bugs to <bug-coreutils@gnu.org>.

COPYRIGHT
Copyright © 2006 Free Software Foundation, Inc.
This is free software. You may redistribute copies of it under the terms of the
GNU General Public
License <http://www.gnu.org/licenses/gpl.html>. There is NO WARRANTY, to the
extent permitted by law.

SEE ALSO
The full documentation for mkdir is maintained as a Texinfo manual. If the info and
mkdir programs are
properly installed at your site, the command
:
#include<stdio.h>
#include<stdlib.h>
int main()
{
char dirname[14];
printf("\n enter the directory name to create and change: \n");
gets(dirname);
if((mkdir(dirname, "."))==-1)
{
printf("\n cannot create a directory!!! failed to create a directory ");
}
else
{
printf("\n Diectory created successfully");
}
}
18.TOUCH.c------------------------------------------------------------

NAME
touch - change file timestamps

SYNOPSIS
touch [OPTION]... FILE...

DESCRIPTION
Update the access and modification times of each FILE to the current time.

Mandatory arguments to long options are mandatory for short options too.

-a change only the access time

-c, --no-create
do not create any files

-d, --date=STRING
parse STRING and use it instead of current time

-f (ignored)
-m change only the modification time

-r, --reference=FILE
use this fileâs times instead of current time

-t STAMP
use [[CC]YY]MMDDhhmm[.ss] instead of current time

--time=WORD
change the specified time: WORD is access, atime, or use: equivalent to -a
WORD is modify or
mtime: equivalent to -m

--help display this help and exit

--version
output version information and exit

Note that the -d and -t options accept different time-date formats.

If a FILE is -, touch standard output.


:

#include<stdio.h>
#include<string.h>
#include<malloc.h>
#include<fcntl.h>
#include<unistd.h>
#include<sys/stat.h>
#include<sys/types.h>
void create(char *source);
void c();
void compare(char *arg,char *source);
void m(char *source);
void help();
struct stat statbuf;
int main(int argc,char *argv[])
{
if(argc==3)
{
compare(argv[1],argv[2]);
}
else if(argc==2)
{
argv[2]="e";
compare(argv[1],argv[2]);
}

}
void compare(char *arg,char *fn)
{
if(strcmp(arg,"-a")==0)
{
a(fn);
}
else if(strcmp(arg,"-c")==0)
{
c();
}
else if(strcmp(arg,"-m")==0)
{
m(fn);
}
else if(strcmp(arg,"--help")==0)
{
help();
}
else
{
create(arg);
}
}
void create(char *source)
{
int fd1;
printf("%s",source);
fd1=creat(source,O_APPEND);
chmod(source,0666);
if(fd1==-1)
{
printf("\n ERROR");
}
else
{
printf("\n File Created\n ");
}
}
void c()
{
printf("\n NO File Created");
}
void a(char *source)
{
create(source);
stat(source,&statbuf);
printf("\n Last Access Time: %s",ctime(&statbuf.st_atime));
}
void m(char *source)
{
create(source);
stat(source,&statbuf);
printf("\n Last Modified Time : %s",ctime(&statbuf.st_atime));
}
void help()
{
printf("\n -------Help For Touch Command-----------\n");
printf("---------OPTIONS-------------------------\n");
printf(" -a to change the access time \n ");
printf(" -m to view last modified time \n ");
printf(" -c no file created \n");
}
20.

You might also like