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