386BSD 0.1 development
[unix-history] / usr / othersrc / contrib / isode / dsap / common / file_print.c
CommitLineData
04c6839a
WJ
1#include "quipu/util.h"
2#include "quipu/attrvalue.h"
3#include "quipu/malloc.h"
4
5char * parse_file = NULLCP;
6RDN parse_rdn = NULLRDN;
7extern LLog * log_dsap;
8extern AttributeType last_at;
9extern int avs_count;
10extern char dsa_mode;
11
12static char used_temp;
13
14#ifndef SYS5
15/* ARGSUSED */
16#endif
17
18int dflt_attr_file (at,x,full)
19AttributeType at;
20AttributeValue x;
21char full;
22{
23 /* make default file name */
24char buffer [LINESIZE];
25PS sps;
26char *pathend = NULLCP;
27char val, *_isodefile();
28char *path = NULLCP;
29struct file_syntax * fs;
30unsigned last_heap;
31
32 used_temp = FALSE;
33
34 fs = (struct file_syntax *) x->av_struct;
35
36 if ((parse_file != NULLCP) && ((pathend = rindex (parse_file,'/')) != NULLCP)) {
37 val = *++pathend;
38 *pathend = 0;
39 path = parse_file;
40 }
41
42 last_heap = mem_heap; /* put PS on general heap */
43 mem_heap = 0;
44
45 sps = ps_alloc (str_open);
46 if (str_setup (sps,buffer,LINESIZE,1) == NOTOK) {
47 mem_heap = last_heap;
48 ps_free (sps);
49 return (NOTOK);
50 }
51 mem_heap = last_heap;
52
53 if (parse_rdn == NULLRDN) {
54 ps_printf (sps,"/tmp/%s_%d",at->oa_ot.ot_name,getpid());
55 used_temp = TRUE;
56 } else {
57 rdn_print (sps,parse_rdn,EDBOUT);
58 ps_printf (sps,".%s",at->oa_ot.ot_name);
59 }
60 *sps->ps_ptr = 0;
61 ps_free (sps);
62
63#ifdef SYS5
64 if ((avs_count > 1)
65 || ((full) && (strlen(buffer) > MAXFILENAMELEN))) {
66#else
67 if (avs_count > 1) {
68 /* Multi valued file attribute - can only default 1st case */
69 /* Would be much nicer if the file names were consistent. */
70 /* At the moment unreferenced files will be left undeleted */
71 /* (Unless management tools clean up ) */
72#endif
73 char *nptr, *mptr;
74 char nbuf [LINESIZE];
75 int i;
76
77 nptr = buffer;
78 mptr = nbuf;
79 used_temp = TRUE;
80
81 for (i=0 ; (*nptr!=0) && (i < MAXFILENAMELEN-6) ; nptr++)
82 if (isalpha(*nptr))
83 *mptr++ = *nptr, i++;
84
85 (void) strcpy (mptr,"XXXXXX");
86 if (path != NULLCP)
87 fs->fs_name = mktemp(strdup (_isodefile(path,nbuf)));
88 else
89 fs->fs_name = mktemp(strdup (nbuf));
90 if (pathend != NULLCP)
91 *pathend = val;
92
93 fs->fs_mode = 0;
94 return (OK);
95 }
96
97 if (path != NULLCP)
98 fs->fs_name = strdup (_isodefile(path,buffer));
99 else
100 fs->fs_name = strdup (buffer);
101 if (pathend != NULLCP)
102 *pathend = val;
103
104 return (OK);
105}
106
107
108
109fileattr_print (ps,y,format)
110PS ps;
111AttributeValue y;
112int format;
113{
114struct file_syntax * fs;
115int um;
116
117 fs = (struct file_syntax *) y->av_struct;
118
119 if (format != EDBOUT) {
120 if (fs->fs_attr)
121 AttrV_print (ps,fs->fs_attr,format);
122 else
123 ps_print (ps,"Internal error, need to load the file!!!");
124 } else {
125 FILE * fptr;
126 PS fps;
127
128 ps_print (ps,"{FILE}");
129
130 if (fs->fs_name == NULLCP) {
131 if (dflt_attr_file (last_at,y,0) == NOTOK) {
132 LLOG (log_dsap,LLOG_EXCEPTIONS,("Could not make default attribute file"));
133 return;
134 }
135 if (used_temp)
136 ps_print (ps,fs->fs_name);
137 } else if ( ! (fs->fs_mode & FS_DEFAULT))
138 ps_print (ps,fs->fs_name);
139
140 if (fs->fs_attr == NULLAttrV) /* already exists */
141 return;
142
143 if (fs->fs_mode & FS_CREATE) /* already written */
144 return;
145
146 um = umask (0177);
147 if ((fptr = fopen (fs->fs_name,"w")) != NULL) {
148 (void) umask (um);
149 if ((fps = ps_alloc (std_open)) == NULLPS) {
150 (void) fclose (fptr);
151 LLOG (log_dsap,LLOG_EXCEPTIONS,("Could not alloc PS file '%s'",fs->fs_name));
152 return;
153 }
154 if ((std_setup (fps,fptr)) == NOTOK) {
155 (void) fclose (fptr);
156 ps_free (fps);
157 LLOG (log_dsap,LLOG_EXCEPTIONS,("Could not open PS file '%s'",fs->fs_name));
158 return;
159 }
160 } else {
161 (void) umask (um);
162 LLOG ( log_dsap,LLOG_EXCEPTIONS,("Could not open attribute file '%s'",fs->fs_name));
163 return;
164 }
165 AttrV_print (fps,fs->fs_attr,FILEOUT);
166 if (dsa_mode) {
167 AttrV_free (fs->fs_attr);
168 fs->fs_attr = NULLAttrV;
169 }
170 (void) fclose (fptr);
171 ps_free (fps);
172 fs->fs_mode |= FS_CREATE;
173
174 DLOG (log_dsap,LLOG_DEBUG,("Written photo file '%s'",fs->fs_name));
175 }
176 return;
177
178}
179
180