odds and sods
authorAndrew Tridgell <tridge@samba.org>
Mon, 25 Jun 2007 04:35:40 +0000 (14:35 +1000)
committerAndrew Tridgell <tridge@samba.org>
Mon, 25 Jun 2007 04:35:40 +0000 (14:35 +1000)
250 files changed:
Time.c [new file with mode: 0644]
Xrintk.c [new file with mode: 0644]
a.c [new file with mode: 0644]
aa.c [new file with mode: 0644]
abs.c [new file with mode: 0644]
addstr.c [new file with mode: 0644]
aiocache.c [new file with mode: 0644]
align.c [new file with mode: 0644]
alignsmb.c [new file with mode: 0644]
alignspeed.c [new file with mode: 0644]
allchars.c [new file with mode: 0644]
alloc.c [new file with mode: 0644]
andy.c [new file with mode: 0644]
ascii.c [new file with mode: 0644]
badcode.c [new file with mode: 0644]
bar.c [new file with mode: 0644]
bits.c [new file with mode: 0644]
bool.c [new file with mode: 0644]
boolbit.c [new file with mode: 0644]
brokenlinks.c [new file with mode: 0644]
bsd_getdents.c [new file with mode: 0644]
bsd_telldir.c [new file with mode: 0644]
bsort.c [new file with mode: 0644]
bss.c [new file with mode: 0644]
bzero.c [new file with mode: 0644]
ccache_parse.c [new file with mode: 0644]
charset.c [new file with mode: 0644]
chmode.c [new file with mode: 0644]
compare.c [new file with mode: 0644]
config-pidtest.c [new file with mode: 0644]
cpu_hog.c [new file with mode: 0644]
cr.c [new file with mode: 0644]
crc32.c [new file with mode: 0644]
creatreuser.c [new file with mode: 0644]
csc.c [new file with mode: 0644]
csum.c [new file with mode: 0644]
ctdb_parser.c [new file with mode: 0644]
cvt.c [new file with mode: 0644]
debug.c [new file with mode: 0644]
decimal.c [new file with mode: 0644]
detect2.c [new file with mode: 0644]
dirtest.c [new file with mode: 0644]
dirtest2.c [new file with mode: 0644]
display_sid.c [new file with mode: 0644]
double.c [new file with mode: 0644]
drivenames.c [new file with mode: 0644]
dst.c [new file with mode: 0644]
dump.c [new file with mode: 0644]
dump_file.c [new file with mode: 0644]
dumpxattr.c [new file with mode: 0644]
e35.c [new file with mode: 0644]
echo.c [new file with mode: 0644]
endpoint.c [new file with mode: 0644]
epoll.c [new file with mode: 0644]
eq.c [new file with mode: 0644]
errno.c [new file with mode: 0644]
error.c [new file with mode: 0644]
etst.c [new file with mode: 0644]
f.c [new file with mode: 0644]
fake_uname.c [new file with mode: 0644]
fchown.c [new file with mode: 0644]
fd_filename.c [new file with mode: 0644]
fifo1.c [new file with mode: 0644]
fifo2.c [new file with mode: 0644]
fillmem.c [new file with mode: 0644]
fname.c [new file with mode: 0644]
fnmatch.c [new file with mode: 0644]
foo.c [new file with mode: 0644]
foo2.c [new file with mode: 0644]
foo3.c [new file with mode: 0644]
foo4.c [new file with mode: 0644]
foo5.c [new file with mode: 0644]
fopen.c [new file with mode: 0644]
forklots.c [new file with mode: 0644]
fsid.c [new file with mode: 0644]
fstest-aio.c [new file with mode: 0644]
fsync.c [new file with mode: 0644]
ftime.c [new file with mode: 0644]
fwide.c [new file with mode: 0644]
fwrite.c [new file with mode: 0644]
getgrent.c [new file with mode: 0644]
getgrouplist2.c [new file with mode: 0644]
getgroups.c [new file with mode: 0644]
gethostbyname.c [new file with mode: 0644]
getpwent.c [new file with mode: 0644]
glide2.c [new file with mode: 0644]
gls.c [new file with mode: 0644]
grp.c [new file with mode: 0644]
gz.c [new file with mode: 0644]
hashreplace.c [new file with mode: 0644]
hello.c [new file with mode: 0644]
histogram.c [new file with mode: 0644]
host.c [new file with mode: 0644]
id_mkdir.c [new file with mode: 0644]
immediate.c [new file with mode: 0644]
inotify.c [new file with mode: 0644]
int32.c [new file with mode: 0644]
iocache.c [new file with mode: 0644]
iotest.c [new file with mode: 0644]
issamba.c [new file with mode: 0644]
ival.c [new file with mode: 0644]
j.c [new file with mode: 0644]
jj.c [new file with mode: 0644]
k.c [new file with mode: 0644]
k5.c [new file with mode: 0644]
kbd.c [new file with mode: 0644]
largefile.c [new file with mode: 0644]
largefiletest.c [new file with mode: 0644]
linklist.c [new file with mode: 0644]
list_compare.c [new file with mode: 0644]
lock64.c [new file with mode: 0644]
locker2.c [new file with mode: 0644]
lockrun.c [new file with mode: 0644]
locktst.c [new file with mode: 0644]
log.c [new file with mode: 0644]
logit.c [new file with mode: 0644]
lowcase.c [new file with mode: 0644]
lseek.c [new file with mode: 0644]
m2.c [new file with mode: 0644]
malloc.c [new file with mode: 0644]
map.c [new file with mode: 0644]
map_table.c [new file with mode: 0644]
maxinitgroups.c [new file with mode: 0644]
maxnamelen.c [new file with mode: 0644]
maxpath.c [new file with mode: 0644]
mem_hog.c [new file with mode: 0644]
memread.c [new file with mode: 0644]
memtest2.c [new file with mode: 0644]
mksparse2.c [new file with mode: 0644]
mmap.c [new file with mode: 0644]
mmap2.c [new file with mode: 0644]
mmap_write.c [new file with mode: 0644]
mode.c [new file with mode: 0644]
modf.c [new file with mode: 0644]
movemail.c [new file with mode: 0644]
names.c [new file with mode: 0644]
newline.c [new file with mode: 0644]
nistest.c [new file with mode: 0644]
not.c [new file with mode: 0644]
noteshack.c [new file with mode: 0644]
nsslist.c [new file with mode: 0644]
null.c [new file with mode: 0644]
open.c [new file with mode: 0644]
opendir.c [new file with mode: 0644]
opentest.c [new file with mode: 0644]
oplock_irix.c [new file with mode: 0644]
parseaddr.c [new file with mode: 0644]
pass.c [new file with mode: 0644]
paste.c [new file with mode: 0644]
pclone.c [new file with mode: 0644]
posarg.c [new file with mode: 0644]
pr.c [new file with mode: 0644]
preload_getenv.c [new file with mode: 0644]
preload_mmap.c [new file with mode: 0644]
preload_notes.c [new file with mode: 0644]
printf-parse.h [new file with mode: 0644]
printf.c [new file with mode: 0644]
printk_trick.c [new file with mode: 0644]
priu64.c [new file with mode: 0644]
ptrace_exploit.c [new file with mode: 0644]
range_find.c [new file with mode: 0644]
rc4.c [new file with mode: 0644]
rc4.h [new file with mode: 0644]
read.c [new file with mode: 0644]
readdir.c [new file with mode: 0644]
regdecrypt.c [new file with mode: 0644]
rep.c [new file with mode: 0644]
rint.c [new file with mode: 0644]
rnotes.c [new file with mode: 0644]
rog.c [new file with mode: 0644]
roglib.c [new file with mode: 0644]
rpcalc.tab.c [new file with mode: 0644]
s.c [new file with mode: 0644]
sccs_csum.c [new file with mode: 0644]
sccs_uncomp.c [new file with mode: 0644]
scrolly.c [new file with mode: 0644]
seek.c [new file with mode: 0644]
segv.c [new file with mode: 0644]
select.c [new file with mode: 0644]
setenv.c [new file with mode: 0644]
setresuid.c [new file with mode: 0644]
setsched.c [new file with mode: 0644]
shift.c [new file with mode: 0644]
short.c [new file with mode: 0644]
show_umask.c [new file with mode: 0644]
signal_errno.c [new file with mode: 0644]
sigspeed.c [new file with mode: 0644]
sigtest.c [new file with mode: 0644]
sizeof2.c [new file with mode: 0644]
slink.c [new file with mode: 0644]
slist.c [new file with mode: 0644]
snprintf.c [new file with mode: 0644]
sock_sink.c [new file with mode: 0644]
sockaddr.c [new file with mode: 0644]
sockspy-random.c [new file with mode: 0644]
sort_density.c [new file with mode: 0644]
sortit.c [new file with mode: 0644]
spair_fork.c [new file with mode: 0644]
srm.c [new file with mode: 0644]
stat.c [new file with mode: 0644]
stat_nsec.c [new file with mode: 0644]
static.c [new file with mode: 0644]
str.c [new file with mode: 0644]
strcasestr.c [new file with mode: 0644]
string.c [new file with mode: 0644]
strnlen-test.c [new file with mode: 0644]
strnlen.c [new file with mode: 0644]
struct.c [new file with mode: 0644]
swaptest.c [new file with mode: 0644]
t.c [new file with mode: 0644]
tbo.c [new file with mode: 0644]
tcpproxy.c [new file with mode: 0644]
test.c [new file with mode: 0644]
testdump.c [new file with mode: 0644]
tfile.c [new file with mode: 0644]
tfloppy.c [new file with mode: 0644]
thread.c [new file with mode: 0644]
threads.c [new file with mode: 0644]
time.c [new file with mode: 0644]
timegm.c [new file with mode: 0644]
tmath.c [new file with mode: 0644]
trap.c [new file with mode: 0644]
tread.c [new file with mode: 0644]
truncate.c [new file with mode: 0644]
tst.c [new file with mode: 0644]
tstcomp.c [new file with mode: 0644]
tty.c [new file with mode: 0644]
tunion.c [new file with mode: 0644]
typeof.c [new file with mode: 0644]
tz.c [new file with mode: 0644]
ucs2.c [new file with mode: 0644]
ucs_to_utf8.c [new file with mode: 0644]
udpsock.c [new file with mode: 0644]
uid.c [new file with mode: 0644]
ulow_count.c [new file with mode: 0644]
umask.c [new file with mode: 0644]
unicode_map.c [new file with mode: 0644]
unicode_map_table.h [new file with mode: 0644]
union.c [new file with mode: 0644]
us.c [new file with mode: 0644]
utime.c [new file with mode: 0644]
ux_dgram.c [new file with mode: 0644]
void.c [new file with mode: 0644]
vslprintf.c [new file with mode: 0644]
waitpid.c [new file with mode: 0644]
watchscript.c [new file with mode: 0644]
write.c [new file with mode: 0644]
xattr.c [new file with mode: 0644]
xx.c [new file with mode: 0644]
zero_sample.c [new file with mode: 0644]

diff --git a/Time.c b/Time.c
new file mode 100644 (file)
index 0000000..63db894
--- /dev/null
+++ b/Time.c
@@ -0,0 +1,6 @@
+#include <stdio.h>
+
+main()
+{
+  printf("%d\n",time(NULL));
+}
diff --git a/Xrintk.c b/Xrintk.c
new file mode 100644 (file)
index 0000000..4b7170c
--- /dev/null
+++ b/Xrintk.c
@@ -0,0 +1,11 @@
+#include <linux/module.h>
+
+int Xrintk(const char *fmt, ...)
+{
+       return 0;
+}
+
+module_init(foo_init);
+module_exit(foo_cleanup);
+
+MODULE_DESCRIPTION("damn debug messages");
diff --git a/a.c b/a.c
new file mode 100644 (file)
index 0000000..3acbf7e
--- /dev/null
+++ b/a.c
@@ -0,0 +1,13 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#define TALLOC_ALIGN 32
+
+main()
+{
+       size_t size;
+       for (size=0;size<100;size++) {
+               size_t size2 = (size + (TALLOC_ALIGN-1)) & ~(TALLOC_ALIGN-1);
+               printf("size=%d size2=%d\n", (int)size, (int)size2);
+       }
+}
diff --git a/aa.c b/aa.c
new file mode 100644 (file)
index 0000000..568228c
--- /dev/null
+++ b/aa.c
@@ -0,0 +1,10 @@
+static void foo1(void)
+{
+       char *p = alloca(32);
+       p[0] = 0x42;
+}
+
+main()
+{
+       foo1();
+}
diff --git a/abs.c b/abs.c
new file mode 100644 (file)
index 0000000..dead822
--- /dev/null
+++ b/abs.c
@@ -0,0 +1,4 @@
+main(int argc, char *argv[])
+{
+       printf("%d\n", abs(atoi(argv[1])));
+}
diff --git a/addstr.c b/addstr.c
new file mode 100644 (file)
index 0000000..bb4e699
--- /dev/null
+++ b/addstr.c
@@ -0,0 +1,15 @@
+
+char* ipstr_list_add(char **ipstr_list, const struct in_addr *ip)
+{
+       char *new_str = NULL;
+
+       if (*ipstr_list) {
+               asprintf(&new_str, "%s:%s", *ipstr_list, inet_ntoa(*ip));
+               free(*ipstr_list);
+       } else {
+               asprintf(&new_str, "%s", inet_ntoa(*ip));
+       }
+       
+       *ipstr_list = new_str;
+       return new_str;
+}
diff --git a/aiocache.c b/aiocache.c
new file mode 100644 (file)
index 0000000..3d3de06
--- /dev/null
@@ -0,0 +1,316 @@
+#define _XOPEN_SOURCE 500
+#include <unistd.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <aio.h>
+#include <getopt.h>
+
+#include <sys/time.h>
+#include <time.h>
+
+static struct timeval tp1,tp2;
+
+static void start_timer()
+{
+       gettimeofday(&tp1,NULL);
+}
+
+static double end_timer()
+{
+       gettimeofday(&tp2,NULL);
+       return (tp2.tv_sec + (tp2.tv_usec*1.0e-6)) - 
+               (tp1.tv_sec + (tp1.tv_usec*1.0e-6));
+}
+
+
+struct aio_block {
+       struct aiocb *cb;
+       unsigned char *buffer;
+       int nread;
+       int done;
+       int blk;
+};
+
+struct aio_ring {
+       int fd;
+       unsigned block_size;
+       unsigned num_blocks;
+       struct aio_block *blocks;
+};
+
+static int a_schedule(struct aio_ring *ring, int blk)
+{
+       struct aio_block *b = &ring->blocks[blk % ring->num_blocks];
+       if (blk == b->blk) return 0;
+
+       if (b->buffer == NULL) {
+               b->buffer = malloc(ring->block_size);
+               if (b->buffer == NULL) goto failed;
+       }
+
+       if (b->cb == NULL) {
+               b->cb = malloc(sizeof(*b->cb));
+               if (b->cb == NULL) goto failed;
+       }
+
+       if (b->blk != -1 && !b->done) {
+               int ret = aio_cancel(ring->fd, b->cb);
+               if (ret == AIO_NOTCANCELED) {
+                       aio_suspend(&b->cb, 1, NULL);
+               }
+               aio_error(b->cb);
+               aio_return(b->cb);
+       }
+
+       b->blk = blk;
+       memset(b->cb, 0, sizeof(*b->cb));
+       b->cb->aio_fildes = ring->fd;
+       b->cb->aio_buf     = b->buffer;
+       b->cb->aio_nbytes  = ring->block_size;
+       b->cb->aio_offset  = blk * (off_t)ring->block_size;
+       if (aio_read(b->cb) != 0) goto failed;
+
+       b->done = 0;
+       return 0;
+
+failed:
+       free(b->buffer);
+       free(b->cb);
+       b->buffer = NULL;
+       b->cb     = NULL;
+       b->blk    = -1;
+       return -1;
+}
+
+static int a_wait(struct aio_ring *ring, int blk)
+{
+       struct aio_block *b = &ring->blocks[blk % ring->num_blocks];
+
+       if (b->blk != blk) return -1;
+
+       if (b->done) return 0;
+
+       if (aio_suspend(&b->cb, 1, NULL) != 0 ||
+           aio_error(b->cb) != 0) {
+               free(b->buffer);
+               free(b->cb);
+               b->buffer = NULL;
+               b->cb     = NULL;
+               b->blk    = -1;
+               return -1;
+       }
+
+       b->done = 1;
+       b->nread = aio_return(b->cb);
+       if (b->nread < 0) return -1;
+
+       return 0;
+}
+
+
+static ssize_t a_read(struct aio_ring *ring, void *buf, size_t count, off_t offset)
+{
+       int blk_start = offset / ring->block_size;
+       int blk_end   = (offset+count-1) / ring->block_size;
+       int blk_sched = blk_start + (ring->num_blocks/2);
+       int i;
+       ssize_t total=0;
+
+       if (blk_sched < blk_end) blk_sched = blk_end;
+
+       for (i=blk_start;i<=blk_sched;i++) {
+               a_schedule(ring, i);
+       }
+
+       while (count) {
+               int n = count;
+               int blk_offset = offset % ring->block_size;
+               int blk = offset / ring->block_size;
+               struct aio_block *b = &ring->blocks[blk % ring->num_blocks];
+               ssize_t nread;
+               if (n > ring->block_size - blk_offset) {
+                       n = ring->block_size - blk_offset;
+               }
+               if (a_wait(ring, blk) != 0) {
+                       nread = pread(ring->fd, buf, n, offset);
+                       printf("sync fallback\n");
+               } else {
+                       if (n > b->nread) n = b->nread;
+                       memcpy(buf, b->buffer + blk_offset, n);
+                       nread = n;
+               }
+               if (nread <= 0) {
+                       if (total > 0) return total;
+                       return nread;
+               }
+               total += nread;
+               buf = (void *)(nread + (char *)buf);
+               count -= nread;
+               offset += nread;
+       }
+
+       return total;
+}
+
+static struct aio_ring *a_init(int fd, int block_size, int nblocks)
+{
+       struct aio_ring *ring;
+       int i;
+
+       ring = malloc(sizeof(*ring));
+       if (ring == NULL) return NULL;
+
+       ring->fd = fd;
+       ring->num_blocks = nblocks;
+       ring->block_size = block_size;
+       ring->blocks = calloc(nblocks, sizeof(ring->blocks[0]));
+       if (ring->blocks == NULL) {
+               free(ring);
+               return NULL;
+       }
+
+       for (i=0;i<nblocks;i++) {
+               ring->blocks[i].blk = -1;
+       }
+       return ring;
+}
+
+static void a_close(struct aio_ring *ring)
+{
+       int i;
+
+       for (i=0;i<ring->num_blocks;i++) {
+               struct aio_block *b = &ring->blocks[i];
+               if (b->blk != -1 && !b->done) {
+                       int ret = aio_cancel(ring->fd, b->cb);
+                       if (ret == AIO_NOTCANCELED) {
+                               aio_suspend(&b->cb, 1, NULL);
+                       }
+                       aio_error(b->cb);
+                       aio_return(b->cb);
+               }
+               if (b->cb) free(b->cb);
+               if (b->buffer) free(b->buffer);
+       }
+       free(ring);
+}
+
+static void async_test(const char *fname, int block_size, int nblocks, int rsize)
+{
+       int fd;
+       struct aio_ring *ring;
+       off_t offset=0;
+       char buf[rsize];
+       int i=0;
+
+       offset=0;
+       start_timer();
+       fd = open(fname, O_RDONLY);
+       if (fd == -1) {
+               perror(fname);
+               exit(1);
+       }
+
+       ring = a_init(fd, block_size, nblocks);
+       if (ring == NULL) {
+               fprintf(stderr, "a_init faild\n");
+               exit(1);
+       }
+
+       while (a_read(ring, buf, sizeof(buf), offset) > 0) {
+               offset += sizeof(buf);
+               if (++i % 5 == 0) {
+                       offset -= 2*sizeof(buf);
+               }
+       }
+
+       printf("async: %.3f MByte/sec\n", 1.0e-6 * offset / end_timer());
+       a_close(ring);
+}
+
+static void sync_test(const char *fname, int rsize)
+{
+       int fd;
+       off_t offset=0;
+       char buf[rsize];
+       int i=0;
+
+       offset=0;
+       start_timer();
+       fd = open(fname, O_RDONLY);
+       if (fd == -1) {
+               perror(fname);
+               exit(1);
+       }
+
+       while (pread(fd, buf, sizeof(buf), offset) > 0) {
+               offset += sizeof(buf);
+               if (++i % 5 == 0) {
+                       offset -= 2*sizeof(buf);
+               }
+       }
+
+       printf("sync : %.3f MByte/sec\n", 1.0e-6 * offset / end_timer());
+       close(fd);
+}
+
+static void usage(void)
+{
+       printf("Usage: aiocache [options] <filename>\n");
+       printf(
+"Options:\n" \
+" -b block_size\n" \
+" -n nblocks\n" \
+" -r readsize\n");
+}
+
+int main(int argc, const char *argv[])
+{
+       const char *fname;
+       int opt;
+       int block_size = 1024*1024;
+       int nblocks = 100;
+       int rsize = 32*1024;
+
+       while ((opt = getopt(argc, argv, "b:n:r:")) != -1) {
+               switch (opt) {
+               case 'b':
+                       block_size = strtol(optarg, NULL, 0);
+                       break;
+               case 'n':
+                       nblocks = strtol(optarg, NULL, 0);
+                       break;
+               case 'r':
+                       rsize = strtol(optarg, NULL, 0);
+                       break;
+               default:
+                       printf("Invalid option '%c'\n", opt);
+                       usage();
+                       exit(1);
+               }
+       }
+
+       argv += optind;
+       argc -= optind;
+
+       if (argc < 1) {
+               usage();
+               exit(1);
+       }
+
+       fname = argv[0];
+
+       printf("Testing with block_size=%d nblocks=%d rsize=%d\n",
+              block_size,nblocks,rsize);
+       
+       async_test(fname, block_size, nblocks, rsize);
+       sync_test(fname, rsize);
+       async_test(fname, block_size, nblocks, rsize);
+       sync_test(fname, rsize);
+
+       return 0;       
+}
diff --git a/align.c b/align.c
new file mode 100644 (file)
index 0000000..e2e962b
--- /dev/null
+++ b/align.c
@@ -0,0 +1,13 @@
+struct foo {
+       char b[1];
+       double x;
+};
+
+
+main()
+{
+       char buf[1024];
+       struct foo *f;
+       f = (struct foo *)&buf[3];
+       printf("%d\n", (&f.x) - (&f.b));
+}
diff --git a/alignsmb.c b/alignsmb.c
new file mode 100644 (file)
index 0000000..020bca5
--- /dev/null
@@ -0,0 +1,57 @@
+#include <stdio.h>
+
+typedef unsigned __u32;
+typedef unsigned char __u8;
+typedef unsigned short __u16;
+
+#pragma pack(1)
+
+# define offsetof(T,F) ((unsigned int)((char *)&((T *)0L)->F - (char *)0L))
+
+struct smb_hdr {
+       __u32 smb_buf_length;   /* big endian on wire *//* BB length is only two or three bytes - with one or two byte type preceding it but that is always zero - we could mask the type byte off just in case BB */
+       __u8 Protocol[4];
+       __u8 Command;
+       union {
+               struct {
+                       __u8 ErrorClass;
+                       __u8 Reserved;
+                       __u16 Error;    /* note: treated as little endian (le) on wire */
+               } DosError;
+               __u32 CifsError;        /* note: le */
+       } Status;
+       __u8 Flags;
+       __u16 Flags2;           /* note: le */
+       __u16 PidHigh;          /* note: le */
+       union {
+               struct {
+                       __u32 SequenceNumber;  /* le */
+                       __u32 Reserved; /* zero */
+               } Sequence;
+               __u8 SecuritySignature[8];      /* le */
+       } Signature;
+       __u8 pad[2];
+       __u16 Tid;
+       __u16 Pid;              /* note: le */
+       __u16 Uid;
+       __u16 Mid;
+       __u8 WordCount;
+};
+
+struct smb_hdr h;
+
+int main(void)
+{
+       printf("size is %d\n", sizeof(h));
+
+       h.Status.CifsError = 3;
+
+       printf("error is at %d\n", offsetof(struct smb_hdr, Status.CifsError));
+
+       
+       h.Status.CifsError++;
+
+       printf("error is %d\n", h.Status.CifsError++);
+
+       return 0;
+}
diff --git a/alignspeed.c b/alignspeed.c
new file mode 100644 (file)
index 0000000..70d7e6e
--- /dev/null
@@ -0,0 +1,142 @@
+#include <stdio.h>
+#include <sys/time.h>
+#include <time.h>
+
+static struct timeval tp1,tp2;
+
+static void start_timer()
+{
+       gettimeofday(&tp1,NULL);
+}
+
+static double end_timer()
+{
+       gettimeofday(&tp2,NULL);
+       return (tp2.tv_sec + (tp2.tv_usec*1.0e-6)) - 
+               (tp1.tv_sec + (tp1.tv_usec*1.0e-6));
+}
+
+typedef unsigned short uint16;
+typedef unsigned int uint32;
+
+#if (defined(__powerpc__) && defined(__GNUC__))
+static __inline__ uint16_t ld_le16(const uint16_t *addr)
+{
+       uint16_t val;
+       __asm__ ("lhbrx %0,0,%1" : "=r" (val) : "r" (addr), "m" (*addr));
+       return val;
+}
+
+static __inline__ void st_le16(uint16_t *addr, const uint16_t val)
+{
+       __asm__ ("sthbrx %1,0,%2" : "=m" (*addr) : "r" (val), "r" (addr));
+}
+
+static __inline__ uint32_t ld_le32(const uint32_t *addr)
+{
+       uint32_t val;
+       __asm__ ("lwbrx %0,0,%1" : "=r" (val) : "r" (addr), "m" (*addr));
+       return val;
+}
+
+static __inline__ void st_le32(uint32_t *addr, const uint32_t val)
+{
+       __asm__ ("stwbrx %1,0,%2" : "=m" (*addr) : "r" (val), "r" (addr));
+}
+#define HAVE_ASM_BYTEORDER 1
+#endif
+
+
+
+#undef CAREFUL_ALIGNMENT
+
+/* we know that the 386 can handle misalignment and has the "right" 
+   byteorder */
+#if defined(__i386__)
+#define CAREFUL_ALIGNMENT 0
+#endif
+
+#ifndef CAREFUL_ALIGNMENT
+#define CAREFUL_ALIGNMENT 1
+#endif
+
+#define CVAL(buf,pos) ((uint_t)(((const uint8_t *)(buf))[pos]))
+#define CVAL_NC(buf,pos) (((uint8_t *)(buf))[pos]) /* Non-const version of CVAL */
+#define PVAL(buf,pos) (CVAL(buf,pos))
+#define SCVAL(buf,pos,val) (CVAL_NC(buf,pos) = (val))
+
+#if HAVE_ASM_BYTEORDER
+
+#define  _PTRPOS(buf,pos) (((const uint8_t *)buf)+(pos))
+#define SVAL(buf,pos) ld_le16((const uint16_t *)_PTRPOS(buf,pos))
+#define IVAL(buf,pos) ld_le32((const uint32_t *)_PTRPOS(buf,pos))
+#define SSVAL(buf,pos,val) st_le16((uint16_t *)_PTRPOS(buf,pos), val)
+#define SIVAL(buf,pos,val) st_le32((uint32_t *)_PTRPOS(buf,pos), val)
+#define SVALS(buf,pos) ((int16_t)SVAL(buf,pos))
+#define IVALS(buf,pos) ((int32_t)IVAL(buf,pos))
+#define SSVALS(buf,pos,val) SSVAL((buf),(pos),((int16_t)(val)))
+#define SIVALS(buf,pos,val) SIVAL((buf),(pos),((int32_t)(val)))
+
+#elif CAREFUL_ALIGNMENT
+
+#define SVAL(buf,pos) (PVAL(buf,pos)|PVAL(buf,(pos)+1)<<8)
+#define IVAL(buf,pos) (SVAL(buf,pos)|SVAL(buf,(pos)+2)<<16)
+#define SSVALX(buf,pos,val) (CVAL_NC(buf,pos)=(uint8_t)((val)&0xFF),CVAL_NC(buf,pos+1)=(uint8_t)((val)>>8))
+#define SIVALX(buf,pos,val) (SSVALX(buf,pos,val&0xFFFF),SSVALX(buf,pos+2,val>>16))
+#define SVALS(buf,pos) ((int16_t)SVAL(buf,pos))
+#define IVALS(buf,pos) ((int32_t)IVAL(buf,pos))
+#define SSVAL(buf,pos,val) SSVALX((buf),(pos),((uint16_t)(val)))
+#define SIVAL(buf,pos,val) SIVALX((buf),(pos),((uint32_t)(val)))
+#define SSVALS(buf,pos,val) SSVALX((buf),(pos),((int16_t)(val)))
+#define SIVALS(buf,pos,val) SIVALX((buf),(pos),((int32_t)(val)))
+
+#else /* CAREFUL_ALIGNMENT */
+
+/* this handles things for architectures like the 386 that can handle
+   alignment errors */
+/*
+   WARNING: This section is dependent on the length of int16_t and int32_t
+   being correct 
+*/
+
+/* get single value from an SMB buffer */
+#define SVAL(buf,pos) (*(const uint16_t *)((const char *)(buf) + (pos)))
+#define SVAL_NC(buf,pos) (*(uint16_t *)((char *)(buf) + (pos))) /* Non const version of above. */
+#define IVAL(buf,pos) (*(const uint32_t *)((const char *)(buf) + (pos)))
+#define IVAL_NC(buf,pos) (*(uint32_t *)((char *)(buf) + (pos))) /* Non const version of above. */
+#define SVALS(buf,pos) (*(const int16_t *)((const char *)(buf) + (pos)))
+#define SVALS_NC(buf,pos) (*(int16_t *)((char *)(buf) + (pos))) /* Non const version of above. */
+#define IVALS(buf,pos) (*(const int32_t *)((const char *)(buf) + (pos)))
+#define IVALS_NC(buf,pos) (*(int32_t *)((char *)(buf) + (pos))) /* Non const version of above. */
+
+/* store single value in an SMB buffer */
+#define SSVAL(buf,pos,val) SVAL_NC(buf,pos)=((uint16_t)(val))
+#define SIVAL(buf,pos,val) IVAL_NC(buf,pos)=((uint32_t)(val))
+#define SSVALS(buf,pos,val) SVALS_NC(buf,pos)=((int16_t)(val))
+#define SIVALS(buf,pos,val) IVALS_NC(buf,pos)=((int32_t)(val))
+
+#endif /* CAREFUL_ALIGNMENT */
+
+
+static void aligntest(char *buf, size_t size)
+{
+       int i;
+       uint32 val=0;
+       for (i=0;i<size-4;i++) {
+               val += IVAL(buf, i);
+       }
+}
+
+int main(void)
+{
+       char *buf = calloc(1, 5000);
+       int count;
+
+       start_timer();
+       for (count=0;end_timer() < 5.0;count++) {
+               aligntest1(buf, 5000);
+       }
+       printf("test1 %.2f loops/sec\n", count/end_timer());
+
+       return 0;
+}
diff --git a/allchars.c b/allchars.c
new file mode 100644 (file)
index 0000000..57fd7e4
--- /dev/null
@@ -0,0 +1,8 @@
+#include <stdio.h>
+
+main()
+{
+int i;
+for (i=0;i<255;i++)
+  printf("%c",(char)i);
+}
diff --git a/alloc.c b/alloc.c
new file mode 100644 (file)
index 0000000..2708422
--- /dev/null
+++ b/alloc.c
@@ -0,0 +1,31 @@
+#include <stdio.h>
+#include <malloc.h>
+
+static int sum;
+
+char *xmalloc(int size)
+{
+       char *ret;
+       sum += size;
+       ret = malloc(size);
+       if (!ret) {
+               fprintf(stderr,"out of memory\n");
+               exit(1);
+       }
+       memset(ret, 1, size);
+       return ret;
+}
+
+main(int argc, char *argv[])
+{
+       int n = atoi(argv[1]);
+       int i;
+       
+       for (i=0;i<n;i++) {
+               xmalloc(56);
+               xmalloc(9);
+       }
+       
+       printf("allocated %d bytes\n", sum);
+       sleep(20);
+}
diff --git a/andy.c b/andy.c
new file mode 100644 (file)
index 0000000..36588aa
--- /dev/null
+++ b/andy.c
@@ -0,0 +1,11 @@
+int foo(int n)
+{
+       if (n == 0) return n-1;
+
+       return n+1;
+}
+main()
+{
+       
+       foo(5);
+}
diff --git a/ascii.c b/ascii.c
new file mode 100644 (file)
index 0000000..e184ae2
--- /dev/null
+++ b/ascii.c
@@ -0,0 +1,11 @@
+typedef unsigned char uint8;
+
+void blah(const uint8 *x)
+{
+       printf("%d\n", x[0]);
+}
+
+void main(void)
+{
+       blah((const uint8 *)"\04");
+}
diff --git a/badcode.c b/badcode.c
new file mode 100644 (file)
index 0000000..09cfbf8
--- /dev/null
+++ b/badcode.c
@@ -0,0 +1,6 @@
+int main(void)
+{
+       int x;
+       printf("x=%d\n", x);
+       return 0;
+}
diff --git a/bar.c b/bar.c
new file mode 100644 (file)
index 0000000..3226911
--- /dev/null
+++ b/bar.c
@@ -0,0 +1,6 @@
+#include <stdio.h>
+
+bar(unsigned v1, unsigned v2, unsigned v3)
+{
+       printf("v1=%x v2=%x v3=%x\n", v1, v2, v3);
+}
diff --git a/bits.c b/bits.c
new file mode 100644 (file)
index 0000000..eb491ec
--- /dev/null
+++ b/bits.c
@@ -0,0 +1,26 @@
+#include <stdio.h>
+
+
+bits(unsigned int x)
+{
+  int i;
+  for (i=0;i<8;i++)
+    if (x & (1<<i)) 
+      putchar('1');
+    else
+      putchar('0');
+}
+
+main()
+{
+  unsigned char x;
+  for (x='a';x<='z';x++)
+    {
+      printf("%c    ",x);
+      bits(x);
+      printf("    ");
+      bits(toupper(x));
+      printf("\n");
+    }
+
+}
diff --git a/bool.c b/bool.c
new file mode 100644 (file)
index 0000000..34b6cd9
--- /dev/null
+++ b/bool.c
@@ -0,0 +1,9 @@
+#include <stdbool.h>
+
+
+int main()
+{
+       static bool i;
+       
+       return i;
+}
diff --git a/boolbit.c b/boolbit.c
new file mode 100644 (file)
index 0000000..44ae118
--- /dev/null
+++ b/boolbit.c
@@ -0,0 +1,14 @@
+#include <stdio.h>
+
+typedef unsigned:1 BOOL;
+
+int main(void)
+{
+       BOOL b;
+
+       b = 3;
+       
+       printf("b=%u\n", (unsigned)b);
+
+       return 0;
+}
diff --git a/brokenlinks.c b/brokenlinks.c
new file mode 100644 (file)
index 0000000..f942590
--- /dev/null
@@ -0,0 +1,63 @@
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <dirent.h>
+#include <sys/stat.h>
+#include <string.h>
+
+
+void findit(char *dir)
+{
+  DIR *d;
+  struct dirent *de;
+
+  d = opendir(dir);
+  if (!d) return;
+
+
+  while ((de = readdir(d))) {
+    char *fname;
+    struct stat st;
+
+    if (strcmp(de->d_name,".")==0) continue;
+    if (strcmp(de->d_name,"..")==0) continue;
+
+    fname = (char *)malloc(strlen(dir) + strlen(de->d_name) + 2);
+    if (!fname) {
+      fprintf(stderr,"out of memory\n");
+      exit(1);
+    }
+    sprintf(fname,"%s/%s", dir, de->d_name);
+
+    if (lstat(fname, &st) != 0) {
+      continue;
+    }
+
+    if (S_ISLNK(st.st_mode) && stat(fname, &st) != 0) {
+           printf("removing '%s'\n", fname);
+           unlink(fname);
+           continue;
+    }
+
+    if (S_ISDIR(st.st_mode)) {
+      findit(fname);
+    }
+
+    free(fname);
+  }
+
+  closedir(d);
+  
+}
+
+
+int main(int argc, char *argv[])
+{
+  if (argc < 2) {
+    fprintf(stderr,"%s: <dir>\n", argv[0]);
+    exit(1);
+  }
+
+  findit(argv[1]);
+  return 0;
+}
diff --git a/bsd_getdents.c b/bsd_getdents.c
new file mode 100644 (file)
index 0000000..58cc65d
--- /dev/null
@@ -0,0 +1,79 @@
+/*
+  test readdir/unlink pattern that OS/2 uses
+  tridge@samba.org July 2005
+*/
+
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <dirent.h>
+#include <errno.h>
+#include <string.h>
+#include <fcntl.h>
+
+#define NUM_FILES 29
+
+#define TESTDIR "test.dir"
+
+#define FAILED() (fprintf(stderr, "Failed at %s:%d - %s\n", __FUNCTION__, __LINE__, strerror(errno)), exit(1), 1)
+
+static void cleanup(void)
+{
+       /* I'm a lazy bastard */
+       system("rm -rf " TESTDIR);
+       mkdir(TESTDIR, 0700) == 0 || FAILED();
+}
+
+static void create_files()
+{
+       int i;
+       for (i=0;i<NUM_FILES;i++) {
+               char fname[40];
+               snprintf(fname, sizeof(fname), TESTDIR "/test%u.txt", i);
+               close(open(fname, O_CREAT|O_RDWR, 0600)) == 0 || FAILED();
+       }
+}
+
+int main(void)
+{
+       int total_deleted = 0;
+       DIR *d;
+       struct dirent *de;
+
+       cleanup();
+       create_files();
+       
+       d = opendir(TESTDIR);
+
+       chdir(TESTDIR) == 0 || FAILED();
+
+       /* skip past . and .. */
+       de = readdir(d);
+       strcmp(de->d_name, ".") == 0 || FAILED();
+       de = readdir(d);
+       strcmp(de->d_name, "..") == 0 || FAILED();
+
+       while ((de = readdir(d))) {
+               off_t ofs = telldir(d);
+               unlink(de->d_name) == 0 || FAILED();
+
+               /* move one more position on */
+               readdir(d);
+
+               /* seek to just after the first readdir() */
+               seekdir(d, ofs);
+               total_deleted++;
+       }
+       closedir(d);
+
+       printf("Deleted %d files of %d\n", total_deleted, NUM_FILES);
+
+       chdir("..") == 0 || FAILED();
+
+       rmdir(TESTDIR) == 0 || FAILED();
+
+       return 0;
+}
diff --git a/bsd_telldir.c b/bsd_telldir.c
new file mode 100644 (file)
index 0000000..58cc65d
--- /dev/null
@@ -0,0 +1,79 @@
+/*
+  test readdir/unlink pattern that OS/2 uses
+  tridge@samba.org July 2005
+*/
+
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <dirent.h>
+#include <errno.h>
+#include <string.h>
+#include <fcntl.h>
+
+#define NUM_FILES 29
+
+#define TESTDIR "test.dir"
+
+#define FAILED() (fprintf(stderr, "Failed at %s:%d - %s\n", __FUNCTION__, __LINE__, strerror(errno)), exit(1), 1)
+
+static void cleanup(void)
+{
+       /* I'm a lazy bastard */
+       system("rm -rf " TESTDIR);
+       mkdir(TESTDIR, 0700) == 0 || FAILED();
+}
+
+static void create_files()
+{
+       int i;
+       for (i=0;i<NUM_FILES;i++) {
+               char fname[40];
+               snprintf(fname, sizeof(fname), TESTDIR "/test%u.txt", i);
+               close(open(fname, O_CREAT|O_RDWR, 0600)) == 0 || FAILED();
+       }
+}
+
+int main(void)
+{
+       int total_deleted = 0;
+       DIR *d;
+       struct dirent *de;
+
+       cleanup();
+       create_files();
+       
+       d = opendir(TESTDIR);
+
+       chdir(TESTDIR) == 0 || FAILED();
+
+       /* skip past . and .. */
+       de = readdir(d);
+       strcmp(de->d_name, ".") == 0 || FAILED();
+       de = readdir(d);
+       strcmp(de->d_name, "..") == 0 || FAILED();
+
+       while ((de = readdir(d))) {
+               off_t ofs = telldir(d);
+               unlink(de->d_name) == 0 || FAILED();
+
+               /* move one more position on */
+               readdir(d);
+
+               /* seek to just after the first readdir() */
+               seekdir(d, ofs);
+               total_deleted++;
+       }
+       closedir(d);
+
+       printf("Deleted %d files of %d\n", total_deleted, NUM_FILES);
+
+       chdir("..") == 0 || FAILED();
+
+       rmdir(TESTDIR) == 0 || FAILED();
+
+       return 0;
+}
diff --git a/bsort.c b/bsort.c
new file mode 100644 (file)
index 0000000..ba531e3
--- /dev/null
+++ b/bsort.c
@@ -0,0 +1,178 @@
+/* 
+   Copyright (C) Andrew Tridgell 1998
+   
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2 of the License, or
+   (at your option) any later version.
+   
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+   
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+*/
+
+/* a dumb implementation of a block sorting compressor, primarily
+   written to make sure I understand the algorithm */
+
+#include "rzip.h"
+
+struct ch {
+       uchar *p; /* pointer into original buffer */
+};
+
+static uchar *block_end;
+static uchar *block_start;
+
+static int ch_cmp(struct ch *d1, struct ch *d2)
+{
+       int len1, len2, len;
+       int ret;
+       uchar *p1=d1->p, *p2=d2->p;
+
+       if (p1[0] != p2[0]) return p1[0] - p2[0];
+
+       len1 = (int)(block_end - p1);
+       len2 = (int)(block_end - p2);
+       len = MIN(len1, len2);
+
+       ret = memcmp(d1->p, d2->p, len);
+       if (ret != 0) return ret;
+
+       p1 += len;
+       p2 += len;
+
+       if (p1 == block_end) p1 = block_start;
+       if (p2 == block_end) p2 = block_start;
+
+       len = (block_end-block_start)-len;
+
+       while (len--) {
+               if (p1[0] != p2[0]) return p1[0] - p2[0];
+               p1++; p2++;
+               if (p1 == block_end) p1 = block_start;
+               if (p2 == block_end) p2 = block_start;
+       }
+
+       return 0;
+}
+
+/* sort a block of data */
+void block_sort(uchar *in, uchar *out, int len)
+{
+       struct ch *d;
+       int i, Index;
+
+       block_start = in;
+       block_end = in+len;
+
+       d = (struct ch *)malloc(len*sizeof(d[0]));
+       if (!d) {
+               fprintf(stderr,"not enough memory in block_sort - exiting\n");
+               exit(1);
+       }
+
+       /* fill the data array */
+       for (i=0;i<len-1;i++) {
+               d[i].p = &in[i+1]; 
+       }
+       d[i].p = &in[0];
+
+       /* sort it */
+       qsort(d, len, sizeof(d[0]), ch_cmp);
+
+       /* pull out the sorted data */
+       for (i=0;i<len;i++) {
+               if (d[i].p == in) {
+                       Index = i;
+                       out[i+4] = in[len-1];
+               } else {
+                       out[i+4] = d[i].p[-1];
+               }
+       }       
+
+       out[0] = Index&0xFF;
+       out[1] = (Index>>8)&0xFF;
+       out[2] = (Index>>16)&0xFF;
+       out[3] = (Index>>24)&0xFF;
+
+       /* cleanup */
+       free(d);
+}
+
+
+static int uch_cmp(int *d1, int *d2)
+{
+       return block_start[*d1] - block_start[*d2];
+}
+
+/* unsort a block of data */
+void block_unsort(uchar *in, uchar *out, int len)
+{
+       int Index, i, j;
+       int *links;
+
+       Index = in[0] | (in[1]<<8) | (in[2]<<16) | (in[3]<<24);
+
+       len -= 4;
+       in += 4;
+
+       block_start = in;
+       
+       /* build the indexes */
+       links = (int *)malloc(len*sizeof(links[0]));
+       for (i=0;i<len;i++) {
+               links[i] = i;
+       }
+
+       /* sort the indexes by transmitted char to reveal the links */
+       qsort(links, len, sizeof(links[0]), uch_cmp);
+
+       /* follow our tail to decode the data */
+       j = links[Index];
+       for (i=0;i<len;i++) {
+               out[i] = in[j];
+               j = links[j];
+       }
+       
+       /* cleanup */
+       free(links);
+}
+
+
+ int main(int argc, char *argv[])
+{
+       char *f1 = argv[1];
+       char *f2 = argv[2];
+       int fd1, fd2;
+       uchar *buf1, *buf2;
+       struct stat st;
+
+       fd1 = open(f1, O_RDONLY);
+       fd2 = open(f2, O_WRONLY|O_CREAT|O_TRUNC, 0666);
+       
+       fstat(fd1, &st);
+
+       buf1 = (uchar *)malloc(st.st_size);
+       buf2 = (uchar *)malloc(st.st_size+4);
+
+       read(fd1, buf1, st.st_size);
+
+       if (!strstr(argv[0],"bunsort")) {
+               block_sort(buf1, buf2, st.st_size);
+
+               write(fd2,buf2,st.st_size+4);
+       } else {
+               block_unsort(buf1, buf2, st.st_size);
+
+               write(fd2,buf2,st.st_size-4);
+       }
+
+       close(fd1);
+       close(fd2);
+       return 0;
+}
diff --git a/bss.c b/bss.c
new file mode 100644 (file)
index 0000000..accc1fc
--- /dev/null
+++ b/bss.c
@@ -0,0 +1,11 @@
+static char in_bss2[16384];
+char in_data = 1;
+
+main()
+{
+       static char in_bss[16384];
+       char on_stack;
+       
+       printf("in_bss=%p in_bss2=%p on_stack=%p in_data=%p in_code=%p\n",
+              in_bss, in_bss2, &on_stack, &in_data, main);
+}
diff --git a/bzero.c b/bzero.c
new file mode 100644 (file)
index 0000000..363490a
--- /dev/null
+++ b/bzero.c
@@ -0,0 +1,4 @@
+void __bzero(void *s, int n)
+{
+       bzero(s, n);
+}
diff --git a/ccache_parse.c b/ccache_parse.c
new file mode 100644 (file)
index 0000000..158171b
--- /dev/null
@@ -0,0 +1,11 @@
+
+
+struct {
+       const char *name;
+       double value;
+} names[] = {
+       {"cache hit", },
+       {"cache hit", },
+       {"cache hit", },
+       {"cache hit", },
+};
diff --git a/charset.c b/charset.c
new file mode 100644 (file)
index 0000000..44119a2
--- /dev/null
+++ b/charset.c
@@ -0,0 +1,157 @@
+#include <stdio.h>\r
+#include <errno.h>\r
+#include <iconv.h>\r
+\r
+typedef unsigned short smb_ucs2_t;\r
+\r
+#define CVAL(buf,pos) (((unsigned char *)(buf))[pos])\r
+#define DEBUG(x, s) printf s\r
+\r
+static size_t utf8_pull(char **inbuf, size_t *inbytesleft,\r
+                        char **outbuf, size_t *outbytesleft)\r
+{\r
+       while (*inbytesleft >= 1 && *outbytesleft >= 2) {\r
+               unsigned char *c = (unsigned char *)*inbuf;\r
+               unsigned char *uc = (unsigned char *)*outbuf;\r
+               int len = 1;\r
+\r
+               if ((c[0] & 0x80) == 0) {\r
+                       uc[0] = c[0];\r
+                       uc[1] = 0;\r
+               } else if ((c[0] & 0xf0) == 0xe0) {\r
+                       if (*inbytesleft < 3) {\r
+                               DEBUG(0,("short utf8 char\n"));\r
+                               goto badseq;\r
+                       }\r
+                       uc[1] = ((c[0]&0xF)<<4) | ((c[1]>>2)&0xF);\r
+                       uc[0] = (c[1]<<6) | (c[2]&0x3f);\r
+                       len = 3;\r
+               } else if ((c[0] & 0xe0) == 0xc0) {\r
+                       if (*inbytesleft < 2) {\r
+                               DEBUG(0,("short utf8 char\n"));\r
+                               goto badseq;\r
+                       }\r
+                       uc[1] = (c[0]>>2) & 0x7;\r
+                       uc[0] = (c[0]<<6) | (c[1]&0x3f);\r
+                       len = 2;\r
+               }\r
+\r
+               (*inbuf)  += len;\r
+               (*inbytesleft)  -= len;\r
+               (*outbytesleft) -= 2;\r
+               (*outbuf) += 2;\r
+       }\r
+\r
+       if (*inbytesleft > 0) {\r
+               errno = E2BIG;\r
+               return -1;\r
+       }\r
+       \r
+       return 0;\r
+\r
+badseq:\r
+       errno = EINVAL;\r
+       return -1;\r
+}\r
+\r
+static size_t utf8_push(char **inbuf, size_t *inbytesleft,\r
+                        char **outbuf, size_t *outbytesleft)\r
+{\r
+       while (*inbytesleft >= 2 && *outbytesleft >= 1) {\r
+               unsigned char *c = (unsigned char *)*outbuf;\r
+               unsigned char *uc = (unsigned char *)*inbuf;\r
+               int len=1;\r
+\r
+               if ((uc[1] & 0xf8) == 0xd8) {\r
+                       if (*outbytesleft < 3) {\r
+                               DEBUG(0,("short utf8 write\n"));\r
+                               goto toobig;\r
+                       }\r
+                       c[0] = 0xed;\r
+                       c[1] = 0x9f;\r
+                       c[2] = 0xbf;\r
+                       len = 3;\r
+               } else if (uc[1] & 0xf8) {\r
+                       if (*outbytesleft < 3) {\r
+                               DEBUG(0,("short utf8 write\n"));\r
+                               goto toobig;\r
+                       }\r
+                       c[0] = 0xe0 | (uc[1]>>4);\r
+                       c[1] = 0x80 | ((uc[1]&0xF)<<2) | (uc[0]>>6);\r
+                       c[2] = 0x80 | (uc[0]&0x3f);\r
+                       len = 3;\r
+               } else if (uc[1] | (uc[0] & 0x80)) {\r
+                       if (*outbytesleft < 2) {\r
+                               DEBUG(0,("short utf8 write\n"));\r
+                               goto toobig;\r
+                       }\r
+                       c[0] = 0xc0 | (uc[1]<<2) | (uc[0]>>6);\r
+                       c[1] = 0x80 | (uc[0]&0x3f);\r
+                       len = 2;\r
+               } else {\r
+                       c[0] = uc[0];\r
+               }\r
+\r
+\r
+               (*inbytesleft)  -= 2;\r
+               (*outbytesleft) -= len;\r
+               (*inbuf)  += 2;\r
+               (*outbuf) += len;\r
+       }\r
+\r
+       if (*inbytesleft == 1) {\r
+               errno = EINVAL;\r
+               return -1;\r
+       }\r
+\r
+       if (*inbytesleft > 1) {\r
+               errno = E2BIG;\r
+               return -1;\r
+       }\r
+       \r
+       return 0;\r
+\r
+toobig:\r
+       errno = E2BIG;\r
+       return -1;\r
+}\r
+\r
+\r
+\r
+main()\r
+{\r
+       smb_ucs2_t s[2] = {0xe4, 0};\r
+       iconv_t cd;\r
+       unsigned char buf[10];\r
+       int i;\r
+       char *inp, *outp;\r
+       int inlen, outlen;\r
+\r
+       printf("Converting 0x%04x\n", s[0]);\r
+\r
+       cd = iconv_open("UTF8", "UCS2");\r
+\r
+       inp = s;\r
+       inlen = 2;\r
+       outp = buf;\r
+       outlen = 10;\r
+\r
+       utf8_push(&inp, &inlen, &outp, &outlen);\r
+\r
+       for (i=0;i<10-outlen;i++) {\r
+               printf("%02x ", buf[i]);\r
+       }\r
+       printf("\n");\r
+\r
+       inp = s;\r
+       inlen = 2;\r
+       outp = buf;\r
+       outlen = 10;\r
+\r
+       iconv(cd, &inp, &inlen, &outp, &outlen);\r
+\r
+       for (i=0;i<10-outlen;i++) {\r
+               printf("%02x ", buf[i]);\r
+       }\r
+       printf("\n");\r
+}\r
diff --git a/chmode.c b/chmode.c
new file mode 100644 (file)
index 0000000..df9773a
--- /dev/null
+++ b/chmode.c
@@ -0,0 +1,31 @@
+#include <stdio.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+
+
+
+int aoct_int(char *s)
+{
+int l = strlen(s);
+int i;
+int res=0;
+for (i=0;i<l;i++)
+       {
+       int v;
+       char c = s[l-i-1];
+       if (c=='0') continue;
+       v = c-'1' + 1;
+       res |= (v<<(i*3));
+       }
+return(res);
+}
+
+
+main(argc,argv)
+int argc;
+char *argv[];
+{
+if (argc>2) chmod(argv[2],aoct_int(argv[1]));
+return(0);
+}
+
diff --git a/compare.c b/compare.c
new file mode 100644 (file)
index 0000000..28475a1
--- /dev/null
+++ b/compare.c
@@ -0,0 +1,24 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <ctype.h>
+
+int main(int argc,char *argv[])
+{
+       int ret;
+       double n1, n2;
+       if (argc < 3) {
+               fprintf(stderr,"Usage: compare n1 n2\n");
+               exit(1);
+       }
+       n1 = atof(argv[1]);
+       n2 = atof(argv[2]);
+       if (n1 > n2) {
+               ret = 1;
+       } else if (n1 < n2) {
+               ret = -1;
+       } else {
+               ret = 0;
+       }
+       printf("%d\n", ret);
+       return ret;
+}
diff --git a/config-pidtest.c b/config-pidtest.c
new file mode 100644 (file)
index 0000000..bb261ae
--- /dev/null
@@ -0,0 +1,110 @@
+
+/* Linux-HA: pid test code
+* 
+* Author: Jia Ming Pan <jmltc@cn.ibm.com>
+* Modified by Guochun Shi <gshi@ncsa.uiuc.edu>
+*
+* Copyright (c) 2005 International Business Machines
+*
+* This program is free software; you can redistribute it and/or
+* modify it under the terms of the GNU General Public License
+* as published by the Free Software Foundation; either version 2
+* of the License, or (at your option) any later version.
+* 
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+* GNU General Public License for more details.
+* 
+* You should have received a copy of the GNU General Public License
+* along with this program; if not, write to the Free Software
+* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+*
+*/
+
+#include <sys/types.h>
+#include <unistd.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <stdlib.h>
+
+#define SAME 1
+#define TRUE 1
+#define FALSE 0
+int childflag = 0;
+int grandchildflag = 0;
+int   pidconsistent = TRUE;
+void *
+grandchild_func(void * data)
+{
+        pid_t pid = (long) data;
+
+        if (pid ==  getpid()){
+                grandchildflag = SAME;
+        }
+
+        if (grandchildflag ^ childflag){
+               pidconsistent = FALSE;
+                printf("Inconsistency detected\n");
+        }
+        return NULL;
+}
+
+void *
+child_func(void * data)
+{
+        pid_t pid = (long) data;
+        pthread_t thread_id;
+
+        if (pid ==  getpid()){
+                childflag = SAME;
+        }
+
+        pthread_create(&thread_id, NULL, grandchild_func, (void*)(long)getpid());
+}
+
+int
+main()
+{
+        pthread_t thread_id;
+        pthread_attr_t tattr;
+        int  firsttime = 1;
+       pid_t   pid;
+       int     status;
+       
+        pthread_attr_init(&tattr);
+        pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
+
+again:
+       pid = fork();
+       if ( pid == 0 ) { 
+                childflag = 0; 
+                grandchildflag =0;
+                if (pthread_create(&thread_id, &tattr, child_func, (void*)(long)getpid()) != 0){
+                        printf("%s: creating thread failed", __FUNCTION__);
+                }
+               usleep(500000);
+                if (firsttime){
+                        firsttime=0;
+                        goto again;
+                }
+               if (pidconsistent){
+                       return 0;
+               }else{
+                       return 1;
+               }
+        }
+       if (waitpid(pid, &status, 0) <= 0){
+               printf("ERROR: wait for child %d failed\n",pid);
+       }
+       if (WIFEXITED(status)){
+               return (WEXITSTATUS(status));
+       }else{
+               printf("child process %d does not exit normally\n",pid);
+       }
+       
+       return 0;
+}
+
diff --git a/cpu_hog.c b/cpu_hog.c
new file mode 100644 (file)
index 0000000..2409e4b
--- /dev/null
+++ b/cpu_hog.c
@@ -0,0 +1,4 @@
+main()
+{
+       while (1) ;
+}
diff --git a/cr.c b/cr.c
new file mode 100644 (file)
index 0000000..0673975
--- /dev/null
+++ b/cr.c
@@ -0,0 +1,4 @@
+main()
+{
+  puts(crypt ("fredfred", "fredfred"));
+}
diff --git a/crc32.c b/crc32.c
new file mode 100644 (file)
index 0000000..9dacc52
--- /dev/null
+++ b/crc32.c
@@ -0,0 +1,44 @@
+static unsigned crc_32_tab[256];
+
+static void
+makecrc(void)
+{
+/* Not copyrighted 1990 Mark Adler     */
+
+  unsigned long c;      /* crc shift register */
+  unsigned long e;      /* polynomial exclusive-or pattern */
+  int i;                /* counter for all possible eight bit values */
+  int k;                /* byte being shifted into crc apparatus */
+
+  /* terms of polynomial defining this crc (except x^32): */
+  static const int p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};
+
+  /* Make exclusive-or pattern from polynomial */
+  e = 0;
+  for (i = 0; i < sizeof(p)/sizeof(int); i++)
+    e |= 1L << (31 - p[i]);
+
+  c = crc_32_tab[0] = 0;
+
+  printf("0x%08x, ", c);
+
+  for (i = 1; i < 256; i++)
+  {
+    c = 0;
+    for (k = i | 256; k != 1; k >>= 1)
+    {
+      c = c & 1 ? (c >> 1) ^ e : c >> 1;
+      if (k & 1)
+        c ^= e;
+    }
+    crc_32_tab[i] = c;
+    printf("0x%08x, ", c);
+    if (i % 8 == 7) printf("\n");
+  }
+
+}
+
+main()
+{
+       makecrc();
+}
diff --git a/creatreuser.c b/creatreuser.c
new file mode 100644 (file)
index 0000000..7884cc6
--- /dev/null
@@ -0,0 +1,48 @@
+
+
+struct UserStruct {
+       char *UserName;
+       uid_t uid;
+       gid_t *gid_list;
+       unsigned num_groups;
+};
+
+
+errid CreateUser(SysCtx *ctx, struct UserStruct *user);
+
+
+main()
+{
+       struct UserStruct user;
+
+
+       user.UserName = "bob";
+       user.uid = 3;
+       user.gid_list = SYS_malloc(ctx, sizeof(gid_t) * num_groups);
+       user.num_groups = i;
+       for (i=0;i<user.num_groups;i++) {
+               user.gid[] = ;
+       }
+
+       CreateUser(ctx, &user);
+
+}
+
+main()
+{
+       struct UserStruct *user;
+
+       SYY_EmptyUser(ctx,&user);
+       strcpy(user->UserName, "bob");
+
+       user.uid = 3;
+       user.gid_list = SYS_malloc(ctx, sizeof(gid_t) * num_groups);
+       user.gid_list = SYS_malloc(ctx, sizeof(gid_t) * num_groups);
+       user.num_groups = i;
+       for (i=0;i<user.num_groups;i++) {
+               user.gid[] = ;
+       }
+
+       CreateUser(ctx, &user);
+
+}
diff --git a/csc.c b/csc.c
new file mode 100644 (file)
index 0000000..2b592cd
--- /dev/null
+++ b/csc.c
@@ -0,0 +1,98 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#define MAX_TRACKS 100
+typedef char str[256];
+
+FILE *dbf=NULL;
+
+char **params;
+
+int f,result;
+str fname[14],wkday[7];
+main(int argc,char *argv[])
+{
+   dbf = fopen("dbf","w");
+   params = argv;
+   set_terminal("VT102");
+   set_port(1);
+   terminal();
+   cputs("\r");
+   download(result,wkday,fname);
+   terminal();
+   exittelix(0,1);
+}
+
+filename(str fname, str wkday)
+{
+  strcpy(fname,params[1]);
+  strcpy(wkday,params[2]);
+}
+
+download(int result, str wkday, str fname)
+{
+int stat,t0,t1,t2,t3,t4,t5,t6,t7=-2,t8,t9;
+
+t0 = track("OK",0);
+t1 = track("cscts:",0);
+t2 = track("cscts:",0);
+t3 = track("ogin:",0);
+t4 = track("word:",0);
+t5 = track("nimbus[",0);
+t6 = track("nimbus[",0);
+t8 = track("NO CARRIER",0);
+
+stat = 0;
+cputs("ath\r");
+while(1)
+{
+   terminal();
+   stat = track_hit(0);
+   if (stat == t0)
+   {
+      cputs("atdt2495858\r");
+      track_free(t0);
+   }
+   if (stat == t8)
+   {
+      cputs("atdt2495858\r");
+   }
+   if (stat == t1)
+   {
+      cputs("stty iflow eia oflow eia\r");
+      track_free(t1);
+   }
+   if (stat == t2)
+   {
+      cputs("rlogin nimbus\r");
+      track_free(t2);
+   }
+   if (stat == t3)
+   {
+      cputs("tridge\r");
+      track_free(t3);
+   }
+   if (stat == t4)
+   {
+      cputs("fred\r");
+      track_free(t4);
+   }
+   if (stat == t5)
+   {
+      cputs("ls\r");
+      track_free(t5);
+   }
+   if (stat == t6)
+   {
+      cputs("logout\r");
+      track_free(t6);
+      t7 = track("cscts:");
+   }
+   if (stat == t7)
+   {
+      cputs("hang\r");
+      track_free(t7);
+   }
+}
+
+}
diff --git a/csum.c b/csum.c
new file mode 100644 (file)
index 0000000..223c295
--- /dev/null
+++ b/csum.c
@@ -0,0 +1,171 @@
+#define __KERNEL__
+
+#include <linux/config.h>
+#include <asm/segment.h>
+#include <asm/system.h>
+#include <linux/types.h>
+#include <linux/kernel.h>
+#include <linux/sched.h>
+#include <linux/string.h>
+#include <linux/errno.h>
+#include <linux/config.h>
+
+#include <linux/socket.h>
+#include <linux/sockios.h>
+#include <linux/in.h>
+#include <linux/inet.h>
+#include <linux/netdevice.h>
+#include <linux/icmp.h>
+#include <linux/udp.h>
+#include <net/ip.h>
+#include <net/protocol.h>
+#include <net/route.h>
+#include <net/tcp.h>
+#include <net/udp.h>
+#include <linux/skbuff.h>
+#include <net/sock.h>
+#include <net/icmp.h>
+#include <linux/firewall.h>
+#include <linux/ip_fw.h>
+#include <net/checksum.h>
+#include <linux/proc_fs.h>
+#include <linux/stat.h>
+#include <net/checksum.h>
+
+/*
+ * computes a partial checksum, e.g. for TCP/UDP fragments
+ */
+
+unsigned int csum_partial(const unsigned char * buff, int len, unsigned int sum) {
+         /*
+          * Experiments with ethernet and slip connections show that buff
+          * is aligned on either a 2-byte or 4-byte boundary.  We get at
+          * least a 2x speedup on 486 and Pentium if it is 4-byte aligned.
+          * Fortunately, it is easy to convert 2-byte alignment to 4-byte
+          * alignment for the unrolled loop.
+          */
+       __asm__("
+           testl $2, %%esi             # Check alignment.
+           jz 2f                       # Jump if alignment is ok.
+           subl $2, %%ecx              # Alignment uses up two bytes.
+           jae 1f                      # Jump if we had at least two bytes.
+           addl $2, %%ecx              # ecx was < 2.  Deal with it.
+           jmp 4f
+1:         movw (%%esi), %%bx
+           addl $2, %%esi
+           addw %%bx, %%ax
+           adcl $0, %%eax
+2:
+           movl %%ecx, %%edx
+           shrl $5, %%ecx
+           jz 2f
+           testl %%esi, %%esi
+1:         movl (%%esi), %%ebx
+           adcl %%ebx, %%eax
+           movl 4(%%esi), %%ebx
+           adcl %%ebx, %%eax
+           movl 8(%%esi), %%ebx
+           adcl %%ebx, %%eax
+           movl 12(%%esi), %%ebx
+           adcl %%ebx, %%eax
+           movl 16(%%esi), %%ebx
+           adcl %%ebx, %%eax
+           movl 20(%%esi), %%ebx
+           adcl %%ebx, %%eax
+           movl 24(%%esi), %%ebx
+           adcl %%ebx, %%eax
+           movl 28(%%esi), %%ebx
+           adcl %%ebx, %%eax
+           lea 32(%%esi), %%esi
+           dec %%ecx
+           jne 1b
+           adcl $0, %%eax
+2:         movl %%edx, %%ecx
+           andl $0x1c, %%edx
+           je 4f
+           shrl $2, %%edx
+           testl %%esi, %%esi
+3:         adcl (%%esi), %%eax
+           lea 4(%%esi), %%esi
+           dec %%edx
+           jne 3b
+           adcl $0, %%eax
+4:         andl $3, %%ecx
+           jz 7f
+           cmpl $2, %%ecx
+           jb 5f
+           movw (%%esi),%%cx
+           leal 2(%%esi),%%esi
+           je 6f
+           shll $16,%%ecx
+5:         movb (%%esi),%%cl
+6:         addl %%ecx,%%eax
+           adcl $0, %%eax
+7:         "
+       : "=a"(sum)
+       : "0"(sum), "c"(len), "S"(buff)
+       : "bx", "cx", "dx", "si");
+       return(sum);
+}
+
+/* 32 bits version of the checksum routines written for the Alpha by Linus */
+static unsigned short from32to16(unsigned long x)
+{
+       /* add up 16-bit and 17-bit words for 17+c bits */
+       x = (x & 0xffff) + (x >> 16);
+       /* add up 16-bit and 2-bit for 16+c bit */
+       x = (x & 0xffff) + (x >> 16);
+       /* add up carry.. */
+       x = (x & 0xffff) + (x >> 16);
+       return x;
+}
+
+
+main(int argc,char *argv[])
+{
+  unsigned long sum1,sum2,sum3,sum4,sum5;
+  int i;
+  __u32 saddr=0x32456787,daddr=0x89764512;
+  int len=512,proto=7;
+  char data1[512];
+  int seed = time(0);
+
+  if (argc > 1) seed = atoi(argv[1]);
+
+  while (1) {
+    srandom(seed);
+    for (i=0; i<512 ; i++) {
+      data1[i] = random();
+    }
+
+  sum1 = csum_tcpudp_magic(saddr,daddr,len,proto,csum_partial(data1,512,0));
+
+  sum2 = csum_tcpudp_magic(saddr,daddr,len,proto,csum_partial(data1,256,0));
+  
+  data1[4]++;
+  daddr ^= 0x765432FE;
+
+  sum3 = csum_tcpudp_magic(saddr,daddr,len,proto,csum_partial(data1,256,0));
+  
+  sum4 = ~sum3 + (~sum1 - ~sum2);
+  if (!(sum4>>16)) sum4++;
+  sum4 = 0xFFFF & (~from32to16(sum4));
+
+  sum5 = csum_tcpudp_magic(saddr,daddr,len,proto,csum_partial(data1,512,0));
+
+  if (sum4 != sum5) {
+    printf("Failed with seed=%d\n",seed);
+    exit(1);
+  }
+  seed++;
+  if (!(seed & 0xFFF))
+    printf("seed=0x%X\n",seed);
+  }
+
+  printf("%X %X\n",~sum3,~sum3 + (~sum1 - ~sum2));
+  printf("sum1=%X\n",(unsigned int)sum1 & 0xFFFF);
+  printf("sum2=%X\n",(unsigned int)sum2 & 0xFFFF);
+  printf("sum3=%X\n",(unsigned int)sum3 & 0xFFFF);
+  printf("sum4=%X\n",(unsigned int)sum4 & 0xFFFF);
+  printf("sum5=%X\n",(unsigned int)sum5 & 0xFFFF);
+}
diff --git a/ctdb_parser.c b/ctdb_parser.c
new file mode 100644 (file)
index 0000000..ced2294
--- /dev/null
@@ -0,0 +1,38 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+#define _STRING_LINE_(s)    #s
+#define _STRING_LINE2_(s)   _STRING_LINE_(s)
+#define __LINESTR__       _STRING_LINE2_(__LINE__)
+#define __location__ __FILE__ ":" __LINESTR__
+
+static void fatal(const char *msg)
+{
+       printf("%s\n", msg);
+       exit(1);
+}
+
+int main(int argc, const char *argv[])
+{
+       int fd;
+       unsigned len;
+       unsigned ofs=0;
+
+       fd = open(argv[1], O_RDONLY);
+       
+       while (read(fd, &len, sizeof(len)) == sizeof(len)) {
+               unsigned char buf[len-4];
+               ofs += 4;
+               
+               if (read(fd, buf, len-4) != (len-4)) {
+                       fatal(__location__);
+               }
+               ofs += len-4;
+               printf("magic: %4.4s length=%u ofs=%u\n", buf, len, ofs);
+       }
+       
+       return 0;
+}
diff --git a/cvt.c b/cvt.c
new file mode 100644 (file)
index 0000000..9354958
--- /dev/null
+++ b/cvt.c
@@ -0,0 +1,13 @@
+#include <stdio.h>
+
+main(int argc, char *argv[])
+{
+       long long x;
+       double d;
+
+       x = atoi(argv[1]);
+
+       d = (double)x;
+
+       printf("%.0f %d\n", (double)x, (int)x);
+}
diff --git a/debug.c b/debug.c
new file mode 100644 (file)
index 0000000..c834777
--- /dev/null
+++ b/debug.c
@@ -0,0 +1,7 @@
+#include <stdio.h>
+
+
+int Debug(const char *format, ...)
+{
+       
+}
diff --git a/decimal.c b/decimal.c
new file mode 100644 (file)
index 0000000..4e803d3
--- /dev/null
+++ b/decimal.c
@@ -0,0 +1,17 @@
+#include <math.h>
+
+/*
+a0c7 7694 ab42 7b80 a4f9 0381 382f 7680 
+ab42 7b80 58e6 1199 0000 0002 
+
+d0c7 7694 64cc 7d80 64cc 7d80 5308 6c80
+7ea5 7380 9070 5096 0000 0002 
+*/
+decimal32 d;
+
+main()
+{
+       d = 19123376;
+       printf("0x%x\n", *(unsigned *)&d);
+       return 0;
+}
diff --git a/detect2.c b/detect2.c
new file mode 100644 (file)
index 0000000..68cf804
--- /dev/null
+++ b/detect2.c
@@ -0,0 +1,707 @@
+/*****************************************************************************
+A simple C program to detect plagiarism
+
+Idea:
+
+- tokenise input
+- collect groups of N tokens, overlapping my M tokens
+- hash each group
+- sort the resultant hashes
+
+Andrew Tridgell <tridge@samba.org> July 2003
+
+******************************************************************************/
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <string.h>
+#include <math.h>
+#include <ctype.h>
+
+typedef enum {False=0,True=1} BOOL;
+
+/* experimental ? */
+BOOL beta = False;
+
+/* should I be case sensitive? */
+BOOL case_sensitive = False;
+
+/* how big can a word get? */
+#define MAX_WORD_LEN 500
+
+/* how many files are we processing? */
+int num_files = 0;
+
+/* the names of the files we are processing - normally stolen from argv */
+char **file_names = NULL;
+
+/* how many words have we found? */
+int num_words = 0;
+
+/* a list of chars to ignore - can be interesting */
+char ignore_chars[MAX_WORD_LEN] = "";
+
+/* what debug level? */
+int debug = 0;
+
+
+/* will I add new words to the word list ? */
+BOOL add_words = True;
+
+/* shall I weight the answers ? */
+BOOL do_weighting = True;
+
+/* shall I concatenate all adjacent words to form word pairs? */
+BOOL concat_words = False;
+
+/* a suspicious filename */
+char *suspicious_file = NULL;
+
+/* how to write a boolean */
+#define bool_str(v) ((v)?"True":"False")
+
+/* 
+a list of all the words found and where they occurred 
+*/
+struct word
+{
+  int count; /* how many files this word occurs in */
+  char *word; /* text of word */
+  BOOL *occurred; /* yes/no has occurred in this file */
+} *words = NULL;
+
+/***************************************************************************** 
+like realloc() but can start from scratch, and exits if there is an error
+******************************************************************************/
+void *Realloc(void *ptr,int size)
+{
+  if (!ptr)
+    ptr = malloc(size);
+  else
+    ptr = realloc(ptr,size);
+  if (!ptr)
+    {
+      printf("Can't allocate %d bytes\n",size);
+      exit(0);
+    }
+  return(ptr);
+}
+
+/*****************************************************************************
+return a filename
+******************************************************************************/
+char *FileName(int i)
+{
+  if (i < num_files)
+    return(file_names[i]);
+  return(suspicious_file);
+}
+
+
+/*****************************************************************************
+decide if two words are equal
+******************************************************************************/
+BOOL wordequal(char *word1,char *word2)
+{
+  if (case_sensitive)
+    return(strcmp(word1,word2) == 0);
+  else
+    return(strcasecmp(word1,word2) == 0);
+}
+
+
+/***************************************************************************** 
+find an insertion position for a new word
+******************************************************************************/
+int insert_position(char *word)
+{
+  int high_i = num_words;
+  int low_i = 0;
+  int guess,ret;
+
+  /* do a bisection search - this assumes the list is kept ordered */
+  while (high_i > low_i)
+    {
+      guess = (high_i + low_i)/2;
+      if (case_sensitive)
+       ret = strcmp(word,words[guess].word);
+      else
+       ret = strcasecmp(word,words[guess].word);
+      if (ret == 0)
+       return(guess);
+      if (ret > 0)
+       low_i = guess+1;
+      if (ret < 0)
+       high_i = guess;
+    }
+  return(low_i);
+}
+
+
+/***************************************************************************** 
+ask if a word has occurred before - return word index if it has 
+return -1 if not
+******************************************************************************/
+int find_word(char *word)
+{
+  int ret;
+  
+  /* we can take advantage of the order of the words */
+  ret = insert_position(word);
+  if (ret >= num_words || !wordequal(word,words[ret].word))
+    return(-1);
+  return(ret);
+
+}
+
+
+/***************************************************************************** 
+Insert a word into the list of words that have occurred.
+If the word already exists then just set the occured bit for the filenum
+******************************************************************************/
+void insert_word(char *word,int filenum)
+{
+  int wordnum,i;
+  wordnum = find_word(word);
+
+  if (wordnum < 0 && !add_words)
+    return;
+
+  if (wordnum < 0)
+    {
+      if (debug > 0)
+       printf("new word %s from filenum %d\n",word,filenum);
+
+      wordnum = insert_position(word);
+      words = (struct word *)Realloc(words,sizeof(struct word)*(num_words+1));
+      num_words++;
+
+      for (i=num_words-2;i>=wordnum;i--)
+       words[i+1] = words[i];
+
+      words[wordnum].count=0;
+      words[wordnum].word = strdup(word);
+      words[wordnum].occurred = 
+       (BOOL *)Realloc(NULL,sizeof(BOOL)*(num_files+1));
+       
+      for (i=0;i<=num_files;i++) words[wordnum].occurred[i] = False;
+    }
+
+  if (!words[wordnum].occurred[filenum]) words[wordnum].count++;
+  words[wordnum].occurred[filenum] = True;
+}
+
+/*****************************************************************************
+dump the word occurrance table
+******************************************************************************/
+void dump_word_table(void)
+{
+  int i,j;
+  for (i=0;i<num_words;i++)
+    {
+      printf("%20.20s ",words[i].word);
+      for (j=0;j<=num_files;j++)
+       {
+         if (words[i].occurred[j])
+           printf("1 ");
+         else
+           printf("  ");
+       }
+      printf("\n");
+    }
+}
+
+/*****************************************************************************
+read one character - skipping over the chars in ignore
+******************************************************************************/
+int Read(FILE *f,char *ignore)
+{
+  int ch = fgetc(f);
+  if (ch == EOF) return(EOF);
+  if (*ignore && (strchr(ignore,(char)ch) != NULL))
+    return(Read(f,ignore));
+  return(ch);
+}
+
+/*****************************************************************************
+get a word from a stream. return the word. return NULL if no more words 
+exist. possibly skip comments?
+******************************************************************************/
+char *get_word(FILE *f)
+{
+  static char word[MAX_WORD_LEN+1];
+  int word_len = 0;
+  char ch;
+
+  strcpy(word,"");
+
+  /* skip over non alphanum chars */
+  while ((ch = Read(f,ignore_chars)) != EOF)
+    {
+      if (isalnum(ch)) break;
+    }
+  if (ch == EOF) return(NULL);
+
+  word[word_len++] = ch;
+  
+  /* grab all the alphanums */
+  while (word_len < MAX_WORD_LEN && (isalnum(ch = Read(f,ignore_chars))))
+    word[word_len++] = ch;
+
+  word[word_len] = '\0';
+  return(word);
+}
+
+
+/* used for cheating matrix */
+struct pair
+{
+  int i,j;
+  int count;
+} *mat = NULL;
+
+/***************************************************************************** 
+   compare 2 pair structures - used for sorting 
+******************************************************************************/
+int pair_compare(struct pair *t1,struct pair *t2)
+{
+  return(t2->count - t1->count);
+}
+
+/***************************************************************************** 
+   compare 2 word structures - used for sorting 
+******************************************************************************/
+int words_compare(struct word *t1,struct word *t2)
+{
+  if (t2->count == t1->count)
+    return(strcmp(t1->word,t2->word));
+  return(t1->count - t2->count);
+}
+
+/***************************************************************************** 
+form the cheating matrix. 
+The match between two files is increased if
+the word occurs in both files but doesn't occur in more than THRESH % of files.
+******************************************************************************/
+void cheat_matrix(double threshold1,double threshold2,double output_thresh)
+{
+  int i,j,wordnum;
+  int max_matches1,max_matches2;
+  int output_count;
+
+  /* max matches can't be less than 2 */
+  if (threshold1 >= 1.0)
+    max_matches1 = threshold1;
+  else
+    max_matches1 = num_files * threshold1;
+  if (max_matches1 < 2) max_matches1 = 2;
+
+  if (threshold2 > 1.0)
+    max_matches2 = threshold2;
+  else
+    max_matches2 = num_files * threshold2;
+  if (max_matches2 < max_matches1) max_matches2 = max_matches1;
+
+
+  if (output_thresh >= 1.0)
+    output_count = output_thresh;
+  else
+    output_count = output_thresh * num_files;
+  if (output_count < 1) output_count = 1;
+  if (output_count > num_files) output_count = num_files;
+  
+
+#define CMAT(i,j) mat[(i)*(num_files+1) + j]
+
+  /* alloc the cheat matrix */
+  mat = (struct pair *)Realloc(mat,(num_files+1)*(num_files+1)*sizeof(*mat));
+
+  /* reset it */
+  for (i=0;i<=num_files;i++)
+    for (j=0;j<=num_files;j++)
+      {
+       CMAT(i,j).i = i; CMAT(i,j).j = j;
+       CMAT(i,j).count=0;
+      }
+
+  /* process the words one at a time */
+  for (wordnum=0;wordnum<num_words;wordnum++)
+    {
+      int occurrances = words[wordnum].count;
+
+      /* if the word is very common then forget it */
+      if (occurrances > max_matches1)
+       {
+         if (debug > 3)
+           printf("ignoring common word %s (%d occurrances)\n",
+                  words[wordnum].word,occurrances);
+
+         if (occurrances > max_matches2)
+           words[wordnum].count = 0;
+
+         continue;
+       }      
+
+      if (debug > 3)
+       printf("%s occurred %d times\n",words[wordnum].word,occurrances);
+
+      /* increment possible cheaters */
+      for (i=0;i<=num_files;i++)
+       {
+         if (words[wordnum].occurred[i])
+           for (j=i+1;j<=num_files;j++)
+                   if (words[wordnum].occurred[j]) {
+                           if (do_weighting) {
+                                   CMAT(i,j).count += ((max_matches1+1) - words[wordnum].count);
+                           }   else {
+                                   CMAT(i,j).count++;
+                           }
+                   }
+       }            
+    }
+
+  /* sort them */
+  qsort(mat,(num_files+1)*(num_files+1),sizeof(*mat),(int (*)())pair_compare);
+
+  /* sort the wordlist so least frequent words are at the top */
+  qsort(words,num_words,sizeof(*words),(int (*)())words_compare);
+
+  /* find the highest */
+  {
+    int f;
+    for (f=0;f<output_count;f++)
+      if (mat[f].count > 0)
+       {
+         i = mat[f].i; j = mat[f].j;
+         printf("scored %3d in (%s %s)\n",mat[f].count,
+                FileName(i),
+                FileName(j));
+       
+         for (wordnum=0;wordnum<num_words;wordnum++)
+           {
+             if (words[wordnum].count>0 &&
+                 words[wordnum].occurred[i] && 
+                 words[wordnum].occurred[j])
+               printf("%s(%d) ",words[wordnum].word,words[wordnum].count);
+           }            
+         printf("\n\n");
+       }
+  }
+}
+
+
+/*****************************************************************************
+process one file
+******************************************************************************/
+void process_one(char *filename,char *filter_prog,int filenum)
+{
+  FILE *f;
+  char *word;
+  static char lastword[MAX_WORD_LEN+1];
+
+  if (filter_prog)
+    {
+      char cmd[1000];
+      sprintf(cmd,"cat %s | %s",filename,filter_prog);
+      f = popen(cmd,"r");
+    }
+  else
+    f = fopen(filename,"r");
+
+  if (!f) 
+    {
+      perror(filename);
+      return;
+    }
+
+  if (debug > 0)
+    printf("processing file %s\n",filename);
+
+  fflush(stdout);
+
+  while ((word = get_word(f)) != NULL) {
+         insert_word(word,filenum);
+         if (concat_words) {
+                 strcat(lastword, word);
+                 insert_word(lastword,filenum);
+                 strcpy(lastword, word);
+         }
+  }
+  if (filter_prog)
+    pclose(f);
+  else
+    fclose(f);
+}
+
+
+/*****************************************************************************
+process the files by opening them and getting the words
+******************************************************************************/
+void process_files(char *filter_prog)
+{
+  int i;
+
+  if (suspicious_file)
+    {
+      process_one(suspicious_file,filter_prog,num_files);
+      add_words = False;
+    }
+
+  for (i=0;i<num_files;i++)
+    {
+      if (suspicious_file && strcmp(suspicious_file,FileName(i))==0)
+       continue;
+
+      printf(".");
+      process_one(FileName(i),filter_prog,i);
+    }
+
+  printf("\nfinished initial processing\n");
+}
+
+/*****************************************************************************
+usage of the program
+******************************************************************************/
+void usage(void)
+{
+  printf("detect: find cheaters by looking at token similarities between files\n");
+  printf("\ndetect [options] <files..>\n");
+  printf("\t-h = give extended help and examples of use (RECOMMENDED)\n");
+  printf("\t-c = be case sensitive\n");
+  printf("\t-n num = output the top n pairs (default top 20%%)\n");
+  printf("\t-t thresh = set threshold1 (default 3)\n");
+  printf("\t-T thresh = set threshold2 (default 10)\n");
+  printf("\t-C concatenate adjacent tokens to make new tokens (very useful!)\n");
+  printf("\t-I ignore_str = ignore these chars in the file\n");
+  printf("\t-f prog = filter the files through the given program\n");
+  printf("\t-s filename = only look for matches to this particular suspicious file\n");
+  printf("\t-w toggle weighting of the score by (threshold1-frequency) (default True)\n");
+  printf("\n");
+}
+
+/*****************************************************************************
+provide extended help
+******************************************************************************/
+void help(void)
+{
+  /* NOTE: this bit of text relies on a compiler that can handle new */
+  /* lines in string literals. gcc is good at this */
+
+  char *help_txt = "
+This program tries to identify common words between a list of files
+in an attempt to find cases of plagiarism. 
+
+Algorithm:
+==========
+
+1) Build a list words that occur in the files, and a bitmap of which
+   words occur in which files.
+2) Discard any words that occur in more than <threshold2> files
+3) Produce a matrix M where M(i,j) is the number of times a word was
+   used both in file i and file j, not counting words that occur in
+   more than <threshold1> files.
+4) Sort this matrix to produce the top <n> pairs, weighting by
+   how infrequent the words are.
+5) Write out the pairs along with what words matched between them and
+   the frequency of those words.
+
+
+Interpreting the output:
+========================
+
+Here is some sample output.
+
+scored  13 in (stu1.mod,stu2.mod)
+AveAss(2) CalculateAss(2) OutputResult(2) StuMark(2) stuNum(2)
+InputFile(3) MaxAss(3) ReadAss(3) index(4) TutTotal(5)
+
+This means that these two files (stu1.mod and stu2.mod) both contained
+all the identifiers listed. The identifiers with a (2) after them
+occurred only in these two files. The identifier TutTotal was used in
+5 files and is therefore less suspicious.
+
+Example 1:
+==========
+
+   detect *.mod
+
+This will find common tokens between a list of modula files, listing
+which files have lots of common tokens, what the common tokens are and
+how common those tokens are across all the files.
+
+Example 2:
+==========
+
+  detect -c -n 10 -t 2 -T 20 -I ',_ ' *.c
+
+This will look in a bunch of C progs, and rank the pairs that used
+tokens that were used by only 2 files. It will list common tokens
+between the files up to those tokens used by 20 files. It will skip
+over all underscores, commas and spaces. This last step is useful to
+catch people who just inserted underscores. Skipping spaces and commas
+has the effect of treating whole argument lists as a single word which
+can be a very effective technique. This example will also be case
+sensitive in comparisons. It will only show the top 10 matching pairs.
+
+Example 3:
+==========
+
+  detect -f 'grep PROCEDURE' *.mod
+
+This will only look at lines that have the word PROCEDURE in them.
+This can be good for checking only whether procedure names match.
+
+
+Example 4:
+==========
+   detect -s stu1.mod *.mod
+
+This will only look for matches between the 'suspicious file' stu1.mod
+and all the other files. This is useful to detect cheating where you
+suspect a particular person, or to find a group who collaborated once
+you find one pair that are a good match.
+
+Example 5:
+==========
+  detect -w *.mod
+
+This will disable weighting of the scores by (threshold1-frequency)
+thus making all matches of equal weight.
+
+Example 6:
+==========
+
+  detect -C `find . -name \"*.txt\"`
+
+This will process all text files below the current directory and will
+also enable token concatenation. Token concatenation is very useful as
+it builds a much larger token pool with much less common tokens. It is
+particularly useful for essay type assignments which lack unique
+programming identifiers.
+
+Advanced Notes:
+===============
+
+*) The filter programs used by -f can be any program that reads from
+   standard input and writes to standard output. 
+
+*) The two thesholds and the output count can be given either as a
+   proportion of the total number of files or as a absolute count. Thus
+   `-t 0.05' will set the first threshold to 5% of the total number of
+   files. Any number less than 1 is interpreted as a proportion.
+
+
+Author:
+=======
+
+ Andrew Tridgell
+ Andrew.Tridgell@anu.edu.au
+
+";
+
+
+  usage();
+  puts(help_txt);
+
+}
+
+
+
+/*****************************************************************************
+the main program 
+******************************************************************************/
+int main(int argc,char **argv)
+{
+  double threshold1 = 3;
+  double threshold2 = 10;
+  double output_thresh = 0.2;
+  char *filter_prog=NULL;
+
+  int opt;
+  extern char *optarg;
+  extern int optind;
+
+  /* get some options */
+  while ((opt = getopt (argc, argv, "Cwct:T:I:d:hbf:n:s:")) != EOF)
+    switch (opt)
+      {
+      case 'n':
+       output_thresh = atof(optarg);
+       break;
+      case 'f':
+       filter_prog = strdup(optarg);
+       break;
+      case 'h':
+       help();
+       exit(0);
+      case 'c':
+       case_sensitive = !case_sensitive;
+       break;
+      case 'C':
+       concat_words = !concat_words;
+       break;
+      case 'w':
+       do_weighting = !do_weighting;
+       break;
+      case 'b':
+       beta = !beta;
+       break;
+      case 't':
+       threshold1 = atof(optarg);
+       break;
+      case 'T':
+       threshold2 = atof(optarg);
+       break;
+      case 'I':
+       strcat(ignore_chars,optarg);
+       break;
+      case 'd':
+       debug = atoi(optarg);
+       break;
+      case 's':
+       suspicious_file = strdup(optarg);
+       break;
+      default:
+       usage();        
+       exit(0);        
+      }
+
+  /* get the file names */
+  num_files = argc-optind;
+  file_names = &argv[optind];
+
+  if (num_files < 2)
+    {
+      usage();
+      exit(0);
+    }
+
+  printf("\nProcessing %d files\n",num_files);
+
+  if (suspicious_file)
+    printf("Suspicious file = %s\n",suspicious_file);
+
+  printf("Weighting=%s\tCase Sensitive=%s\n",
+        bool_str(do_weighting),bool_str(case_sensitive));
+
+  printf("Threshold1=%g\tThreshold2=%g\n",threshold1,threshold2);
+
+  if (*ignore_chars)
+    printf("Ignoring chars `%s'\n",ignore_chars);
+
+  if (filter_prog)
+    printf("Filtering with `%s'\n",filter_prog);
+
+  /* process the files */
+  process_files(filter_prog);
+
+  if (debug > 2)
+    dump_word_table();
+
+  /* and get the matches */
+  cheat_matrix(threshold1,threshold2,output_thresh);
+
+  return 0;
+}
diff --git a/dirtest.c b/dirtest.c
new file mode 100644 (file)
index 0000000..b3102dc
--- /dev/null
+++ b/dirtest.c
@@ -0,0 +1,113 @@
+#define _GNU_SOURCE
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/mman.h>
+#include <dirent.h>
+#include <fcntl.h>
+#include <errno.h>
+
+
+
+static int create_files(int dir, int nfiles)
+{
+       int i, fd;
+       char *dname;
+       asprintf(&dname, "dir%d", dir);
+
+       if (mkdir(dname, 0755) != 0 && errno != EEXIST) {
+               perror(dname);
+               exit(1);
+       }
+
+       for (i=0;i<nfiles;i++) {
+               char *fname;
+               asprintf(&fname, "%s/File%d", dname, i);
+               fd = open(fname, O_RDWR|O_CREAT|O_TRUNC, 0644);
+               if (fd == -1) {
+                       perror(fname);
+                       exit(1);
+               }
+               write(fd, fname, strlen(fname)+1);
+               close(fd);
+               free(fname);
+       }
+
+       return 0;
+}
+
+static int check_files(int dir, int nfiles)
+{
+       int i, fd;
+       char *dname;
+       asprintf(&dname, "dir%d", dir);
+
+       for (i=0;i<nfiles;i++) {
+               char *fname;
+               char s[100];
+               asprintf(&fname, "%s/File%d", dname, i);
+               fd = open(fname, O_RDONLY, 0644);
+               if (fd == -1) {
+                       perror(fname);
+                       exit(1);
+               }
+               read(fd, s, sizeof(s));
+               close(fd);
+
+               if (strcmp(fname, s)) {
+                       printf("Name mismatch! %s %s\n", fname, s);
+               }
+
+               unlink(fname);
+               free(fname);
+       }
+
+       if (rmdir(dname) != 0) {
+               perror(dname);
+       }
+
+       return 0;
+}
+
+int main(int argc, char *argv[])
+{
+       int nprocs, nfiles, i, ret=0, status;
+
+       nprocs = atoi(argv[1]);
+       nfiles = atoi(argv[2]);
+
+       printf("Creating %d files in %d dirs\n", nfiles, nprocs);
+
+       for (i=0;i<nprocs;i++) {
+               if (fork() == 0) {
+                       exit(create_files(i, nfiles));
+               }
+       }
+       
+       while (waitpid(0, &status, 0) > 0 || errno != ECHILD) {
+               if (WEXITSTATUS(status) != 0) {
+                       ret = WEXITSTATUS(status);
+                       printf("Child exited with status %d\n", ret);
+               }
+       }
+
+       for (i=0;i<nprocs;i++) {
+               if (fork() == 0) {
+                       exit(check_files(i, nfiles));
+               }
+       }
+       
+       while (waitpid(0, &status, 0) > 0 || errno != ECHILD) {
+               if (WEXITSTATUS(status) != 0) {
+                       ret = WEXITSTATUS(status);
+                       printf("Child exited with status %d\n", ret);
+               }
+       }
+
+       return ret;
+}
diff --git a/dirtest2.c b/dirtest2.c
new file mode 100644 (file)
index 0000000..b3102dc
--- /dev/null
@@ -0,0 +1,113 @@
+#define _GNU_SOURCE
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/mman.h>
+#include <dirent.h>
+#include <fcntl.h>
+#include <errno.h>
+
+
+
+static int create_files(int dir, int nfiles)
+{
+       int i, fd;
+       char *dname;
+       asprintf(&dname, "dir%d", dir);
+
+       if (mkdir(dname, 0755) != 0 && errno != EEXIST) {
+               perror(dname);
+               exit(1);
+       }
+
+       for (i=0;i<nfiles;i++) {
+               char *fname;
+               asprintf(&fname, "%s/File%d", dname, i);
+               fd = open(fname, O_RDWR|O_CREAT|O_TRUNC, 0644);
+               if (fd == -1) {
+                       perror(fname);
+                       exit(1);
+               }
+               write(fd, fname, strlen(fname)+1);
+               close(fd);
+               free(fname);
+       }
+
+       return 0;
+}
+
+static int check_files(int dir, int nfiles)
+{
+       int i, fd;
+       char *dname;
+       asprintf(&dname, "dir%d", dir);
+
+       for (i=0;i<nfiles;i++) {
+               char *fname;
+               char s[100];
+               asprintf(&fname, "%s/File%d", dname, i);
+               fd = open(fname, O_RDONLY, 0644);
+               if (fd == -1) {
+                       perror(fname);
+                       exit(1);
+               }
+               read(fd, s, sizeof(s));
+               close(fd);
+
+               if (strcmp(fname, s)) {
+                       printf("Name mismatch! %s %s\n", fname, s);
+               }
+
+               unlink(fname);
+               free(fname);
+       }
+
+       if (rmdir(dname) != 0) {
+               perror(dname);
+       }
+
+       return 0;
+}
+
+int main(int argc, char *argv[])
+{
+       int nprocs, nfiles, i, ret=0, status;
+
+       nprocs = atoi(argv[1]);
+       nfiles = atoi(argv[2]);
+
+       printf("Creating %d files in %d dirs\n", nfiles, nprocs);
+
+       for (i=0;i<nprocs;i++) {
+               if (fork() == 0) {
+                       exit(create_files(i, nfiles));
+               }
+       }
+       
+       while (waitpid(0, &status, 0) > 0 || errno != ECHILD) {
+               if (WEXITSTATUS(status) != 0) {
+                       ret = WEXITSTATUS(status);
+                       printf("Child exited with status %d\n", ret);
+               }
+       }
+
+       for (i=0;i<nprocs;i++) {
+               if (fork() == 0) {
+                       exit(check_files(i, nfiles));
+               }
+       }
+       
+       while (waitpid(0, &status, 0) > 0 || errno != ECHILD) {
+               if (WEXITSTATUS(status) != 0) {
+                       ret = WEXITSTATUS(status);
+                       printf("Child exited with status %d\n", ret);
+               }
+       }
+
+       return ret;
+}
diff --git a/display_sid.c b/display_sid.c
new file mode 100644 (file)
index 0000000..1db685d
--- /dev/null
@@ -0,0 +1,107 @@
+/*
+  display a binary sid
+
+  assumes x86 CPU. This code is NOT portable.
+
+  tridge@samba.org, December 2003
+*/
+
+#include <stdio.h>
+#include <sys/types.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <errno.h>
+
+
+typedef unsigned char uint8;
+typedef unsigned int  uint32;
+
+struct dom_sid {
+#if CONFORMANT_SID
+       uint32  conf_size;
+#endif
+       uint8  sid_rev_num;
+       uint8  num_auths;
+       uint8  id_auth[6];
+       uint32 sub_auths[0];
+};
+
+
+static void display_sid(const char *blob, off_t size)
+{
+       struct dom_sid *sid = (struct dom_sid *)blob;
+       uint32 ia;
+       int i;
+
+       if (size < sizeof(*sid) ||
+#if CONFORMANT_SID
+           sid->conf_size != sid->num_auths ||
+#endif
+           size != sizeof(*sid) + 4*sid->num_auths) {
+               printf("Bad sid blob - maybe its not a IDL conformant SID?\n");
+               exit(1);
+       }
+
+       ia = (sid->id_auth[5]) +
+               (sid->id_auth[4] << 8 ) +
+               (sid->id_auth[3] << 16) +
+               (sid->id_auth[2] << 24);
+
+       printf("S-%u-%u", (unsigned)sid->sid_rev_num, (unsigned)ia);
+
+       for (i=0;i<sid->num_auths;i++) {
+               printf("-%u", (unsigned)sid->sub_auths[i]);
+       }
+       printf("\n");
+}
+
+
+static const char *file_load(const char *filename, off_t *size)
+{
+       char *ret;
+       struct stat st;
+       int fd;
+
+       fd = open(filename, O_RDONLY);
+       if (fd == -1) return NULL;
+
+       fstat(fd, &st);
+       
+       ret = malloc(st.st_size);
+       if (!ret) return NULL;
+
+       if (read(fd, ret, st.st_size) != st.st_size) {
+               free(ret);
+               return NULL;
+       }
+
+       *size = st.st_size;
+
+       return ret;
+}
+
+int main(int argc, const char *argv[])
+{
+       const char *filename;
+       const char *blob;
+       off_t size;
+
+       if (argc < 2) {
+               fprintf(stderr,"Usage: display_sid <filename>\n");
+               exit(1);
+       }
+       
+       filename = argv[1];
+
+       blob = file_load(filename, &size);
+       if (!blob) {
+               perror(filename);
+               exit(1);
+       }
+
+       display_sid(blob, size);
+       return 0;
+}
+
diff --git a/double.c b/double.c
new file mode 100644 (file)
index 0000000..31fcdb2
--- /dev/null
+++ b/double.c
@@ -0,0 +1,18 @@
+#include <math.h>
+
+main()
+{
+       int i;
+       double x;
+       long long y;
+
+       y = x = 1;
+
+       for (i=0;i<64;i++) {
+               x = (x * 2.0);
+               x++;
+               y = (y * 2.0);
+               y++;
+               printf("%d %.0f %lld %llx %g %g\n", i, x, y, y, x/(10.0*1000*1000*60*60*24*365), log10(x));
+       }
+}
diff --git a/drivenames.c b/drivenames.c
new file mode 100644 (file)
index 0000000..ef237a1
--- /dev/null
@@ -0,0 +1,59 @@
+
+#include <stdio.h>
+#include <sys/types.h>
+#include <dirent.h>
+
+/*
+  find a list of drive names on this machine
+  return the number of drives found, return the names as 
+  a string array in **names
+*/
+static int find_drive_names(char ***names)
+{
+       DIR *dir;
+       struct dirent *de;
+       int count = 0;
+
+       dir = opendir("/proc/ide");
+       if (!dir) {
+               perror("/proc/ide");
+               return 0;
+       }
+
+       (*names) = NULL;
+
+       while ((de = (readdir(dir)))) {
+               /* /proc/ide contains more than just a list of drives. We need
+                  to select only those bits that match 'hd*' */
+               if (strncmp(de->d_name, "hd", 2) == 0) {
+                       /* found one */
+                       (*names) = realloc(*names, sizeof(char *) * (count+2));
+                       if (! (*names)) {
+                               fprintf(stderr,"Out of memory in find_drive_names\n");
+                               return 0;
+                       }
+                       asprintf(&(*names)[count],"/dev/%s", de->d_name);
+                       count++;
+               }
+       }
+
+       if (count != 0) {
+               (*names)[count] = NULL;
+       }
+       
+       closedir(dir);
+       return count;
+}
+
+main()
+{
+       char **names;
+       int n, i;
+
+       n = find_drive_names(&names);
+
+       for (i=0;i<n;i++) {
+               printf("%s\n", names[i]);
+       }
+       
+}
diff --git a/dst.c b/dst.c
new file mode 100644 (file)
index 0000000..97ff9b3
--- /dev/null
+++ b/dst.c
@@ -0,0 +1,64 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/time.h>
+
+#define DAY (24*60*60)
+#define YEAR (365*DAY)
+
+void get_time_diff(time_t t,int *timediff,int *is_dst)
+{
+  struct tm tm_utc,tm_local;
+  tm_utc = *(gmtime(&t));
+  tm_local = *(localtime(&t));
+
+  *timediff = mktime(&tm_utc) - mktime(&tm_local);
+  *is_dst = tm_local.tm_isdst;
+}
+
+main()
+{
+  time_t tnow = time(NULL);
+  int is_dst,timediff;
+  int last_dst,lastdiff;
+  time_t endt = tnow + 2*YEAR;
+  time_t t = tnow - 2*YEAR;
+  time_t high,low;
+  int count=0;
+
+  while (1) {
+
+    get_time_diff(t,&lastdiff,&last_dst);
+    low = t;
+
+    /* run forward till we get a change */
+    for (timediff = lastdiff, is_dst = last_dst;
+        timediff == lastdiff && last_dst == is_dst && t < endt ; 
+        t += 10*DAY)
+      get_time_diff(t,&timediff,&is_dst);
+
+    if (t >= endt) break;
+
+    high = t;
+
+    /* find the change */
+    while (low+1 < high) {
+      t = (low+high)/2;
+      get_time_diff(t,&timediff,&is_dst);
+      if (timediff == lastdiff && is_dst == last_dst) 
+       low = t;
+      else
+       high = t;
+    }
+    t = high;
+
+    get_time_diff(t,&timediff,&is_dst);
+    printf("TimeDiff=%d is_dst=%d at %s",
+          timediff,is_dst,asctime(localtime(&t)));
+    count++;
+  }
+
+  if (!count)
+    printf("No timezone or DST changes?\n");
+
+  return(0);
+}
diff --git a/dump.c b/dump.c
new file mode 100644 (file)
index 0000000..50d20e1
--- /dev/null
+++ b/dump.c
@@ -0,0 +1,23 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/mman.h>
+
+int main(int argc, char *argv[])
+{
+       int fd = open("/dev/mem", O_RDONLY);
+       unsigned base, length;
+       char *p;
+       
+       sscanf(argv[1],"%x", &base);
+       sscanf(argv[2],"%x", &length);
+
+       fprintf(stderr,"Dumping 0x%x at 0x%x\n", length, base);
+
+       p = mmap(0, length, PROT_READ, MAP_PRIVATE, fd, base);
+       if (p != (char *)-1) {
+               fwrite(p, 1, length, stdout);
+       }
+       return 0;
+}
diff --git a/dump_file.c b/dump_file.c
new file mode 100644 (file)
index 0000000..809b818
--- /dev/null
@@ -0,0 +1,77 @@
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <ctype.h>
+#include <sys/types.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+typedef unsigned char uint8_t;
+typedef unsigned uint32;
+
+void dump_data(int level, const char *buf1,int len)
+{
+#define DEBUGADD(lvl, x) printf x
+#define MIN(a,b) ((a)<(b)?(a):(b))
+       void print_asc(int level, const uint8_t *buf,int len) {
+               int i;
+               for (i=0;i<len;i++)
+                       DEBUGADD(level,("%c", isprint(buf[i])?buf[i]:'.'));
+       }
+       const uint8_t *buf = (const uint8_t *)buf1;
+       int i=0;
+       if (len<=0) return;
+
+
+       DEBUGADD(level,("[%06X] ",i));
+       for (i=0;i<len;) {
+               DEBUGADD(level,("%02X ",(int)buf[i]));
+               i++;
+               if (i%8 == 0) DEBUGADD(level,(" "));
+               if (i%16 == 0) {      
+                       print_asc(level,&buf[i-16],8); DEBUGADD(level,(" "));
+                       print_asc(level,&buf[i-8],8); DEBUGADD(level,("\n"));
+                       if (i<len) DEBUGADD(level,("[%06X] ",i));
+               }
+       }
+       if (i%16) {
+               int n;
+               n = 16 - (i%16);
+               DEBUGADD(level,(" "));
+               if (n>8) DEBUGADD(level,(" "));
+               while (n--) DEBUGADD(level,("   "));
+               n = MIN(8,i%16);
+               print_asc(level,&buf[i-(i%16)],n); DEBUGADD(level,( " " ));
+               n = (i%16) - n;
+               if (n>0) print_asc(level,&buf[i-n],n); 
+               DEBUGADD(level,("\n"));    
+       }       
+}
+
+static void *map_file(char *fname, size_t *size)
+{
+       int fd = open(fname, O_RDONLY);
+       struct stat st;
+       void *p;
+       
+       fstat(fd, &st);
+       p = mmap(0, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
+       close(fd);
+
+       *size = st.st_size;
+       return p;
+}
+
+int main(int argc, char *argv[])
+{
+       unsigned char *p;
+       size_t size;
+
+       p = map_file(argv[1], &size);   
+
+       dump_data(0, p, size);
+
+       return 0;
+}
diff --git a/dumpxattr.c b/dumpxattr.c
new file mode 100644 (file)
index 0000000..563827b
--- /dev/null
@@ -0,0 +1,39 @@
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <attr/xattr.h>
+
+
+int main(int argc, const char *argv[])
+{
+       int rc;
+       const char *attrname, *fname;
+       unsigned char *buf;
+
+       if (argc < 3) {
+               printf("usage: dumpxattr <fname> <attrname>\n");
+               exit(1);
+       }
+
+       fname = argv[1];
+       attrname = argv[2];
+
+       rc = getxattr(fname, attrname, NULL, 0);
+       if (rc == -1) {
+               perror(fname);
+               exit(1);
+       }
+
+       buf = malloc(rc);
+
+       rc = getxattr(fname, attrname, buf, rc);
+       if (rc == -1) {
+               perror(fname);
+               exit(1);
+       }
+
+       write(1, buf, rc);
+
+       return 0;
+}
diff --git a/e35.c b/e35.c
new file mode 100644 (file)
index 0000000..dd9f372
--- /dev/null
+++ b/e35.c
@@ -0,0 +1,8 @@
+#include <stdio.h>
+
+main(int argc,char *argv[])
+{
+  if (argc == 35) {
+    puts("blah\n");
+  }
+}
diff --git a/echo.c b/echo.c
new file mode 100644 (file)
index 0000000..c8be109
--- /dev/null
+++ b/echo.c
@@ -0,0 +1,21 @@
+#include <stdio.h>
+
+
+#define MIN(a,b) ((a)<(b)?(a):(b))
+
+
+static void foo(char *inbuf)
+{
+       unsigned int data_len = 0x10000 - 39;
+
+       data_len = MIN(data_len, (sizeof(inbuf)-(37)));
+
+       printf("data_len=%u\n", data_len);
+}
+
+int main(void)
+{
+       char buf[0x10000];
+
+       foo(buf);
+}
diff --git a/endpoint.c b/endpoint.c
new file mode 100644 (file)
index 0000000..3920afd
--- /dev/null
@@ -0,0 +1,10 @@
+struct dcerpc_endpoint_list {
+       int count;
+       char *names[];
+};
+
+
+struct dcerpc_endpoint_list xx = {
+       2,
+       { "foo", "bar" }
+};
diff --git a/epoll.c b/epoll.c
new file mode 100644 (file)
index 0000000..9248f7a
--- /dev/null
+++ b/epoll.c
@@ -0,0 +1,57 @@
+#include <sys/epoll.h>
+#include <errno.h>
+
+/*
+ written by Stefan Metzmacher <metze@samba.org>
+
+ this code calls epoll_wait() in a endless loop with a
+ timeout of 1000 = 1s, before the loop we add one event
+ to epoll via epoll_ctl(EPOLL_CTL_ADD), we add it with an event mask
+ of 0, so we don't request notification for any events!
+
+ this test demonstrates that we'll get an EPOLLHUP notification,
+ when the fd was closed by the other end of the pipe/socket
+
+ this programm used the stdin (fd=0).
+ so use it like this:
+ (sleep 5 &) | ./epoll_test
+*/
+int main(void) {
+       int epoll_fd;
+       struct epoll_event event;
+       int socket_fd;
+       int ret, i;
+#define MAXEVENTS 1
+       struct epoll_event events[MAXEVENTS];
+       int timeout = -1;
+
+       epoll_fd = epoll_create(64);
+
+       socket_fd = 0;
+
+       memset(&event, 0, sizeof(event));
+       event.events = 0;
+       event.data.fd = socket_fd;
+       ret = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, socket_fd, &event);
+       printf("epoll_ctl:ADD: %d\n", ret);
+
+       while (1) {
+               ret = epoll_wait(epoll_fd, events, MAXEVENTS, 1000);
+               if (ret == -1 && errno != EINTR) {
+                       return -1;
+               }
+               printf("epool_wait: ret[%d] errno[%d]\n",ret, errno);
+       
+               for (i=0;i<ret;i++) {
+                       printf("event i[%d] fd[%d] events[0x%08X]", i, events[i].data.fd, events[i].events);
+                       if (events[i].events & EPOLLIN) printf(" EPOLLIN");
+                       if (events[i].events & EPOLLHUP) printf(" EPOLLHUP");
+                       if (events[i].events & EPOLLERR) printf(" EPOLLERR");
+                       if (events[i].events & EPOLLOUT) printf(" EPOLLOUT");
+                       printf("\n");
+               }
+       }
+
+       return 0;
+}
diff --git a/eq.c b/eq.c
new file mode 100644 (file)
index 0000000..4844461
--- /dev/null
+++ b/eq.c
@@ -0,0 +1,7 @@
+main()
+{
+       int x, y, z;
+       y = 7; z = 7;
+       x = y == z;
+       printf("%d\n", x);
+}
diff --git a/errno.c b/errno.c
new file mode 100644 (file)
index 0000000..3f15726
--- /dev/null
+++ b/errno.c
@@ -0,0 +1,12 @@
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+
+int main(void)
+{
+       int i;
+       for (i=0;i<32;i++) {
+               printf("%3d  %s\n", i, strerror(i));
+       }
+       return 0;
+}
diff --git a/error.c b/error.c
new file mode 100644 (file)
index 0000000..87da3a5
--- /dev/null
+++ b/error.c
@@ -0,0 +1,4 @@
+main()
+{
+       exit(107007);
+}
diff --git a/etst.c b/etst.c
new file mode 100644 (file)
index 0000000..fde14d7
--- /dev/null
+++ b/etst.c
@@ -0,0 +1,15 @@
+#include <errno.h>
+
+
+main()
+{
+  int x=0;
+  errno = 0;
+  write(7,&x,sizeof(x));
+  printf("errno=%d error=%s errno=%d\n",errno,strerror(errno),errno);
+  write(1,&x,0);
+  printf("errno=%d error=%s errno=%d\n",errno,strerror(errno),errno);
+  printf("hello\n");
+  printf("errno=%d error=%s errno=%d\n",errno,strerror(errno),errno);
+  printf("errno=%d error=%s errno=%d\n",errno,strerror(errno),errno);
+}
diff --git a/f.c b/f.c
new file mode 100644 (file)
index 0000000..c761972
--- /dev/null
+++ b/f.c
@@ -0,0 +1,6 @@
+int main(int argc, char *argv[])
+{
+       register int f;
+       f = 1;
+       return f;
+}
diff --git a/fake_uname.c b/fake_uname.c
new file mode 100644 (file)
index 0000000..9499a5e
--- /dev/null
@@ -0,0 +1,20 @@
+#include <pwd.h>
+#include <sys/types.h>
+#include <sys/utsname.h>
+
+int uname(struct utsname *buf)
+{
+
+}
+
+struct passwd *getpwuid(uid_t uid)
+{
+       static void *h;
+       static struct passwd * (*getpwuid_orig)(uid_t uid);
+       if (!h) {
+               h = dlopen("/lib/libc.so.6", RTLD_LAZY);
+               getpwuid_orig = dlsym(h, "getpwuid");
+       }
+       
+       return getpwuid_orig(uid+1);
+}
diff --git a/fchown.c b/fchown.c
new file mode 100644 (file)
index 0000000..63b71ec
--- /dev/null
+++ b/fchown.c
@@ -0,0 +1,9 @@
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <asm/fcntl.h>
+
+main()
+{
+       int fd = open(".", O_DIRECTORY|O_RDONLY);
+       fchown(fd, 2015, 2015);
+}
diff --git a/fd_filename.c b/fd_filename.c
new file mode 100644 (file)
index 0000000..f9c9b24
--- /dev/null
@@ -0,0 +1,22 @@
+#include <sys/types.h>
+#include <utime.h>
+#include <stdio.h>
+#include <errno.h>
+
+
+
+int fd_utime(int fd, struct utimbuf *buf)
+{
+       char *fd_path = NULL;
+       int ret;
+
+       asprintf(&fd_path, "/proc/self/%d", fd);
+       if (!fd_path) {
+               errno = ENOMEM;
+               return -1;
+       }
+
+       ret = utime(fd_path, buf);
+       free(fd_path);
+       return ret;
+}
diff --git a/fifo1.c b/fifo1.c
new file mode 100644 (file)
index 0000000..c8fbc52
--- /dev/null
+++ b/fifo1.c
@@ -0,0 +1,18 @@
+#include <stdio.h>
+#include <sys/fcntl.h>
+
+main()
+{
+  char buf[100];
+  int fd;
+  
+  mkfifo("ttyc",0777);
+  fd = open("ttyc",O_RDWR);
+
+  read(fd,buf,8);
+  printf("got [%s]\n",buf);
+
+  write(fd,"world!\n",7);
+  printf("done\n");
+}
+
diff --git a/fifo2.c b/fifo2.c
new file mode 100644 (file)
index 0000000..51d9771
--- /dev/null
+++ b/fifo2.c
@@ -0,0 +1,17 @@
+#include <stdio.h>
+#include <sys/fcntl.h>
+
+main()
+{
+  char buf[100];
+  int fd;
+
+  sleep(5);
+  fd = open("ttyc",O_RDWR);
+
+  write(fd,"helloxxY",8);
+
+  read(fd,buf,7);
+  printf("got [%s]\n",buf);
+}
+
diff --git a/fillmem.c b/fillmem.c
new file mode 100644 (file)
index 0000000..f5e5334
--- /dev/null
+++ b/fillmem.c
@@ -0,0 +1,24 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+int main(int argc, char *argv[])
+{
+       int size = 250;
+       char *p;
+
+       if (argc > 1) {
+               size = atoi(argv[1]);
+       }
+       
+       size *= 1024 * 1024;
+
+       p = malloc(size);
+       if (!p) {
+               fprintf(stderr,"Malloc failed!\n");
+               exit(1);
+       }
+
+       memset(p, 0, size);
+       exit(0);
+}
diff --git a/fname.c b/fname.c
new file mode 100644 (file)
index 0000000..3d83af5
--- /dev/null
+++ b/fname.c
@@ -0,0 +1,4 @@
+main()
+{
+       printf("%s\n", __FUNCTION__);
+}
diff --git a/fnmatch.c b/fnmatch.c
new file mode 100644 (file)
index 0000000..a2d5860
--- /dev/null
+++ b/fnmatch.c
@@ -0,0 +1,5 @@
+
+main(int argc, char *argv[])
+{
+       printf("%d\n", fnmatch(argv[1], argv[2], 0));
+}
diff --git a/foo.c b/foo.c
new file mode 100644 (file)
index 0000000..6f34a9e
--- /dev/null
+++ b/foo.c
@@ -0,0 +1,7 @@
+void test(int n)
+{
+       struct foo {
+               int x[n];
+       } blah;
+
+}
diff --git a/foo2.c b/foo2.c
new file mode 100644 (file)
index 0000000..729345b
--- /dev/null
+++ b/foo2.c
@@ -0,0 +1,87 @@
+
+FILE *fopen(const char *fname, char *rw)
+{
+
+}
+
+struct vnode *vn;
+
+struct dentry *de;
+
+void *opendir();
+
+
+int myfunction(struct SysCtx *ctx, const char *name)
+
+
+
+
+foo_fopen(FILE *f, char *rw)
+{
+
+}
+
+
+foo_free() OR pools ?
+
+
+main()
+{
+       FILE f;
+
+       foo_fopen(&f, "w");
+
+}
+
+struct Sys_SrvName {
+       char *srvname;
+       char *adminname;
+       char *location;
+}
+
+static void dans_set_ip(SysCtx *ctx, SYS_IP *ip)
+{
+       Sys_PushMemCtx(ctx);
+       foo(ctx, ip);
+       Sys_PopMemCtx(ctx);
+}
+
+void dans_func(void)
+{
+       struct Sys_SrvName *buf;
+       SysCtx *ctx;
+
+       ctx = Sys_InitCtx();
+
+       Sys_Lock(ctx);
+       Sys_GetSrvName(ctx, &buf);
+
+       printf("Current is %s - %s - %s\n",
+              buf->srvname, buf->adminname, buf->location);
+
+       Sys_Free(ctx, buf->location);
+
+       ctx_asprintf(ctx, &buf->location, "newloc");
+       ctx_asprintf(ctx, &buf->srvname, "%s - srvnamenew", buf->srvname);
+
+       if (my_var > 3) {
+               int fd;
+               foo2();
+               fd = Sys_OpenAlan(ctx, "/tmp/xxx",O_RDONLY);
+               if (fd == -1) {
+                       return SYS_FAILURE;
+               }
+       }
+
+       Sys_SetSrvName(ctx, buf);
+
+       Sys_UnLock(ctx);
+
+       Sys_CtxNew(ctx);
+}
+
+
+main()
+{
+       dans_func(ctx);
+}
diff --git a/foo3.c b/foo3.c
new file mode 100644 (file)
index 0000000..ec5a36a
--- /dev/null
+++ b/foo3.c
@@ -0,0 +1,24 @@
+#include <stdio.h>
+
+struct user {
+       char username[256];
+       char groups[200][256];
+};
+
+void dans_func(IN/OUT STRING char x[100])
+{
+       char x[100];
+       struct user users[MAX_USERS];
+
+
+       strcpy(x, "foo");
+       printf("%s\n", x);
+
+       x[4] = '3';
+}
+
+
+main()
+{
+       dans_func();
+}
diff --git a/foo4.c b/foo4.c
new file mode 100644 (file)
index 0000000..934264b
--- /dev/null
+++ b/foo4.c
@@ -0,0 +1,14 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <fcntl.h>
+#include <sys/mman.h>
+#include <fcntl.h>
+
+main()
+{
+       int fd = open("/dev/zero", O_RDWR);
+       char *map;
+
+       map = mmap(0, 0x4000, PROT_READ | PROT_WRITE, MAP_LOCKED| MAP_PRIVATE | MAP_FILE,
+                  fd, 0);
+}
diff --git a/foo5.c b/foo5.c
new file mode 100644 (file)
index 0000000..cdfed16
--- /dev/null
+++ b/foo5.c
@@ -0,0 +1,26 @@
+
+int sys_open(const char *fname, int opts)
+{
+       int ret;
+
+       do {
+               ret = open(fname, opts);
+       } while (ret == -1 && errno == EINTR);
+       return ret;
+}
+
+main()
+{
+       char *fname = "foo.txt";
+       int fd;
+
+again:
+       fd = sys_open(fname, O_RDONLY);
+       if (fd == -1) {
+               if (errno == EINTR) goto again;
+               perror(fname);
+               exit(1);
+       }
+       
+
+}
diff --git a/fopen.c b/fopen.c
new file mode 100644 (file)
index 0000000..80a42c1
--- /dev/null
+++ b/fopen.c
@@ -0,0 +1,8 @@
+#include <stdio.h>
+
+int main(void)
+{
+       FILE *f = fopen("foo", "w");
+       fclose(f);
+       return 1;
+}
diff --git a/forklots.c b/forklots.c
new file mode 100644 (file)
index 0000000..a4f7fd0
--- /dev/null
@@ -0,0 +1,23 @@
+#include <stdio.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+int main(int argc, char *argv[])
+{
+       int i, n = atoi(argv[1]);
+       pid_t *pids = malloc(n * sizeof(pid_t));
+
+       for (i=0;i<n;i++) {
+               pids[i] = fork();
+               if (pids[i] == 0) {
+                       sleep(1000);
+                       exit(0);
+               }
+       }
+
+
+       for (i=0;i<n;i++) {
+               waitpid(pids[i], NULL, 0);
+       }
+       return 0;
+}
diff --git a/fsid.c b/fsid.c
new file mode 100644 (file)
index 0000000..8fa2527
--- /dev/null
+++ b/fsid.c
@@ -0,0 +1,43 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/vfs.h>
+#include <sys/statvfs.h>
+
+int main(int argc, const char *argv[])
+{
+       struct statfs s1;
+       struct statvfs s2;
+       const char *path;
+       unsigned char buf[sizeof(fsid_t)];
+       int i;
+
+       if (argc != 2) {
+               printf("Usage: fsid <path>\n");
+               exit(1);
+       }
+
+       path = argv[1];
+       
+       if (statfs(path, &s1) != 0) {
+               perror("statfs");
+               exit(1);
+       }
+
+       memcpy(buf, &s1.f_fsid, sizeof(fsid_t));
+
+       printf("statfs  ");
+       for (i=0;i<sizeof(buf);i++) {
+               printf("%02x", buf[i]);
+       }
+       printf("\n");
+
+       if (statvfs(path, &s2) != 0) {
+               perror("statvfs");
+               exit(1);
+       }
+
+       printf("statvfs %llx\n", (unsigned long long)s2.f_fsid);
+
+       return 0;       
+}
diff --git a/fstest-aio.c b/fstest-aio.c
new file mode 100644 (file)
index 0000000..879c728
--- /dev/null
@@ -0,0 +1,371 @@
+/* filesystem verification tool, designed to detect data corruption on a filesystem
+
+   tridge@samba.org, March 2002
+ */
+#define _GNU_SOURCE
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/mman.h>
+#include <dirent.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <libaio.h>
+
+/* variables settable on the command line */
+static int loop_count = 100;
+static int num_files = 1;
+static int file_size = 1024*1024;
+static int block_size = 1024;
+static char *base_dir = ".";
+static int use_mmap;
+static int use_sync;
+
+typedef unsigned char uchar;
+
+#ifndef MIN
+#define MIN(a,b) ((a)<(b)?(a):(b))
+#endif
+
+static void fatal(char * msg)
+{
+       perror(msg);
+       exit(1);
+}
+
+static ssize_t a_pread(int fd, void *buf, size_t count, off_t offset)
+{
+       struct io_event ev;
+       struct iocb iocb;
+       struct iocb *iocbp = &iocb;
+       io_context_t io_ctx;
+
+       memset(&io_ctx, 0, sizeof(io_ctx));
+
+       if (io_setup(1, &io_ctx) != 0) {
+               fatal("io_setup failed");
+       }
+
+       io_prep_pread(&iocb, fd, buf, count, offset);
+
+       if (io_submit(io_ctx, 1, &iocbp) != 1) {
+               fatal("io_submit failed");
+       }
+
+       if (io_getevents(io_ctx, 1, 1, &ev, NULL) != 1) {
+               fatal("io_getevents failed");
+       }
+
+       io_destroy(io_ctx);
+
+       return iocb.u.c.nbytes;
+}
+
+static void *x_malloc(int size)
+{
+       void *ret = malloc(size);
+       if (!ret) {
+               fprintf(stderr,"Out of memory for size %d!\n", size);
+               exit(1);
+       }
+       return ret;
+}
+
+
+/* generate a buffer for a particular child, fnum etc. Just use a simple buffer
+   to make debugging easy 
+*/
+static void gen_buffer(uchar *buf, int loop, int child, int fnum, int ofs)
+{
+       uchar v = (loop+child+fnum+(ofs/block_size)) % 256;
+       memset(buf, v, block_size);
+}
+
+/* 
+   check if a buffer from disk is correct
+*/
+static void check_buffer(uchar *buf, int loop, int child, int fnum, int ofs)
+{
+       uchar *buf2;
+
+       buf2 = x_malloc(block_size);
+
+       gen_buffer(buf2, loop, child, fnum, ofs);
+       
+       if (memcmp(buf, buf2, block_size) != 0) {
+               int i, j;
+               for (i=0;buf[i] == buf2[i] && i<block_size;i++) ;
+               fprintf(stderr,"Corruption in child %d fnum %d at offset %d\n",
+                       child, fnum, ofs+i);
+
+               printf("Correct:   ");
+               for (j=0;j<MIN(20, block_size-i);j++) {
+                       printf("%02x ", buf2[j+i]);
+               }
+               printf("\n");
+
+               printf("Incorrect: ");
+               for (j=0;j<MIN(20, block_size-i);j++) {
+                       printf("%02x ", buf[j+i]);
+               }
+               printf("\n");
+               exit(1);
+       }
+
+       free(buf2);
+}
+
+/*
+  create a file with a known data set for a child
+ */
+static void create_file(const char *dir, int loop, int child, int fnum)
+{
+       uchar *buf;
+       int size, fd;
+       char fname[1024];
+
+       buf = x_malloc(block_size);
+       sprintf(fname, "%s/file%d", dir, fnum);
+       fd = open(fname, O_RDWR|O_CREAT|O_TRUNC | (use_sync?O_SYNC:0), 0644);
+       if (fd == -1) {
+               perror(fname);
+               exit(1);
+       }
+               
+       if (!use_mmap) {
+               for (size=0; size<file_size; size += block_size) {
+                       gen_buffer(buf, loop, child, fnum, size);
+                       if (pwrite(fd, buf, block_size, size) != block_size) {
+                               fprintf(stderr,"Write failed at offset %d\n", size);
+                               exit(1);
+                       }
+               }
+       } else {
+               char *p;
+               if (ftruncate(fd, file_size) != 0) {
+                       perror("ftruncate");
+                       exit(1);
+               }
+               p = mmap(NULL, file_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
+               if (p == (char *)-1) {
+                       perror("mmap");
+                       exit(1);
+               }
+               for (size=0; size<file_size; size += block_size) {
+                       gen_buffer(p+size, loop, child, fnum, size);
+               }
+               munmap(p, file_size);
+       }
+
+       free(buf);
+       close(fd);
+}
+
+/* 
+   check that a file has the right data
+ */
+static void check_file(const char *dir, int loop, int child, int fnum)
+{
+       uchar *buf;
+       int size, fd;
+       char fname[1024];
+
+       buf = x_malloc(block_size);
+
+       sprintf(fname, "%s/file%d", dir, fnum);
+       fd = open(fname, O_RDONLY);
+       if (fd == -1) {
+               perror(fname);
+               exit(1);
+       }
+
+       for (size=0; size<file_size; size += block_size) {
+               if (a_pread(fd, buf, block_size, size) != block_size) {
+                       fprintf(stderr,"read failed at offset %d\n", size);
+                       exit(1);
+               }
+               check_buffer(buf, loop, child, fnum, size);
+       }
+
+       free(buf);
+       close(fd);
+}
+
+/* 
+   revsusive directory traversal - used for cleanup
+   fn() is called on all files/dirs in the tree
+ */
+void traverse(const char *dir, int (*fn)(const char *))
+{
+       DIR *d;
+       struct dirent *de;
+
+       d = opendir(dir);
+       if (!d) return;
+
+       while ((de = readdir(d))) {
+               char fname[1024];
+               struct stat st;
+
+               if (strcmp(de->d_name,".") == 0) continue;
+               if (strcmp(de->d_name,"..") == 0) continue;
+
+               sprintf(fname, "%s/%s", dir, de->d_name);
+               if (lstat(fname, &st)) {
+                       perror(fname);
+                       continue;
+               }
+
+               if (S_ISDIR(st.st_mode)) {
+                       traverse(fname, fn);
+               }
+
+               fn(fname);
+       }
+
+       closedir(d);
+}
+
+/* the main child function - this creates/checks the file for one child */
+static void run_child(int child)
+{
+       int i, loop;
+       char dir[1024];
+
+       sprintf(dir, "%s/child%d", base_dir, child);
+
+       /* cleanup any old files */
+       if (remove(dir) != 0 && errno != ENOENT) {
+               printf("Child %d cleaning %s\n", child, dir);
+               traverse(dir, remove);
+               remove(dir);
+       }
+
+       if (mkdir(dir, 0755) != 0) {
+               perror(dir);
+               exit(1);
+       }
+
+       for (loop = 0; loop < loop_count; loop++) {
+               printf("Child %d loop %d\n", child, loop);
+               for (i=0;i<num_files;i++) {
+                       create_file(dir, loop, child, i);
+               }
+               for (i=0;i<num_files;i++) {
+                       check_file(dir, loop, child, i);
+               }
+       }
+
+       /* cleanup afterwards */
+       printf("Child %d cleaning up %s\n", child, dir);
+       traverse(dir, remove);
+       remove(dir);
+
+       exit(0);
+}
+
+static void usage(void)
+{
+       printf("\n"
+"Usage: fstest [options]\n"
+"\n"
+" -n num_children       set number of child processes\n"
+" -f num_files          set number of files\n"
+" -s file_size          set file sizes\n"
+" -b block_size         set block (IO) size\n"
+" -p path               set base path\n"
+" -l loops              set loop count\n"
+" -m                    use mmap\n"
+" -S                    use synchronous IO\n"
+" -h                    show this help message\n");
+}
+
+/* main program */
+int main(int argc, char *argv[])
+{
+       int c;
+       extern char *optarg;
+       extern int optind;
+       int num_children = 1;
+       int i, status, ret;
+
+       while ((c = getopt(argc, argv, "n:s:f:p:l:b:Shm")) != -1) {
+               switch (c) {
+               case 'n':
+                       num_children = strtol(optarg, NULL, 0);
+                       break;
+               case 'b':
+                       block_size = strtol(optarg, NULL, 0);
+                       break;
+               case 'f':
+                       num_files = strtol(optarg, NULL, 0);
+                       break;
+               case 's':
+                       file_size = strtol(optarg, NULL, 0);
+                       break;
+               case 'p':
+                       base_dir = optarg;
+                       break;
+               case 'm':
+                       use_mmap = 1;
+                       break;
+               case 'S':
+                       use_sync = 1;
+                       break;
+               case 'l':
+                       loop_count = strtol(optarg, NULL, 0);
+                       break;
+               case 'h':
+                       usage();
+                       exit(0);
+               default:
+                       usage();
+                       exit(1);
+               }
+       }
+
+       argc -= optind;
+       argv += optind;
+
+       /* round up the file size */
+       if (file_size % block_size != 0) {
+               file_size = (file_size + (block_size-1)) / block_size;
+               file_size *= block_size;
+               printf("Rounded file size to %d\n", file_size);
+       }
+
+       printf("num_children=%d file_size=%d num_files=%d loop_count=%d block_size=%d\nmmap=%d sync=%d\n",
+              num_children, file_size, num_files, loop_count, block_size, use_mmap, use_sync);
+
+       printf("Total data size %.1f Mbyte\n",
+              num_files * num_children * 1.0e-6 * file_size);
+
+       /* fork and run run_child() for each child */
+       for (i=0;i<num_children;i++) {
+               if (fork() == 0) {
+                       run_child(i);
+                       exit(0);
+               }
+       }
+
+       ret = 0;
+
+       /* wait for children to exit */
+       while (waitpid(0, &status, 0) == 0 || errno != ECHILD) {
+               if (WEXITSTATUS(status) != 0) {
+                       ret = WEXITSTATUS(status);
+                       printf("Child exited with status %d\n", ret);
+               }
+       }
+
+       if (ret != 0) {
+               printf("fstest failed with status %d\n", ret);
+       }
+
+       return ret;
+}
diff --git a/fsync.c b/fsync.c
new file mode 100644 (file)
index 0000000..3f79672
--- /dev/null
+++ b/fsync.c
@@ -0,0 +1,77 @@
+#define _XOPEN_SOURCE 500
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/mman.h>
+#include <fcntl.h>
+#include <sys/file.h>
+#include <sys/time.h>
+#include <time.h>
+#include <string.h>
+
+static struct timeval tp1,tp2;
+
+static void start_timer()
+{
+       gettimeofday(&tp1,NULL);
+}
+
+static double end_timer()
+{
+       gettimeofday(&tp2,NULL);
+       return (tp2.tv_sec + (tp2.tv_usec*1.0e-6)) - 
+               (tp1.tv_sec + (tp1.tv_usec*1.0e-6));
+}
+
+int main(int argc, const char *argv[])
+{
+       int fd;
+       char *p;
+       size_t size = 4096;
+       const char *fname = "fsync.dat";
+
+       unlink(fname);
+       fd = open(fname, O_RDWR|O_CREAT|O_EXCL, 0600);
+       if (fd == -1) {
+               perror(argv[1]);
+               return -1;
+       }
+
+       while (size < 100*1024*1024) {
+               int count = 0;
+               ftruncate(fd, size);
+
+               p = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
+               if (p == (void *)-1) {
+                       perror("mmap");
+                       return -1;
+               }
+
+               memset(p, 1, size);
+               msync(p, size, MS_SYNC);
+               fsync(fd);
+
+               start_timer();
+               while (end_timer() < 2) {
+#if 0
+                       pwrite(fd, &count, 1, size-1);
+                       fsync(fd);
+#else
+                       memset(p+size-100, count, 100);
+//                     msync(p, size, MS_SYNC);
+                       msync(p+size-4096, 4096, MS_SYNC);
+#endif
+                       count++;
+               }
+
+               printf("%7d  %.2f us  (count=%d)\n", 
+                      size, 1.0e6 * end_timer() / count, count);
+
+               munmap(p, size);
+               size *= 2;
+       }
+
+       close(fd);
+       unlink(fname);
+
+       return 0;
+}
diff --git a/ftime.c b/ftime.c
new file mode 100644 (file)
index 0000000..6f0aa2d
--- /dev/null
+++ b/ftime.c
@@ -0,0 +1,22 @@
+#include <sys/types.h>
+#include <utime.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <sys/time.h>
+
+main()
+{
+  struct utimbuf times;
+  int fd = open("xx",O_RDWR|O_CREAT|O_TRUNC,0666);
+
+  write(fd,"hello",6);
+
+  bzero(&times,sizeof(times));
+
+  times.actime = 3600;
+  times.modtime = 7200;
+
+  utime("xx",&times);
+
+  close(fd);
+}
diff --git a/fwide.c b/fwide.c
new file mode 100644 (file)
index 0000000..34e9731
--- /dev/null
+++ b/fwide.c
@@ -0,0 +1,49 @@
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <dlfcn.h>
+#include <stdio.h>
+#include <stdarg.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <wchar.h>
+
+FILE *xxfopen1(const char *filename, const char *mode)
+{
+       FILE *(*fopen_orig)(const char *, const char *);
+       void *dl = dlopen("/lib/libc.so.6", RTLD_NOW);
+       fopen_orig = dlsym(dl, "fopen");
+       printf("fopen: %p  fopen_orig_dlopen: %p\n", fopen, fopen_orig);
+       return fopen_orig(filename, mode);
+}
+
+FILE *xxfopen2(const char *filename, const char *mode)
+{
+       FILE *(*fopen_orig)(const char *, const char *);
+       FILE *(*fopen_orig2)(const char *, const char *);
+       fopen_orig = dlsym(RTLD_NEXT, "fopen");
+       fopen_orig2 = dlsym(RTLD_NEXT, "fopen");
+       printf("fopen: %p  fopen_orig_next: %p %p\n", fopen, fopen_orig, fopen_orig2);
+       return fopen_orig(filename, mode);
+}
+
+int main(void)
+{
+       FILE *f1, *f2, *f3;
+       int ret1, ret2, ret3;
+       char *s;
+       
+       f1 = xxfopen1("test.txt", "w");
+       f2 = xxfopen2("test.txt", "w");
+       f3 = fopen("test.txt", "w");
+       ret1 = fwide(f1, 1);
+       ret2 = fwide(f2, 1);
+       ret3 = fwide(f3, 1);
+
+       printf("f1 %d  f2 %d f3 %d\n", ret1, ret2, ret3);
+
+       asprintf(&s, "cat /proc/%d/maps", getpid());
+       system(s);
+
+       return 0;
+}
diff --git a/fwrite.c b/fwrite.c
new file mode 100644 (file)
index 0000000..1419cf1
--- /dev/null
+++ b/fwrite.c
@@ -0,0 +1,14 @@
+#include <stdio.h>
+
+int main(int argc, const char *argv[])
+{
+       FILE *f;
+       char buf[8 * 1024 * 1024];
+
+       f = fopen(argv[1], "r+");
+
+       fwrite(buf, sizeof(buf), 1, f);
+
+       sleep(100);
+       return 0;
+}
diff --git a/getgrent.c b/getgrent.c
new file mode 100644 (file)
index 0000000..c073122
--- /dev/null
@@ -0,0 +1,12 @@
+#include <grp.h>
+#include <sys/types.h>
+#include <stdio.h>
+
+main()
+{
+       struct group *grp;
+       while (grp = getgrent()) {
+               printf("%s : %d\n", grp->gr_name, (int)grp->gr_gid);
+       }
+       return 0;
+}
diff --git a/getgrouplist2.c b/getgrouplist2.c
new file mode 100644 (file)
index 0000000..aaed1b7
--- /dev/null
@@ -0,0 +1,76 @@
+#define NGROUPS_MAX 1000
+
+#include <stdio.h>
+#include <unistd.h>
+
+#include <sys/types.h>
+#include <string.h>
+#include <grp.h>
+#include <pwd.h>
+
+
+/*
+  This is a *much* faster way of getting the list of groups for a user
+  without changing the current supplemenrary group list. The old
+  method user getgrent() which could take 20 minutes on a really big
+  network with hundeds of thousands of groups and users. The new method
+  takes a couple of seconds. (tridge@samba.org)
+
+  unfortunately it only works if we are root!
+  */
+int
+getgrouplist(uname, agroup, groups, grpcnt)
+       const char *uname;
+       gid_t agroup;
+       register gid_t *groups;
+       int *grpcnt;
+{
+       gid_t gids_saved[NGROUPS_MAX + 1];
+       int ret, ngrp_saved;
+       
+       ngrp_saved = getgroups(NGROUPS_MAX, gids_saved);
+       if (ngrp_saved == -1) {
+               return -1;
+       }
+
+       if (initgroups(uname, agroup) != 0) {
+               return -1;
+       }
+
+       ret = getgroups(*grpcnt, groups);
+       if (ret >= 0) {
+               *grpcnt = ret;
+       }
+
+       if (setgroups(ngrp_saved, gids_saved) != 0) {
+               /* yikes! */
+               fprintf(stderr,"getgrouplist: failed to reset group list!\n");
+               exit(1);
+       }
+
+       return ret;
+}
+
+int main(int argc, char *argv[])
+{
+       char *user = argv[1];
+       struct passwd *pwd;
+       gid_t gids[NGROUPS_MAX + 1];
+       int count, ret, i;
+
+       pwd = getpwnam(user);
+
+       count = NGROUPS_MAX;
+
+       ret = getgrouplist(user, pwd->pw_gid, gids, &count);
+
+       printf("Got %d groups\n", ret);
+       if (ret != -1) {
+               for (i=0;i<count;i++) {
+                       printf("%u ", (unsigned)gids[i]);
+               }
+               printf("\n");
+       }
+       
+       return 0;
+}
diff --git a/getgroups.c b/getgroups.c
new file mode 100644 (file)
index 0000000..42f5cb1
--- /dev/null
@@ -0,0 +1,87 @@
+#include <stdio.h>
+#include <grp.h>
+#include <pwd.h>
+#include <sys/types.h>
+
+int listgroups1(char *user)
+{
+       struct passwd *pass;
+       struct group *grp;
+       int ngroups, i;
+       gid_t *grps;
+
+       pass = getpwnam(user);
+
+       initgroups(user, pass->pw_gid);
+
+       ngroups = getgroups(0, NULL);
+
+       grps = (gid_t *)malloc(ngroups * sizeof(gid_t));
+       
+       getgroups(ngroups, grps);
+
+       grp = getgrgid(pass->pw_gid);
+       printf("%5d %s\n", pass->pw_gid, grp->gr_name);
+
+       for (i=0;i<ngroups;i++) {
+               grp = getgrgid(grps[i]);
+               printf("%5d %s\n", grps[i], grp->gr_name);
+       }
+       return ngroups;
+}
+
+int listgroups2(char *user)
+{
+       struct group *grp;
+       char *p;
+       int i;
+
+       setgrent();
+
+       while ((grp = getgrent())) {
+               if (!grp->gr_mem) continue;
+               for (i=0; grp->gr_mem[i]; i++) {
+                       if (strcmp(grp->gr_mem[i], user) == 0) {
+                               printf("%s\n", grp->gr_name);
+                               break;
+                       }
+               }
+       }
+
+       endgrent();
+}
+
+
+void listgroups3(char *domain)
+{
+       struct group *grp;
+       int len = strlen(domain);
+       
+       setgrent();
+
+       while ((grp = getgrent())) {
+               if (strncasecmp(grp->gr_name, domain, len) == 0 &&
+                   grp->gr_name[len] == '/') {
+                       printf("%s\n", grp->gr_name);
+               }
+       }
+
+       endgrent();
+
+}
+
+void listusers(void)
+{
+       struct passwd *pass;    
+       setpwent();
+       while ((pass = getpwent())) {
+                       printf("%s\n", pass->pw_name);
+       }
+       endpwent();
+}
+
+
+main(int argc, char *argv[])
+{
+       listusers();
+}
diff --git a/gethostbyname.c b/gethostbyname.c
new file mode 100644 (file)
index 0000000..ab7f20e
--- /dev/null
@@ -0,0 +1,24 @@
+#include <sys/time.h>
+#include <sys/types.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netdb.h>
+
+
+main(int argc, char *argv[])
+{
+       char *name = argv[1];
+       struct hostent *hp;
+       struct in_addr addr;
+
+       if (inet_aton(name, &addr) != 0) {
+               printf("It's an IP\n");
+       } else {
+               hp = gethostbyname(name);
+       }
+}
+
diff --git a/getpwent.c b/getpwent.c
new file mode 100644 (file)
index 0000000..e46aaeb
--- /dev/null
@@ -0,0 +1,18 @@
+#include <stdio.h>
+#include <pwd.h>
+#include <sys/types.h>
+
+int main(void)
+{
+       struct passwd *pwd;
+
+       setpwent();
+
+       while ((pwd = getpwent())) {
+               printf("%s\n", pwd->pw_name);
+       }
+       
+       endpwent();
+
+       return 0;
+}
diff --git a/glide2.c b/glide2.c
new file mode 100644 (file)
index 0000000..4eb9f7b
--- /dev/null
+++ b/glide2.c
@@ -0,0 +1,138 @@
+#include <stdio.h>
+#include <string.h>
+
+
+unsigned char Data[100001];
+unsigned char keystream[1001];
+int Rpoint[300];
+
+
+main (int argc,char *argv[]) {
+  FILE *fd;
+  int  i,j,k;
+  int  size;
+  char ch;
+  char *name;
+  int cracked;
+  int sizemask;
+  int maxr;
+  int rsz;
+  int pos;
+  int Rall[300]; /* resource allocation table */
+
+  if (argc<2) {
+    printf("usage: glide filename (username)");
+    exit(1);
+  }
+
+  /* read PWL file */
+
+  fd=fopen(argv[1],"rb");
+  if(fd==NULL) {
+    printf("can't open file %s",argv[2]);
+    exit(1);
+  }
+  size=0;
+  while(!feof(fd)) {
+    Data[size++]=fgetc(fd);
+  }
+  size--;
+  fclose(fd);
+
+  /* find username */
+  name=argv[1];
+  if(argc>2) {
+    name=argv[2];  
+  } else {
+    char *p = strrchr(name,'/');
+    if (p) name = p+1;
+  }
+  printf("Username: %s\n",name);
+
+  /* copy encrypted text into keystream */
+  cracked=size-0x0208;
+  if(cracked<0) cracked=0;
+  if(cracked>1000) cracked=1000;
+  memcpy(keystream,Data+0x208,cracked );
+
+       /* generate 20 bytes of keystream */
+  for(i=0;i<20;i++) {
+    ch=toupper(name[i]);
+    if(ch==0) break;
+    if(ch=='.') break;
+    keystream[i]^=ch;
+  };
+  cracked=20;
+
+
+       /* find allocated resources */
+
+  sizemask=keystream[0]+(keystream[1]<<8);
+  printf("Sizemask: %04X\n",sizemask);
+
+  for(i=0;i<256;i++) Rall[i]=0;
+
+  maxr=0;
+  for(i=0x108;i<0x208;i++) {
+    if(Data[i]!=0xff) {
+      Rall[Data[i]]++;
+      if (Data[i]>maxr) maxr=Data[i];
+    }
+  }
+  maxr=(((maxr/16)+1)*16);     /* resource pointer table size appears
+to be divisible by 16 */
+
+       /* search after resources */
+
+  Rpoint[0]=0x0208+2*maxr+20+2;        /* first resource */
+  for(i=0;i<maxr;i++) {
+    /* find size of current resource */
+    pos=Rpoint[i];
+    rsz=Data[pos]+(Data[pos+1]<<8);
+    rsz^=sizemask;
+    printf("Analyzing block with size:
+%04x\t(%d:%d)\n",rsz,i,Rall[i]);
+    if( (Rall[i]==0) && (rsz!=0) ) {
+      printf("unused resource has nonzero size !!!\n");
+      exit(0);
+    }
+
+    pos+=rsz;
+
+               /* Resources have a tendency to have the wrong size for some
+reason */
+               /* check for correct size */
+
+    if(i<maxr-1) {
+      while(Data[pos+3]!=keystream[1]) {
+       printf(":(%02x)",Data[pos+3]);
+       pos+=2; /* very rude may fail */
+      }
+    }
+
+    pos+=2;    /* include pointer in size */
+    Rpoint[i+1]=pos;
+  }
+  Rpoint[maxr]=size;
+
+       /* insert Table data into keystream */
+  for(i=0;i <= maxr;i++) {
+    keystream[20+2*i]^=Rpoint[i] & 0x00ff;
+    keystream[21+2*i]^=(Rpoint[i] >> 8) & 0x00ff;
+  }
+  cracked+=maxr*2+2;
+
+  printf("%d bytes of keystream recovered\n",cracked);
+
+  /* decrypt resources */
+  for(i=0;i < maxr;i++) {
+    rsz=Rpoint[i+1]-Rpoint[i];
+    if (rsz>cracked) rsz=cracked;
+    printf("Resource[%d] (%d)\n",i,rsz);
+    for(j=0;j<rsz;j++) printf("%c",Data[Rpoint[i]+j]^keystream[j]);
+    printf("\n");
+  }
+
+
+  exit(0);
+}
diff --git a/gls.c b/gls.c
new file mode 100644 (file)
index 0000000..9c65ded
--- /dev/null
+++ b/gls.c
@@ -0,0 +1,9 @@
+#include <stdio.h>
+#include <dirent.h>
+
+
+main()
+{
+  DIR *d = opendir(".");
+  readdir(d);
+}
diff --git a/grp.c b/grp.c
new file mode 100644 (file)
index 0000000..f9383e6
--- /dev/null
+++ b/grp.c
@@ -0,0 +1,22 @@
+#include <stdio.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+
+struct stat buf;
+
+int group(file)
+char *file;
+{
+buf.st_mode = 0;
+stat(file,&buf);
+return(buf.st_gid);
+}
+
+main(argc,argv)
+int argc;
+char *argv[];
+{
+if (argc>1) printf("%d\n",group(argv[1]));
+return(0);
+}
+
diff --git a/gz.c b/gz.c
new file mode 100644 (file)
index 0000000..39e8f48
--- /dev/null
+++ b/gz.c
@@ -0,0 +1,15 @@
+#include <stdio.h>
+#include "zlib.h"
+
+int main(void)
+{
+       gzFile *g = gzdopen(1, "w");
+       char buf[1024];
+       int n;
+
+       while ((n=read(0, buf, sizeof(buf))) > 0) {
+               gzwrite(g, buf, n);
+       }
+       gzclose(g);
+       return 0;
+}
diff --git a/hashreplace.c b/hashreplace.c
new file mode 100644 (file)
index 0000000..a3e4129
--- /dev/null
@@ -0,0 +1,141 @@
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <string.h>
+#include <math.h>
+#include <time.h>
+
+
+#define TOTAL_SIZE (1024*1024)
+#define HASH_SIZE (1000)
+#define NBUCKETS 8
+
+typedef unsigned u32;
+
+struct hash_entry {
+       u32 offset;
+       u32 tag;
+};
+
+
+
+static struct hash_entry hash_table[HASH_SIZE];
+
+static int bucket_flat(u32 offset, u32 limit)
+{
+       u32 b = (offset / (limit/NBUCKETS));
+       return NBUCKETS - b;
+}
+
+static int bucket(u32 offset, u32 limit)
+{
+       u32 rem = limit-offset;
+       u32 m = limit>>NBUCKETS;
+       int i = 0;
+       u32 b = m;
+
+       if (m == 0) return 0;
+
+       while (rem > b) {
+               i++;
+               b += (m<<i);
+       }
+
+       return i;
+}
+
+
+static void print_distribution(u32 limit)
+{
+       u32 counts[NBUCKETS+1];
+       int i;
+       memset(counts, 0, sizeof(counts));
+
+       for (i=0;i<HASH_SIZE;i++) {
+               u32 b = bucket(hash_table[i].offset, limit);
+               counts[b]++;
+       }
+
+       for (i=0;i<NBUCKETS+1;i++) {
+               printf("bucket %3d  %.3f\n", 
+                      i, (100.0 * counts[i]) / HASH_SIZE);
+       }
+}
+
+
+static int replace(u32 old_offset, u32 new_offset)
+{
+       u32 x;
+
+       x = 1.2 * sqrt(new_offset * old_offset) / HASH_SIZE;
+
+       if (x == 0) return 1;
+
+       if (random() % x == 0) return 1;
+       return 0;
+}
+
+static u32 lbs(u32 x)
+{
+       u32 i;
+       for (i=0;i<32;i++) {
+               if (x & (1<<i)) break;
+       }
+       return i;
+}
+
+static int yy_replace(u32 old_offset, u32 new_offset)
+{
+       u32 x = 20;
+
+       if (1.0*lbs(old_offset) < (bucket(old_offset, new_offset)+1)) {
+               return 1;
+       }
+
+       return 0;
+}
+
+static int XX_replace(u32 old_offset, u32 new_offset)
+{
+       double p;
+       p = 0.5 / (((double)new_offset) / (HASH_SIZE * NBUCKETS));
+       return (random() % 1000) < 1000 * p;
+}
+
+static void fill_hash_table(void)
+{
+       int i;
+       for (i=0;i<TOTAL_SIZE/2;i++) {
+               u32 tag = random();
+               u32 t = tag % HASH_SIZE;
+
+               if (hash_table[t].offset == 0 ||
+                   replace(hash_table[t].offset, i)) {
+                       hash_table[t].offset = i;
+                       hash_table[t].tag = tag;
+               }
+       }
+
+       print_distribution(i);
+       printf("\n");
+
+       for (;i<TOTAL_SIZE;i++) {
+               u32 tag = random();
+               u32 t = tag % HASH_SIZE;
+
+               if (hash_table[t].offset == 0 ||
+                   replace(hash_table[t].offset, i)) {
+                       hash_table[t].offset = i;
+                       hash_table[t].tag = tag;
+               }
+       }
+
+       print_distribution(i);
+}
+
+ int main(int argc, const char *argv[])
+{
+       srandom(time(NULL));
+       fill_hash_table();
+       return 0;
+}
diff --git a/hello.c b/hello.c
new file mode 100644 (file)
index 0000000..58f1014
--- /dev/null
+++ b/hello.c
@@ -0,0 +1,8 @@
+#include <stdio.h>
+
+
+int main(int argc, char *argv[])
+{
+       printf("hello world\n");
+       return 0;
+}
diff --git a/histogram.c b/histogram.c
new file mode 100644 (file)
index 0000000..10b222c
--- /dev/null
@@ -0,0 +1,2 @@
+#include <stdio.h>
+
diff --git a/host.c b/host.c
new file mode 100644 (file)
index 0000000..1fa17a5
--- /dev/null
+++ b/host.c
@@ -0,0 +1,9 @@
+#include <stdio.h>
+#include <unistd.h>
+
+main()
+{
+       char buf[1024];
+       gethostname(buf, sizeof(buf)-1);
+       puts(buf);
+}
diff --git a/id_mkdir.c b/id_mkdir.c
new file mode 100644 (file)
index 0000000..d8634af
--- /dev/null
@@ -0,0 +1,38 @@
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+int main(int argc, const char *argv[])
+{
+       const char *name;
+       uid_t id;
+       int fd;
+
+       if (argc < 3) {
+               printf("id_create <name> <uid>\n");
+               exit(1);
+       }
+
+       name = argv[1];
+       id = atoi(argv[2]);
+
+       if (seteuid(id) != 0) {
+               perror("seteuid");
+               return -1;
+       }
+       
+       unlink(name);
+       fd = open(name, O_CREAT|O_EXCL|O_RDWR, 0644);
+       if (fd == -1) {
+               perror("open");
+               return -1;
+       }
+
+       close(fd);
+       return 0;
+}
+
diff --git a/immediate.c b/immediate.c
new file mode 100644 (file)
index 0000000..de62ce5
--- /dev/null
@@ -0,0 +1,14 @@
+#include <stdio.h>
+
+typedef struct {unsigned x;} FOOBAR;
+
+#define X_FOOBAR(x) ((FOOBAR) { x })
+#define FOO_ONE X_FOOBAR(1)
+
+FOOBAR f = FOO_ONE;   
+
+static const struct {
+       FOOBAR y; 
+} f2[] = {
+       {FOO_ONE}
+};   
diff --git a/inotify.c b/inotify.c
new file mode 100644 (file)
index 0000000..8acfb16
--- /dev/null
+++ b/inotify.c
@@ -0,0 +1,89 @@
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <linux/inotify.h>
+#include <errno.h>
+#include <asm/unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <signal.h>
+
+#if 0
+_syscall0(int, inotify_init);
+_syscall3(int, inotify_add_watch, int, fd, const char *, path, __u32, mask);
+_syscall2(int, inotify_rm_watch, int, fd, int, wd);
+
+#else
+
+static int inotify_init(void)
+{
+       return syscall(__NR_inotify_init);
+}
+
+static int inotify_add_watch(int fd, const char *path, __u32 mask)
+{
+       static int xx;
+       return syscall(__NR_inotify_add_watch, fd, path, mask+(++xx));
+}
+
+static int inotify_rm_watch(int fd, int wd)
+{
+       return syscall(__NR_inotify_rm_watch, fd, wd);
+}
+#endif
+
+static void usage(void)
+{
+       printf("usage: inotify <DIRECTORY>\n");
+}
+
+int main(int argc, const char *argv[])
+{
+       const char *path;
+       int fd, wd1, wd2;
+       pid_t child;
+
+       if (argc < 2) {
+               usage();
+               exit(1);
+       }
+
+       path = argv[1];
+
+       if ((child = fork()) == 0) {
+               char *testname;
+               asprintf(&testname, "%s/inotify_test.dat", path);
+               while (1) {
+                       close(open(testname, O_CREAT|O_RDWR, 0666));
+                       unlink(testname);
+                       sleep(1);
+               }
+       }
+
+       fd = inotify_init();
+       wd1 = inotify_add_watch(fd, path, IN_CREATE|IN_DELETE);
+       wd2 = inotify_add_watch(fd, path, IN_CREATE|IN_DELETE);
+
+       while (1) {
+               char buf[1024];
+               int ret = read(fd, buf, sizeof(buf));
+               char *p = buf;
+               struct inotify_event *ev;
+               while (ret > sizeof(ev)) {
+                       ev = (struct inotify_event *)p;
+                       printf("%d 0x%08x %d '%s'\n", 
+                              ev->wd, ev->mask, ev->len, ev->name);
+                       p += sizeof(*ev) + ev->len;
+                       ret -= sizeof(*ev) + ev->len;
+               }
+       }
+
+       inotify_rm_watch(fd, wd1);
+       inotify_rm_watch(fd, wd2);
+       close(fd);
+
+       kill(child, SIGKILL);
+
+       return 0;
+}
diff --git a/int32.c b/int32.c
new file mode 100644 (file)
index 0000000..fb6b1e3
--- /dev/null
+++ b/int32.c
@@ -0,0 +1,13 @@
+#include <stdio.h>
+
+typedef struct {
+       unsigned val:32;
+} uint32;
+
+int main(void)
+{
+       uint32 x;
+       x = 5;
+       printf("sizeof(x)=%d\n", sizeof(x));
+       return 0;
+}
diff --git a/iocache.c b/iocache.c
new file mode 100644 (file)
index 0000000..1af698a
--- /dev/null
+++ b/iocache.c
@@ -0,0 +1,319 @@
+#define _XOPEN_SOURCE 500
+#include <unistd.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <libaio.h>
+#include <getopt.h>
+#include <errno.h>
+
+#include <sys/time.h>
+#include <time.h>
+
+static struct timeval tp1,tp2;
+
+static void start_timer()
+{
+       gettimeofday(&tp1,NULL);
+}
+
+static double end_timer()
+{
+       gettimeofday(&tp2,NULL);
+       return (tp2.tv_sec + (tp2.tv_usec*1.0e-6)) - 
+               (tp1.tv_sec + (tp1.tv_usec*1.0e-6));
+}
+
+
+struct io_block {
+       struct iocb *cb;
+       unsigned char *buffer;
+       int nread;
+       int done;
+       int blk;
+};
+
+struct io_ring {
+       int fd;
+       unsigned block_size;
+       unsigned num_blocks;
+       struct io_block *blocks;
+       io_context_t io_ctx;
+};
+
+static int a_schedule(struct io_ring *ring, int blk)
+{
+       struct io_block *b = &ring->blocks[blk % ring->num_blocks];
+       
+       if (blk == b->blk) return 0;
+
+       if (b->buffer == NULL) {
+               b->buffer = malloc(ring->block_size);
+               if (b->buffer == NULL) goto failed;
+       }
+
+       if (b->cb == NULL) {
+               b->cb = malloc(sizeof(*b->cb));
+               if (b->cb == NULL) goto failed;
+       }
+
+       if (b->blk != -1 && !b->done) {
+               struct io_event res;
+               int ret = io_cancel(ring->io_ctx, b->cb, &res);
+               if (ret == EAGAIN) {
+                       goto failed;
+               }
+       }
+
+       b->blk = blk;
+       b->done = 0;
+       memset(b->cb, 0, sizeof(*b->cb));
+        io_prep_pread(b->cb, ring->fd, b->buffer, ring->block_size,
+                     blk * (off_t)ring->block_size);
+       if (io_submit(ring->io_ctx, 1, &b->cb) != 0) goto failed;
+       b->cb->key = blk;
+       return 0;
+
+failed:
+       free(b->buffer);
+       free(b->cb);
+       b->buffer = NULL;
+       b->cb     = NULL;
+       b->blk    = -1;
+       return -1;
+}
+
+static int a_wait(struct io_ring *ring, int blk)
+{
+       struct io_block *b = &ring->blocks[blk % ring->num_blocks];
+       struct io_event ev[10];
+
+       if (b->blk != blk) return -1;
+
+       while (!b->done) {
+               int i, nr = io_getevents(ring->io_ctx, 1, 10, ev, NULL);
+               for (i=0;i<nr;i++) {
+                       struct iocb *cb = ev[i].obj;
+                       struct io_block *b2 = &ring->blocks[cb->key % ring->num_blocks];
+                       b2->done = 1;
+                       b2->nread = ev[i].res;
+               }
+       }
+
+       if (b->nread < 0) return -1;
+
+       return 0;
+}
+
+
+static ssize_t a_read(struct io_ring *ring, void *buf, size_t count, off_t offset)
+{
+       int blk_start = offset / ring->block_size;
+       int blk_end   = (offset+count-1) / ring->block_size;
+       int blk_sched = blk_start + (ring->num_blocks/2);
+       int i;
+       ssize_t total=0;
+
+       if (blk_sched < blk_end) blk_sched = blk_end;
+
+       for (i=blk_start;i<=blk_sched;i++) {
+               a_schedule(ring, i);
+       }
+
+       while (count) {
+               int n = count;
+               int blk_offset = offset % ring->block_size;
+               int blk = offset / ring->block_size;
+               struct io_block *b = &ring->blocks[blk % ring->num_blocks];
+               ssize_t nread;
+               if (n > ring->block_size - blk_offset) {
+                       n = ring->block_size - blk_offset;
+               }
+               if (a_wait(ring, blk) != 0) {
+                       nread = pread(ring->fd, buf, n, offset);
+                       printf("sync fallback\n");
+               } else {
+                       if (n > b->nread) n = b->nread;
+                       memcpy(buf, b->buffer + blk_offset, n);
+                       nread = n;
+               }
+               if (nread <= 0) {
+                       if (total > 0) return total;
+                       return nread;
+               }
+               total += nread;
+               buf = (void *)(nread + (char *)buf);
+               count -= nread;
+               offset += nread;
+       }
+
+       return total;
+}
+
+static struct io_ring *a_init(int fd, int block_size, int nblocks)
+{
+       struct io_ring *ring;
+       int i;
+       int ret;
+
+       ring = malloc(sizeof(*ring));
+       if (ring == NULL) return NULL;
+
+       memset(ring, 0, sizeof(*ring));
+       ret = io_queue_init(nblocks, &ring->io_ctx);
+       if (ret != 0) {
+               free(ring);
+               return NULL;
+       }
+       ring->fd = fd;
+       ring->num_blocks = nblocks;
+       ring->block_size = block_size;
+       ring->blocks = calloc(nblocks, sizeof(ring->blocks[0]));
+       if (ring->blocks == NULL) {
+               free(ring);
+               return NULL;
+       }
+
+       for (i=0;i<nblocks;i++) {
+               ring->blocks[i].blk = -1;
+       }
+       return ring;
+}
+
+static void a_close(struct io_ring *ring)
+{
+       int i;
+
+       for (i=0;i<ring->num_blocks;i++) {
+               struct io_block *b = &ring->blocks[i];
+               if (b->blk != -1 && !b->done) {
+                       struct io_event res;
+                       int ret = io_cancel(ring->io_ctx, b->cb, &res);
+                       if (ret == EAGAIN) {
+                               a_wait(ring, b->blk);
+                       }
+               }
+               if (b->cb) free(b->cb);
+               if (b->buffer) free(b->buffer);
+       }
+       free(ring);
+}
+
+static void async_test(const char *fname, int block_size, int nblocks, int rsize)
+{
+       int fd;
+       struct io_ring *ring;
+       off_t offset=0;
+       char buf[rsize];
+       int i=0;
+
+       offset=0;
+       start_timer();
+       fd = open(fname, O_RDONLY);
+       if (fd == -1) {
+               perror(fname);
+               exit(1);
+       }
+
+       ring = a_init(fd, block_size, nblocks);
+       if (ring == NULL) {
+               fprintf(stderr, "a_init faild\n");
+               exit(1);
+       }
+
+       while (a_read(ring, buf, sizeof(buf), offset) > 0) {
+               offset += sizeof(buf);
+               if (++i % 5 == 0) {
+                       offset -= 2*sizeof(buf);
+               }
+       }
+
+       printf("async: %.3f MByte/sec\n", 1.0e-6 * offset / end_timer());
+       a_close(ring);
+}
+
+static void sync_test(const char *fname, int rsize)
+{
+       int fd;
+       off_t offset=0;
+       char buf[rsize];
+       int i=0;
+
+       offset=0;
+       start_timer();
+       fd = open(fname, O_RDONLY);
+       if (fd == -1) {
+               perror(fname);
+               exit(1);
+       }
+
+       while (pread(fd, buf, sizeof(buf), offset) > 0) {
+               offset += sizeof(buf);
+               if (++i % 5 == 0) {
+                       offset -= 2*sizeof(buf);
+               }
+       }
+
+       printf("sync : %.3f MByte/sec\n", 1.0e-6 * offset / end_timer());
+       close(fd);
+}
+
+static void usage(void)
+{
+       printf("Usage: aiocache [options] <filename>\n");
+       printf(
+"Options:\n" \
+" -b block_size\n" \
+" -n nblocks\n" \
+" -r readsize\n");
+}
+
+int main(int argc, const char *argv[])
+{
+       const char *fname;
+       int opt;
+       int block_size = 1024*1024;
+       int nblocks = 100;
+       int rsize = 32*1024;
+
+       while ((opt = getopt(argc, argv, "b:n:r:")) != -1) {
+               switch (opt) {
+               case 'b':
+                       block_size = strtol(optarg, NULL, 0);
+                       break;
+               case 'n':
+                       nblocks = strtol(optarg, NULL, 0);
+                       break;
+               case 'r':
+                       rsize = strtol(optarg, NULL, 0);
+                       break;
+               default:
+                       printf("Invalid option '%c'\n", opt);
+                       usage();
+                       exit(1);
+               }
+       }
+
+       argv += optind;
+       argc -= optind;
+
+       if (argc < 1) {
+               usage();
+               exit(1);
+       }
+
+       fname = argv[0];
+
+       printf("Testing with block_size=%d nblocks=%d rsize=%d\n",
+              block_size,nblocks,rsize);
+       
+       async_test(fname, block_size, nblocks, rsize);
+       sync_test(fname, rsize);
+       async_test(fname, block_size, nblocks, rsize);
+       sync_test(fname, rsize);
+
+       return 0;       
+}
diff --git a/iotest.c b/iotest.c
new file mode 100644 (file)
index 0000000..c86c015
--- /dev/null
+++ b/iotest.c
@@ -0,0 +1,17 @@
+#include <stdio.h>
+#include <unistd.h>
+
+main()
+{
+       int pid;
+
+       if ((pid=fork()) != 0) {
+               int i = 0;
+               while (1) {
+                       printf("%d\n", i++);
+                       fflush(stdout);
+                       sleep(1);
+               }
+       }
+       sleep(1000);
+}
diff --git a/issamba.c b/issamba.c
new file mode 100644 (file)
index 0000000..ff4bd62
--- /dev/null
+++ b/issamba.c
@@ -0,0 +1,71 @@
+#include <netinet/in.h>
+#include <netdb.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <string.h>
+#include <errno.h>
+
+
+static int open_socket_out(char *host, int port)
+{
+       struct sockaddr_in sock_out;
+       int res;
+       struct hostent *hp;
+
+       res = socket(PF_INET, SOCK_STREAM, 0);
+
+       hp = gethostbyname(host);
+       if (!hp) {
+               fprintf(stderr,"unknown host: %s\n", host);
+               exit(1);
+       }
+
+       memcpy(&sock_out.sin_addr, hp->h_addr, hp->h_length);
+       sock_out.sin_port = htons(port);
+       sock_out.sin_family = PF_INET;
+
+       if (connect(res,(struct sockaddr *)&sock_out,sizeof(sock_out))) {
+               fprintf(stderr, "failed to connect to %s - %s\n", host, strerror(errno));
+               exit(1);
+       }
+
+       return res;
+}
+
+int main(int argc, char *argv[])
+{
+       int port = 139;
+       char *host;
+       unsigned char buf[4];
+       int sock;
+
+       if (argc < 2) {
+               printf("usage: issamba <host>\n");
+               exit(1);
+       }
+
+       host = argv[1];
+       bzero(buf, 4);
+       buf[0] = 0x89;
+
+       sock = open_socket_out(host, port);
+
+       if (write(sock, buf, 4) != 4) {
+               fprintf(stderr,"Failed to send request\n");
+               exit(1);
+       }
+
+       bzero(buf, 4);
+       if (read(sock, buf, 4) != 4) {
+               fprintf(stderr,"Failed to recv request\n");
+               exit(1);
+       }
+
+       if (buf[0] == 0x85) {
+               printf("%s is a Samba server\n", host);
+       } else {
+               printf("%s is not a Samba server (0x%02x)\n", host, buf[0]);
+       }
+       return 0;
+}
diff --git a/ival.c b/ival.c
new file mode 100644 (file)
index 0000000..21954c6
--- /dev/null
+++ b/ival.c
@@ -0,0 +1,31 @@
+#include <stdio.h>
+#include <string.h>
+
+typedef unsigned short uint16;
+typedef unsigned int uint32;
+
+#ifndef X86
+#define PVAL(buf,pos) ((((const unsigned char *)(buf))[pos]))
+#define SVAL(buf,pos) ((PVAL(buf,pos)|PVAL(buf,(pos)+1)<<8))
+#define IVAL(buf,pos) ((SVAL(buf,pos)|SVAL(buf,(pos)+2)<<16))
+#else
+#define SVAL(buf,pos) (*(const uint16 *)((const char *)(buf) + (pos)))
+#define IVAL(buf,pos) (*(const uint32 *)((const char *)(buf) + (pos)))
+#endif
+
+#define SMB_BIG_UINT unsigned long long
+
+static char buf[4];
+
+int main(void)
+{
+       SMB_BIG_UINT x;
+       
+       memset(buf, 0xFF, 4);
+       buf[3] = 0x7F;
+
+       x = IVAL(buf, 0);
+
+       printf("%.0f\n", (double)x);
+       return 0;
+}
diff --git a/j.c b/j.c
new file mode 100644 (file)
index 0000000..1634246
--- /dev/null
+++ b/j.c
@@ -0,0 +1,24 @@
+       #include <sys/socket.h>
+       #include <netinet/in.h>
+       #include <arpa/inet.h>
+
+
+ int inet_xton(const char *cp, struct in_addr *inp)
+{
+       if (strcmp(cp, "255.255.255.255") == 0) {
+               inp->s_addr = (unsigned) -1;
+               return 1;
+       }
+
+       inp->s_addr = inet_addr(cp);
+       if (inp->s_addr == (unsigned) -1) {
+               return 0;
+       }
+       return 1;
+}
+
+main(int argc, char *argv[])
+{
+       struct in_addr ip;
+       printf("%d\n", inet_xton(argv[1], &ip));
+}
diff --git a/jj.c b/jj.c
new file mode 100644 (file)
index 0000000..88a5549
--- /dev/null
+++ b/jj.c
@@ -0,0 +1,16 @@
+main()
+{
+  unsigned end = 0xf0001000;
+  unsigned start = 0xf0000000;
+  unsigned limit = 128*1024;
+  int size;
+
+  size = start - end;
+  
+  printf("%d %x\n",size,size);
+
+  if (size > limit)
+    printf("yes\n");
+  else
+    printf("no\n");    
+}
diff --git a/k.c b/k.c
new file mode 100644 (file)
index 0000000..cd30831
--- /dev/null
+++ b/k.c
@@ -0,0 +1,21 @@
+main()
+{
+
+       fd_set fds;
+
+       
+       while (!read_all_data) {
+               tval.tv_sec = 1;
+               tval.tv_usec = 0;
+               FD_ZERO(&fds);
+               FS_SET(&fds, s);
+               ret = select(s+1, &fds, NULL, NULL, &tval);
+               if (ret > 0) {
+                       if (FD_ISSET(&fds, s)) {
+                               read(s, lksnfglkm);
+                       }
+               }
+               do_background_processing();
+       }
+
+}
diff --git a/k5.c b/k5.c
new file mode 100644 (file)
index 0000000..0377b48
--- /dev/null
+++ b/k5.c
@@ -0,0 +1,8 @@
+#include <stdio.h>
+
+main(int argc, char *argv[])
+{
+       int x1=0;
+       sscanf(argv[1], "%x", &x1);
+       printf("%d\n", x1);
+}
diff --git a/kbd.c b/kbd.c
new file mode 100644 (file)
index 0000000..bcf895e
--- /dev/null
+++ b/kbd.c
@@ -0,0 +1,21 @@
+#include <stdio.h>
+#include <fcntl.h>
+#include <sys/kd.h>
+
+main()
+{
+int kbd = open("/dev/console", O_RDWR | O_NDELAY);
+char buf[60];
+int n,i;
+
+ioctl(kbd, KDSKBMODE, K_RAW);
+
+while (1)
+  {
+    n = read(kbd,buf,60);
+    if (n==0) continue;
+    for (i=0;i<n;i++)
+      printf("%x\n",buf[i]);
+  }
+
+}
diff --git a/largefile.c b/largefile.c
new file mode 100644 (file)
index 0000000..4178a5d
--- /dev/null
@@ -0,0 +1,5 @@
+#include <errno.h>
+#include <asm/unistd.h>
+#include <asm/stat.h>
+
+_syscall2(long, stat64, char *, fname, struct stat64 *, st)
diff --git a/largefiletest.c b/largefiletest.c
new file mode 100644 (file)
index 0000000..1ee8b03
--- /dev/null
@@ -0,0 +1,34 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+/*
+  try to determine if the filesystem supports large files
+*/
+static int large_file_support(const char *path)
+{
+       int fd;
+       ssize_t ret;
+       char c;
+
+       fd = open(path, O_RDWR|O_CREAT, 0600);
+       unlink(path);
+       if (fd == -1) {
+               /* have to assume large files are OK */
+               return 1;
+       }
+       ret = pread(fd, &c, 1, ((unsigned long long)1)<<32);
+       close(fd);
+       return ret == 0;
+}
+
+int main(int argc, const char *argv[])
+{
+       int s = large_file_support(argv[1]);
+       printf("sizeof(off_t)=%d s=%d\n", sizeof(off_t), s);
+       return 0;
+}
diff --git a/linklist.c b/linklist.c
new file mode 100644 (file)
index 0000000..6cacbda
--- /dev/null
@@ -0,0 +1,6 @@
+#define DLIST_ADD(list, p) dlist_add((void *)list, (void *)p, \
+                                     (void **)&list->next, \ 
+                                    (void **)&list->prev, \
+                                     (void **)&p->next, \
+                                    (void **)&p->prev)
+
diff --git a/list_compare.c b/list_compare.c
new file mode 100644 (file)
index 0000000..e62745b
--- /dev/null
@@ -0,0 +1,111 @@
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <stdlib.h>
+#include <sched.h>
+#include <unistd.h>
+#include <time.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <string.h>
+#include <sys/wait.h>
+#include <sys/socket.h>
+#include <sys/time.h>
+
+static struct timeval tp1,tp2;
+
+static void start_timer()
+{
+       gettimeofday(&tp1,NULL);
+}
+
+static double end_timer()
+{
+       gettimeofday(&tp2,NULL);
+       return (tp2.tv_sec + (tp2.tv_usec*1.0e-6)) - 
+               (tp1.tv_sec + (tp1.tv_usec*1.0e-6));
+}
+
+
+
+/* hook into the front of the list */
+#define DLIST_ADD(list, p) \
+do { \
+        if (!(list)) { \
+               (list) = (p); \
+               (p)->next = (p)->prev = NULL; \
+       } else { \
+               (list)->prev = (p); \
+               (p)->next = (list); \
+               (p)->prev = NULL; \
+               (list) = (p); \
+       }\
+} while (0)
+
+/* remove an element from a list - element doesn't have to be in list. */
+#define DLIST_REMOVE(list, p) \
+do { \
+       if ((p) == (list)) { \
+               (list) = (p)->next; \
+               if (list) (list)->prev = NULL; \
+       } else { \
+               if ((p)->prev) (p)->prev->next = (p)->next; \
+               if ((p)->next) (p)->next->prev = (p)->prev; \
+       } \
+       if ((p) != (list)) (p)->next = (p)->prev = NULL; \
+} while (0)
+
+/* promote an element to the top of the list */
+#define DLIST_PROMOTE(list, p) \
+do { \
+          DLIST_REMOVE(list, p); \
+          DLIST_ADD(list, p); \
+} while (0)
+
+
+struct foo {
+       struct foo *next, *prev;
+       const char *name;
+};
+
+static struct foo *list;
+
+static struct foo *findit(const char *name)
+{
+       struct foo *f;
+       for (f=list;f;f=f->next) {
+               if (strcmp(name, f->name) == 0) {
+//                     if (f != list) DLIST_PROMOTE(list, f);
+                       return f;
+               }
+       }
+       return NULL;
+}
+
+int main(void)
+{
+       int i;
+       
+       for (i=0;i<20;i++) {
+               struct foo *f;
+               f = malloc(sizeof(*f));
+               asprintf(&f->name, "%u%s", i, "foobar");
+               DLIST_ADD(list, f);
+       }
+
+
+       for (i=0;i<20;i++) {
+               char *name;
+               int j;
+               asprintf(&name, "%u%s", i, "foobar");
+               start_timer();
+               for (j=0;j<100000;j++) {
+                       findit(name);
+               }
+               printf("%s: %.2f usec/op\n", name, 1.0e6*end_timer()/j);
+               free(name);
+       }
+
+       return 0;
+}
diff --git a/lock64.c b/lock64.c
new file mode 100644 (file)
index 0000000..7aa9458
--- /dev/null
+++ b/lock64.c
@@ -0,0 +1,44 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <string.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+
+
+#define fcntl fcntl64
+#undef F_SETLKW 
+#undef F_SETLK
+#define F_SETLK 13
+#define F_SETLKW 14
+
+
+int fcntl64(int fd, int cmd, struct flock * lock)
+{
+       return syscall(221, fd, cmd, lock);
+}
+
+int main(int argc, char *argv[])
+{
+       struct flock fl;
+       int fd, ret;
+
+       printf("sizeof(fl.l_start)=%d\n", sizeof(fl.l_start));
+       fd = open("lock64.dat", O_RDWR|O_CREAT);
+
+       fl.l_type = F_RDLCK;
+       fl.l_whence = SEEK_SET;
+       fl.l_start = 16;
+       fl.l_len = 1;
+       fl.l_pid = 0;
+
+       ret = fcntl(fd, F_SETLKW, &fl);
+
+       printf("ret=%d pid=%d\n", ret, getpid());
+
+       sleep(30);
+       return 0;
+}
diff --git a/locker2.c b/locker2.c
new file mode 100644 (file)
index 0000000..5170d38
--- /dev/null
+++ b/locker2.c
@@ -0,0 +1,99 @@
+/*
+  a simple demonsration of a scaling problem in byte range locks on solaris
+
+  try this as 'locker 800 1' and compare to 'locker 800 0'
+
+  tridge@samba.org, July 2002
+*/
+
+#include <stdio.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <string.h>
+#include <signal.h>
+#include <sys/wait.h>
+
+static int brlock(int fd, off_t offset, int rw_type, int lck_type)
+{
+       struct flock fl;
+       int ret;
+
+       fl.l_type = rw_type;
+       fl.l_whence = SEEK_SET;
+       fl.l_start = offset;
+       fl.l_len = 1;
+       fl.l_pid = 0;
+
+       ret = fcntl(fd,lck_type,&fl);
+
+       if (ret == -1) {
+               printf("brlock failed offset=%d rw_type=%d lck_type=%d : %s\n", 
+                      (int)offset, rw_type, lck_type, strerror(errno));
+       }
+       return 0;
+}
+
+static void slave(int fd, int nlocks)
+{
+       int i;
+       for (i=1; i<nlocks+1; i++) {
+               brlock(fd, i, F_RDLCK, F_SETLKW);
+               usleep(1); /* essentially just a yield() */
+       }
+
+       /* this last lock will block until the master unlocks it */
+       brlock(fd, 0, F_WRLCK, F_SETLKW);
+       brlock(fd, 0, F_UNLCK, F_SETLKW);
+}
+
+int main(int argc, char *argv[])
+{
+       int i, fd;
+       int nproc, nlocks;
+       const char *fname = "locker.dat";
+       char c;
+
+       if (argc < 3) {
+               printf("Usage: locker <nproc> <nlocks>\n");
+               exit(1);
+       }
+
+       nproc = atoi(argv[1]);
+       nlocks = atoi(argv[2]);
+       setpgrp();
+
+       fd = open(fname, O_RDWR | O_CREAT | O_TRUNC, 0600);
+       if (fd == -1) {
+               perror(fname);
+               exit(1);
+       }
+
+       /* set the master lock - this stops the children exiting */
+       brlock(fd, 0, F_WRLCK, F_SETLKW);
+
+       for (i=0;i<nproc;i++) {
+               switch (fork()) {
+               case 0:
+                       slave(fd, nlocks);
+                       exit(0);
+               case -1:
+                       perror("fork");
+                       kill(0, SIGTERM);
+                       exit(1);
+               }
+       }
+
+       printf("hit enter to continue\n");
+       read(0, &c, 1);
+
+       printf("unlocking ....\n");
+       brlock(fd, 0, F_UNLCK, F_SETLKW);
+
+       while (waitpid(0, NULL, 0) > 0 || errno != ECHILD) /* noop */ ;
+
+       printf("done\n");
+
+       return 0;
+}
diff --git a/lockrun.c b/lockrun.c
new file mode 100644 (file)
index 0000000..3c3fd36
--- /dev/null
+++ b/lockrun.c
@@ -0,0 +1,45 @@
+#include <unistd.h>
+#include <fcntl.h>
+
+/* lock a byte range in a open file */
+int lock_range(int fd, int offset, int len)
+{
+       struct flock lock;
+
+       lock.l_type = F_WRLCK;
+       lock.l_whence = SEEK_SET;
+       lock.l_start = offset;
+       lock.l_len = len;
+       lock.l_pid = 0;
+       
+       return fcntl(fd,F_SETLK,&lock) == 0;
+}
+
+int main(int argc, char *argv[])
+{
+       char *cmd, *lockf;
+       int fd;
+       struct flock lock;
+
+       if (argc < 3) {
+               printf("lockrun <lockfile> <cmd>\n");
+               exit(1);
+       }
+
+       lockf = argv[1];
+       cmd = argv[2];
+
+       fd = open(lockf, O_CREAT|O_TRUNC|O_RDWR, 0600);
+       if (fd == -1) exit(1);
+
+       lock.l_type = F_WRLCK;
+       lock.l_whence = SEEK_SET;
+       lock.l_start = 0;
+       lock.l_len = 1;
+       lock.l_pid = 0;
+
+       if (fcntl(fd,F_SETLKW,&lock) == 0) {
+               system(cmd);
+       }
+       return 0;
+}
diff --git a/locktst.c b/locktst.c
new file mode 100644 (file)
index 0000000..0781b7b
--- /dev/null
+++ b/locktst.c
@@ -0,0 +1,132 @@
+/* simple program to test file locking */
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/fcntl.h>
+#include <errno.h>
+
+#define True (1==1)
+#define False (!True)
+
+#define BOOL int
+#define DEBUG(x,body) (printf body)
+#define HAVE_FCNTL_LOCK 1
+
+#ifndef strerror
+#define strerror(i) sys_errlist[i]
+#endif
+
+/****************************************************************************
+simple routines to do connection counting
+****************************************************************************/
+BOOL fcntl_lock(int fd,int op,int offset,int count,int type)
+{
+#if HAVE_FCNTL_LOCK
+  struct flock lock;
+  int ret;
+  unsigned long mask = (1<<31);
+
+  /* interpret negative counts as large numbers */
+  if (count < 0)
+    count &= ~mask;
+
+  /* no negative offsets */
+  offset &= ~mask;
+
+  /* count + offset must be in range */
+  while ((offset < 0 || (offset + count < 0)) && mask)
+    {
+      offset &= ~mask;
+      mask = mask >> 1;
+    }
+
+  DEBUG(5,("fcntl_lock %d %d %d %d %d\n",fd,op,offset,count,type));
+
+  lock.l_type = type;
+  lock.l_whence = SEEK_SET;
+  lock.l_start = offset;
+  lock.l_len = count;
+  lock.l_pid = 0;
+
+  errno = 0;
+
+  ret = fcntl(fd,op,&lock);
+
+  if (errno != 0)
+    DEBUG(3,("fcntl lock gave errno %d (%s)\n",errno,strerror(errno)));
+
+  /* a lock query */
+  if (op == F_GETLK)
+    {
+      if ((ret != -1) &&
+         (lock.l_type != F_UNLCK) && 
+         (lock.l_pid != 0) && 
+         (lock.l_pid != getpid()))
+       {
+         DEBUG(3,("fd %d is locked by pid %d\n",fd,lock.l_pid));
+         return(True);
+       }
+
+      /* it must be not locked or locked by me */
+      return(False);
+    }
+
+  /* a lock set or unset */
+  if (ret == -1)
+    {
+      DEBUG(3,("lock failed at offset %d count %d op %d type %d (%s)\n",
+              offset,count,op,type,strerror(errno)));
+
+      /* perhaps it doesn't support this sort of locking?? */
+      if (errno == EINVAL)
+       {
+         DEBUG(3,("locking not supported? returning True\n"));
+         return(True);
+       }
+
+      return(False);
+    }
+
+  /* everything went OK */
+  return(True);
+#else
+  return(False);
+#endif
+}
+
+void lock_test(char *fname)
+{
+  int fd = open(fname,O_RDWR | O_CREAT,0644);
+  if (fd < 0) return;
+
+  write(fd, "hello", 5);
+
+  fcntl_lock(fd,F_SETLK,1,1,F_WRLCK);
+
+  sleep(60);
+
+  fcntl_lock(fd,F_GETLK,0,0,F_WRLCK);
+  fcntl_lock(fd,F_SETLK,0,0,F_UNLCK);
+
+  fcntl_lock(fd,F_SETLK,0,20,F_WRLCK);
+  fcntl_lock(fd,F_GETLK,0,20,F_WRLCK);
+  fcntl_lock(fd,F_SETLK,0,20,F_UNLCK);
+
+  fcntl_lock(fd,F_SETLK,0x7FFFFF80,20,F_WRLCK);
+  fcntl_lock(fd,F_GETLK,0x7FFFFF80,20,F_WRLCK);
+  fcntl_lock(fd,F_SETLK,0x7FFFFF80,20,F_UNLCK);
+
+  fcntl_lock(fd,F_SETLK,0xFFFFFF80,20,F_WRLCK);
+  fcntl_lock(fd,F_GETLK,0xFFFFFF80,20,F_WRLCK);
+  fcntl_lock(fd,F_SETLK,0xFFFFFF80,20,F_UNLCK);
+
+  fcntl_lock(fd,F_SETLK,0xFFFFFFFF,20,F_WRLCK);
+  fcntl_lock(fd,F_GETLK,0xFFFFFFFF,20,F_WRLCK);
+  fcntl_lock(fd,F_SETLK,0xFFFFFFFF,20,F_UNLCK);
+
+}
+
+
+main(int argc,char *argv[])
+{
+       lock_test(argv[1]);
+}
diff --git a/log.c b/log.c
new file mode 100644 (file)
index 0000000..89efd91
--- /dev/null
+++ b/log.c
@@ -0,0 +1,7 @@
+main()
+{
+       char buf[1024];
+       sprintf(buf, "Rhosts authentication refused for %.100: no home directory %.200s", "hello", "goodbye");
+
+       printf(buf);
+}
diff --git a/logit.c b/logit.c
new file mode 100644 (file)
index 0000000..89c5932
--- /dev/null
+++ b/logit.c
@@ -0,0 +1,10 @@
+static void logit(const char *format, ...)
+{
+       va_list ap;
+       FILE *f = fopen("/tmp/mylog", "a");
+       if (!f) return;
+       va_start(ap, format);
+       vfprintf(f, format, ap);
+       va_end(ap);
+       fclose(f);
+}
diff --git a/lowcase.c b/lowcase.c
new file mode 100644 (file)
index 0000000..e0bfcd9
--- /dev/null
+++ b/lowcase.c
@@ -0,0 +1,72 @@
+/* 
+   Unix SMB/Netbios implementation.
+   Version 3.0
+   Copyright (C) Andrew Tridgell 2001
+   
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2 of the License, or
+   (at your option) any later version.
+   
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+   
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <fcntl.h>
+#include <errno.h>
+
+/* 
+   given an upcase table file, create a lowcase table file
+*/
+
+typedef unsigned short smb_ucs2_t;
+
+static smb_ucs2_t upcase[0x10000], lowcase[0x10000];
+
+int main(void)
+{
+       int fd, i;
+       char *fname = "upcase.dat";
+
+       fd = open(fname, O_RDONLY, 0);
+       if (fd == -1) {
+               perror(fname);
+               exit(1);
+       }
+
+       if (read(fd, upcase, 0x20000) != 0x20000) {
+               printf("%s is too short\n", fname);
+               exit(1);
+       }
+
+       for (i=0;i<0x10000;i++) {
+               if (upcase[i] != i) lowcase[upcase[i]] = i;
+       }
+
+       for (i=0;i<0x10000;i++) {
+               if (lowcase[i] == 0) lowcase[i] = i;
+       }
+
+       close(fd);
+
+       fname = "lowcase.dat";
+
+       fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0644);
+       if (fd == -1) {
+               perror(fname);
+               exit(1);
+       }
+
+       write(fd, lowcase, 0x20000);
+       close(fd);
+       return 0;
+}
diff --git a/lseek.c b/lseek.c
new file mode 100644 (file)
index 0000000..d541e41
--- /dev/null
+++ b/lseek.c
@@ -0,0 +1,23 @@
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <linux/inotify.h>
+#include <errno.h>
+#include <asm/unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <signal.h>
+
+int main(void)
+{
+       int fd = open("xx.dat", O_CREAT, 0666);
+
+       if (fork() == 0) {
+               lseek(fd, 10, SEEK_SET);
+               _exit(0);
+       }
+
+       printf("ofs %d\n", lseek(fd, 0, SEEK_CUR));
+       return 0;
+}
diff --git a/m2.c b/m2.c
new file mode 100644 (file)
index 0000000..387bd5e
--- /dev/null
+++ b/m2.c
@@ -0,0 +1,13 @@
+int foo(int y)
+{
+       int x[3];
+
+       printf("x2=%d\n", x[2]);
+
+       x[3] = 0;
+}
+
+main()
+{
+       foo(0);
+}
diff --git a/malloc.c b/malloc.c
new file mode 100644 (file)
index 0000000..0026ee9
--- /dev/null
+++ b/malloc.c
@@ -0,0 +1,6 @@
+main()
+{
+int s=1024*1024;
+char *p;
+while ((p=(char *)malloc(s))) bzero(p,s);
+}
diff --git a/map.c b/map.c
new file mode 100644 (file)
index 0000000..5ae05cf
--- /dev/null
+++ b/map.c
@@ -0,0 +1,10 @@
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/mman.h>
+
+main()
+{
+       int fd = open("/dev/null", O_WRONLY);
+
+       mmap(0, 0x1000, PROT_READ, MAP_PRIVATE, fd, 0);
+}
diff --git a/map_table.c b/map_table.c
new file mode 100644 (file)
index 0000000..e36e341
--- /dev/null
@@ -0,0 +1,31 @@
+#include <stdio.h>
+
+#define smb_ucs2_t unsigned short
+#define UNI_UPPER    0x1
+#define UNI_LOWER    0x2
+#define UNI_DIGIT    0x4
+#define UNI_XDIGIT   0x8
+#define UNI_SPACE    0x10
+
+typedef struct {
+       smb_ucs2_t lower;
+       smb_ucs2_t upper;
+       unsigned char flags;
+} smb_unicode_table_t;
+
+smb_unicode_table_t map_table[] = {
+#include "unicode_map_table.h"
+};
+
+
+main()
+{
+       int i, count=0;
+       for (i=0;i<65536;i++) {
+               if (map_table[i].lower != map_table[i].upper) {
+                       printf("0x%04x %d\n", i, map_table[i].upper - map_table[i].lower);
+                       count++;
+               }
+       }
+       printf("count=%d\n", count);
+}
diff --git a/maxinitgroups.c b/maxinitgroups.c
new file mode 100644 (file)
index 0000000..6329fbc
--- /dev/null
@@ -0,0 +1,343 @@
+/* 
+   Unix SMB/CIFS implementation.
+   max initgroups nss tester
+   Copyright (C) Andrew Tridgell 2003
+   
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2 of the License, or
+   (at your option) any later version.
+   
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+   
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <nss.h>
+#include <dlfcn.h>
+#include <pwd.h>
+#include <grp.h>
+#include <string.h>
+#include <sys/types.h>
+
+typedef enum nss_status NSS_STATUS;
+
+static const char *so_path = "/lib/libnss_winbind.so";
+static char *nss_name;
+static int nss_errno;
+static NSS_STATUS last_error;
+static int total_errors;
+static int max_groups;
+
+static void *find_fn(const char *name)
+{
+       char s[1024];
+       static void *h;
+       void *res;
+
+       snprintf(s,sizeof(s), "_nss_%s_%s", nss_name, name);
+
+       if (!h) {
+               h = dlopen(so_path, RTLD_LAZY);
+       }
+       if (!h) {
+               printf("Can't open shared library %s : %s\n", so_path, dlerror());
+               exit(1);
+       }
+       res = dlsym(h, s);
+       if (!res) {
+               printf("Can't find function %s : %s\n", s, dlerror());
+               return NULL;
+       }
+       return res;
+}
+
+static void report_nss_error(const char *who, NSS_STATUS status)
+{
+       last_error = status;
+       total_errors++;
+       printf("ERROR %s: NSS_STATUS=%d  %d (nss_errno=%d)\n", 
+              who, status, NSS_STATUS_SUCCESS, nss_errno);
+}
+
+static struct passwd *nss_getpwent(void)
+{
+       NSS_STATUS (*_nss_getpwent_r)(struct passwd *, char *, 
+                                     size_t , int *) = find_fn("getpwent_r");
+       static struct passwd pwd;
+       static char buf[1000];
+       NSS_STATUS status;
+
+       status = _nss_getpwent_r(&pwd, buf, sizeof(buf), &nss_errno);
+       if (status == NSS_STATUS_NOTFOUND) {
+               return NULL;
+       }
+       if (status != NSS_STATUS_SUCCESS) {
+               report_nss_error("getpwent", status);
+               return NULL;
+       }
+       return &pwd;
+}
+
+static struct passwd *nss_getpwnam(const char *name)
+{
+       NSS_STATUS (*_nss_getpwnam_r)(const char *, struct passwd *, char *, 
+                                     size_t , int *) = find_fn("getpwnam_r");
+       static struct passwd pwd;
+       static char buf[1000];
+       NSS_STATUS status;
+       
+       status = _nss_getpwnam_r(name, &pwd, buf, sizeof(buf), &nss_errno);
+       if (status == NSS_STATUS_NOTFOUND) {
+               return NULL;
+       }
+       if (status != NSS_STATUS_SUCCESS) {
+               report_nss_error("getpwnam", status);
+               return NULL;
+       }
+       return &pwd;
+}
+
+static struct passwd *nss_getpwuid(uid_t uid)
+{
+       NSS_STATUS (*_nss_getpwuid_r)(uid_t , struct passwd *, char *, 
+                                     size_t , int *) = find_fn("getpwuid_r");
+       static struct passwd pwd;
+       static char buf[1000];
+       NSS_STATUS status;
+       
+       status = _nss_getpwuid_r(uid, &pwd, buf, sizeof(buf), &nss_errno);
+       if (status == NSS_STATUS_NOTFOUND) {
+               return NULL;
+       }
+       if (status != NSS_STATUS_SUCCESS) {
+               report_nss_error("getpwuid", status);
+               return NULL;
+       }
+       return &pwd;
+}
+
+static void nss_setpwent(void)
+{
+       NSS_STATUS (*_nss_setpwent)(void) = find_fn("setpwent");
+       NSS_STATUS status;
+       status = _nss_setpwent();
+       if (status != NSS_STATUS_SUCCESS) {
+               report_nss_error("setpwent", status);
+       }
+}
+
+static void nss_endpwent(void)
+{
+       NSS_STATUS (*_nss_endpwent)(void) = find_fn("endpwent");
+       NSS_STATUS status;
+       status = _nss_endpwent();
+       if (status != NSS_STATUS_SUCCESS) {
+               report_nss_error("endpwent", status);
+       }
+}
+
+
+static struct group *nss_getgrent(void)
+{
+       NSS_STATUS (*_nss_getgrent_r)(struct group *, char *, 
+                                     size_t , int *) = find_fn("getgrent_r");
+       static struct group grp;
+       static char *buf;
+       static int buflen = 1024;
+       NSS_STATUS status;
+
+       if (!buf) buf = malloc(buflen);
+
+again: 
+       status = _nss_getgrent_r(&grp, buf, buflen, &nss_errno);
+       if (status == NSS_STATUS_TRYAGAIN) {
+               buflen *= 2;
+               buf = realloc(buf, buflen);
+               goto again;
+       }
+       if (status == NSS_STATUS_NOTFOUND) {
+               return NULL;
+       }
+       if (status != NSS_STATUS_SUCCESS) {
+               report_nss_error("getgrent", status);
+               return NULL;
+       }
+       return &grp;
+}
+
+static struct group *nss_getgrnam(const char *name)
+{
+       NSS_STATUS (*_nss_getgrnam_r)(const char *, struct group *, char *, 
+                                     size_t , int *) = find_fn("getgrnam_r");
+       static struct group grp;
+       static char *buf;
+       static int buflen = 1000;
+       NSS_STATUS status;
+
+       if (!buf) buf = malloc(buflen);
+again: 
+       status = _nss_getgrnam_r(name, &grp, buf, buflen, &nss_errno);
+       if (status == NSS_STATUS_TRYAGAIN) {
+               buflen *= 2;
+               buf = realloc(buf, buflen);
+               goto again;
+       }
+       if (status == NSS_STATUS_NOTFOUND) {
+               return NULL;
+       }
+       if (status != NSS_STATUS_SUCCESS) {
+               report_nss_error("getgrnam", status);
+               return NULL;
+       }
+       return &grp;
+}
+
+static struct group *nss_getgrgid(gid_t gid)
+{
+       NSS_STATUS (*_nss_getgrgid_r)(gid_t , struct group *, char *, 
+                                     size_t , int *) = find_fn("getgrgid_r");
+       static struct group grp;
+       static char *buf;
+       static int buflen = 1000;
+       NSS_STATUS status;
+       
+       if (!buf) buf = malloc(buflen);
+again: 
+       status = _nss_getgrgid_r(gid, &grp, buf, buflen, &nss_errno);
+       if (status == NSS_STATUS_TRYAGAIN) {
+               buflen *= 2;
+               buf = realloc(buf, buflen);
+               goto again;
+       }
+       if (status == NSS_STATUS_NOTFOUND) {
+               return NULL;
+       }
+       if (status != NSS_STATUS_SUCCESS) {
+               report_nss_error("getgrgid", status);
+               return NULL;
+       }
+       return &grp;
+}
+
+static void nss_setgrent(void)
+{
+       NSS_STATUS (*_nss_setgrent)(void) = find_fn("setgrent");
+       NSS_STATUS status;
+       status = _nss_setgrent();
+       if (status != NSS_STATUS_SUCCESS) {
+               report_nss_error("setgrent", status);
+       }
+}
+
+static void nss_endgrent(void)
+{
+       NSS_STATUS (*_nss_endgrent)(void) = find_fn("endgrent");
+       NSS_STATUS status;
+       status = _nss_endgrent();
+       if (status != NSS_STATUS_SUCCESS) {
+               report_nss_error("endgrent", status);
+       }
+}
+
+static int nss_initgroups(char *user, gid_t group, gid_t **groups, long int *start, long int *size)
+{
+       NSS_STATUS (*_nss_initgroups)(char *, gid_t , long int *,
+                                     long int *, gid_t **, long int , int *) = 
+               find_fn("initgroups_dyn");
+       NSS_STATUS status;
+
+       if (!_nss_initgroups) return NSS_STATUS_UNAVAIL;
+
+       status = _nss_initgroups(user, group, start, size, groups, 0, &nss_errno);
+       if (status != NSS_STATUS_SUCCESS) {
+               report_nss_error("initgroups", status);
+       }
+       return status;
+}
+
+static void print_passwd(struct passwd *pwd)
+{
+       printf("%s:%s:%d:%d:%s:%s:%s\n", 
+              pwd->pw_name,
+              pwd->pw_passwd,
+              pwd->pw_uid,
+              pwd->pw_gid,
+              pwd->pw_gecos,
+              pwd->pw_dir,
+              pwd->pw_shell);
+}
+
+static void nss_test_initgroups(char *name, gid_t gid)
+{
+       long int size = 10240;
+       long int start = 1;
+       gid_t *groups = NULL;
+       int i;
+       NSS_STATUS status;
+
+       groups = (gid_t *)malloc(size * sizeof(gid_t));
+       groups[0] = gid;
+
+       status = nss_initgroups(name, gid, &groups, &start, &size);
+       if (status == NSS_STATUS_UNAVAIL) {
+               printf("No initgroups fn\n");
+               return;
+       }
+
+       for (i=0; i<start-1; i++) {
+               printf("%d, ", groups[i]);
+       }
+       printf("%d\n", groups[i]);
+
+       if (start > max_groups) {
+               max_groups = start;
+       }
+}
+
+
+static void nss_test_users(void)
+{
+       struct passwd *pwd;
+
+       nss_setpwent();
+       /* loop over all users */
+       while ((pwd = nss_getpwent())) {
+               printf("Testing user %s\n", pwd->pw_name);
+               printf("initgroups: "); nss_test_initgroups(pwd->pw_name, pwd->pw_gid);
+               printf("\n");
+       }
+       nss_endpwent();
+}
+
+ int main(int argc, char *argv[])
+{      
+       char *p;
+
+       if (argc > 1) so_path = argv[1];
+
+       p = strrchr(so_path, '_');
+       if (!p) {
+               printf("Badly formed name for .so - must be libnss_FOO.so\n");
+               exit(1);
+       }
+       nss_name = strdup(p+1);
+       p = strchr(nss_name, '.');
+       if (p) *p = 0;
+
+       printf("so_path=%s nss_name=%s\n\n", so_path, nss_name);
+
+       nss_test_users();
+
+       printf("total_errors=%d\n", total_errors);
+       printf("maximum number of groups: %d\n", max_groups);
+
+       return total_errors;
+}
diff --git a/maxnamelen.c b/maxnamelen.c
new file mode 100644 (file)
index 0000000..26ad4b1
--- /dev/null
@@ -0,0 +1,30 @@
+#define _GNU_SOURCE
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <errno.h>
+
+int main(void)
+{
+       int i, fd;
+
+       for (i=1;i<1000;i++) {
+               char *fname;
+               fname = malloc(i+1);
+               memset(fname, 'X', i);
+               fname[i] = 0;
+               fd = open(fname, O_CREAT|O_EXCL|O_RDWR, 0644);
+               if (fd == -1) {
+                       printf("failed at %d - %s\n", i, strerror(errno));
+                       break;
+               }
+               close(fd);
+               unlink(fname);
+       }
+
+       return -1;
+}
diff --git a/maxpath.c b/maxpath.c
new file mode 100644 (file)
index 0000000..5bba720
--- /dev/null
+++ b/maxpath.c
@@ -0,0 +1,45 @@
+#include <sys/param.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+
+ size_t strlcpy(char *d, const char *s, size_t bufsize)
+{
+       size_t len = strlen(s);
+       size_t ret = len;
+       if (bufsize <= 0) return 0;
+       if (len >= bufsize) len = bufsize-1;
+       memcpy(d, s, len);
+       d[len] = 0;
+       return ret;
+}
+
+static unsigned char read_byte(void)
+{
+       return 254;
+}
+
+main()
+{
+       char *dirname;
+       int off;
+       char *p;
+       char *basename = "test";
+       p = malloc(MAXPATHLEN);
+       int len = read_byte();
+
+       printf("len=%d\n", len);
+
+       dirname = malloc(MAXPATHLEN+1);
+       memset(dirname, 'x', MAXPATHLEN);
+       dirname[MAXPATHLEN-2] = 0;
+
+       off = strlcpy(p, dirname, MAXPATHLEN);
+       printf("off=%d\n", off);
+       off += strlcpy(p + off, "/", MAXPATHLEN - off);
+       printf("off=%d\n", off);
+       off += strlcpy(p + off, basename, MAXPATHLEN - off);
+       printf("off=%d\n", off);
+
+       printf("%d\n", MAXPATHLEN);
+}
diff --git a/mem_hog.c b/mem_hog.c
new file mode 100644 (file)
index 0000000..c96f64b
--- /dev/null
+++ b/mem_hog.c
@@ -0,0 +1,13 @@
+#define SIZE (2*1024*1024)
+
+void main(void)
+{
+       unsigned *p = (char *)malloc(SIZE);
+       char sum=0;
+       memset(p,42,SIZE);
+
+       while (1) {
+               int i;
+               for (i=0;i<SIZE/4;i++) sum += p[i];
+       }
+}
diff --git a/memread.c b/memread.c
new file mode 100644 (file)
index 0000000..cc53685
--- /dev/null
+++ b/memread.c
@@ -0,0 +1,70 @@
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/mman.h>
+#include <fcntl.h>
+#include <sys/file.h>
+#include <sys/time.h>
+
+struct timeval tp1,tp2;
+
+static void start_timer()
+{
+  gettimeofday(&tp1,NULL);
+}
+
+static double end_timer()
+{
+  gettimeofday(&tp2,NULL);
+  return((tp2.tv_sec - tp1.tv_sec) + 
+        (tp2.tv_usec - tp1.tv_usec)*1.0e-6);
+}
+
+
+#define MB (1024*1024)
+
+static void fmemcpy(char *dest, char *src, int size)
+{
+        double d;
+        double *dp, *sp;
+
+        dp = (double *)dest;
+        sp = (double *)src;
+        
+        while (size >= sizeof(d)) {
+               d = sp[0];
+               dp[0] = d;
+                dp++; sp++;
+                size -= sizeof(d);
+        }
+}
+
+static void frame_copy(char *dst, char *src)
+{
+       int ysize = 512;
+       while (ysize--) {
+               dlmemcpy(dst, src, 512*2);
+               src += 1024*2;
+               dst += 1280*2;
+       }
+
+}
+
+int main(int argc, char *argv[])
+{
+       int fd;
+       char *p, *p2;
+       int size = 4*MB;
+       int i;
+
+       fd = open(argv[1], O_RDWR);
+       p = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0xe0000000);
+       p2= mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0xe4400000 - 0x1000);
+       close(fd);
+
+       printf("p2=%p p=%p\n", p2, p);
+
+       while (1)
+               frame_copy(p, p2);
+
+       return 0;
+}
diff --git a/memtest2.c b/memtest2.c
new file mode 100644 (file)
index 0000000..338c513
--- /dev/null
@@ -0,0 +1,37 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+int main(int argc, char *argv[])
+{
+       unsigned char *buf;
+       int size;
+       int i, count;
+
+       if (argc < 2) {
+               printf("memtest <size>\n");
+               exit(1);
+       }
+
+       size = atoi(argv[1]);
+
+       buf = (unsigned char *)malloc(size);
+       
+       count = 0;
+
+       while (1) {
+               unsigned char v = count % 256;
+               memset(buf, v, size);
+               for (i=0;i<size;i++) {
+                       if (buf[i] != v) {
+                               printf("\nbuf[%d]=0x%x v=0x%x\n", 
+                                      i, buf[i], v);
+                       }
+               }
+               count++;
+               printf("%d\r", count);
+               fflush(stdout);
+       }
+
+       return 0;
+}
diff --git a/mksparse2.c b/mksparse2.c
new file mode 100644 (file)
index 0000000..c7b85eb
--- /dev/null
@@ -0,0 +1,16 @@
+#include <unistd.h>
+#include <stdio.h>
+#include <fcntl.h>
+
+int main(int argc, char *argv[])
+{
+       char *fname = argv[1];
+       off_t megs = atoi(argv[2]);
+       int fd;
+
+       fd = open(fname,O_WRONLY|O_CREAT, 0600);
+       lseek(fd, megs * 1024*1024, SEEK_SET);
+       write(fd, fname, strlen(fname));
+       close(fd);
+       return 0;
+}
diff --git a/mmap.c b/mmap.c
new file mode 100644 (file)
index 0000000..3c40eef
--- /dev/null
+++ b/mmap.c
@@ -0,0 +1,39 @@
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/mman.h>
+#include <fcntl.h>
+#include <sys/file.h>
+
+int main(void)
+{
+       int fd, fd2;
+       char buf[1024]="";
+       int i;
+       char *p;
+
+       memset(buf,'Z', sizeof(buf));
+
+       fd = open("mmap.tst", O_WRONLY|O_CREAT|O_TRUNC, 0600);
+
+       for (i=0;i<32;i++) {
+               write(fd, buf, sizeof(buf));
+       }
+
+       close(fd);
+
+       fd = open("mmap.tst", O_RDONLY);
+
+       p = mmap(NULL, i*sizeof(buf), PROT_READ, MAP_SHARED, fd, 0);
+
+       truncate("mmap.tst", 0);
+
+       fd2 = open("mmap.tst.2", O_WRONLY|O_TRUNC|O_CREAT, 0600);
+
+       printf("accessing ...\n");
+
+       write(fd2, p, i*sizeof(buf));
+
+       close(fd2);
+
+       return 0;
+}
diff --git a/mmap2.c b/mmap2.c
new file mode 100644 (file)
index 0000000..e7969b6
--- /dev/null
+++ b/mmap2.c
@@ -0,0 +1,26 @@
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/mman.h>
+#include <fcntl.h>
+#include <sys/file.h>
+
+int main(void)
+{
+       int fd;
+       int i;
+       char *p;
+
+       fd = open("mmap.tst", O_RDWR|O_CREAT|O_TRUNC, 0600);
+
+       ftruncate(fd, 8192);
+
+       p = mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
+
+       while (1) {
+               sleep(4);
+               p[1024]++;
+               pwrite(fd, p, 1, 0);
+       }
+
+       return 0;
+}
diff --git a/mmap_write.c b/mmap_write.c
new file mode 100644 (file)
index 0000000..3dd98fa
--- /dev/null
@@ -0,0 +1,30 @@
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/mman.h>
+#include <fcntl.h>
+#include <sys/file.h>
+
+int main(int argc, const char *argv[])
+{
+       int fd;
+       char *p;
+
+       fd = open(argv[1], O_RDWR, 0600);
+       if (fd == -1) {
+               perror(argv[1]);
+               return -1;
+       }
+
+       p = mmap(NULL, 4, PROT_WRITE, MAP_SHARED, fd, 0);
+       if (p == (void *)-1) {
+               perror("mmap");
+               return -1;
+       }
+
+       p[0]++;
+
+       munmap(p, 4);
+       close(fd);
+
+       return 0;
+}
diff --git a/mode.c b/mode.c
new file mode 100644 (file)
index 0000000..c730a65
--- /dev/null
+++ b/mode.c
@@ -0,0 +1,22 @@
+#include <stdio.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+
+struct stat buf;
+
+int mode(file)
+char *file;
+{
+buf.st_mode = 0;
+stat(file,&buf);
+return(buf.st_mode);
+}
+
+main(argc,argv)
+int argc;
+char *argv[];
+{
+if (argc>1) printf("%o\n",mode(argv[1]));
+return(0);
+}
+
diff --git a/modf.c b/modf.c
new file mode 100644 (file)
index 0000000..822f862
--- /dev/null
+++ b/modf.c
@@ -0,0 +1,60 @@
+#include <stdio.h>
+#include <math.h>
+
+/* a replacement for modf that doesn't need the math library. Should
+   be portable, but slow */
+static double my_modf(double x0, double *iptr)
+{
+       int i;
+       long l;
+       double x = x0;
+       double f = 1.0;
+
+       for (i=0;i<100;i++) {
+               l = (long)x;
+               if (l <= (x+1) && l >= (x-1)) break;
+               x *= 0.1;
+               f *= 10.0;
+       }
+
+       if (i == 100) {
+               /* yikes! the number is beyond what we can handle. What do we do? */
+               (*iptr) = 0;
+               return 0;
+       }
+
+       if (i != 0) {
+               double i2;
+               double ret;
+
+               ret = my_modf(x0-l*f, &i2);
+               (*iptr) = l*f + i2;
+               return ret;
+       } 
+
+       (*iptr) = l;
+       return x - (*iptr);
+}
+
+static void test1(double x)
+{
+       double i1, i2;
+       double d1, d2;
+       d1 = modf(x, &i1);
+       d2 = my_modf(x, &i2);
+       if (d1 != d2 || i1 != i2) {
+               printf("%f\t%f\n", d1, i1);
+               printf("%f\t%f\n", d2, i2);
+       }
+}
+
+int main()
+{
+       test1(1.9);
+       test1(164976598.8749875);
+       test1(16497659895798297498763943987984.8749875);
+       test1(-8734987047074075050509709874000789.1749875);
+       test1(-16497659895798297498763943987984.8749875);
+       test1(8734903083084098487047074075050509709874000789.1749875);
+       return 0;
+}
diff --git a/movemail.c b/movemail.c
new file mode 100644 (file)
index 0000000..de3769a
--- /dev/null
@@ -0,0 +1,847 @@
+/* movemail foo bar -- move file foo to file bar,
+   locking file foo the way /bin/mail respects.
+   Copyright (C) 1986, 1992, 1993, 1994 Free Software Foundation, Inc.
+
+This file is part of GNU Emacs.
+
+GNU Emacs is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Emacs is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Emacs; see the file COPYING.  If not, write to
+the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
+
+/* Extracted from XEmacs 19.13, which said: "synched up with: FSF 19.28." */
+
+/* Important notice: defining MAIL_USE_FLOCK or MAIL_USE_LOCKF *will
+   cause loss of mail* if you do it on a system that does not normally
+   use flock as its way of interlocking access to inbox files.  The
+   setting of MAIL_USE_FLOCK and MAIL_USE_LOCKF *must agree* with the
+   system's own conventions.  It is not a choice that is up to you.
+
+   So, if your system uses lock files rather than flock, then the only way
+   you can get proper operation is to enable movemail to write lockfiles there.
+   This means you must either give that directory access modes
+   that permit everyone to write lockfiles in it, or you must make movemail
+   a setuid or setgid program.  */
+
+/*
+ * Modified January, 1986 by Michael R. Gretzinger (Project Athena)
+ *
+ * Added POP (Post Office Protocol) service.  When compiled -DPOP
+ * movemail will accept input filename arguments of the form
+ * "po:username".  This will cause movemail to open a connection to
+ * a pop server running on $MAILHOST (environment variable).  Movemail
+ * must be setuid to root in order to work with POP.
+ * 
+ * New module: popmail.c
+ * Modified routines:
+ *     main - added code within #ifdef MAIL_USE_POP; added setuid (getuid ())
+ *             after POP code. 
+ * New routines in movemail.c:
+ *     get_errmsg - return pointer to system error message
+ *
+ */
+
+#define NO_SHORTNAMES   /* Tell config not to load remap.h */
+#include <stdlib.h>
+#include <unistd.h>
+#include <time.h> /* for time() */
+#include <stdio.h> /* for printf() */
+#include <string.h> /* strcpy() */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/file.h>
+#include <errno.h>
+
+#ifndef WAITTYPE
+#define WAITTYPE int
+#endif /* !WAITTYPE */
+
+#ifndef WIFEXITED
+#include <sys/wait.h>
+#endif /* !WIFEXITED */
+
+#ifndef WRETCODE
+#define WRETCODE(w) WEXITSTATUS(w)
+#endif /* !WRETCODE */
+
+
+#ifdef MSDOS
+#undef access
+#endif /* MSDOS */
+
+#ifdef USG
+#include <fcntl.h>
+#include <unistd.h>
+#if defined (sun)
+#include <stdlib.h>
+#endif /* sun */
+#ifndef F_OK
+#define F_OK 0
+#define X_OK 1
+#define W_OK 2
+#define R_OK 4
+#endif
+#endif /* USG */
+
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+
+#ifdef XENIX
+#include <sys/locking.h>
+#endif
+
+#ifdef MAIL_USE_LOCKF
+#define MAIL_USE_SYSTEM_LOCK
+#endif
+
+#ifdef MAIL_USE_FLOCK
+#define MAIL_USE_SYSTEM_LOCK
+#endif
+
+#ifdef MAIL_USE_MMDF
+extern int lk_open (), lk_close ();
+#endif
+
+/* Cancel substitutions made by config.h for Emacs.  */
+#undef open
+#undef read
+#undef write
+#undef close
+
+static char *concat (char *s1, char *s2, char *s3);
+static void *xmalloc (unsigned int size);
+#ifndef errno
+extern int errno;
+#endif
+
+static void error (char *s1, char *s2, char *s3);
+static void fatal (char *s1, char *s2);
+static void pfatal_with_name (char *name);
+static void pfatal_and_delete (char *name);
+
+#if defined(linux) && defined(__GLIBC__)
+
+#if !defined(HAVE_STRERROR)
+#define HAVE_STRERROR 1
+#endif /* HAVE_STRERROR */
+
+#endif /* LINUX && __GLIBC__ */
+
+#ifndef HAVE_STRERROR
+char *strerror (int);
+#endif
+
+/* Nonzero means this is name of a lock file to delete on fatal error.  */
+char *delete_lockname;
+
+void
+main (argc, argv)
+     int argc;
+     char **argv;
+{
+  char *inname, *outname;
+  int indesc, outdesc;
+  int nread;
+  WAITTYPE status;
+
+#ifndef MAIL_USE_SYSTEM_LOCK
+  struct stat st;
+  long now;
+  int tem;
+  char *lockname, *p;
+  char *tempname;
+  int desc;
+#endif /* not MAIL_USE_SYSTEM_LOCK */
+
+  delete_lockname = 0;
+
+  if (argc < 3)
+    fatal ("two arguments required", "");
+
+  inname = argv[1];
+  outname = argv[2];
+
+#ifdef MAIL_USE_MMDF
+  mmdf_init (argv[0]);
+#endif
+
+  /* Check access to output file.  */
+  if (access (outname, F_OK) == 0 && access (outname, W_OK) != 0)
+    pfatal_with_name (outname);
+
+  /* Also check that outname's directory is writeable to the real uid.  */
+  {
+    char *buf = (char *) xmalloc (strlen (outname) + 1);
+    char *p;
+    strcpy (buf, outname);
+    p = buf + strlen (buf);
+    while (p > buf && p[-1] != '/')
+      *--p = 0;
+    if (p == buf)
+      *p++ = '.';
+    if (access (buf, W_OK) != 0)
+      pfatal_with_name (buf);
+    free (buf);
+  }
+
+#ifdef MAIL_USE_POP
+  if (!memcmp (inname, "po:", 3))
+    {
+      int status; char *user;
+
+      for (user = &inname[strlen (inname) - 1]; user >= inname; user--)
+       if (*user == ':')
+         break;
+
+      status = popmail (user, outname);
+      exit (status);
+    }
+
+  setuid (getuid ());
+#endif /* MAIL_USE_POP */
+
+  /* Check access to input file.  */
+  if (access (inname, R_OK | W_OK) != 0)
+    pfatal_with_name (inname);
+
+#ifndef MAIL_USE_MMDF
+#ifndef MAIL_USE_SYSTEM_LOCK
+  /* Use a lock file named /usr/spool/mail/$USER.lock:
+     If it exists, the mail file is locked.  */
+  /* Note: this locking mechanism is *required* by the mailer
+     (on systems which use it) to prevent loss of mail.
+
+     On systems that use a lock file, extracting the mail without locking
+     WILL occasionally cause loss of mail due to timing errors!
+
+     So, if creation of the lock file fails
+     due to access permission on /usr/spool/mail,
+     you simply MUST change the permission
+     and/or make movemail a setgid program
+     so it can create lock files properly.
+
+     You might also wish to verify that your system is one
+     which uses lock files for this purpose.  Some systems use other methods.
+
+     If your system uses the `flock' system call for mail locking,
+     define MAIL_USE_SYSTEM_LOCK in config.h or the s-*.h file
+     and recompile movemail.  If the s- file for your system
+     should define MAIL_USE_SYSTEM_LOCK but does not, send a bug report
+     to bug-gnu-emacs@prep.ai.mit.edu so we can fix it.  */
+
+  lockname = concat (inname, ".lock", "");
+  tempname = (char *) xmalloc (strlen (inname) + strlen ("EXXXXXX") + 1);
+  strcpy (tempname, inname);
+  p = tempname + strlen (tempname);
+  while (p != tempname && p[-1] != '/')
+    p--;
+  *p = 0;
+  strcpy (p, "EXXXXXX");
+  mktemp (tempname);
+  unlink (tempname);
+
+  while (1)
+    {
+      /* Create the lock file, but not under the lock file name.  */
+      /* Give up if cannot do that.  */
+      desc = open (tempname, O_WRONLY | O_CREAT | O_EXCL, 0666);
+      if (desc < 0)
+        pfatal_with_name ("lock file--see source file lib-src/movemail.c");
+      close (desc);
+
+      tem = link (tempname, lockname);
+      unlink (tempname);
+      if (tem >= 0)
+       break;
+      sleep (1);
+
+      /* If lock file is a minute old, unlock it.  */
+      if (stat (lockname, &st) >= 0)
+       {
+         now = time (0);
+         if (st.st_ctime < now - 60)
+            unlink (lockname);
+       }
+    }
+
+  delete_lockname = lockname;
+#endif /* not MAIL_USE_SYSTEM_LOCK */
+#endif /* not MAIL_USE_MMDF */
+
+  if (fork () == 0)
+    {
+      setuid (getuid ());
+
+#ifndef MAIL_USE_MMDF
+#ifdef MAIL_USE_SYSTEM_LOCK
+      indesc = open (inname, O_RDWR);
+#else  /* if not MAIL_USE_SYSTEM_LOCK */
+      indesc = open (inname, O_RDONLY);
+#endif /* not MAIL_USE_SYSTEM_LOCK */
+#else  /* MAIL_USE_MMDF */
+      indesc = lk_open (inname, O_RDONLY, 0, 0, 10);
+#endif /* MAIL_USE_MMDF */
+
+      if (indesc < 0)
+       pfatal_with_name (inname);
+
+#if defined (BSD) || defined (XENIX)
+      /* In case movemail is setuid to root, make sure the user can
+        read the output file.  */
+      /* This is desirable for all systems
+        but I don't want to assume all have the umask system call */
+      umask (umask (0) & 0333);
+#endif /* BSD or Xenix */
+      outdesc = open (outname, O_WRONLY | O_CREAT | O_EXCL, 0666);
+      if (outdesc < 0)
+       pfatal_with_name (outname);
+#ifdef MAIL_USE_SYSTEM_LOCK
+#ifdef MAIL_USE_LOCKF
+      if (lockf (indesc, F_LOCK, 0) < 0) pfatal_with_name (inname);
+#else /* not MAIL_USE_LOCKF */
+#ifdef XENIX
+      if (locking (indesc, LK_RLCK, 0L) < 0) pfatal_with_name (inname);
+#else
+      if (flock (indesc, LOCK_EX) < 0) pfatal_with_name (inname);
+#endif
+#endif /* not MAIL_USE_LOCKF */
+#endif /* MAIL_USE_SYSTEM_LOCK */
+
+      {
+       char buf[1024];
+
+       while (1)
+         {
+           nread = read (indesc, buf, sizeof buf);
+           if (nread != write (outdesc, buf, nread))
+             {
+               int saved_errno = errno;
+               unlink (outname);
+               errno = saved_errno;
+               pfatal_with_name (outname);
+             }
+           if (nread < sizeof buf)
+             break;
+         }
+      }
+
+#ifdef BSD
+      if (fsync (outdesc) < 0)
+       pfatal_and_delete (outname);
+#endif
+
+      /* Check to make sure no errors before we zap the inbox.  */
+      if (close (outdesc) != 0)
+       pfatal_and_delete (outname);
+
+#ifdef MAIL_USE_SYSTEM_LOCK
+#if defined (STRIDE) || defined (XENIX)
+      /* Stride, xenix have file locking, but no ftruncate.  This mess will do. */
+      close (open (inname, O_CREAT | O_TRUNC | O_RDWR, 0666));
+#else
+      ftruncate (indesc, 0L);
+#endif /* STRIDE or XENIX */
+#endif /* MAIL_USE_SYSTEM_LOCK */
+
+#ifdef MAIL_USE_MMDF
+      lk_close (indesc, 0, 0, 0);
+#else
+      close (indesc);
+#endif
+
+#ifndef MAIL_USE_SYSTEM_LOCK
+      /* Delete the input file; if we can't, at least get rid of its
+        contents.  */
+#ifdef MAIL_UNLINK_SPOOL
+      /* This is generally bad to do, because it destroys the permissions
+        that were set on the file.  Better to just empty the file.  */
+      if (unlink (inname) < 0 && errno != ENOENT)
+#endif /* MAIL_UNLINK_SPOOL */
+       creat (inname, 0600);
+#endif /* not MAIL_USE_SYSTEM_LOCK */
+
+      exit (0);
+    }
+
+  wait (&status);
+  if (!WIFEXITED (status))
+    exit (1);
+  else if (WRETCODE (status) != 0)
+    exit (WRETCODE (status));
+
+#if !defined (MAIL_USE_MMDF) && !defined (MAIL_USE_SYSTEM_LOCK)
+  unlink (lockname);
+#endif /* not MAIL_USE_MMDF and not MAIL_USE_SYSTEM_LOCK */
+  exit (0);
+}
+\f
+/* Print error message and exit.  */
+
+static void
+fatal (s1, s2)
+     char *s1, *s2;
+{
+  if (delete_lockname)
+    unlink (delete_lockname);
+  error (s1, s2, "");
+  exit (1);
+}
+
+/* Print error message.  `s1' is printf control string, `s2' is arg for it. */
+
+static void
+error (s1, s2, s3)
+     char *s1, *s2, *s3;
+{
+  printf ("movemail: ");
+  printf (s1, s2, s3);
+  printf ("\n");
+}
+
+static void
+pfatal_with_name (name)
+     char *name;
+{
+  char *s;
+
+  s = concat ("", strerror (errno), " for %s");
+  fatal (s, name);
+}
+
+static void
+pfatal_and_delete (name)
+     char *name;
+{
+  char *s;
+
+  s = concat ("", strerror (errno), " for %s");
+  unlink (name);
+  fatal (s, name);
+}
+
+/* Return a newly-allocated string whose contents concatenate those of s1, s2, s3.  */
+
+static char *
+concat (s1, s2, s3)
+     char *s1, *s2, *s3;
+{
+  int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
+  char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
+
+  strcpy (result, s1);
+  strcpy (result + len1, s2);
+  strcpy (result + len1 + len2, s3);
+  *(result + len1 + len2 + len3) = 0;
+
+  return result;
+}
+
+/* Like malloc but get fatal error if memory is exhausted.  */
+
+static void *
+xmalloc (size)
+     unsigned int size;
+{
+  void *result = (void *) malloc (size);
+  if (!result)
+    fatal ("virtual memory exhausted", (char *) 0);
+  return result;
+}
+\f
+/* This is the guts of the interface to the Post Office Protocol.  */
+
+#ifdef MAIL_USE_POP
+
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netdb.h>
+#include <stdio.h>
+#include <pwd.h>
+
+#ifdef USG
+#include <fcntl.h>
+/* Cancel substitutions made by config.h for Emacs.  */
+#undef open
+#undef read
+#undef write
+#undef close
+#endif /* USG */
+
+#define NOTOK (-1)
+#define OK 0
+#define DONE 1
+
+char *progname;
+FILE *sfi;
+FILE *sfo;
+char Errmsg[80];
+
+static int debug = 0;
+
+char *get_errmsg ();
+char *getenv ();
+int mbx_write ();
+
+int
+popmail (user, outfile)
+     char *user;
+     char *outfile;
+{
+  char *host;
+  int nmsgs, nbytes;
+  char response[128];
+  register int i;
+  int mbfi;
+  FILE *mbf;
+  struct passwd *pw = (struct passwd *) getpwuid (getuid ());
+  if (pw == NULL)
+    fatal ("cannot determine user name");
+
+  host = getenv ("MAILHOST");
+  if (host == NULL) 
+    {
+      fatal ("no MAILHOST defined");
+    }
+
+  if (pop_init (host) == NOTOK) 
+    {
+      fatal (Errmsg);
+    }
+
+  if (getline (response, sizeof response, sfi) != OK) 
+    {
+      fatal (response);
+    }
+
+  if (pop_command ("USER %s", user) == NOTOK 
+      || pop_command ("RPOP %s", pw->pw_name) == NOTOK) 
+    {
+      pop_command ("QUIT");
+      fatal (Errmsg);
+    }
+
+  if (pop_stat (&nmsgs, &nbytes) == NOTOK) 
+    {
+      pop_command ("QUIT");
+      fatal (Errmsg);
+    }
+
+  if (!nmsgs)
+  {
+    pop_command ("QUIT");
+    return 0;
+  }
+
+  mbfi = open (outfile, O_WRONLY | O_CREAT | O_EXCL, 0666);
+  if (mbfi < 0)
+  {
+    pop_command ("QUIT");
+    pfatal_and_delete (outfile);
+  }
+  fchown (mbfi, getuid (), -1);
+
+  if ((mbf = fdopen (mbfi, "w")) == NULL)
+    {
+      pop_command ("QUIT");
+      pfatal_and_delete (outfile);
+    }
+
+  for (i = 1; i <= nmsgs; i++) 
+    {
+      mbx_delimit_begin (mbf);
+      if (pop_retr (i, mbx_write, mbf) != OK) 
+        {
+          pop_command ("QUIT");
+          close (mbfi);
+         unlink (outfile);
+         fatal (Errmsg);
+        }
+      mbx_delimit_end (mbf);
+      fflush (mbf);
+    }
+
+  if (fsync (mbfi) < 0)
+    {
+      pop_command ("QUIT");
+      pfatal_and_delete (outfile);
+    }
+
+  if (close (mbfi) == -1)
+    {
+      pop_command ("QUIT");
+      pfatal_and_delete (outfile);
+    }
+
+  for (i = 1; i <= nmsgs; i++) 
+    {
+      if (pop_command ("DELE %d", i) == NOTOK) 
+        {
+         /* Better to ignore this failure.  */
+        }
+    }
+
+  pop_command ("QUIT");
+  return (0);
+}
+
+int
+pop_init (host)
+     char *host;
+{
+  register struct hostent *hp;
+  register struct servent *sp;
+  int lport = IPPORT_RESERVED - 1;
+  struct sockaddr_in sin;
+  register int s;
+
+  hp = gethostbyname (host);
+  if (hp == NULL) 
+    {
+      sprintf (Errmsg, "MAILHOST unknown: %s", host);
+         /* Reviewed 4.51 safe use of sprintf */
+      return NOTOK;
+    }
+
+  sp = getservbyname ("pop", "tcp");
+  if (sp == 0) 
+    {
+      strcpy (Errmsg, "tcp/pop: unknown service");
+      return NOTOK;
+    }
+
+  sin.sin_family = hp->h_addrtype;
+  memcpy ((char *)&sin.sin_addr, hp->h_addr, hp->h_length);
+  sin.sin_port = sp->s_port;
+  s = rresvport (&lport);
+  if (s < 0) 
+    {
+      sprintf (Errmsg, "error creating socket: %s", get_errmsg ());
+         /* Reviewed 4.51 safe use of sprintf */
+      return NOTOK;
+    }
+
+  if (connect (s, (char *)&sin, sizeof sin) < 0) 
+    {
+      sprintf (Errmsg, "error during connect: %s", get_errmsg ());
+         /* Reviewed 4.51 safe use of sprintf */
+      close (s);
+      return NOTOK;
+    }
+
+  sfi = fdopen (s, "r");
+  sfo = fdopen (s, "w");
+  if (sfi == NULL || sfo == NULL) 
+    {
+      sprintf (Errmsg, "error in fdopen: %s", get_errmsg ());
+         /* Reviewed 4.51 safe use of sprintf */
+      close (s);
+      return NOTOK;
+    }
+
+  return OK;
+}
+
+int
+pop_command (fmt, a, b, c, d)
+     char *fmt;
+{
+  char buf[128];
+  char errmsg[64];
+
+  sprintf (buf, fmt, a, b, c, d);   /* Reviewed 4.51 safe use of sprintf */
+
+  if (debug) fprintf (stderr, "---> %s\n", buf);
+  if (putline (buf, Errmsg, sfo) == NOTOK) return NOTOK;
+
+  if (getline (buf, sizeof buf, sfi) != OK) 
+    {
+      strcpy (Errmsg, buf);
+      return NOTOK;
+    }
+
+  if (debug) fprintf (stderr, "<--- %s\n", buf);
+  if (*buf != '+') 
+    {
+      strcpy (Errmsg, buf);
+      return NOTOK;
+    } 
+  else 
+    {
+      return OK;
+    }
+}
+
+    
+pop_stat (nmsgs, nbytes)
+     int *nmsgs, *nbytes;
+{
+  char buf[128];
+
+  if (debug) fprintf (stderr, "---> STAT\n");
+  if (putline ("STAT", Errmsg, sfo) == NOTOK)
+    return NOTOK;
+
+  if (getline (buf, sizeof buf, sfi) != OK) 
+    {
+      strcpy (Errmsg, buf);
+      return NOTOK;
+    }
+
+  if (debug) fprintf (stderr, "<--- %s\n", buf);
+  if (*buf != '+') 
+    {
+      strcpy (Errmsg, buf);
+      return NOTOK;
+    } 
+  else 
+    {
+      sscanf (buf, "+OK %d %d", nmsgs, nbytes);
+      return OK;
+    }
+}
+
+pop_retr (msgno, action, arg)
+     int (*action)();
+{
+  char buf[128];
+
+  sprintf (buf, "RETR %d", msgno);   /* Reviewed 4.51 safe use of sprintf */
+  if (debug) fprintf (stderr, "%s\n", buf);
+  if (putline (buf, Errmsg, sfo) == NOTOK) return NOTOK;
+
+  if (getline (buf, sizeof buf, sfi) != OK) 
+    {
+      strcpy (Errmsg, buf);
+      return NOTOK;
+    }
+
+  while (1) 
+    {
+      switch (multiline (buf, sizeof buf, sfi)) 
+        {
+        case OK:
+          (*action)(buf, arg);
+          break;
+        case DONE:
+          return OK;
+        case NOTOK:
+          strcpy (Errmsg, buf);
+          return NOTOK;
+        }
+    }
+}
+
+getline (buf, n, f)
+     char *buf;
+     register int n;
+     FILE *f;
+{
+  register char *p;
+  int c;
+
+  p = buf;
+  while (--n > 0 && (c = fgetc (f)) != EOF)
+    if ((*p++ = c) == '\n') break;
+
+  if (ferror (f)) 
+  {
+    strcpy (buf, "error on connection");
+    return NOTOK;
+  }
+
+  if (c == EOF && p == buf) 
+  {
+    strcpy (buf, "connection closed by foreign host");
+    return DONE;
+  }
+
+  *p = NULL;
+  if (*--p == '\n') *p = NULL;
+  if (*--p == '\r') *p = NULL;
+  return OK;
+}
+
+multiline (buf, n, f)
+     char *buf;
+     register int n;
+     FILE *f;
+{
+  if (getline (buf, n, f) != OK) return NOTOK;
+  if (*buf == '.') 
+    {
+      if (*(buf+1) == NULL) 
+       return DONE;
+      else 
+        strcpy (buf, buf+1);
+    }
+  return OK;
+}
+
+char *
+get_errmsg ()
+{
+  return strerror (errno);
+}
+
+putline (buf, err, f)
+     char *buf;
+     char *err;
+     FILE *f;
+{
+  fprintf (f, "%s\r\n", buf);
+  fflush (f);
+  if (ferror (f)) 
+    {
+      strcpy (err, "lost connection");
+      return NOTOK;
+    }
+  return OK;
+}
+
+mbx_write (line, mbf)
+     char *line;
+     FILE *mbf;
+{
+  fputs (line, mbf);
+  fputc (0x0a, mbf);
+}
+
+mbx_delimit_begin (mbf)
+     FILE *mbf;
+{
+  fputs ("\f\n0, unseen,,\n", mbf);
+}
+
+mbx_delimit_end (mbf)
+     FILE *mbf;
+{
+  putc ('\037', mbf);
+}
+
+#endif /* MAIL_USE_POP */
+\f
+#ifndef HAVE_STRERROR
+char *
+strerror (errnum)
+     int errnum;
+{
+  extern char *sys_errlist[];
+  extern int sys_nerr;
+
+  if (errnum >= 0 && errnum < sys_nerr)
+    return sys_errlist[errnum];
+  return (char *) "Unknown error";
+}
+
+#endif /* ! HAVE_STRERROR */
diff --git a/names.c b/names.c
new file mode 100644 (file)
index 0000000..ae492b4
--- /dev/null
+++ b/names.c
@@ -0,0 +1,14 @@
+
+static const char *rpcecho_endpoint_strings[] = {
+       "rpcecho"
+};
+
+struct dcerpc_endpoint_list {
+       int count;
+       const char **names;
+};
+
+static const struct dcerpc_endpoint_list rpcecho_endpoints = {
+       1,
+       rpcecho_endpoint_strings
+};
diff --git a/newline.c b/newline.c
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/nistest.c b/nistest.c
new file mode 100644 (file)
index 0000000..3f3a8e9
--- /dev/null
+++ b/nistest.c
@@ -0,0 +1,270 @@
+/************************************************
+nss_funcs.c
+-------------------------------------------------
+based on nsstest.c by a. tridgell
+expanded by j. trostel
+-------------------------------------------------
+get user and group info for different nss modules
+************************************************/
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <nss.h>
+#include <dlfcn.h>
+#include <pwd.h>
+#include <grp.h>
+#include <stdlib.h>
+
+typedef enum nss_status NSS_STATUS;
+int nss_errno;
+
+void *find_fn(const char *nss_name, const char *funct_name)
+{
+       char *so_path, *s;
+       void *h;
+       void *res;
+
+       // create the library name
+       asprintf(&so_path, "libnss_%s.so.2", nss_name);
+       h = dlopen(so_path, RTLD_LAZY);
+
+       if (!h){
+               free(so_path);
+               exit(1);
+       }
+
+       // create the function name
+       asprintf(&s, "_nss_%s_%s", nss_name, funct_name);
+
+       // and find it in the library
+       res = dlsym(h, s);
+
+       if (!res) {
+               free(so_path);
+               free(s);
+               return NULL;
+       }
+
+       // free the strings from asprintf
+       free(so_path);
+       free(s);
+       return res;
+}
+
+struct passwd *nss_getpwent(const char *nss_name)
+{
+       NSS_STATUS (*_nss_getpwent_r)(struct passwd *, char *, 
+                                     size_t , int *) = find_fn(nss_name, "getpwent_r");
+       static struct passwd pwd;
+       static char buf[1000];
+       NSS_STATUS status;
+
+       status = _nss_getpwent_r(&pwd, buf, sizeof(buf), &nss_errno);
+
+       if (status != NSS_STATUS_SUCCESS)
+               return NULL;
+
+       return &pwd;
+}
+
+struct passwd *nss_getpwnam(const char *nss_name, const char *name)
+{
+       NSS_STATUS (*_nss_getpwnam_r)(const char *, struct passwd *, char *, 
+                                     size_t , int *) = find_fn(nss_name, "getpwnam_r");
+       static struct passwd pwd;
+       static char buf[1000];
+       NSS_STATUS status;
+
+       status = _nss_getpwnam_r(name, &pwd, buf, sizeof(buf), &nss_errno);
+
+       if (status != NSS_STATUS_SUCCESS) 
+               return NULL;
+
+       return &pwd;
+}
+
+struct passwd *nss_getpwuid(const char *nss_name, uid_t uid)
+{
+       NSS_STATUS (*_nss_getpwuid_r)(uid_t, struct passwd *, char *, 
+                                     size_t , int *) = find_fn(nss_name, "getpwuid_r");
+       static struct passwd pwd;
+       static char buf[1000];
+       NSS_STATUS status;
+
+       status = _nss_getpwuid_r(uid, &pwd, buf, sizeof(buf), &nss_errno);
+
+       if (status != NSS_STATUS_SUCCESS) 
+               return NULL;
+
+       return &pwd;
+}
+
+NSS_STATUS nss_setpwent(const char *nss_name)
+{
+       NSS_STATUS (*_nss_setpwent)(void) = find_fn(nss_name, "setpwent");
+       return( _nss_setpwent() );
+}
+
+NSS_STATUS nss_endpwent(const char *nss_name)
+{
+       NSS_STATUS (*_nss_endpwent)(void) = find_fn(nss_name, "endpwent");
+       return( _nss_endpwent() );
+}
+
+struct group *nss_getgrent(const char *nss_name)
+{
+       NSS_STATUS (*_nss_getgrent_r)(struct group *, char *, 
+                                     size_t , int *) = find_fn(nss_name, "getgrent_r");
+       static struct group grp;
+       int my_errno = 0;
+       static char *buf = NULL;
+       static int buflen = 1024;
+       NSS_STATUS status;
+       
+       if(!buf) {
+               if((buf = malloc(buflen)) == NULL)
+               return NULL;
+       }
+       
+again:
+       status = _nss_getgrent_r(&grp, buf, buflen, &my_errno);
+       if (status == NSS_STATUS_TRYAGAIN) {
+               buflen *= 2;
+               free(buf);
+               if( (buf = malloc(buflen)) == NULL) {
+                       buflen = 1024;
+                       return NULL;
+               }
+               goto again;
+       }
+
+       if (status != NSS_STATUS_SUCCESS) {
+               free(buf);
+               buf = NULL;
+               return NULL;
+       }
+
+       return &grp;
+}
+
+struct group *nss_getgrnam(const char *nss_name, const char *name)
+{
+       NSS_STATUS (*_nss_getgrnam_r)(const char *, struct group *, char *, 
+                                     size_t , int *) = find_fn(nss_name, "getgrnam_r");
+       static struct group grp;
+       static char *buf = NULL;
+       static int buflen = 1024;
+       NSS_STATUS status;
+
+       if (!buf) {
+               if( (buf = malloc(buflen)) == NULL)
+                       return NULL;
+       }
+       
+again:
+       status = _nss_getgrnam_r(name, &grp, buf, buflen, &nss_errno);
+       if (status == NSS_STATUS_TRYAGAIN) {
+               buflen *= 2;
+               free(buf);
+               if ( (buf = malloc(buflen)) == NULL) {
+                       buflen = 1024;
+                       return NULL;
+               }
+               goto again;
+       }
+
+       if (status != NSS_STATUS_SUCCESS) {
+               free(buf);
+               buf = NULL;
+               return NULL;
+       }
+
+       return &grp;
+}
+
+struct group *nss_getgrgid(const char *nss_name, gid_t gid)
+{
+       NSS_STATUS (*_nss_getgrgid_r)(gid_t, struct group *, char *, 
+                                     size_t , int *) = find_fn(nss_name, "getgrgid_r");
+       static struct group grp;
+       static char *buf = NULL;
+       static int buflen = 1024;
+       NSS_STATUS status;
+       
+       if (!buf) {
+               if( (buf = malloc(buflen)) == NULL)
+                       return NULL;
+       }
+       
+again:
+       status = _nss_getgrgid_r(gid, &grp, buf, buflen, &nss_errno);
+       if (status ==  NSS_STATUS_TRYAGAIN) {
+               buflen *= 2;
+               free(buf);
+               if( (buf = malloc(buflen)) == NULL) {
+                       buflen = 1024;
+                       return NULL;
+               }
+               goto again;
+       }
+       
+       if (status != NSS_STATUS_SUCCESS) {
+               free(buf);
+               buf = NULL;
+               return NULL;
+       }
+
+       return &grp;
+}
+       
+
+NSS_STATUS nss_setgrent(const char *nss_name)
+{
+       NSS_STATUS (*_nss_setgrent)(void) = find_fn(nss_name, "setgrent");
+       return( _nss_setgrent() );
+}
+
+NSS_STATUS nss_endgrent(const char *nss_name)
+{
+       NSS_STATUS (*_nss_endgrent)(void) = find_fn(nss_name, "endgrent");
+       return( _nss_endgrent() );
+}
+
+/*
+  this trick resets the internal glibc cached copy of the NIS domain name
+  read the glibc nis code (in particular the __ypdomain[] static array) to
+  understand whats going on
+ */
+static void nis_reset_domain(void)
+{
+       void (*fn_yp_get_default_domain)(char **);
+       void *h = dlopen("libnss_nis.so.2", RTLD_LAZY);
+       char *domain = NULL;
+
+       fn_yp_get_default_domain = dlsym(h, "yp_get_default_domain");
+       
+       if (!fn_yp_get_default_domain) return;
+
+       fn_yp_get_default_domain(&domain);
+       if (domain) {
+               domain[0] = 0;
+       }
+}
+
+int main(void)
+{
+       while (1) {
+               struct passwd *pwd;
+               int i;
+
+               sleep(1);
+
+               nis_reset_domain();
+
+               nss_setpwent("nis");
+               i=0;
+               while (nss_getpwent("nis")) i++;
+               printf("i=%d\n", i);
+               nss_endpwent("nis");
+       }
+       return 0;
+}
diff --git a/not.c b/not.c
new file mode 100644 (file)
index 0000000..f28524c
--- /dev/null
+++ b/not.c
@@ -0,0 +1,11 @@
+#include <stdio.h>
+main()
+{
+       if (! 2 & 1) {
+               printf("1\n");
+       }
+       if (! (2 & 1)) {
+               printf("2\n");
+       }
+
+}
diff --git a/noteshack.c b/noteshack.c
new file mode 100644 (file)
index 0000000..e5b950b
--- /dev/null
@@ -0,0 +1,21 @@
+#include <stdio.h>
+
+typedef unsigned WORD;
+
+WORD OSTranslate(
+       WORD TranslateMode,
+       char *In,
+       WORD InLength,
+       char *Out,
+       WORD OutLength)
+{
+       printf("%s\n", In);
+       return 0x42;
+}
+
+
+int main(void)
+{
+       OSTranslate(0, "foo", 3, "", 1);
+       return 0;
+}
diff --git a/nsslist.c b/nsslist.c
new file mode 100644 (file)
index 0000000..4760c6e
--- /dev/null
+++ b/nsslist.c
@@ -0,0 +1,119 @@
+/* list users for a particular nss module
+ */
+
+#include <stdio.h>
+#include <nss.h>
+#include <dlfcn.h>
+#include <pwd.h>
+#include <grp.h>
+
+typedef enum nss_status NSS_STATUS;
+
+static char *nss_name;
+static int nss_errno;
+static NSS_STATUS last_error;
+static int total_errors;
+
+static void *find_fn(const char *name)
+{
+       char *so_path, *s;
+       static void *h;
+       void *res;
+
+       asprintf(&s, "_nss_%s_%s", nss_name, name);
+       asprintf(&so_path, "/lib/libnss_%s.so.2", nss_name);
+
+       if (!h) {
+               h = dlopen(so_path, RTLD_LAZY);
+       }
+       if (!h) {
+               printf("Can't open shared library %s\n", so_path);
+               exit(1);
+       }
+       res = dlsym(h, s);
+       if (!res) {
+               printf("Can't find function %s\n", s);
+               return NULL;
+       }
+       free(so_path);
+       free(s);
+       return res;
+}
+
+static void report_nss_error(const char *who, NSS_STATUS status)
+{
+       last_error = status;
+       total_errors++;
+       printf("ERROR %s: NSS_STATUS=%d  %d (nss_errno=%d)\n", 
+              who, status, NSS_STATUS_SUCCESS, nss_errno);
+}
+
+static struct passwd *nss_getpwent(void)
+{
+       NSS_STATUS (*_nss_getpwent_r)(struct passwd *, char *, 
+                                     size_t , int *) = find_fn("getpwent_r");
+       static struct passwd pwd;
+       static char buf[1000];
+       NSS_STATUS status;
+
+       status = _nss_getpwent_r(&pwd, buf, sizeof(buf), &nss_errno);
+       if (status == NSS_STATUS_NOTFOUND) {
+               return NULL;
+       }
+       if (status != NSS_STATUS_SUCCESS) {
+               report_nss_error("getpwent", status);
+               return NULL;
+       }
+       return &pwd;
+}
+
+static void nss_setpwent(void)
+{
+       NSS_STATUS (*_nss_setpwent)(void) = find_fn("setpwent");
+       NSS_STATUS status;
+       status = _nss_setpwent();
+       if (status != NSS_STATUS_SUCCESS) {
+               report_nss_error("setpwent", status);
+       }
+}
+
+static void nss_endpwent(void)
+{
+       NSS_STATUS (*_nss_endpwent)(void) = find_fn("endpwent");
+       NSS_STATUS status;
+       status = _nss_endpwent();
+       if (status != NSS_STATUS_SUCCESS) {
+               report_nss_error("endpwent", status);
+       }
+}
+
+static void list_users(void)
+{
+       struct passwd *pwd;
+
+       nss_setpwent();
+       /* loop over all users */
+       while ((pwd = nss_getpwent())) {
+               printf("%s\n", pwd->pw_name);
+       }
+       nss_endpwent();
+}
+
+static void usage(void)
+{
+       printf("nsslist <nsstype>\n");
+}
+
+int main(int argc, char *argv[])
+{      
+       if (argc < 2) {
+               usage();
+               exit(1);
+       }
+
+       nss_name = argv[1];
+
+       list_users();
+
+       return 0;
+}
diff --git a/null.c b/null.c
new file mode 100644 (file)
index 0000000..ebc100e
--- /dev/null
+++ b/null.c
@@ -0,0 +1,8 @@
+main()
+{
+char *p = 0;
+int x;
+
+x = *(int *)p;
+exit(x);
+}
diff --git a/open.c b/open.c
new file mode 100644 (file)
index 0000000..0883d97
--- /dev/null
+++ b/open.c
@@ -0,0 +1,13 @@
+#include <stdlib.h>
+#include <fcntl.h>
+
+int main()
+{
+       int i;
+
+       for (i=0; ; i++)
+               if (open("/dev/null", O_RDONLY) == -1) break;
+
+       printf("opened %d files\n", i);
+       sleep(100);
+}
diff --git a/opendir.c b/opendir.c
new file mode 100644 (file)
index 0000000..e2d0124
--- /dev/null
+++ b/opendir.c
@@ -0,0 +1,13 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <dirent.h>
+#include <fcntl.h>
+#include <errno.h>
+
+
+
+int foo(void)
+{
+       return sizeof(DIR);
+}
diff --git a/opentest.c b/opentest.c
new file mode 100644 (file)
index 0000000..673c870
--- /dev/null
@@ -0,0 +1,33 @@
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <errno.h>
+
+
+char *fname = "test.tmp";
+
+void main()
+{
+  int fd;
+
+  /* create a readonly file */
+  unlink(fname);
+  if ((fd=open(fname,O_RDWR|O_CREAT,0444)) == -1) {
+    printf("first open failed : %s\n",strerror(errno));
+    exit(1);
+  }
+  close(fd);
+
+  /* try and open it for reading. This should work. Under linux 2.0 it doesn't */
+  if ((fd=open(fname,O_RDONLY|O_CREAT,0666)) == -1) {
+    printf("2nd open failed : %s\n",strerror(errno));
+    exit(1);
+  }
+  close(fd);
+
+  /* it worked - clean up */
+  unlink(fname);
+
+  printf("all OK\n");
+}
diff --git a/oplock_irix.c b/oplock_irix.c
new file mode 100644 (file)
index 0000000..ec381dd
--- /dev/null
@@ -0,0 +1,50 @@
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <signal.h>
+#include <sys/capability.h>
+
+#define FNAME "test.dat"
+
+static int fd;
+static int have_lease;
+static int got_signal;
+
+static void sig_usr1(int sig)
+{
+       got_signal = 1;
+}
+
+int main(int argc, char *argv[])
+{
+       int pfd[2];
+       cap_t cap = cap_get_proc();
+
+       cap->cap_effective |= CAP_NETWORK_MGT;
+       if (cap_set_proc(cap) == -1) failed("cap_set_proc");
+       cap_free(cap);
+
+       if (pipe(pfd) != 0) failed("pipe");
+
+       if ((fd = open(FNAME, O_RDWR|O_CREAT|O_EXCL|O_TRUNC, 0600)) == -1) failed("open");
+
+       if (fcntl(fd, F_SETSIG, SIGUSR1) == -1) failed("setsig");
+
+       signal(SIGUSR1, sig_usr1);
+
+       while (1) {
+               sleep(1);
+
+               if (got_signal) {
+                       if (fcntl(fd, F_OPLKACK, OP_REVOKE) == -1) failed("revoke");
+                       got_signal = 0;
+                       have_lease = 0;
+               }
+
+               if (!have_lease && fcntl(fd, F_OPLKREG, pfd[1]) == -1) continue;
+               have_lease = 1;
+       }
+
+       return 0;
+}
+
diff --git a/parseaddr.c b/parseaddr.c
new file mode 100644 (file)
index 0000000..3187814
--- /dev/null
@@ -0,0 +1,31 @@
+#include <sys/time.h>
+#include <sys/types.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netdb.h>
+#include <arpa/inet.h>
+
+int ipstr_list_parse(const char* ipstr_list, struct in_addr** ip_list)
+{
+       int count;
+       for (ip_list=NULL, count=0; ipstr_list; count++) {
+               struct in_addr a;
+
+               if (inet_aton(ipstr_list, &a) == -1) break;
+
+               *ip_list = Realloc(*ip_list, (count+1) * sizeof(struct in_addr));
+               if (!ip_list) {
+                       return -1;
+               }
+
+               (*ip_list)[count] = a;
+
+               ipstr_list = strchr(ipstr_list, ':');
+               if (ipstr_list) ipstr_list++;
+       }
+       return count;
+}
diff --git a/pass.c b/pass.c
new file mode 100644 (file)
index 0000000..8c2c5bf
--- /dev/null
+++ b/pass.c
@@ -0,0 +1,56 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <ctype.h>
+#include <sys/types.h>
+#include <sys/file.h>
+#include <sys/stat.h>
+#include <sys/param.h>
+#include <sys/mount.h>
+#include <pwd.h>
+#include <unistd.h>
+#include <pwd.h>
+
+#ifdef SHADOW_PWD
+#include <shadow.h>
+#endif
+
+
+void main()
+{
+  char *password;
+#ifdef SHADOW_PWD
+  struct spwd *spass = NULL;
+#endif
+  struct passwd *pass = NULL;
+  char salt[100];
+  char user[100];
+
+  printf("Username: ");
+  if (scanf("%s",user) != 1)
+    exit(0);
+      
+  password = getpass("Password: ");
+  pass = getpwnam(user);
+  if (pass == NULL)
+    printf("couldn't find account %s\n",user); 
+  else
+    {
+      int pwok = 0;
+#ifdef PWDAUTH
+      pwok = (pwdauth(user,password) == 0);
+#else
+#ifdef SHADOW_PWD
+      spass = getspnam(user);
+      if (spass && spass->sp_pwdp)
+       pass->pw_passwd = spass->sp_pwdp;
+#endif
+      strncpy(salt,pass->pw_passwd,2);
+      salt[2] = 0;
+      pwok = (strcmp(crypt(password,salt),pass->pw_passwd) == 0);
+#endif
+      if (!pwok)
+       printf("invalid password\n");
+      else
+       printf("password OK\n");
+    }
+}
diff --git a/paste.c b/paste.c
new file mode 100644 (file)
index 0000000..958dac5
--- /dev/null
+++ b/paste.c
@@ -0,0 +1,11 @@
+#define NDR_RQST_FUNCTION(name) \
+int #name(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, guint8 *drep) \
+{ \
+        struct pidl_pull *ndr = pidl_pull_init(tvb, offset, pinfo, drep); \
+        struct #name *r = talloc_p(NULL, struct #name); \
+        pidl_tree ptree; \
+        ptree.proto_tree = tree; \
+        ptree.subtree_list = NULL; \
+        ndr_pull_#name(ndr, NDR_IN, &ptree, r); \
+        return ndr->offset; \
+}
diff --git a/pclone.c b/pclone.c
new file mode 100644 (file)
index 0000000..c122043
--- /dev/null
+++ b/pclone.c
@@ -0,0 +1,16 @@
+/* test the clone syscall with new flags */
+#include <stdio.h>
+#include <signal.h>
+#include <linux/sched.h>
+#include <linux/unistd.h>
+
+_syscall2(int, clone, int, flags, int, sp);
+
+main()
+{
+  printf("pid=%d\n",getpid());
+
+  /* clone(SIGCLD|CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND,0);  */
+  clone(SIGCLD|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_PID|CLONE_VM,0);
+  printf("pid=%d\n",getpid());
+}
diff --git a/posarg.c b/posarg.c
new file mode 100644 (file)
index 0000000..9197d58
--- /dev/null
+++ b/posarg.c
@@ -0,0 +1,8 @@
+#include <stdio.h>
+
+int main(void)
+{
+       printf("%*d\n", 3, 12);
+       printf("%2$*1$.*3$f\n", 3, 12.12345, 2);
+       return 0;
+}
diff --git a/pr.c