Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dtudcfonted / libfuty / oakfuty.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $XConsortium: oakfuty.c /main/12 1996/11/11 10:55:57 drk $ */
24 /*
25  *  (c) Copyright 1995 FUJITSU LIMITED
26  *  This is source code modified by FUJITSU LIMITED under the Joint
27  *  Development Agreement for the CDEnext PST.
28  *  This is unpublished proprietary source code of FUJITSU LIMITED
29  */
30
31
32 #include  <sys/types.h>
33 #include  <stdio.h>
34 #include  <stdlib.h>
35 #include  <string.h>
36 #include  <errno.h>
37 #ifndef SVR4
38 #if !defined( SYSV )
39 #include <sys/resource.h>
40 #endif
41 #ifdef __osf__
42 #define _BSD
43 #endif
44 #include  <sys/wait.h>
45 #ifdef __osf__
46 #undef _BSD
47 #endif
48 #else
49 #include  <sys/mman.h>
50 #include  <wait.h>
51 #endif
52
53 #include  <unistd.h>
54 #include  <sys/stat.h>
55 #include  <signal.h>
56 #include  <fcntl.h>
57
58 #include  "bdfgpf.h"
59 #include  "udcutil.h"
60
61 static  void    exline();
62
63 char    *oakgtobdf;
64 char    *bdftosnf = BDFTOSNF;
65 char    *bdftopcf;
66
67 static  int     put_file_create_err_msg = 0;
68
69 #define DEVICE_FAIL             2
70 #define WRITE_FAIL              3
71 #define OPEN_FAIL               4
72 #define READ_FAIL               5
73
74 int
75 #if NeedFunctionPrototypes
76 ReadBdfHeader(
77     struct btophead *head,
78     char        *buf
79 )
80 #else
81 ReadBdfHeader( head, buf )
82 struct btophead *head;
83 char    *buf;
84 #endif
85 {
86         char *p;
87         unsigned int    getstat = 0;
88
89         fgets(buf, BUFSIZE, head->input);
90         p = buf;
91         SCAN_TO_NONSP(p)
92
93         if (strncmp(p, STARTFONT, STARTFONTsz)) {
94                 return(BDF_INVAL);
95         }
96
97         while( 1 ) {
98                 if (fgets(buf, BUFSIZE, head->input) == NULL) {
99                         return (BDF_INVAL);
100                 }
101                 p = buf;
102                 SCAN_TO_NONSP(p)
103                     if (!strncmp(p, SIZE, SIZEsz)) {
104                         if ((sscanf(p, "SIZE %f%d",
105                             &(head->bdf_point), &(head->bdf_xdpi))) != 2) {
106                                 return(BDF_INVAL);
107                         }
108                         getstat |= 0x01;
109                 } else if (!strncmp(p, FONTBOUNDINGBOX, FONTBOUNDINGBOXsz)) {
110                         if (sscanf(p, "FONTBOUNDINGBOX %d%d%d%d",
111                                 &(head->bdf_width), &(head->bdf_height), 
112                                 &(head->bdf_x), &(head->bdf_y)) != 4) {
113                                 return  BDF_INVAL;
114                         }
115                         getstat |= 0x02;
116
117                 } else if (!strncmp(p, CHARS, CHARSsz)) {
118                         if ((sscanf(p, "CHARS %d", &(head->num_chars))) != 1) {
119                                 return(BDF_INVAL);
120                         }
121                         getstat |= 0x04;
122                 } else if (!strncmp(p, STARTCHAR, STARTCHARsz)) {
123                         break;
124                 }
125         }
126
127         if (getstat != 0x07) {
128                 return(BDF_INVAL);
129         }
130         return  0;
131 }
132
133
134 int
135 #if NeedFunctionPrototypes
136 ReadGpfHeader(
137     struct ptobhead *head,
138     char   *buf
139 )
140 #else
141 ReadGpfHeader(head, buf)
142 struct ptobhead *head;
143 char   *buf;
144 #endif
145 {
146         char *p;
147         unsigned int  getstat = 0;
148
149         while(1) {
150                 if (getstat == 0x07) break;
151
152                 if (fgets(buf, BUFSIZE, head->input) == NULL) {
153                         return  GPF_INVAL;
154                 }
155                 p = buf;
156                 SCAN_TO_NONSP(p)
157
158                     if (!strncmp(p, NUMFONTS, NUMFONTSsz)) {
159                         if (sscanf(p, "numfonts:%d", &(head->num_chars)) != 1) {
160                                 return(GPF_INVAL);
161                         }
162                         getstat |= 0x01;
163                 } else if (!strncmp(p, WIDTH, WIDTHsz)) {
164                         if (sscanf(p, "width:%d", &(head->p_width)) != 1) {
165                                 return(GPF_INVAL);
166                         }
167                         getstat |= 0x02;
168                 } else if (!strncmp(p, HEIGHT, HEIGHTsz)) {
169                         if (sscanf(p, "height:%d", &(head->p_height)) != 1) {
170                                 return(GPF_INVAL);
171                         }
172                         getstat |= 0x04;
173                 }
174         }
175         return  0;
176 }
177
178
179 int
180 #if NeedFunctionPrototypes
181 WriteBdfHeader( struct ptobhead *head )
182 #else
183 WriteBdfHeader(head)
184 struct ptobhead *head;
185 #endif
186 {
187         FILE    *fp;
188         int             fd[2];
189         unsigned int            getstat;
190         char  buf[BUFSIZE], *p;
191
192         pid_t   chld_pid = 0;
193 #if defined( SVR4 ) || defined( SYSV )
194         int     chld_stat ;
195 #else
196         union   wait    chld_stat ;
197 #endif
198
199
200         if (head->snf_file != NULL) {
201                 if (pipe(fd) != 0) {
202                         return  PIPE_ERROR;
203                 }
204                 switch (chld_pid = fork()) {
205                 case    0:
206                         close(1);
207                         if(dup(fd[1]) < 0) {
208                                 return(DUP_ERROR);
209                         }
210                         close(fd[0]);
211                         close(fd[1]);
212                         execl( oakgtobdf, oakgtobdf, head->snf_file, 0);
213
214                         return  EXEC_ERROR;
215
216                 case    -1:
217                         return(FORK_ERROR);
218
219                 default:
220                         break;
221                 }
222                 close(fd[1]);
223                 if((fp = (FILE *)fdopen(fd[0], "r")) == NULL) {
224                         close( fd[0] );
225                         kill( chld_pid, SIGKILL );
226                         WaitID( chld_pid, chld_stat ) ;
227                         return  FDOPEN_ERROR;
228                 }
229         } else {
230                 if ((fp = fopen(head->bdf_file, "r")) == NULL) {
231                         return(BDF_OPEN_HEAD);
232                 }
233         }
234
235         getstat = 0;
236
237         while ( 1 ) {
238                 if (fgets(buf, BUFSIZE, fp) == NULL) {
239                     fclose( fp );
240                     if (head->snf_file != NULL) {
241                         kill( chld_pid, SIGKILL );
242                         WaitID( chld_pid, chld_stat ) ;
243                     }
244                     return(BDF_INVAL);
245                 }
246                 p = buf;
247                 SCAN_TO_NONSP(p);
248
249                 if (!strncmp(p, CHARS, CHARSsz)) {
250                     fclose( fp );
251                     if (head->snf_file != NULL) {
252                         kill( chld_pid, SIGKILL );
253                         WaitID( chld_pid, chld_stat ) ;
254                     }
255                     break;
256                 }
257
258                 fprintf(head->output, "%s", buf);
259
260                 if (!strncmp(p, SIZE, SIZEsz)) {
261                     if ((sscanf(p, "SIZE %f%d",
262                         &(head->bdf_point), &(head->bdf_xdpi))) != 2) {
263                         fclose(fp);
264                         if (head->snf_file != NULL) {
265                             kill( chld_pid, SIGKILL );
266                             WaitID( chld_pid, chld_stat ) ;
267                         }
268                         return(BDF_INVAL);
269                     }
270                     getstat |= 0x01;
271                     continue;
272                 }
273                 if (!strncmp(p, FONTBOUNDINGBOX, FONTBOUNDINGBOXsz)) {
274                     if ( sscanf( p, "FONTBOUNDINGBOX %d%d%d%d", 
275                                                     &(head->bdf_width), 
276                                                     &(head->bdf_height), 
277                                                     &(head->bdf_x), 
278                                                     &(head->bdf_y)) != 4
279                     ) {
280                         fclose(fp);
281                         if (head->snf_file != NULL) {
282                             kill( chld_pid, SIGKILL );
283                             WaitID( chld_pid, chld_stat ) ;
284                         }
285                         return  BDF_INVAL;
286                     }
287                     getstat |= 0x02;
288                     continue;
289                 }
290                 get_charset_registry(head, p) ; 
291         }
292
293
294         if (getstat != 0x03) {
295                 return  BDF_INVAL;
296         }
297
298         return  0;
299 }
300
301
302 void
303 #if NeedFunctionPrototypes
304 WriteGpfHeader( struct btophead *head )
305 #else
306 WriteGpfHeader(head)
307 struct btophead *head;
308 #endif
309 {
310         fprintf(head->output, "numfonts:%d\n", head->num_chars);
311         fprintf(head->output, "width:%d\n",    head->p_width  );
312         fprintf(head->output, "height:%d\n",   head->p_height );
313 }
314
315
316
317
318 int
319 #if NeedFunctionPrototypes
320 WritePtnToBdf( struct ptobhead *head )
321 #else
322 WritePtnToBdf(head)
323 struct ptobhead *head;
324 #endif
325 {
326         int     msize, swidth, rtn, i, nchar;
327         char    *zoomptn;
328         int     bbw, bbh, bbx, bby, dw ;
329         char    glyph_name[BUFSIZE] ;
330
331         nchar = head->num_chars;
332         put_default_chars(head, nchar, rtn) ;
333
334         if (head->zoomf) {
335                 msize = ((head->bdf_width + 7) / 8) * head->bdf_height;
336                 if ((zoomptn = (char *)malloc(msize)) == NULL) {
337                         return(MALLOC_ERROR);
338                 }
339         }
340
341         for (i=0; i<head->num_chars; i++) {
342             sprintf( glyph_name, "%x", head->code[i]);
343             swidth = (head->bdf_width * 72270)/((int)(head->bdf_point * (float)head->bdf_xdpi));
344             dw  = head->bdf_width ;
345             bbw = head->bdf_width ;
346             bbh = head->bdf_height ;
347             bbx = head->bdf_x ;
348             bby = head->bdf_y ;
349
350             fprintf(head->output, "STARTCHAR %s\n", glyph_name );
351             fprintf(head->output, "ENCODING %d\n", head->code[i]);
352             fprintf(head->output, "SWIDTH %d 0\n", swidth );
353             fprintf(head->output, "DWIDTH %d 0\n", dw );
354             fprintf(head->output, "BBX %d %d %d %d\n", bbw, bbh, bbx, bby );
355             fprintf(head->output, "BITMAP\n");
356
357             if (head->zoomf) {
358                     if ((rtn = PtnZoom(zoomptn, head->ptn[i], head->p_width,
359                         head->p_height, head->bdf_width, head->bdf_height))) {
360                             return(rtn);
361                     }
362                     if( (rtn = PutBdfPtn(zoomptn, head->bdf_width,
363                         head->bdf_height, head->output))) {
364                             return(rtn);
365                     }
366             } else {
367                     if( (rtn = PutBdfPtn(head->ptn[i],
368                         head->bdf_width, head->bdf_height, head->output))) {
369                             return(rtn);
370                     }
371             }
372             fprintf(head->output, "ENDCHAR\n");
373             free(head->ptn[i]);
374         }
375         fprintf(head->output, "ENDFONT\n");
376         return(0);
377 }
378
379
380
381
382 int
383 #if NeedFunctionPrototypes
384 putDefaultChars( struct ptobhead *head )
385 #else
386 putDefaultChars(head )
387 struct ptobhead *head;
388 #endif
389 {
390         int     swidth, bytew, i, j;
391         unsigned int     posbit ;
392         unsigned char    *ptnbuf, *ptnbuf2 ;
393         unsigned char    tmp;
394
395         put_default_encode( head ) ;
396         swidth = (head->bdf_width * 72270)/((int)(head->bdf_point * (float)head->bdf_xdpi));
397         fprintf(head->output, "SWIDTH %d 0\n", swidth);
398         fprintf(head->output, "DWIDTH %d 0\n", head->bdf_width);
399         fprintf( head->output, "BBX %d %d %d %d\n", head->bdf_width, head->bdf_height, head->bdf_x, head->bdf_y );
400         fprintf(head->output, "BITMAP\n");
401
402         bytew = (head->bdf_width + 7) / 8;
403         if ((ptnbuf = (unsigned char *)malloc(bytew * 2)) == NULL) {
404                 return(MALLOC_ERROR);
405         }
406         put_default_bitmap(head, bytew, posbit, i, j, ptnbuf, ptnbuf2, tmp) ;
407
408         fprintf(head->output, "ENDCHAR\n");
409         free(ptnbuf);
410         return  0;
411 }
412
413
414 int
415 #if NeedFunctionPrototypes
416 WritePtnToGpf( struct btophead *head )
417 #else
418 WritePtnToGpf(head)
419 struct btophead *head;
420 #endif
421 {
422         int     msize, rtn, i;
423         char    *zoomptn;
424
425
426         if (head->zoomf) {
427                 msize = ((head->p_width + 7) / 8) * head->p_height;
428                 if ((zoomptn = (char *)malloc(msize)) == NULL) {
429                         return(MALLOC_ERROR);
430                 }
431         }
432
433         for (i=0; i<head->num_chars; i++) {
434
435                 if ( head->code[i] > 0xff ) {
436                         fprintf(head->output, "code:0x%x\n", SHIFT_ON(head->code[i]) );
437                 } else {
438                         fprintf(head->output, "code:0x%x\n", head->code[i]);
439                 }
440                 if (head->zoomf) {
441                         if ((rtn = PtnZoom(zoomptn, head->ptn[i], head->bdf_width,
442                             head->bdf_height, head->p_width, head->p_height))) {
443                                 return(rtn);
444                         }
445                         if ((rtn = PutGpfPtn(zoomptn, head->p_width,
446                             head->p_height, head->output))) {
447                                 return(rtn);
448                         }
449                 } else {
450                         if ((rtn = PutGpfPtn(head->ptn[i],
451                             head->bdf_width, head->bdf_height, head->output))) {
452                                 return(rtn);
453                         }
454                 }
455                 free(head->ptn[i]);
456         }
457         fprintf(head->output, "enddata\n");
458         return(0);
459 }
460
461
462 int
463 #if NeedFunctionPrototypes
464 ReadBdfToMemory(
465     struct btophead *head,
466     char   *buf
467 )
468 #else
469 ReadBdfToMemory(head, buf)
470 struct btophead *head;
471 char   *buf;
472 #endif
473 {
474         int         code, mwidth, num_char, bsize, rtn;
475         char    *ptn;
476
477         num_char = 0;
478         mwidth = (head->bdf_width + 7) / 8;
479         bsize = mwidth * head->bdf_height;
480         while(1) {
481                 if ((rtn = GetBdfCode(head, buf, &code)) < 0) {
482                         return(rtn);    /* contain BDF_INVAL */
483                 } else if (rtn == FILE_END) {
484                         head->num_chars = num_char;
485                         break;
486                 }
487
488                 if ( ( code > head->end_code )
489                         || ( code < head->start_code )
490                         || ( !IN_CODE( head->code_category, SHIFT_ON( code ) ) )
491                         || COMM_ISDEFAULTCHAR( code )
492                 ) {
493                         continue;
494                 }
495                 head->code[num_char] = code;
496                 if ( ( ptn = head->ptn[num_char++] = (char *)malloc( bsize ) ) == NULL ) {
497                         return(MALLOC_ERROR);
498                 }
499
500                 if ((rtn = GetBdfPtn(head, buf, ptn, mwidth, bsize)) != 0) {
501                         return(rtn);
502                 }
503         }
504         return(0);
505 }
506
507
508 int
509 #if NeedFunctionPrototypes
510 ReadBdfToMemory_with_init(
511     struct btophead *head,
512     int init_start,
513     int init_end,
514     char   *buf
515 )
516 #else
517 ReadBdfToMemory_with_init(head, init_start, init_end, buf)
518 struct btophead *head;
519 int     init_start;
520 int     init_end;
521 char   *buf;
522 #endif
523 {
524         int         code, mwidth, num_char, bsize, rtn;
525         char    *ptn;
526
527         num_char = 0;
528         mwidth = (head->bdf_width + 7) / 8;
529         bsize = mwidth * head->bdf_height;
530         while(1) {
531                 if ((rtn = GetBdfCode(head, buf, &code)) < 0) {
532                         return(rtn);    /* contain BDF_INVAL */
533                 } else if (rtn == FILE_END) {
534                         head->num_chars = num_char;
535                         break;
536                 }
537
538                 if ( ( code > head->end_code )
539                         || ( code < head->start_code )
540                         || ( !IN_CODE( head->code_category, SHIFT_ON( code ) ) )
541                         || ( ( code >= init_start ) && ( code <= init_end ) )
542                         || COMM_ISDEFAULTCHAR( code )
543                 ) {
544                         continue;
545                 }
546
547                 head->code[num_char] = code;
548                 if ((ptn = head->ptn[num_char++] =
549                     (char *)malloc(bsize)) == NULL) {
550                         return(MALLOC_ERROR);
551                 }
552
553                 if ((rtn = GetBdfPtn(head, buf, ptn, mwidth, bsize)) != 0) {
554                         return(rtn);
555                 }
556         }
557         return(0);
558 }
559
560
561 int
562 #if NeedFunctionPrototypes
563 GetBdfCode(
564     struct btophead *head,
565     char   *buf,
566     int    *code
567 )
568 #else
569 GetBdfCode(head, buf, code)
570 struct btophead *head;
571 char   *buf;
572 int    *code;
573 #endif
574 {
575         char *p;
576
577         while(1) {
578                 if (fgets(buf, BUFSIZE, head->input) == NULL) {
579                         return (BDF_INVAL);
580                 }
581                 p = buf;
582                 SCAN_TO_NONSP(p)
583                     if (!strncmp(p, ENDFONT, ENDFONTsz)) {
584                         return(FILE_END);
585                 }
586
587                 if (!strncmp(p, ENCODING, ENCODINGsz)) {
588                         if ((sscanf(p, "ENCODING %d", code)) != 1) {
589                                 return(BDF_INVAL);
590                         }
591                         break;
592                 }
593         }
594         return(0);
595 }
596
597
598 int
599 #if NeedFunctionPrototypes
600 GetBdfPtn(
601     struct btophead *head,
602     char   *buf,
603     char   *ptn,
604     int    mwidth,
605     int    bsize
606 )
607 #else
608 GetBdfPtn(head, buf, ptn, mwidth, bsize)
609 struct btophead *head;
610 char   *buf;
611 char   *ptn;
612 int    mwidth;
613 int    bsize;
614 #endif
615 {
616         int         skip, i, j;
617         char    *p;
618
619         while(1) {
620                 if (fgets(buf, BUFSIZE, head->input) == NULL) {
621                         return (BDF_INVAL);
622                 }
623                 p = buf;
624                 SCAN_TO_NONSP(p)
625                     if (!strncmp(p, BITMAP, BITMAPsz)) {
626                         break;
627                 }
628         }
629
630         ptn[bsize - 1] = NULL;
631
632         for (i=0, skip=0; i<head->bdf_height; i++) {
633                 if (skip) {
634                         for(j=0; j<mwidth; j++) {
635                                 ptn[j] = NULL;
636                         }
637                         ptn += mwidth;
638                         continue;
639                 }
640                 if (fgets(buf, BUFSIZE, head->input) == NULL) {
641                         return (BDF_INVAL);
642                 }
643                 p = buf;
644                 SCAN_TO_NONSP(p);
645                 if (!strncmp(p, ENDCHAR, ENDCHARsz)) {
646                         skip = 1;
647                         for(j=0; j<mwidth; j++) {
648                                 ptn[j] = NULL;
649                         }
650                         ptn += mwidth;
651                         continue;
652                 }
653                 GetBdfLinePtn(ptn, buf, head->bdf_width);
654                 ptn += mwidth;
655         }
656         return(0);
657 }
658
659
660 int
661 #if NeedFunctionPrototypes
662 ReadGpfToMemory(
663     struct ptobhead *head,
664     char   *buf
665 )
666 #else
667 ReadGpfToMemory(head, buf)
668 struct ptobhead *head;
669 char   *buf;
670 #endif
671 {
672         int         code, mwidth, num_char, bsize, rtn;
673         char    *ptn;
674
675         num_char = 0;
676         mwidth = (head->p_width + 7) / 8;
677         bsize = mwidth * head->p_height;
678         while(1) {
679                 if ((rtn = GetGpfCode(head, buf, &code)) < 0) {
680                         return(rtn);    /* contain GPF_INVAL */
681                 } else if (rtn == FILE_END) {
682                         head->num_chars = num_char;
683                         break;
684                 }
685                 if ( ( code > head->end_code ) 
686                         || (code < head->start_code ) 
687                         || ( !IN_CODE( head->code_category, SHIFT_ON( code ) ) )
688                         || COMM_ISDEFAULTCHAR( code )
689                 ) {
690                         continue;
691                 }
692                 head->code[num_char] = code;
693                 if ((ptn = head->ptn[num_char++] =
694                     (char *)malloc(bsize)) == NULL) {
695                         return(MALLOC_ERROR);
696                 }
697
698                 if ((rtn = GetGpfPtn(head, buf, ptn, mwidth, bsize)) != 0) {
699                         return(rtn);
700                 }
701         }
702         return(0);
703 }
704
705
706 int
707 #if NeedFunctionPrototypes
708 GetGpfCode(
709     struct ptobhead *head,
710     char   *buf,
711     int    *code
712 )
713 #else
714 GetGpfCode(head, buf, code)
715 struct ptobhead *head;
716 char   *buf;
717 int    *code;
718 #endif
719 {
720         char    *p;
721
722         while(1) {
723                 p = buf;
724                 SCAN_TO_NONSP(p)
725
726                     if (!strncmp(p, ENDDATA, ENDDATAsz)) {
727                         return(FILE_END);
728                 }
729                 if (!strncmp(p, CODE, CODEsz)) {
730                         *code = (int)strtol(buf+CODEsz, NULL, 0);
731                         CONVGLYPHINDEX( *code ) ;
732                         break;
733                 }
734
735                 if (fgets(buf, BUFSIZE, head->input) == NULL) {
736                         return (GPF_INVAL);
737                 }
738         }
739         return(0);
740 }
741
742
743 int
744 #if NeedFunctionPrototypes
745 GetGpfPtn(
746     struct ptobhead *head,
747     char    *buf,
748     char    *ptn,
749     int mwidth,
750     int bsize
751 )
752 #else
753 GetGpfPtn(head, buf, ptn, mwidth, bsize)
754 struct ptobhead *head;
755 char    *buf;
756 char    *ptn;
757 int     mwidth;
758 int     bsize;
759 #endif
760 {
761         int     skip, i, j;
762         char    *p;
763
764         for (i=0, skip=0; i<head->p_height; i++) {
765                 if (skip) {
766                         for (j=0; j<mwidth; j++) {
767                                 ptn[j] = NULL;
768                         }
769                         ptn += mwidth;
770                         continue;
771                 }
772                 if (fgets(buf, BUFSIZE, head->input) == NULL) {
773                         return (GPF_INVAL);
774                 }
775                 p = buf;
776                 SCAN_TO_NONSP(p);
777                 if ((!strncmp(p, CODE, CODEsz)) ||
778                     (!strncmp(p, ENDDATA, ENDDATAsz))) {
779                         skip = 1;
780                         for (j=0; j<mwidth; j++) {
781                                 ptn[j] = NULL;
782                         }
783                         ptn += mwidth;
784                         continue;
785                 }
786
787                 GetGpfLinePtn(ptn, buf, head->p_width);
788                 ptn += mwidth;
789         }
790         return(0);
791 }
792
793
794 void
795 #if NeedFunctionPrototypes
796 GetBdfLinePtn(
797     char        *mem,
798     char        *buf,
799     int width
800 )
801 #else
802 GetBdfLinePtn(mem, buf, width)
803 char    *mem;
804 char    *buf;
805 int     width;
806 #endif
807 {
808         int     i, iend, len;
809         char   *p, str[3];
810
811         str[2] = NULL;
812
813         SCAN_TO_NONSP(buf);
814
815         iend = (width + 3) / 4;
816
817         if ((len = strlen(buf)) < iend) {
818                 p = buf+len;
819                 for (i=0; i<(iend-len); i++) {
820                         *p++ = '0';
821                 }
822                 *p ='\0';
823         }
824
825         for (i=0; i<iend/2; i++) {
826                 str[0] = *buf++;
827                 str[1] = *buf++;
828                 *mem++ = (char)strtol(str, NULL, 16);
829         }
830         if (iend%2) {
831                 str[0] = *buf;
832                 str[1] = NULL;
833                 *mem = (char)strtol(str, NULL, 16) << 4;
834         }
835 }
836
837 void
838 #if NeedFunctionPrototypes
839 GetGpfLinePtn(
840     char        *mem,
841     char        *buf,
842     int width
843 )
844 #else
845 GetGpfLinePtn(mem, buf, width)
846 char    *mem;
847 char    *buf;
848 int     width;
849 #endif
850 {
851         unsigned int       skip, i, iend, j;
852         unsigned char   ptn;
853
854         SCAN_TO_NONSP(buf);
855
856         iend = (width + 7) / 8;
857
858         for (i=0, skip=0; i<iend; i++) {
859                 if (skip) {
860                         *mem++ = NULL;
861                         continue;
862                 }
863                 for (j=0, ptn = NULL; j<8; j++) {
864                         if ((*buf == '\n') || (*buf == NULL)) {
865                                 skip = 1;
866                                 ptn <<= (8-j);
867                                 break;
868                         }
869                         if (j) ptn <<= 1;
870                         if (*buf == '0') {
871                                 ptn |= 1;
872                         }
873                         buf++;
874                 }
875                 *mem++ = ptn;
876         }
877 }
878
879
880
881
882 int
883 #if NeedFunctionPrototypes
884 PutBdfPtn(
885     unsigned char       *ptn,
886     int width,
887     int height,
888     FILE        *fp
889 )
890 #else
891 PutBdfPtn(ptn, width, height, fp)
892 unsigned char   *ptn;
893 int     width;
894 int     height;
895 FILE    *fp;
896 #endif
897 {
898         int         i, j, nbyte ;
899         unsigned char    *pbuf, x, c;
900         static unsigned char    *buf=NULL;
901
902         if (buf==NULL) {
903                 buf = (unsigned char *)malloc(width*height+2);
904                 if ( buf == NULL) {
905                         return(MALLOC_ERROR);
906                 }
907         }
908         nbyte = (width + 7) / 8;
909
910         pbuf=buf;
911         for(i=0; i<height; i++) {
912                 for (j=0 ; j<nbyte; j++) {
913                         x = *ptn >> 4;
914                         c = (x>=10)? 'a'-0xa : '0';
915                         *pbuf++ = c + x;
916                         x = *ptn++ & 0x0f;
917                         c = (x>=10)? 'a'-0xa : '0';
918                         *pbuf++ = c + x;
919                 }
920                 *pbuf++ = '\n';
921         }
922         *pbuf = '\0';
923         fprintf(fp, "%s", buf);
924         return(0);
925 }
926
927
928
929
930 int
931 #if NeedFunctionPrototypes
932 PutGpfPtn(
933     char        *ptn,
934     int width,
935     int height,
936     FILE        *fp
937 )
938 #else
939 PutGpfPtn(ptn, width, height, fp)
940 char    *ptn;
941 int     width;
942 int     height;
943 FILE    *fp;
944 #endif
945 {
946         int         i, j, k, nbyte, tw;
947         unsigned char    p, *pbuf;
948         static unsigned char    *buf=NULL;
949
950         if (buf==NULL) {
951             buf = (unsigned char *)malloc(((width+1)*height)+1);
952             if ( buf == NULL) {
953                 return(MALLOC_ERROR);
954             }
955         }
956
957         nbyte = (width + 7) / 8;
958
959         pbuf=buf;
960         for(i=0; i<height; i++) {
961             for (j=0, tw=width; j<nbyte; j++) {
962                 p = *ptn++;
963                 for (k=0; k<8 && tw>0; k++, tw--) {
964                     if (p & 0x80) {
965                             *pbuf++ = '0';
966                     } else {
967                             *pbuf++ = '-';
968                     }
969                     p = p << 1;
970                 }
971             }
972             *pbuf++ = '\n';
973         }
974         *pbuf = '\0';
975         fprintf(fp, "%s", buf);
976         return(0);
977 }
978
979
980 int
981 #if NeedFunctionPrototypes
982 PtnZoom(
983     char        *dmem,
984     char        *smem,
985     int         sw,
986     int         sh,
987     int         dw,
988     int         dh
989 )
990 #else
991 PtnZoom(dmem, smem, sw, sh, dw, dh)
992 char    *dmem;
993 char    *smem;
994 int     sw;
995 int     sh;
996 int     dw;
997 int     dh;
998 #endif
999 {
1000         int         swidth;
1001         int         dwidth;
1002         int         i, lcnt;
1003         char   *dbuf, *sp, *dp;
1004
1005         swidth = (sw + 7) / 8;
1006         dwidth = (dw + 7) / 8;
1007
1008         if ((dbuf = (char *)malloc(dwidth)) == NULL) {
1009                 return(MALLOC_ERROR);
1010         }
1011
1012         lcnt = 0;
1013         sp = smem;
1014         dp = dmem;
1015
1016         for(i=0; i < sh; i++) {
1017                 lcnt += dh;
1018                 if (lcnt >= sh) {
1019                         exline(sp, dbuf, sw, dw);
1020                         sp += swidth;
1021                         lcnt -= sh;
1022                         memcpy(dp, dbuf, dwidth);
1023                         dp += dwidth;
1024                         for (; lcnt >= sh; lcnt -= sh) {
1025                                 memcpy(dp, dbuf, dwidth);
1026                                 dp += dwidth;
1027                         }
1028                 } else {
1029                         sp += swidth;
1030                 }
1031         }
1032         free(dbuf);
1033         return(0);
1034 }
1035
1036
1037 static void
1038 exline(sp, dbuf, sw, dw)
1039 char    *sp;
1040 char    *dbuf;
1041 int     sw;
1042 int     dw;
1043 {
1044         unsigned int    i, bit, sval, dval, dcnt, bcnt;
1045
1046         bcnt = dval = 0;
1047         dcnt = 8;
1048
1049         for(i=0; i<sw; i++) {
1050                 if (i % 8 == 0) {
1051                         sval = *sp++;
1052                 }
1053                 bit = ((sval & 0x80) ? 1 : 0);
1054                 sval <<=  1;
1055                 for (bcnt += dw; bcnt >= sw; bcnt -= sw) {
1056                         dval = (dval << 1) | bit;
1057                         if (--dcnt == 0) {
1058                                 *dbuf++ = (char)dval;
1059                                 dval = 0;
1060                                 dcnt = 8;
1061                         }
1062                 }
1063         }
1064         if (dcnt != 8) {
1065                 dval <<= dcnt;
1066                 *dbuf = (char)dval;
1067         }
1068 }
1069
1070
1071 char *
1072 #if NeedFunctionPrototypes
1073 GetTmpPath( char *path )
1074 #else
1075 GetTmpPath( path )
1076 char    *path;
1077 #endif
1078 {
1079         char    *p=NULL, *sp, *buf ;
1080         int     len ;
1081         struct  stat    statbuf ;
1082
1083         /* Get directory of temporary file */
1084         if( !(p = (char *)getenv( "TMPDIR" )) ){
1085             p = DEFAULT_TMPPATH ;
1086         }
1087         /* Get memory for temporary file name */
1088         sp = p ;
1089         len = strlen(p) ;
1090         if( *(sp + len -1) == '/' ){
1091             len += (strlen( TEMPFILEKEY ) + 1) ;
1092         }else{
1093             len += (strlen( TEMPFILEKEY ) +1 + 1) ;
1094         }
1095         if ((buf = (char *)malloc( len )) == NULL) {
1096             return NULL;
1097         }
1098         /* Get temporary file path */
1099         strcpy( buf, p ) ;
1100         sp = buf + strlen(buf) -1 ;
1101         if( *sp == '/' )        *sp-- = '\0' ;
1102         sprintf( sp+1, "/%s", TEMPFILEKEY ) ;
1103         /* Get temporary file name */
1104         return mktemp( buf ) ;
1105 }
1106
1107
1108 int
1109 #if NeedFunctionPrototypes
1110 Link_NewFile( char *rd_file, char *wr_file )
1111 #else
1112 Link_NewFile( rd_file, wr_file )
1113 char    *rd_file ;
1114 char    *wr_file ;
1115 #endif
1116 {
1117         FILE    *rfp, *wfp ;
1118         int     i, c ;
1119         int     rfd, wfd ;
1120         struct  stat    statbuf ;
1121         unsigned char   bufc ;
1122         char    *buf ;
1123         int     ismmap = 0 ;
1124
1125         if( stat( wr_file, &statbuf ) == -1 ){
1126             if( (wfd = open( wr_file, O_CREAT | O_WRONLY, 0644 )) == -1 ){
1127                 return -1 ;
1128             }
1129         }else{
1130             if( (wfd = open( wr_file, O_WRONLY | O_TRUNC )) == -1 ){
1131                 return -1 ;
1132             }
1133         }
1134         if( stat( rd_file, &statbuf ) == -1 ){
1135             close(wfd) ; 
1136             return -1 ;
1137         }
1138         if( (rfd = open( rd_file, O_RDONLY )) == -1 ){
1139             close(wfd) ; 
1140             return -1 ;
1141         }
1142
1143 #if     defined( SVR4 )
1144         if( (buf = (char *)mmap(0, statbuf.st_size, PROT_READ, MAP_SHARED, rfd, 0)) == (char *)-1 ){
1145 #endif  /* SVR4 */
1146             if( !(buf = (char *)malloc(statbuf.st_size)) ){
1147                 close(wfd) ; 
1148                 close(rfd) ; 
1149                 return -1 ;
1150             }
1151             if( read(rfd, buf, statbuf.st_size) != statbuf.st_size ){
1152                 close(wfd) ; 
1153                 close(rfd) ; 
1154                 return -1 ;
1155             }
1156 #if     defined( SVR4 )
1157         }else{
1158             ismmap = 1 ;
1159         }
1160 #endif  /* SVR4 */
1161
1162         close(rfd) ; 
1163
1164         if( write(wfd, buf, statbuf.st_size) != statbuf.st_size ){
1165 #if     defined( SVR4 )
1166             if( ismmap ){
1167                 munmap(buf, statbuf.st_size) ;
1168             }else{
1169 #endif  /* SVR4 */
1170                 free(buf) ;
1171 #if     defined( SVR4 )
1172             }
1173 #endif  /* SVR4 */
1174             close(wfd) ; 
1175             return (DEVICE_FAIL);
1176         }
1177 #if     defined( SVR4 )
1178         if( ismmap ){
1179             munmap(buf, statbuf.st_size) ;
1180         }else{
1181 #endif  /* SVR4 */
1182             free(buf) ;
1183 #if     defined( SVR4 )
1184         }
1185 #endif  /* SVR4 */
1186         close(wfd) ;
1187
1188         return 0 ;
1189 }
1190
1191
1192 int
1193 #if NeedFunctionPrototypes
1194 ChkNumString( char      *str )
1195 #else
1196 ChkNumString( str )
1197 char    *str;
1198 #endif
1199 {
1200         char    *tmp;
1201         int             num;
1202
1203         num = (int)strtol( str, &tmp, 10 );
1204         if ((num == 0)&&(tmp == str)) {
1205                 return(-1);
1206         }
1207         if ((size_t)(tmp - str) != strlen(str)){
1208                 return(-1);
1209         }
1210         return( 0 );
1211 }
1212
1213
1214 void
1215 #if NeedFunctionPrototypes
1216 ErrMsgTable_AndExit(
1217     int         er_no,
1218     char        *snf_in,
1219     char        *snf_out,
1220     char        *gpf_in,
1221     char        *gpf_out,
1222     char        *bdf_in,
1223     char        *prog_name 
1224 )
1225 #else
1226 ErrMsgTable_AndExit(er_no, snf_in, snf_out, gpf_in, gpf_out, bdf_in, prog_name)
1227 int     er_no;
1228 char    *snf_in;
1229 char    *snf_out;
1230 char    *gpf_in;
1231 char    *gpf_out;
1232 char    *bdf_in;
1233 char    *prog_name ;
1234 #endif
1235 {
1236         int     rtn = 0 ;
1237         switch(er_no) {
1238         case 0:
1239                 break;
1240         case BDF_OPEN_IN :
1241                 USAGE2("%s : The input font file cannot be opened.\"%s\"¡¥\n", prog_name, (snf_in != NULL)? snf_in : "\0" );
1242                 rtn = OPEN_FAIL ;
1243                 break;
1244
1245         case BDF_OPEN_OUT :
1246                 USAGE2("%s : The output font file cannot be opened.\"%s\"¡¥\n", prog_name, (snf_out != NULL)? snf_out : "\0" );
1247                 rtn = OPEN_FAIL ;
1248                 break;
1249
1250
1251         case BDF_READ :
1252                 USAGE2("%s : Information from the font file cannot be extracted.\"%s\"¡¥ \n", prog_name, (snf_in != NULL) ? snf_in : "\0" );
1253                 rtn = READ_FAIL ;
1254                 break;
1255
1256         case BDF_WRITE :
1257                 USAGE2("%s : It is not possible to write to the font file.\"%s\"¡¥\n", prog_name, (snf_out != NULL) ? snf_out : "\0" );
1258                 rtn = DEVICE_FAIL ;
1259                 break;
1260
1261         case BDF_INVAL :
1262                 USAGE2("%s : The format of the font file is illegal.\"%s\"¡¥ \n", prog_name, (snf_in != NULL) ? snf_in : "\0" );
1263                 rtn = 1 ;
1264                 break;
1265
1266         case BDF_OPEN_HEAD :
1267                 USAGE2("%s : The BDF file cannot be opened.\"%s\"¡¥\n", prog_name, (bdf_in != NULL)? bdf_in : "\0" );
1268                 rtn = OPEN_FAIL ;
1269                 break;
1270
1271         case BDF_READ_HEAD :
1272                 USAGE2("%s : Information of the BDF file cannot be extracted.\"%s\"¡¥ \n", prog_name, (bdf_in != NULL) ? bdf_in : "\0" );
1273                 rtn = READ_FAIL ;
1274                 break;
1275
1276         case GPF_OPEN_IN :
1277                 USAGE2("%s : The input character pattern file cannot be opened.\"%s\"¡¥\n", prog_name, (gpf_in != NULL) ? gpf_in : "\0" );
1278                 rtn = OPEN_FAIL ;
1279                 break;
1280
1281         case GPF_OPEN_OUT :
1282                 USAGE2("%s : The output character pattern file cannot be opened.\"%s\"¡¥\n", prog_name, (gpf_out != NULL) ? gpf_out : "\0" );
1283                 rtn = OPEN_FAIL ;
1284                 break;
1285
1286         case GPF_READ :
1287                 USAGE2("%s : The character pattern file cannot be read.\"%s\"¡¥\n", prog_name, (gpf_in != NULL) ? gpf_in : "\0" );
1288                 rtn = READ_FAIL ;
1289                 break;
1290
1291         case GPF_WRITE :
1292                 USAGE2("%s : It is not possible to write to the character pattern file.\"%s\"¡¥\n", prog_name, (gpf_out != NULL) ? gpf_out : "\0" );
1293                 rtn = DEVICE_FAIL ;
1294                 break;
1295
1296         case GPF_INVAL :
1297                 USAGE2("%s : The format of the character pattern file is illegal.\"%s\"¡¥\n", prog_name, (gpf_in != NULL) ? gpf_in : "\0" );
1298                 rtn = 1 ;
1299                 break;
1300
1301         default :
1302                 fatal_err_msg( er_no, prog_name );
1303                 rtn = 1 ;
1304                 break;
1305         }
1306         exit(rtn);
1307 }
1308
1309
1310 void
1311 #if NeedFunctionPrototypes
1312 fatal_err_msg( 
1313     int         er_no,
1314     char        *prog_name
1315 )
1316 #else
1317 fatal_err_msg( er_no, prog_name )
1318 int     er_no;
1319 char    *prog_name;
1320 #endif
1321 {
1322         USAGE1("%s : The font file failed to be converted. ", prog_name);
1323         switch( er_no ) {
1324         case MALLOC_ERROR:
1325                 USAGE("There is no memory any more.\n");
1326                 break;
1327         case FORK_ERROR:
1328                 USAGE("The child process cannot be created.\n" );
1329                 break;
1330         case EXEC_ERROR:
1331                 USAGE("Cannot execute dtgpftobdf.\n" );
1332                 break;
1333         case PIPE_ERROR:
1334                 USAGE("The pipe buffer cannot be open.\n" );
1335                 break;
1336         case DUP_ERROR:
1337                 USAGE("The file descripter cannot be duplicated.\n" );
1338                 break;
1339         case POPEN_ERROR:
1340                 USAGE("The X font compiler cannot be executed.\n" );
1341                 break;
1342         case PCLOSE_ERROR:
1343                 USAGE("The pipe buffer cannot be close.\n" );
1344                 break;
1345         case FDOPEN_ERROR:
1346                 USAGE("The file descripter cannot be got.\n" );
1347                 break;
1348         case STAT_ERROR:
1349                 USAGE("The status of font cannot be got.\n" );
1350                 break;
1351         case MKTMP_ERROR:
1352                 USAGE("The temporally file cannot be got.\n" );
1353                 break;
1354         case PCFFONTC_ERROR:
1355                 USAGE("Cannot execute bdftopcf.\n" );
1356                 break;
1357         case SNFFONTC_ERROR:
1358                 USAGE("Cannot execute bdftosnf.\n" );
1359                 break;
1360         default:
1361                 USAGE1("Terminates abnormally. ( err_code(%d) )\n", er_no);
1362                 break;
1363         }
1364         return;
1365 }
1366
1367
1368
1369 static  int     sig_flg=0;
1370
1371 #define GPF_BUCK_UP_FAIL        1
1372 #define GPF_MODIFY_FAIL         2
1373 #define RESTORE_FAIL            3
1374 #define CATCH_SIGNAL            10
1375
1376 void
1377 ChatchSig()
1378 {
1379         sig_flg = 1;
1380 }
1381
1382 void
1383 #if NeedFunctionPrototypes
1384 Put_File_Create_Err_Msg( 
1385     int         msg_level,
1386     char        *org_name,
1387     char        *tmp_name,
1388     char        *save_name,
1389     char        *com
1390 )
1391 #else
1392 Put_File_Create_Err_Msg( msg_level, org_name, tmp_name, save_name, com )
1393 int     msg_level;
1394 char    *org_name;
1395 char    *tmp_name;
1396 char    *save_name;
1397 char    *com;
1398 #endif
1399 {
1400         switch( msg_level ) {
1401         case GPF_BUCK_UP_FAIL:
1402                 USAGE1("%s : Failed to write temporary file. Terminates abnormally.\n", com );
1403                 put_file_create_err_msg = DEVICE_FAIL ;
1404                 break;
1405
1406         case GPF_MODIFY_FAIL:
1407                 USAGE1("%s : Failed in the renewal of the font file. Terminates abnormally.\n", com );
1408                 put_file_create_err_msg = WRITE_FAIL ;
1409                 break;
1410
1411         case RESTORE_FAIL:
1412                 USAGE3("%s : The file before updating cannot be changed to former file name.\tPlease execute the following command after the inquiry the system manager.\n\t mv %s %s\n",
1413                         com, save_name, org_name);
1414                 put_file_create_err_msg = WRITE_FAIL ;
1415                 break;
1416
1417         case CATCH_SIGNAL:
1418                 USAGE1("%s : The signal was received. Terminates abnormally.\n", com);
1419                 put_file_create_err_msg = 1 ;
1420                 break;
1421         }
1422 }
1423
1424 int
1425 #if NeedFunctionPrototypes
1426 Make_NewFefFile( 
1427     char        *org_name,
1428     char        *tmp_name,
1429     mode_t      mode,
1430     uid_t       owner,
1431     gid_t       group,
1432     char        *com
1433 )
1434 #else
1435 Make_NewFefFile( org_name, tmp_name, mode, owner, group, com )
1436 char    *org_name;
1437 char    *tmp_name;
1438 mode_t  mode;
1439 uid_t   owner;
1440 gid_t   group;
1441 char    *com;
1442 #endif
1443 {
1444         struct  stat    statbuf;
1445         char    *save_name = NULL;
1446         int             ret_val;
1447         int             msg_level;
1448         int             rtn ;
1449
1450 #ifndef SVR4
1451         signal( SIGHUP , (void(*)())ChatchSig);
1452         signal( SIGINT , (void(*)())ChatchSig);
1453         signal( SIGQUIT, (void(*)())ChatchSig);
1454         signal( SIGTERM, (void(*)())ChatchSig);
1455 #else
1456         sigset( SIGHUP , (void(*)())ChatchSig);
1457         sigset( SIGINT , (void(*)())ChatchSig);
1458         sigset( SIGQUIT, (void(*)())ChatchSig);
1459         sigset( SIGTERM, (void(*)())ChatchSig);
1460 #endif
1461
1462         errno = 0;
1463         if (sig_flg || ((save_name = GetTmpPath( org_name )) == NULL)) {
1464                 msg_level = (sig_flg) ? CATCH_SIGNAL : GPF_BUCK_UP_FAIL;
1465                 Put_File_Create_Err_Msg( msg_level, org_name, tmp_name,
1466                                                         (char *)NULL, com );
1467                 Unlink_Tmpfile ( tmp_name, com );
1468                 return(put_file_create_err_msg);
1469         }
1470
1471         if (sig_flg || ((rtn=Link_NewFile( org_name, save_name )) != 0)) {
1472                 msg_level = (sig_flg) ? CATCH_SIGNAL : GPF_BUCK_UP_FAIL;
1473                 Put_File_Create_Err_Msg( msg_level, org_name, tmp_name,
1474                                                         save_name, com );
1475                 Unlink_Tmpfile ( save_name, com );
1476                 Unlink_Tmpfile ( tmp_name, com );
1477                 return( (put_file_create_err_msg == DEVICE_FAIL)?
1478                         put_file_create_err_msg : rtn );
1479         }
1480
1481         if (sig_flg || ((rtn=Link_NewFile( tmp_name, org_name )) != 0)) {
1482                 msg_level = (sig_flg) ? CATCH_SIGNAL : GPF_MODIFY_FAIL;
1483                 Put_File_Create_Err_Msg( msg_level, org_name, tmp_name,
1484                                                         save_name, com );
1485                 if ( (rtn=Link_NewFile( save_name, org_name )) != 0 ) {
1486                         Put_File_Create_Err_Msg( RESTORE_FAIL, org_name,
1487                                                     tmp_name, save_name, com );
1488                         Unlink_Tmpfile ( tmp_name, com );
1489                 } else {
1490                         Unlink_Tmpfile ( tmp_name, com );
1491                         Unlink_Tmpfile ( save_name, com );
1492                 }
1493                 return( (put_file_create_err_msg == DEVICE_FAIL)?
1494                         put_file_create_err_msg : rtn );
1495         }
1496
1497         ret_val = 0;
1498         ret_val += Unlink_Tmpfile ( tmp_name,  com );
1499         ret_val += Unlink_Tmpfile ( save_name, com );
1500
1501         return( (ret_val)? 1 : 0 );
1502 }
1503
1504
1505 int
1506 #if NeedFunctionPrototypes
1507 Unlink_Tmpfile( 
1508     char        *file,
1509     char        *com
1510 )
1511 #else
1512 Unlink_Tmpfile( file, com )
1513 char    *file;
1514 char    *com;
1515 #endif
1516 {
1517         errno = 0;
1518         if ( unlink( file ) != 0 ) {
1519                 USAGE2("%s : The work file cannot be deleted. Please unlink the following files\tafter the inquiry the system manager.\"%s\" \n", com, file );
1520                 return(1);
1521         }
1522         return(0);
1523 }
1524
1525
1526 int
1527 #if NeedFunctionPrototypes
1528 Chmod_File ( 
1529     char        *fname,
1530     mode_t      mode,
1531     char        *com
1532 )
1533 #else
1534 Chmod_File ( fname, mode, com )
1535 char    *fname;
1536 mode_t  mode;
1537 char    *com;
1538 #endif
1539 {
1540         errno = 0;
1541         if ( mode == 0 )        return( 0 ) ;
1542         if ( chmod (fname, mode) != 0) {
1543                 USAGE3("%s : Failed in the mode change of the following files. Please change the mode to %o\tafter the inquiry the system manager.\"%s\" \n", com, (int)mode, fname );
1544                 return( 1 );
1545         }
1546         return( 0 );
1547 }
1548
1549
1550 int
1551 #if NeedFunctionPrototypes
1552 Chown_File ( 
1553     char        *fname,
1554     uid_t       owner,
1555     gid_t       group,
1556     char        *com
1557 )
1558 #else
1559 Chown_File ( fname, owner, group, com )
1560 char    *fname;
1561 uid_t   owner;
1562 gid_t   group;
1563 char    *com;
1564 #endif
1565 {
1566         errno = 0;
1567         if ( chown (fname, owner, group) != 0) {
1568                 USAGE2("%s : Failed in setteing of the owner and the group of the following files. \tPlease change to the file of the owner and the group of following ID \tafter the inquiry the system manager.\"%s\"\n", com, fname );
1569                 USAGE1("\t\t Owner ID ¡§%d\n", (int)owner);
1570                 USAGE1("\t\t Group ID ¡§%d\n", (int)group);
1571                 return( 1 );
1572         }
1573         return( 0 );
1574 }
1575
1576
1577
1578
1579 int     
1580 #if NeedFunctionPrototypes
1581 ChkPcfFontFile( char    *filename )
1582 #else
1583 ChkPcfFontFile( filename )
1584 char    *filename;
1585 #endif
1586 {
1587         char    *suffix;
1588
1589         if ( !filename ) {
1590                 return  -1;
1591         }
1592         suffix = ( char * )strrchr( filename, '.' );
1593         if ( !suffix ) {
1594                 return  -1;
1595         }
1596
1597         return  strcmp( PCFSUFFIX, suffix );
1598 }
1599
1600
1601 int     
1602 #if NeedFunctionPrototypes
1603 ChkSnfFontFile( char    *filename )
1604 #else
1605 ChkSnfFontFile( filename )
1606 char    *filename;
1607 #endif
1608 {
1609         char    *suffix;
1610
1611         if ( !filename ) {
1612                 return  -1;
1613         }
1614         suffix = ( char * )strrchr( filename, '.' );
1615         if ( !suffix ) {
1616                 return  -1;
1617         }
1618
1619         return  strcmp( SNFSUFFIX, suffix );
1620 }
1621
1622
1623 char    *
1624 #if NeedFunctionPrototypes
1625 get_cmd_path( 
1626     char        *path,
1627     char        *cmd
1628 )
1629 #else
1630 get_cmd_path( path, cmd )
1631 char    *path;
1632 char    *cmd;
1633 #endif
1634 {
1635         char    *cmd_path;
1636         struct stat     st;
1637         char    *end;
1638         char    chr_sv;
1639
1640         if ( !path || !cmd ) {
1641                 return  NULL;
1642         }
1643
1644         for ( ; end = ( char * )strchr( path, ':' ); path = end + 1 ) {
1645                 chr_sv = *end;
1646                 *end = NULL;
1647                 AllocString( cmd_path, path, NULL ) ;
1648                 *end = chr_sv;
1649
1650                 AddString( cmd_path, "/", NULL ) ;
1651                 AddString( cmd_path, cmd, NULL ) ;
1652
1653                 if ( stat( cmd_path, &st ) == 0 ) {
1654                     if( st.st_mode & S_IFREG ) {
1655                         cmd_path = realloc( cmd_path, strlen( cmd_path ) + 1 );
1656                         return  cmd_path;
1657                     } else      continue ;
1658                 }
1659                 FreeString( cmd_path ) ;
1660         }
1661         AllocString( cmd_path, path, NULL ) ;
1662         AddString( cmd_path, "/", NULL ) ;
1663         AddString( cmd_path, cmd, NULL ) ;
1664         if ( stat( cmd_path, &st ) == 0 ) {
1665             if( st.st_mode & S_IFREG ) {
1666                 cmd_path = realloc( cmd_path, strlen( cmd_path ) + 1 );
1667                 return  cmd_path;
1668             }
1669         }
1670         free( cmd_path );
1671         return  NULL;
1672 }
1673
1674 int
1675 #if NeedFunctionPrototypes
1676 SetCmdPath( 
1677     char        *com ,
1678     char        **path ,
1679     char        *dflt_path ,
1680     char        *cmd 
1681 )
1682 #else
1683 SetCmdPath( com, path, dflt_path, cmd )
1684 char    *com ;
1685 char    **path ;
1686 char    *dflt_path ;
1687 char    *cmd ;
1688 #endif
1689 {
1690         struct  stat    statbuf ;
1691         char    *pbuf ;
1692
1693         if( stat( dflt_path, &statbuf ) ){
1694             if( !( pbuf = (char *)get_cmd_path( getenv( "PATH" ), cmd )) ){
1695                 USAGE2("%s: There is not \"%s\" command in \"PATH\".\n", com, cmd ) ;
1696                 return -1 ;
1697             }
1698             if( stat( pbuf, &statbuf ) ){
1699                 USAGE2("%s: There is not \"%s\" command.\n", com, cmd ) ;
1700                 return STAT_ERROR ;
1701             }
1702             if( !(statbuf.st_mode & S_IXUSR) ){
1703                 USAGE2("%s: \"%s\" command don't have permission to execute.\n", com, cmd ) ;
1704                 return STAT_ERROR ;
1705             }
1706         }else{
1707             if( !(statbuf.st_mode & S_IXUSR) ){
1708                 USAGE2("%s: \"%s\" command don't have permission to execute.\n", com, cmd ) ;
1709                 return STAT_ERROR ;
1710             }
1711             pbuf = dflt_path ;
1712         }
1713         *path = pbuf ;
1714         return 0 ;
1715 }
1716
1717 /*****************************< end of oakfuty.c >************************/