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