2 * ----------------------------------------------------------------------------
3 * "THE BEER-WARE LICENSE" (Revision 42):
4 * <phk@FreeBSD.org> wrote this file. As long as you retain this notice you
5 * can do whatever you want with this stuff. If we meet some day, and you think
6 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
7 * ----------------------------------------------------------------------------
15 /*---------------------------------------------------------------------------*/
17 Fatal_(int ln, char *fn, char *kind)
20 fprintf(stderr,"Fatal error. (%s:%d)\n",fn,ln);
21 fprintf(stderr,"%s Fatal error: %s\n",FileName, kind);
23 #define Fatal(foo) Fatal_(__LINE__,__FILE__,foo)
24 #define Assert() Fatal_(__LINE__,__FILE__,"Assert failed.")
26 /*---------------------------------------------------------------------------*/
27 /* get next field, check that the terminating whitespace is what we expect */
29 Ffield(FILE *fd, MD5_CTX *ctx,u_char term)
31 static u_char buf[BUFSIZ];
35 if((i=getc(fd)) == EOF) {
36 Fatal("Truncated patch.");
43 Fatal("Corrupt patch.");
44 printf("Token is too long.\n");
50 if(buf[l-1] != term) {
51 Fatal("Corrupt patch.");
52 fprintf(stderr,"Expected \"%s\" but didn't find it {%02x}.\n",
53 term == '\n' ? "\\n" : " ",buf[l-1]);
55 fprintf(stderr,"{%s}\n",buf);
60 fprintf(stderr,"<%s>\n",buf);
65 Fbytecnt(FILE *fd, MD5_CTX *ctx, u_char term)
70 p = Ffield(fd,ctx,term);
74 Fatal("Bytecount contains non-digit.");
78 u_chars += (*q - '0');
80 if(u_chars > MAXSIZE) {
81 Fatal("Bytecount too large.");
88 Fdata(FILE *fd, int u_chars, MD5_CTX *ctx)
90 u_char *p = Malloc(u_chars+1);
92 if(u_chars+1 != fread(p,1,u_chars+1,fd)) {
93 Fatal("Truncated patch.");
96 MD5Update(ctx,p,u_chars+1);
97 if(p[u_chars] != '\n') {
99 printf("FileData wasn't followed by a newline.\n");
100 Fatal("Corrupt patch.");
107 /*---------------------------------------------------------------------------*/
108 /* get the filename in the next field, prepend BaseDir and give back the result
109 strings. The sustitute filename is return (the one with the suffix SUBSUFF)
110 if it exists and the qualifier contains CTM_Q_Name_Subst
111 NOTA: Buffer is already initialize with BaseDir, CatPtr is the insertion
112 point on this buffer + the length test in Ffield() is enough for Fname() */
115 Fname(FILE *fd, MD5_CTX *ctx,u_char term,int qual, int verbose)
120 if ((p = Ffield(fd,ctx,term)) == NULL) return(NULL);
124 if (!(qual & CTM_Q_Name_Subst)) return(Buffer);
126 p = Buffer + strlen(Buffer);
128 strcat(Buffer, SUBSUFF);
130 if ( -1 == stat(Buffer, &st) ) {
134 fprintf(stderr,"Using %s as substitute file\n", Buffer);