Question: #include #include #include #include #include #include #include #include amap.h #include aux.h / * scanner.c - process directories / files , counting words in

#include
#include
#include
#include
#include
#include
#include
#include "amap.h"
#include "aux.h"
/* scanner.c - process directories/files, counting words in each
* regular text file. Accumulate counts in the amap_t, then pass them
* along to reducers via the reducer pipes.
* Each reducer collects counts for a portion of the alphabet.
* Which reducer pipe a (word,count) pair should be written to is
* determined by the first letter of word, using the "whichpipe" global.
* The definition of a "word" is a contiguous sequence of alphabetic
* characters - no numbers, whitespace, or punctuation.
*/
#define FTYPE_REG 1
#define FTYPE_DIR 2
#define FTYPE_SKIP 3
#define SAMPLESIZE 16
#define NAMESIZE 1024
/* Map of characters to reducer pipe #s. Init'ed in driver once for all. */
extern int whichpipe[ALPHABETLEN];
/* what kind of file is this? Return value indicates type via constants above.
* If it's a text file to be processed, the file descriptor is passed via
* the "fd" out parameter.
*/
static int check_type(char *name, int *fd){
struct stat finfo;
int rv, fildes;
char buf[SAMPLESIZE];
rv = stat(name,&finfo);
if (rv <0){
return FTYPE_SKIP;
}
if ((finfo.st_mode & S_IFMT)== S_IFREG){
fildes = open(name,O_RDONLY);
if (fildes <0)
return FTYPE_SKIP;
/* Read a few characters to try to get type */
if ((rv=read(fildes,buf,SAMPLESIZE))<0)
return FTYPE_SKIP;
if ((buf[0]==0x7f &&
buf[1]=='E' &&
buf[2]=='L' &&
buf[3]=='F')// executable
||
(buf[0]=='G' &&
buf[1]=='I' &&
buf[2]=='F')// image
||
(buf[0]==0x89 &&
buf[1]=='P' &&
buf[2]=='N' &&
buf[3]=='G')// image
||
(buf[0]=='%' &&
buf[1]=='P' &&
buf[2]=='D' &&
buf[3]=='F')){// PDF
close(fildes);
return FTYPE_SKIP;
}
for (int i=0; i

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!