From 8aadaa7e9396be72cbc26975611a7edf1197e3e0 Mon Sep 17 00:00:00 2001 From: hades Date: Thu, 18 May 2000 16:28:47 +0000 Subject: [PATCH] New Functions for taping used by the event builde, first guess (just compiles seperately). --- hadaq/hwtape.c | 113 ++++++++++++++++++++++++++++ hadaq/hwtape.h | 9 +++ hadaq/hwtapeal.c | 163 ++++++++++++++++++++++++++++++++++++++++ hadaq/hwtapeal.h | 22 ++++++ hadaq/hwtapeansichar.c | 165 +++++++++++++++++++++++++++++++++++++++++ hadaq/hwtapeansichar.h | 7 ++ hadaq/hwtapeunixchar.c | 22 ++++++ hadaq/hwtapeunixchar.h | 7 ++ hadaq/hwtapevol.c | 72 ++++++++++++++++++ hadaq/hwtapevol.h | 11 +++ 10 files changed, 591 insertions(+) create mode 100644 hadaq/hwtape.c create mode 100644 hadaq/hwtape.h create mode 100644 hadaq/hwtapeal.c create mode 100644 hadaq/hwtapeal.h create mode 100644 hadaq/hwtapeansichar.c create mode 100644 hadaq/hwtapeansichar.h create mode 100644 hadaq/hwtapeunixchar.c create mode 100644 hadaq/hwtapeunixchar.h create mode 100644 hadaq/hwtapevol.c create mode 100644 hadaq/hwtapevol.h diff --git a/hadaq/hwtape.c b/hadaq/hwtape.c new file mode 100644 index 0000000..23c923c --- /dev/null +++ b/hadaq/hwtape.c @@ -0,0 +1,113 @@ +/* For fdopen(), fclose(), sscanf(), printf() */ +#include + +/* For allocMem(), freeMem() */ +#include + +/* For msglog() */ +#include +#include + +/* For open() */ +#include +#include +#include + +/* For read(), close() */ +#include + +/* For st */ +#include +#include + +#include "hwtape.h" +#include "hwtapeal.h" +#include "hwtapevol.h" + +static FILE *openTape(const char* filename) { + int tape; + int fileSeqNum; + int stat; + int i = 0; + + char *vol; + char *label; + + FILE *outFile; + + struct mtop *eom, *sbm, *rewind; + + tape = open(filename, O_RDWR); + if(tape == -1) { + msglog(LOG_ERR, "Could not open tape!\n"); + exit(-2); + } + + eom->mt_op = MTEOM; + eom->mt_count = 1; + stat = ioctl(tape, MTIOCTOP, &eom); + if (stat == -1) { + msglog(LOG_ERR, "Could not go to the eon of the tape!\n"); + exit(-2); + } + + sbm->mt_op = MTBSF; + sbm->mt_count = 2; + stat = ioctl(tape, MTIOCTOP, &sbm); + if (stat == -1) { + + rewind->mt_op = MTREW; + rewind->mt_count = 1; + stat = ioctl(tape, MTIOCTOP, &rewind); + if (stat == -1) { + msglog(LOG_ERR, "Could not rewind tape!\n"); + exit(-2); + } + + vol = readVolumeLabel(tape); + + stat = ioctl(tape, MTIOCTOP, &rewind); + if (stat == -1) { + msglog(LOG_ERR, "Could not rewind tape!\n"); + exit(-2); + } + + writeVolumeLabel(vol, tape); + fileSeqNum = 1; + } else { + label = allocMem(80*sizeof(char)); + do { + stat = read(tape, label, 80); + if (i == 0) { + sscanf(label, "%*31c%4d", &fileSeqNum); + printf("File Sequence Number: %04i\n", fileSeqNum); + } + i++; + } while (stat != -1); + freeMem(label); + } + + writeHeader(tape, fileSeqNum, filename); + + outFile = fdopen(tape, "wb"); +} + +int closeTape(FILE *openTape, int tape, int numBlocks, const char *filename) { + int stat; + stat = fclose(openTape); + if (stat == -1) { + msglog(LOG_ERR, "Could not close file on tape!\n"); + exit(-2); + } + + writeTrailer(tape, 0, numBlocks, filename); + + stat = close(tape); + if (stat == -1) { + msglog(LOG_ERR, "Could not close tape!\n"); + exit(-2); + } + + return 0; +} + diff --git a/hadaq/hwtape.h b/hadaq/hwtape.h new file mode 100644 index 0000000..8cf0ae6 --- /dev/null +++ b/hadaq/hwtape.h @@ -0,0 +1,9 @@ +#ifndef HWTAPE_H +#define HWTAPE_H + +static FILE *openTape(const char *); + +int closeTape(FILE *, int, int, const char *); + +#endif + diff --git a/hadaq/hwtapeal.c b/hadaq/hwtapeal.c new file mode 100644 index 0000000..267480d --- /dev/null +++ b/hadaq/hwtapeal.c @@ -0,0 +1,163 @@ +/* For msglog() */ +#include +#include + +/* For time() */ +#include + +/* For gethostname() */ +#include + +/* For getpwent() */ +#include +#include + +/* For getuid() */ +#include +#include + +#include "hwtapeal.h" + +int writeHeader(int tape, int fileSeqNum, const char *filename) { + int stat; + int i; + int blockSize = BLOCKSIZE; + char *hostname; + char *creaCent; + char *label; +#ifdef LINUX + const char *impId = "LINUXHADAQ023"; +#endif +#ifdef UNIX + const char *impId = "UNIXHADAQ0023"; +#endif +#ifndef LINUX + #ifndef UNIX + const char *impId = " HADAQ0023"; + #endif +#endif + uid_t uid, first_uid; + time_t tim; + struct passwd *pwentry; + struct tm *cd; + + gethostname(hostname, 20); + + tim = time(0); + cd = gmtime(&tim); + if ((cd->tm_year)/100 == 19) { + *creaCent = ' '; + } else { + *creaCent = '0'; + } + + label = allocMem(80*sizeof(char)); + + sprintf(label, "HDR1 %s0000010001%04d000100%s%02d%03d 99366 000000%s ", ansichar(filename, 17), fileSeqNum, creaCent, (cd->tm_year)%100, cd->tm_yday, impId); + stat = write(tape, label, 80); + if(stat == -1) { + msglog(LOG_ERR, "Could not write ANSI label on tape!\n"); + exit(-3); + } + + sprintf(label, "HDR2F%05d%05d010664 bin 33000 ", blockSize, blockSize); + stat = write(tape, label, 80); + if(stat == -1) { + msglog(LOG_ERR, "Could not write ANSI label on tape!\n"); + exit(-3); + } + + uid = getuid(); + pwentry = getpwent(); + first_uid = pwentry->pw_uid; + do { + pwentry = getpwent(); + } while (pwentry->pw_uid != uid && pwentry->pw_uid != first_uid); + + if (pwentry->pw_name == "root") { + pwentry->pw_name = "hades"; + } + + sprintf(label, "HDR3%010d%s%s%s", tim, unixchar(pwentry->pw_name, 10), unixchar(hostname, 20), unixchar(filename, 36)); + stat = write(tape, label, 80); + if(stat == -1) { + msglog(LOG_ERR, "Could not write ANSI label on tape!\n"); + exit(-3); + } + + freeMem(label); + return 0; +} + + +int writeTrailer(int tape, int fileSeqNum, int numBlocks, const char *filename) { + int stat; + int i; + int blockSize = BLOCKSIZE; + char *hostname; + char *creaCent; + char *label; +#ifdef LINUX + const char *impId = "LINUXHADAQ023"; +#endif +#ifdef UNIX + const char *impId = "UNIXHADAQ0023"; +#endif +#ifndef LINUX + #ifndef UNIX + const char *impId = " HADAQ0023"; + #endif +#endif + uid_t uid, first_uid; + time_t tim; + struct passwd *pwentry; + struct tm *cd; + + gethostname(hostname, 20); + + tim = time(0); + cd = gmtime(&tim); + if ((cd->tm_year)/100 == 19) { + *creaCent = ' '; + } else { + *creaCent = '0'; + } + + label = allocMem(80*sizeof(char)); + + sprintf(label, "HDR1 %s0000010001%04d000100%s%02d%03d 99366 %06d%s ", ansichar(filename, 17), fileSeqNum, creaCent, (cd->tm_year)%100, cd->tm_yday, numBlocks, impId); + stat = write(tape, label, 80); + if(stat == -1) { + msglog(LOG_ERR, "Could not write ANSI label on tape!\n"); + exit(-3); + } + + sprintf(label, "HDR2F%05d%05d010664 bin %010d33000 ", blockSize, blockSize, numBlocks*blockSize); + stat = write(tape, label, 80); + if(stat == -1) { + msglog(LOG_ERR, "Could not write ANSI label on tape!\n"); + exit(-3); + } + + uid = getuid(); + pwentry = getpwent(); + first_uid = pwentry->pw_uid; + do { + pwentry = getpwent(); + } while (pwentry->pw_uid != uid && pwentry->pw_uid != first_uid); + + if (pwentry->pw_name == "root") { + pwentry->pw_name = "hades"; + } + + sprintf(label, "HDR3%010d%s%s%s", tim, unixchar(pwentry->pw_name, 10), unixchar(hostname, 20), unixchar(filename, 36)); + stat = write(tape, label, 80); + if(stat == -1) { + msglog(LOG_ERR, "Could not write ANSI label on tape!\n"); + exit(-3); + } + + freeMem(label); + return 0; +} + diff --git a/hadaq/hwtapeal.h b/hadaq/hwtapeal.h new file mode 100644 index 0000000..233472b --- /dev/null +++ b/hadaq/hwtapeal.h @@ -0,0 +1,22 @@ +#ifndef AL_H +#define AL_H + +/* For write() */ +#include + +/* For gethostname() */ +#include + +/* For stat() */ +#include +#include + +#include "hwtapeansichar.h" + +#define BLOCKSIZE 8192 + +int writeHeader(int, int, const char *); +int writeTrailer(int, int, int, const char *); + +#endif + diff --git a/hadaq/hwtapeansichar.c b/hadaq/hwtapeansichar.c new file mode 100644 index 0000000..59b81fb --- /dev/null +++ b/hadaq/hwtapeansichar.c @@ -0,0 +1,165 @@ +#include "hwtapeansichar.h" + +const char *ansichar(const char *s, int length) { + char *r; + int i, end; + end = 0; + *r = *s; + for(i=0 ; i'): + case ('?'): + break; + case ('a'): + r[i] = 'A'; + break; + case ('b'): + r[i] = 'B'; + break; + case ('c'): + r[i] = 'C'; + break; + case ('d'): + r[i] = 'D'; + break; + case ('e'): + r[i] = 'E'; + break; + case ('f'): + r[i] = 'F'; + break; + case ('g'): + r[i] = 'G'; + break; + case ('h'): + r[i] = 'H'; + break; + case ('i'): + r[i] = 'I'; + break; + case ('j'): + r[i] = 'J'; + break; + case ('k'): + r[i] = 'K'; + break; + case ('l'): + r[i] = 'L'; + break; + case ('m'): + r[i] = 'M'; + break; + case ('n'): + r[i] = 'N'; + break; + case ('o'): + r[i] = 'O'; + break; + case ('p'): + r[i] = 'P'; + break; + case ('q'): + r[i] = 'Q'; + break; + case ('r'): + r[i] = 'R'; + break; + case ('s'): + r[i] = 'S'; + break; + case ('t'): + r[i] = 'T'; + break; + case ('u'): + r[i] = 'U'; + break; + case ('v'): + r[i] = 'V'; + break; + case ('w'): + r[i] = 'W'; + break; + case ('x'): + r[i] = 'X'; + break; + case ('y'): + r[i] = 'Y'; + break; + case ('z'): + r[i] = 'Z'; + break; + case ('\0'): + r[i] = ' '; + end = 1; + break; + default: + r[i] = 'Z'; + break; + } + } else { + r[i] = ' '; + } + } + r[length] = '\0'; + + return r; +} + diff --git a/hadaq/hwtapeansichar.h b/hadaq/hwtapeansichar.h new file mode 100644 index 0000000..b013ad4 --- /dev/null +++ b/hadaq/hwtapeansichar.h @@ -0,0 +1,7 @@ +#ifndef HWTAPEANSICHAR_H +#define HWTAPEANSICHAR_H + +const char *ansichar(const char *, int); + +#endif + diff --git a/hadaq/hwtapeunixchar.c b/hadaq/hwtapeunixchar.c new file mode 100644 index 0000000..01ae504 --- /dev/null +++ b/hadaq/hwtapeunixchar.c @@ -0,0 +1,22 @@ +#include "hwtapeunixchar.h" + +const char *unixchar(const char *s, int length) { + char *r; + int i, end; + *r = *s; + end = 0; + for(i=0 ; i + +/* For allocMem(), freeMem() */ +#include + +/* For msglog() */ +#include +#include + +/* For memcpy() */ +#include + +#include "hwtapevol.h" + +char *readVolumeLabel(int tape) { + char *vol; + int stat; + vol = allocMem(80*sizeof(char)); + stat = read(tape, vol, 80); + if(stat == -1) { + msglog(LOG_ERR, "Could not read volume label from tape!\n"); + exit(-3); + } + return vol; + freeMem(vol); +} + +int writeVolumeLabel(const char *vol, int tape) { + char *v; + int stat; + v = allocMem(80*sizeof(char)); + memcpy(v, vol, 80); + stat = write(tape, v, 80); + if(stat == -1) { + msglog(LOG_ERR, "Could not write volume label to tape!\n"); + exit(-3); + } + freeMem(v); + return 0; +} + +int writeNewVolumeLabel(const char *volId, const char *ownerId, int tape) { + char *vol; + int stat; + +#ifdef LINUX + const char *impId = "LINUXHADAQ023"; +#endif +#ifdef UNIX + const char *impId = "UNIXHADAQ0023"; +#endif +#ifndef LINUX +#ifndef UNIX + const char *impId = " HADAQ0023"; +#endif +#endif + + vol = allocMem(80*sizeof(char)); + + sprintf(vol, "VOL1HADAQ %s%s 4", impId, ansichar(ownerId, 14)); + + stat = write(tape, vol, 80); + if(stat == -1) { + msglog(LOG_ERR, "Could not write volume label to tape!\n"); + exit(-3); + } + freeMem(vol); + + return 0; +} + diff --git a/hadaq/hwtapevol.h b/hadaq/hwtapevol.h new file mode 100644 index 0000000..5dc942d --- /dev/null +++ b/hadaq/hwtapevol.h @@ -0,0 +1,11 @@ +#ifndef HWTAPEVOL_H +#define HWTAPEVOL_H + +char *readVolumeLabel(int); + +int writeVolumeLabel(const char *, int); + +int writeNewVolumeLabel(const char *, const char *, int); + +#endif + -- 2.43.0