e904a5f39a83ce71b19048483b786f68ec0ff094
[oweals/cde.git] / cde / lib / tt / mini_isam / isapplmw.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 /*%%  (c) Copyright 1993, 1994 Hewlett-Packard Company                   */
24 /*%%  (c) Copyright 1993, 1994 International Business Machines Corp.     */
25 /*%%  (c) Copyright 1993, 1994 Sun Microsystems, Inc.                    */
26 /*%%  (c) Copyright 1993, 1994 Novell, Inc.                              */
27 /*%%  $XConsortium: isapplmw.c /main/3 1995/10/23 11:35:35 rswiston $                                                    */
28 #ifndef lint
29 static char sccsid[] = "@(#)isapplmw.c 1.5 89/07/17 Copyr 1988 Sun Micro";
30 #endif
31 /*
32  * Copyright (c) 1988 by Sun Microsystems, Inc.
33  */
34
35 /*
36  * isapplmw.c
37  *
38  * Description:
39  *      Write Application magic string
40  */
41
42
43 #include "isam_impl.h"
44 #include <sys/file.h>
45 #include <sys/time.h>
46
47 /*
48  * string = isapplmw(isfd)
49  *
50  * Isapplmw() writes an application specific 'magic string' into ISAM
51  * file. The 'file' program (using /etc/magic) may be used to report
52  * this magic string.
53  *
54  */
55
56 int 
57 _isapplmw(int isfd, char *magicstring)
58 {
59     Fab *fab;
60     int                 ret;
61     Fcb                 *fcb;
62     char                cntl_page[CP_NKEYS_OFF];
63
64     /*
65      * Get File Access Block.
66      */
67     if ((fab = _isfd_find(isfd)) == NULL) {
68         _setiserrno2(ENOTOPEN, '9', '0');
69         return (ISERROR);
70     }
71
72     /*
73      * Check that the open mode was ISOUTPUT, or ISINOUT.
74      */
75     if (fab->openmode != OM_OUTPUT && fab->openmode != OM_INOUT) {
76         _setiserrno2(ENOTOPEN, '9', '0');
77         return (ISERROR);
78     }
79
80     /*
81      * Check the length of the magic string
82      */
83     if ((int)strlen(magicstring) > CP_APPLMAGIC_LEN) {
84         _setiserrno2(EBADARG, '9', '0');
85         return (ISERROR);
86     }
87
88     _isam_entryhook();
89
90     /*
91      * Get FCB corresponding to the isfhandle handle.
92      */
93     if ((fcb = _openfcb(&fab->isfhandle, &fab->errcode)) == NULL) {
94         _isam_exithook();
95         return (ISERROR);
96     }
97
98     /* Write the new data */
99     
100     _isseekpg(fcb->datfd, ISCNTLPGOFF);
101     (void)read(fcb->datfd, cntl_page, sizeof(cntl_page));
102     strncpy(cntl_page + CP_APPLMAGIC_OFF, magicstring, CP_APPLMAGIC_LEN);
103     _isseekpg(fcb->datfd, ISCNTLPGOFF);
104     (void)write(fcb->datfd, cntl_page, sizeof(cntl_page));
105
106     _amseterrcode(&fab->errcode, ISOK);
107     _isam_exithook();
108     ret = ISOK;
109
110     _seterr_errcode(&fab->errcode);
111
112     return (ret);                            /* Successful write */
113 }