<html>

<head><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- MyFirstUnitAd -->
<ins class="adsbygoogle"
     style="display:inline-block;width:970px;height:250px"
     data-ad-client="ca-pub-5778386704669218"
     data-ad-slot="1503492166"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>Linux file operations</title>
</head>

<body>



<p align="left"><font size="6" color="#FF0000"><span lang="en-ca"><b>&nbsp; 
 
</b></span><b>&nbsp;&nbsp;&nbsp; <span lang="en-ca">&nbsp;&nbsp; </span></b></font>
<span lang="en-ca"><font size="6" color="#FF0000"><b>File searching, replacing, 
copying...</b></font></span></p>

<div align="left">
  <pre><b><font color="#ff0000" size="5">A. <span lang="en-ca">First </span>Edition</font></b></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>This is comp444 lab1 and it is concentrated on file operations such as searching, replacing, copying...</b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>I want to keep this simple version for possible future modification.</b></span></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">B</span>.<span lang="en-ca"><a name="problem"></a>The problem</span></font></b></pre>
</div>
<div align="left">
  <p ALIGN="LEFT"><span lang="en-ca">Open a file and search a word with starting 
  of your argument.</span></p>
  <b><font color="#ff0000" size="5"><span lang="en-ca"><a name="explain"></a>C</span>.<span lang="en-ca">The
  </span></font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>idea of 
  program</b></font></span></div>
<div align="left">
  　</div>
<p ALIGN="LEFT"><span lang="en-ca"><font size="2"><b>From point of view of 
compiler project, it is a toy scanner with much simplified functionality. But it 
is </b></font></span></p>
<p ALIGN="LEFT"><span lang="en-ca"><font size="2"><b>difficult for me to debug c 
in Linux. Another reason is the awkward C grammar. There is no &quot;bool&quot; type, 
there</b></font></span></p>
<p ALIGN="LEFT"><span lang="en-ca"><font size="2"><b>is no function overloading, 
there is no ... </b></font></span></p>
<div align="left">
  <pre><b><font color="#ff0000" size="5">D.<span lang="en-ca"><a name="Method"></a>The </span>major functions</font></b></pre>
</div>
<div align="left">
  <pre>　</pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">E</span>.</font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>Further improvement</b></font></span></pre>
</div>
<div align="left">
  <pre>　</pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">F</span>.</font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>File listing</b></font></span></pre>
</div>
<div align="left">
  <pre><font size="3"><b><span lang="en-ca">1. myhead.h</span></b></font></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><font size="3"><b>2</b></font></span><font size="3"><b><span lang="en-ca">. searchString.h</span></b></font></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><font size="3"><b>3</b></font></span><font size="3"><b><span lang="en-ca">. searchString.c</span></b></font></pre>
</div>
<div align="left">
  <pre><font size="3"><b><span lang="en-ca">4. main.c</span></b></font></pre>
</div>
<div align="left">
  <pre><font size="3"><b><span lang="en-ca">5. errHandle.</span></b></font><span lang="en-ca"><font size="3"><b>c</b></font></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><font size="3"><b>6</b></font></span><font size="3"><b><span lang="en-ca">. makefile</span></b></font></pre>
</div>
<div align="left">
  <pre>　</pre>
</div>
<div align="left">
  <pre>　</pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><font size="3" color="#FF0000"><b>file name: myhead.h</b></font></span></pre>
</div>
<pre>#ifndef MYHEAD_H
#define MYHEAD_H

#include &lt;sys/types.h&gt;
#include &lt;sys/stat.h&gt;
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;errno.h&gt;
#include &lt;dirent.h&gt;
#include &lt;unistd.h&gt;
#include &lt;string.h&gt;
#include &lt;fcntl.h&gt;

typedef int bool;

#define TRUE  1
#define FALSE 0

#define BuffSize   128

void errHandle(char* msg);

	
#endif
</pre>
<pre>　</pre>
<pre><span lang="en-ca"><font size="3" color="#FF0000"><b>file name: searchString.h</b></font></span></pre>
<pre>#ifndef SEARCHSTRING_H
#define SEARCHSTRING_H

#include &quot;myhead.h&quot;

struct FileBuf
{
	int fd;
	int size;
	int offset;
	int cur;
	char buf[128];
	//char buf[BuffSize];
}fileBuf;

typedef struct FileBuf* FilePtr;

int findString(char* fileName, char* str);

//define three mode and it is purely like DFA=Deterministic Finite Automaton
#define FindNextWord   	0
#define SkipTillNext	1
#define Comparing	2

//search in a file with my own file information struct FileBuf*, 
//target is the string for searching
//repeating call this function and return each location, 
//return -1 indicating end of file
int searchString(FilePtr stream, char* target);

//specify if it is white space char
//notice here, in C, there is no boolean type and I define it
//as alias of int in &quot;myhead.h&quot;
bool isBreak(char ch);

//my edition of getc
FilePtr openFile(int fd);

//a-z, A-Z
bool isChar(char ch);

//my edition of getc
char nextChar(struct FileBuf* stream);

#endif</pre>
<pre>　</pre>
<pre><span lang="en-ca"><font size="3" color="#FF0000"><b>file name: searchString.c</b></font></span></pre>
<pre>#include &quot;myhead.h&quot;
#include &quot;searchString.h&quot;


bool findString(char* fileName, char* str)
{
	int fd, n=0;
	FilePtr stream;
	bool result=FALSE;
	if ((fd=open(fileName, O_RDONLY))&lt;0)
	{
		errHandle(&quot;cannot open searching files\n&quot;);
	}
	if ((stream=openFile(fd))==NULL)
	{
		errHandle(&quot;openfile error\n&quot;);
	}
	while ((n=searchString(stream, str))!=-1)
	{
		result=TRUE;
		printf(&quot;test: find target at %d\n&quot;, n);
	}
	return result;
}


//my edition of &quot;fopen&quot;
FilePtr openFile(int fd)
{
	fileBuf.fd=fd;
	fileBuf.offset=0;
	if ((fileBuf.size=read(fd, fileBuf.buf, BuffSize))&lt;0)
	{
		return NULL;
	}
	fileBuf.cur=0;
	return &amp;fileBuf;
}

//getc
char nextChar(FilePtr stream)
{
	//if buff is fully read
	if (stream-&gt;cur &gt;= stream-&gt;size)
	{
		if (stream-&gt;size==0)//must be end of file
		{
			return -1;
			//return EOF;
		}
		//if erro happened during reading
		if ((stream-&gt;size=read(stream-&gt;fd, stream-&gt;buf, BuffSize))&lt;0)
		{
			errHandle(&quot;read error of file buf\n&quot;);
		}
		//reset cur
		stream-&gt;cur=0;
	}
	//offset is always inc for each call
	stream-&gt;offset++;
	return stream-&gt;buf[stream-&gt;cur++];
}
	
//what is white space
bool isBreak(char ch)
{
	return (ch==' '||ch=='\t'||ch=='\n');
}

bool isChar(char ch)
{
	return ((ch&gt;='A'&amp;&amp;ch&lt;='Z')||(ch&gt;='a'&amp;&amp;ch&lt;='z'));
}

//starting a particular file offset and search the target string
//if found return the file offset of target, else return -1
int searchString(FilePtr stream, char* target)
{
	int stringLength=strlen(target);
	char ch;
	int result=-1;//this is the starting offset of word
	//the length of word compared
	int length;
	int mode;
	length=0;
	mode=FindNextWord;//initial status
	//-1 means EOF
	while ((ch=nextChar(stream))!=-1)
	{
		switch (mode)
		{
		case FindNextWord:
			if (isChar(ch))
			{
				//check the very first 
				if (ch==target[0])
				{
					mode=Comparing;
					length=1;//need to compare with next
					result=stream-&gt;offset-1;//as it already passed it
				}
				else
				{
					mode=SkipTillNext;
				}				
			}
			//else keep going
			break;
		case SkipTillNext:
			if (isBreak(ch))
			{
				mode=FindNextWord;
			}
			break;				
		case Comparing:
			//it is a match
			if (ch==target[length])
			{
				//a match then keep going
				length++;
			        //a white space
                        	if (length==stringLength)
                        	{
                                	return result;
                        	}
			}
			else
			{
				//it means mis-match
				mode=SkipTillNext;
			}
			break;
		}//switch
	}//while					
	return -1;
}		
						

		
<span lang="en-ca"><font size="3" color="#FF0000"><b>file name: errHandle.c</b></font></span></pre>
<pre>#include &quot;myhead.h&quot;


void errHandle(char* msg)
{
	if (msg!=NULL)
	{
		perror(msg);
	}
	else
	{
		strerror(errno);
	}		
	exit(errno);
}
	</pre>
<pre><span lang="en-ca"><font size="3" color="#FF0000"><b>file name: makefile</b></font></span></pre>
<pre>all: searchString.o  main.o errHandle.o
	@echo &quot;make complete for searchString.o &quot;

errHandle.o : errHandle.c myhead.h
	@echo &quot;compiling errHanle module\n&quot;
	gcc -g -c errHandle.c -o errHandle.o

main.o : searchString.o main.c errHandle.o
	gcc -g main.c searchString.o errHandle.o -o main.o
	@echo &quot;compiling main.o ...&quot; 

searchString.o : searchString.h searchString.c myhead.h
	gcc -g -c searchString.c -o searchString.o
	@echo &quot;compiling searchString.o as searchString.c is modified...&quot;

</pre>
<pre>　</pre>
<pre>
</pre>
<pre></pre>
<pre></pre>
<pre></pre>
<pre><span lang="en-ca"><font color="#0000FF"><b>How to run?</b></font></span></pre>
<pre><span lang="en-ca"><font color="#0000FF"><b>1. You need the file name as argument in command line</b></font></span></pre>

<pre>[qingz_hu@alamanni ~/lab1] % ./main.o test.txt
test: find target at 0
test: find target at 39
test: find target at 92
test: find target at 127
test: find target at 334
ok
</pre>

<pre></pre>

<pre></pre>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                                   
&nbsp;&nbsp;&nbsp; <a href="PocketRuler.htm">                  







                       <img src="picture/back.gif" style="border: medium none" alt="back.gif (341 bytes)" width="32" height="35"></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
<a href="index.htm">
<img src="picture/up.gif" style="border: medium none" alt="up.gif (335 bytes)" width="35" height="32"></a>       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                         
<img src="picture/next.gif" style="border: medium none" alt="next.gif (337 bytes)" width="32" height="35">          


</p>

</body>

</html>