updating OID to new sambaAccount
[Samba.git] / source / utils / nbio.c
blob4866e52d4ecdd4ccf25abd7ea0d4b60a0ada92ae
1 #define NBDEBUG 0
3 /*
4 Unix SMB/Netbios implementation.
5 Version 1.9.
6 SMB torture tester
7 Copyright (C) Andrew Tridgell 1997-1998
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #define NO_SYSLOG
26 #include "includes.h"
28 #define MAX_FILES 1000
30 static char buf[70000];
31 extern int line_count;
33 static struct {
34 int fd;
35 int handle;
36 } ftable[MAX_FILES];
38 static struct cli_state *c;
40 static void sigsegv(int sig)
42 char line[200];
43 printf("segv at line %d\n", line_count);
44 slprintf(line, sizeof(line), "/usr/X11R6/bin/xterm -e gdb /proc/%d/exe %d",
45 (int)getpid(), (int)getpid());
46 system(line);
47 exit(1);
50 void nb_setup(struct cli_state *cli)
52 signal(SIGSEGV, sigsegv);
53 c = cli;
57 void nb_unlink(char *fname)
59 strupper(fname);
61 if (!cli_unlink(c, fname)) {
62 #if NBDEBUG
63 printf("(%d) unlink %s failed (%s)\n",
64 line_count, fname, cli_errstr(c));
65 #endif
69 void nb_open(char *fname, int handle, int size)
71 int fd, i;
72 int flags = O_RDWR|O_CREAT;
73 size_t st_size;
74 static int count;
76 strupper(fname);
78 if (size == 0) flags |= O_TRUNC;
80 fd = cli_open(c, fname, flags, DENY_NONE);
81 if (fd == -1) {
82 #if NBDEBUG
83 printf("(%d) open %s failed for handle %d (%s)\n",
84 line_count, fname, handle, cli_errstr(c));
85 #endif
86 return;
88 cli_getattrE(c, fd, NULL, &st_size, NULL, NULL, NULL);
89 if (size > st_size) {
90 #if NBDEBUG
91 printf("(%d) needs expanding %s to %d from %d\n",
92 line_count, fname, size, (int)st_size);
93 #endif
94 } else if (size < st_size) {
95 #if NBDEBUG
96 printf("(%d) needs truncating %s to %d from %d\n",
97 line_count, fname, size, (int)st_size);
98 #endif
100 for (i=0;i<MAX_FILES;i++) {
101 if (ftable[i].handle == 0) break;
103 if (i == MAX_FILES) {
104 printf("file table full for %s\n", fname);
105 exit(1);
107 ftable[i].handle = handle;
108 ftable[i].fd = fd;
109 if (count++ % 100 == 0) {
110 printf(".");
114 void nb_write(int handle, int size, int offset)
116 int i;
118 if (buf[0] == 0) memset(buf, 1, sizeof(buf));
120 for (i=0;i<MAX_FILES;i++) {
121 if (ftable[i].handle == handle) break;
123 if (i == MAX_FILES) {
124 #if NBDEBUG
125 printf("(%d) nb_write: handle %d was not open size=%d ofs=%d\n",
126 line_count, handle, size, offset);
127 #endif
128 return;
130 if (cli_smbwrite(c, ftable[i].fd, buf, offset, size) != size) {
131 printf("(%d) write failed on handle %d, fd %d \
132 errno %d (%s)\n", line_count, handle, ftable[i].fd, errno, strerror(errno));
133 if (errno == ENOSPC) {
134 printf("Halting.\n");
135 fflush(stdout);
136 fflush(stderr);
137 exit(3);
142 void nb_read(int handle, int size, int offset)
144 int i, ret;
146 for (i=0;i<MAX_FILES;i++) {
147 if (ftable[i].handle == handle) break;
149 if (i == MAX_FILES) {
150 printf("(%d) nb_read: handle %d was not open size=%d ofs=%d\n",
151 line_count, handle, size, offset);
152 return;
154 if ((ret=cli_read(c, ftable[i].fd, buf, offset, size)) != size) {
155 #if NBDEBUG
156 printf("(%d) read failed on handle %d ofs=%d size=%d res=%d fd %d errno %d (%s)\n",
157 line_count, handle, offset, size, ret, ftable[i].fd, errno, strerror(errno));
158 #endif
162 void nb_close(int handle)
164 int i;
165 for (i=0;i<MAX_FILES;i++) {
166 if (ftable[i].handle == handle) break;
168 if (i == MAX_FILES) {
169 printf("(%d) nb_close: handle %d was not open\n",
170 line_count, handle);
171 return;
173 cli_close(c, ftable[i].fd);
174 ftable[i].handle = 0;
177 void nb_mkdir(char *fname)
179 strupper(fname);
181 if (!cli_mkdir(c, fname)) {
182 #if NBDEBUG
183 printf("mkdir %s failed (%s)\n",
184 fname, cli_errstr(c));
185 #endif
189 void nb_rmdir(char *fname)
191 strupper(fname);
193 if (!cli_rmdir(c, fname)) {
194 #if NBDEBUG
195 printf("rmdir %s failed (%s)\n",
196 fname, cli_errstr(c));
197 #endif
201 void nb_rename(char *old, char *new)
203 strupper(old);
204 strupper(new);
206 if (!cli_rename(c, old, new)) {
207 #if NBDEBUG
208 printf("rename %s %s failed (%s)\n",
209 old, new, cli_errstr(c));
210 #endif
215 void nb_stat(char *fname, int size)
217 size_t st_size;
219 strupper(fname);
221 if (!cli_getatr(c, fname, NULL, &st_size, NULL)) {
222 #if NBDEBUG
223 printf("(%d) nb_stat: %s size=%d %s\n",
224 line_count, fname, size, cli_errstr(c));
225 #endif
226 return;
228 if (st_size != size) {
229 #if NBDEBUG
230 printf("(%d) nb_stat: %s wrong size %d %d\n",
231 line_count, fname, (int)st_size, size);
232 #endif
236 void nb_create(char *fname, int size)
238 nb_open(fname, 5000, size);
239 nb_close(5000);