backport
[AROS.git] / workbench / c / Lock.c
blob8c0af0357fdd835fe236ce383233fc796d80efc1
1 /*
2 Copyright © 1995-2007, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Lock - Set the write protect status of a device.
6 Lang: english
7 */
9 /**************************************************************************
11 NAME
12 Lock
14 FORMAT
15 Lock <drive> [ON|OFF] [<passkey>]
17 SYNOPSIS
18 DRIVE/A,ON/S,OFF/S,PASSKEY
20 LOCATION
23 FUNCTION
24 Lock will cause the specified device or partition to be made write-
25 protected or write-enabled. This write protection is a soft write
26 protection which is handled by the volume filesystem. Hence the
27 protection will be reset (to writable) on the next system reboot.
29 It is possible to specify an optional passkey which can be used to
30 password protect the locking. The same passkey that is used to lock
31 the volume must be used to unlock the volume. The passkey may be
32 any number of characters in length.
34 The volume given MUST be the device or root volume name, not an
35 assign.
37 EXAMPLE
39 1.SYS:> Lock Work:
41 This will lock the volume called Work: without a passkey.
43 1.SYS:> Lock Work:
44 1.SYS:> MakeDir Work:SomeDir
45 Can't create directory Work:Test
46 MakeDir: Disk is write-protected
48 The volume Work: is locked, so it is impossible to create a
49 directory.
51 1.SYS:> Lock Work: OFF
53 This will unlock the volume work.
55 1.SYS:> Lock Work: MyPassword
57 This will lock Work: with the passkey "MyPassword"
59 **************************************************************************/
61 #include <exec/types.h>
62 #include <exec/memory.h>
63 #include <dos/dosextens.h>
64 #include <dos/filesystem.h>
65 #include <dos/rdargs.h>
66 #include <proto/exec.h>
67 #include <proto/dos.h>
68 #include <utility/tagitem.h>
70 #define ARG_TEMPLATE "DRIVE/A,ON/S,OFF/S,PASSKEY"
71 #define ARG_DRIVE 0
72 #define ARG_ON 1
73 #define ARG_OFF 2
74 #define ARG_PASSKEY 3
75 #define TOTAL_ARGS 4
77 const TEXT version[] = "$VER: Lock 41.2 (16.1.1999)";
78 static const char exthelp[] =
79 "Lock : Set the write-protect state of a volume\n"
80 "\tDRIVE/A The name of the volume to lock\n"
81 "\tON/S Turn write protect on\n"
82 "\tOFF/S Turn write protect off\n"
83 "\tPASSKEY The password string for the lock (optional)\n";
85 int lockDevice(struct IOFileSys *iofs, STRPTR key)
87 if( iofs->io_Union.io_MOUNT_MODE.io_MountMode & MMF_LOCKED )
89 /* We are already locked? What do we do? */
90 return ERROR_DISK_WRITE_PROTECTED;
92 iofs->io_Union.io_MOUNT_MODE.io_MountMode = MMF_LOCKED;
93 iofs->io_Union.io_MOUNT_MODE.io_Mask = MMF_LOCKED;
94 iofs->io_Union.io_MOUNT_MODE.io_Password = key;
96 DoIO((struct IORequest *)iofs);
97 return iofs->io_DosError;
100 int unlockDevice(struct IOFileSys *iofs, STRPTR key)
102 if(!(iofs->io_Union.io_MOUNT_MODE.io_MountMode & MMF_LOCKED))
104 /* We are not locked, so lets return success. */
105 return 0;
107 /* We are locked, lets try and unlock it */
108 iofs->io_Union.io_MOUNT_MODE.io_MountMode = 0;
109 iofs->io_Union.io_MOUNT_MODE.io_Mask = MMF_LOCKED;
110 iofs->io_Union.io_MOUNT_MODE.io_Password = key;
112 DoIO((struct IORequest *)iofs);
113 return iofs->io_DosError;
116 int __nocommandline;
118 int main(void)
120 struct Process *pr = (struct Process *)FindTask(NULL);
121 struct RDArgs *rd, *rda = NULL;
122 IPTR args[TOTAL_ARGS] = { NULL, FALSE, FALSE, NULL };
123 struct IOFileSys *iofs;
124 struct DevProc *dp;
125 int error = 0;
127 if((rda = AllocDosObject(DOS_RDARGS, NULL)))
129 rda->RDA_ExtHelp = (STRPTR)exthelp;
130 if((rd = ReadArgs(ARG_TEMPLATE, args, NULL)))
132 /* Firstly, find out the volume */
133 dp = GetDeviceProc((STRPTR)args[ARG_DRIVE], NULL);
135 if( dp == NULL || dp->dvp_DevNode == NULL
136 || ( dp->dvp_DevNode->dol_Type != DLT_DEVICE
137 && dp->dvp_DevNode->dol_Type != DLT_VOLUME )
140 if( dp != NULL )
141 error = ERROR_OBJECT_WRONG_TYPE;
142 else
143 error = IoErr();
145 else
148 It is a real volume, not an assign...
149 We send the device a I/O request.
151 iofs = (struct IOFileSys *)CreateIORequest(
152 &pr->pr_MsgPort, sizeof(struct IOFileSys)
154 if( iofs != NULL )
156 /* First of find out current state. */
157 iofs->io_Union.io_MOUNT_MODE.io_Mask = 0;
158 iofs->IOFS.io_Device = (struct Device *)dp->dvp_Port;
159 iofs->IOFS.io_Command = FSA_MOUNT_MODE;
161 DoIO((struct IORequest *)iofs);
162 if (args[ARG_ON] && args[ARG_OFF])
164 /* Both are set? */
165 error = ERROR_TOO_MANY_ARGS;
167 else if (args[ARG_OFF])
169 error = unlockDevice(iofs, (STRPTR)args[ARG_PASSKEY]);
171 else
173 error = lockDevice(iofs, (STRPTR)args[ARG_PASSKEY]);
175 DeleteIORequest((struct IORequest *)iofs);
177 } /* iofs != NULL */
178 else
179 error = ERROR_NO_FREE_STORE;
182 if( dp )
183 FreeDeviceProc(dp);
185 FreeArgs(rd);
186 } /* if( ReadArgs() ) */
187 else
188 error = IoErr();
190 FreeDosObject(DOS_RDARGS, rda);
191 } /* if( AllocDosObject() ) */
192 else
193 error = ERROR_NO_FREE_STORE;
195 if( error != 0 )
197 PrintFault(error, "Lock");
198 return RETURN_FAIL;
201 SetIoErr(0);
202 return 0;