#include "apasrch_sm.h" main() { FILE *fp; int shmid; /* shared memory id */ char line[256]; /* line buffer */ char fsrc[256]; /* source file made by '\ls -FCs1' */ DATABUF *memptr; /* data buffer address at shared memory */ int maxfs; /* the longest file name */ int i, cnt; int fsize; /* file size in kb */ char fname[256]; /* file name */ /* check whether shared memory segment exists * * if so, then clear the shared memory segment */ if ((shmid = shmget(SHMKEY, 0, 0444|SHM_RDONLY)) >= 0) { shmctl(shmid, IPC_RMID, (struct shmid_ds *)0); } /* create shared memory segment */ if ((shmid = shmget(SHMKEY, SM_SEG_SIZE, 0644|IPC_CREAT|IPC_EXCL)) < 0) fatal("shmget - shared memory segment may already exists"); /* attach shared memory segment */ if ((memptr = (DATABUF *) shmat(shmid, 0, 0)) == ERR) fatal("shmat"); /* read file names and sizes from source records */ cnt = 0; maxfs = 0; /* load the data into the shared memory region */ for (i=1; i <= MAXARCHNUM; i++) { /* open source file name correspoding to the archive */ sprintf(fsrc, "%s/%s%d", DIR_ROOT, FNAME_ROOT, i); if ((fp = fopen(fsrc, "r")) == NULL) fatal("fopen"); /* remove the first line : "total xxxxx" */ fgets(line, 256, fp); /* dump record to the shared memory */ while (fgets(line, 256, fp)) { sscanf(line, "%d %s", &fsize, fname); if ( strstr(fname, ".txt") || strstr(fname, ".TXT") || \ strstr(fname, ".html") || strstr(fname, ".HTML") ) { ; } else { (memptr+cnt)->an = cnt; (memptr+cnt)->arch = i + 48; /* ASCII '0' = 48 */ (memptr+cnt)->size = fsize; strcpy((memptr+cnt)->name, fname); if ( maxfs < strlen((memptr+cnt)->name) ) maxfs = strlen( (memptr+cnt)->name ); cnt++; } } fclose(fp); } /* set the end, both 'an' and 'arch' as -1 */ (memptr+cnt)->an = -1; (memptr+cnt)->arch = -1; fprintf(stderr, "\ttotal %d image info were read\n", cnt); fprintf(stderr, "\tmaximum length of file name is %d\n", maxfs); /* dettach shared memory segment */ if (shmdt((char *) memptr) < 0) fatal("shmat"); exit(0); }