...My Shell...
I am using Ubuntu 11.04 Natty Narwhal linux version. And this blog is about
How to add user function to our shell so that we can use our command in terminal.
- A "shell" is software that provides an interface for users of an operating system to access the services of a kernel.
- The name shell
originates from shells being an outer layer of interface between the
user and the internals of the operating system (the kernel).
Now , I have shared one program which can use to add comment(at the starting line) in the program without open your C code.
- Its useful when you have done many programs but not added any comments in your program.
-that, what this program going to do.
Step-01
To see what shell you have :
command : #echo $SHELL
To see the path from which your shell commands executable file stored :
command : #echo $PATH
(Notes :- Now when we type any command on terminal, than that command is having some program to do target process.
Like : #cp filename.c /home
-this command check program of cp from system, and run that program so
that filename.c will be copied to given path.
-Now from where it check CP programs executable file??? that path is saved in PATH environment.)
You can add your programs executable file here. in any of the path. (I suggest last path (/usr/game) as it is having lowest priority.)
Step-02
(optional)
-This is extra step if you want to add your own path in PATH environment.
(but its temporary path adding till you close the terminal.)
-if you want to make your own path than you first add that path in PATH environment by
command : #export PATH=$PATH:(add your path)
As in below I have added /home/my_shell path to PATH, check export command in image..>>
-You can see that new added path by giving again
command : #echo $PATH
Step-03
-Add your executable file in that path.
-Now, instead of using command for executable
as : #./cmnt
you can directly give command without "./"
# program_01 (comment.c)
//***************************************************
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4
5 // Set heading line to your code.
6 char *heading = "\n//Commenter 1.2 by Ratnesh Zinzuwadia\n";
7 void init_file(char *,FILE *,char *,char *);
8 void get_comment(FILE *);
9 void copy_file(FILE *,FILE *);
10 void clean_file(char *);
11
12 int main( int argc, char *argv[])
13 {
14 FILE *in_file, *out_file;
15 int c;
16 char * s;
17 int status;
18 pid_t pid1;
19
20 if( argc == 2 )
21 {
22 pid1 = fork ();
23 if(pid1 == 0)
24 {
25 in_file = fopen( argv[1], "r");
26 if( in_file == NULL )
27 printf("Cannot open %s for reading\n", argv[1]);
28 else
29 {
30 out_file = fopen( "temp.c", "w+");
31 if ( out_file == NULL )
32 printf("Cannot open %s for writing\n", argv[2]);
33 else
34 {
35 //initialize first comment line
36 init_file(heading,out_file,argv[1],argv[2]);
37
38 //get comment to add from user
39 get_comment(out_file);
40 //copy in_file to out_file
41 copy_file(in_file,out_file);
42
43 fclose(in_file);
44 in_file = fopen(argv[1],"w+");
45 rewind(out_file);
46 //fclose( out_file);
47 //out_file = fopen ("temp.c","r");
48 while ((c=getc(out_file)) != EOF )
49 putc( c, in_file );
50 fclose( out_file);
51 fclose( in_file);
52 clean_file("temp.c");
53 exit(5);
54 }
55 }
56 }
57 else if(pid1 > 0)
58 {
59 waitpid(-1,&status,NULL);
60 //printf("I am in parent.\n");
61 }
62 }
63 //**********************************************
64 else if(argc == 3)
65 {
66 pid1 = fork ();
67 if(pid1 == 0)
68 {
69 in_file = fopen( argv[1], "r");
70 if( in_file == NULL )
71 printf("Cannot open %s for reading\n", argv[1]);
72 else
73 {
74 out_file = fopen( argv[2], "w+");
75 if ( out_file == NULL )
76 printf("Cannot open %s for writing\n", argv[2]);
77 else
78 {
79 //initialize first comment line
80 init_file(heading,out_file,argv[1],argv[2]);
81 //get comment to add from user
82 get_comment(out_file);
83 //copy in_file to out_file
84 copy_file(in_file,out_file);
85 fclose(in_file);
86 fclose( out_file);
87 }
88 }
89 exit(0);
90 }
91 else if(pid1 > 0)
92 {
93 waitpid(-1,&status,NULL);
94 //printf("I am in parent.\n");
95 }
96
97 }
98 else
99 {
100 printf("Incorrect argument,2 or 3 argument should passed\n");
101 exit(2);
102 }
103
104 exit(0);
105 }
106
107 void init_file(char *s,FILE *out_file,char *argv,char *argv2)
108 {
109 int c;
110 //printf("File copy program, copying %s to %s\n", argv, argv2);
111 while ((c = *s++) != '\0')
112 putc(c,out_file);
113 }
114
115 void get_comment(FILE *out_file)
116 {
117 int c;
118 printf("Enter comment : ");
119 c = '/';
120 putc(c,out_file);
121 putc(c,out_file);
122 while ((c = getchar()) != '\n')
123 putc(c,out_file);
124 putc('\n',out_file);
125 }
126
127 void copy_file(FILE *in_file,FILE *out_file)
128 {
129 int c;
130 while ((c=getc( in_file) ) != EOF )
131 putc( c, out_file );
132 //printf("File has been copied.\n");
133 }
134
135 void clean_file(char *del)
136 {
137 pid_t pid2;
138 pid2 = fork();
139 if(pid2 == 0)
140 {
141 execlp("rm","rm",del,NULL);
142 perror("File is not cleaned\n");
143 exit(3);
144 }
145 else if (pid2 > 0)
146 {
147 int status;
148 waitpid(pid2,&status,NULL);
149 //printf(">>>> %s removed\n",del);
150 }
151 else
152 {
153 fprintf(stdout,"Fork error.\n");
154 }
155 }
//***************************************************
SYNTEX
(1)
#cmnt filename.c
-press enter
-now type comment what you want to add in filename.c
(2)
#cmnt filename1.c filename2.c
-press enter
-now type comment in filename2.c
commented file is copied in filename2.c. (filename1.c will be unchanged.)
-Compile This program by
command : #gcc -Wall comment.c -o cmnt
-Copy cmnt executable file in to (/use/games OR your own path) directory.
command : #cp cmnt /usr/games
>>>> Yeah, its done now...:-)
you can use cmnt command to add any comment in your .c,.cpp programs.
********************Comment your view and idea on this page...:-))*********************