From 7e0f021a9aec35fd8e6725e87e3313b101d26f5e Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Sun, 27 Jan 2008 11:37:44 +0100 Subject: Initial import (2.0.2-6) --- reference/C/CONTRIB/SNIP/strucfil.c | 190 ++++++++++++++++++++++++++++++++++++ 1 file changed, 190 insertions(+) create mode 100755 reference/C/CONTRIB/SNIP/strucfil.c (limited to 'reference/C/CONTRIB/SNIP/strucfil.c') diff --git a/reference/C/CONTRIB/SNIP/strucfil.c b/reference/C/CONTRIB/SNIP/strucfil.c new file mode 100755 index 0000000..a0b6522 --- /dev/null +++ b/reference/C/CONTRIB/SNIP/strucfil.c @@ -0,0 +1,190 @@ +/******************************************************************* + * Generic structure <> disk file manipulations. These functions + * form a basic template for reading and writing structures to a + * sequential file. This template is probably most useful for files + * with 500 or less records and eliminates the need for a more + * elaborate file handler such as C-Tree, DB-Vista, Mix etc. + * Routines to put data in the struct is out of scope here. + * Written by Lynn Nash 8/28/91 and donated to the public domain. + */ +#include +#include +#include +#include + +#ifndef ERROR +#define ERROR -1 +#define OK 0 +#endif + +#ifndef TRUE +#define TRUE 1 +#define FALSE !TRUE +#endif + +/* make sure the record structure is byte aligned */ + +#if defined(__TURBOC__) + #pragma option -a- +#elif defined(__ZTC__) + #pragma ZTC align 1 +#else /* MSC/QC/WATCOM/METAWARE */ + #pragma pack(1) +#endif + +static struct blackbook { + int delete_flag; /* 0 = active -1 = deleted */ + int recordnum; /* a sequential number in the file */ + /* The data fields in asciiz. */ + char firstname[11]; + char lastname[16]; + char addr[26]; + char city[16]; + char state[3]; + char zip[10]; + char phone[11]; +} rec, oldrec; /* 97 byte record * 2 */ + +#pragma pack() + +/*-------------------- general globals ---------------------*/ + +static long cur_rec = 0; /* the current record number */ +static FILE *fsptr = NULL; /* fixed record data file pointer */ + +/* if file exists open in read/write mode else create file */ + +FILE * open_file(char *filename) +{ + if (access(filename, 0) == 0) + fsptr = fopen(filename, "rb+"); + else fsptr = fopen(filename, "wb+"); + return fsptr; /* return the file pointer */ +} + +/* add new records to the data file */ + +int datadd(void) +{ + if (fsptr) + { + if (fseek(fsptr, 0L, SEEK_END) != 0) + return ERROR; /* seek failure */ + rec.delete_flag = 0; /* active record tag */ + rec.recordnum = (int) (ftell(fsptr) / + (long) sizeof(struct blackbook)); + if (fwrite(&rec, sizeof(struct blackbook), 1, fsptr) != 1) + { + return ERROR; /* write error */ + } + else + { + /* put your clean up code here */ + return OK; + } + } + return ERROR; +} + +/* tag the last record read in the file as deleted */ + +int data_delete(void) +{ + if (fsptr) + { + if (fseek(fsptr, (long) sizeof(struct blackbook) * -1L, + SEEK_CUR) != 0) + { + return ERROR; + } + rec.delete_flag = -1; /* tag the record as deleted */ + if (fwrite(&rec, sizeof(struct blackbook), 1, fsptr) != 1) + return ERROR; + else return OK; + } + return ERROR; +} + +/* read a random structure. If successful the global cur_rec will + * contain the number of the last record read & it can be compared + * to the number in the struct as a double check (belt & suspenders) + */ + +int data_read(long recnum) +{ + if (fseek(fsptr, (long) sizeof(struct blackbook) * recnum, + SEEK_SET) != 0) + { + return ERROR; + } + cur_rec = recnum; /* keep tabs on record pointer in global */ + + /* now read the record into save struct*/ + + if (fread(&oldrec, sizeof(struct blackbook), 1, fsptr) != 1) + { + return ERROR; + } + else /* copy save struct to edit struct */ + { + memcpy(&rec, &oldrec, sizeof(struct blackbook)); + return OK; + } +} + +/* rewrite the last read record back to disk */ + +int data_update(void) +{ + if (memcmp(&rec, &oldrec, sizeof(struct blackbook)) == 0) + return TRUE; /* no update needed */ + + /* back up one record before writing */ + + if (fseek(fsptr, (long) sizeof(struct blackbook) * -1L, + SEEK_CUR) != 0) + { + return ERROR; /* seek error */ + } + + /* now write the record */ + + if (fwrite(&rec, sizeof(struct blackbook), 1, fsptr) != 1) + return ERROR; /* write error */ + return OK; +} + +/* get the next valid record in the file */ + +int read_forward(void) +{ + do + { + cur_rec++; /* upcount the record number */ + if (data_read(cur_rec) != 0) + { + cur_rec--; /* decrement the record number */ + return ERROR; + } + } while (oldrec.delete_flag != 0); /* record read was deleted */ + return OK; +} + +/* get the previous valid record in the file */ + +int read_backward(void) +{ + do + { + cur_rec--; /* decrement the record number */ + if (cur_rec >= 0) + { + if ( data_read(cur_rec) != 0 ) + { + cur_rec++; /* increment the record number */ + return ERROR; + } + } + } while (oldrec.delete_flag != 0); /* record read was deleted */ + return OK; +} -- cgit v1.2.3-54-g00ecf when inserting certain netfilter rules when NFT_SET_HASH is disabled, from Liping Zhang. 4) Memory leak when nft_expr_clone() fails, also from Liping Zhang. 5) Disable UFO when path will apply IPSEC tranformations, from Jakub Sitnicki. 6) Don't bogusly double cwnd in dctcp module, from Florian Westphal. 7) skb_checksum_help() should never actually use the value "0" for the resulting checksum, that has a special meaning, use CSUM_MANGLED_0 instead. From Eric Dumazet. 8) Per-tx/rx queue statistic strings are wrong in qed driver, fix from Yuval MIntz. 9) Fix SCTP reference counting of associations and transports in sctp_diag. From Xin Long. 10) When we hit ip6tunnel_xmit() we could have come from an ipv4 path in a previous layer or similar, so explicitly clear the ipv6 control block in the skb. From Eli Cooper. 11) Fix bogus sleeping inside of inet_wait_for_connect(), from WANG Cong. 12) Correct deivce ID of T6 adapter in cxgb4 driver, from Hariprasad Shenai. 13) Fix potential access past the end of the skb page frag array in tcp_sendmsg(). From Eric Dumazet. 14) 'skb' can legitimately be NULL in inet{,6}_exact_dif_match(). Fix from David Ahern. 15) Don't return an error in tcp_sendmsg() if we wronte any bytes successfully, from Eric Dumazet. 16) Extraneous unlocks in netlink_diag_dump(), we removed the locking but forgot to purge these unlock calls. From Eric Dumazet. 17) Fix memory leak in error path of __genl_register_family(). We leak the attrbuf, from WANG Cong. 18) cgroupstats netlink policy table is mis-sized, from WANG Cong. 19) Several XDP bug fixes in mlx5, from Saeed Mahameed. 20) Fix several device refcount leaks in network drivers, from Johan Hovold. 21) icmp6_send() should use skb dst device not skb->dev to determine L3 routing domain. From David Ahern. 22) ip_vs_genl_family sets maxattr incorrectly, from WANG Cong. 23) We leak new macvlan port in some cases of maclan_common_netlink() errors. Fix from Gao Feng. 24) Similar to the icmp6_send() fix, icmp_route_lookup() should determine L3 routing domain using skb_dst(skb)->dev not skb->dev. Also from David Ahern. 25) Several fixes for route offloading and FIB notification handling in mlxsw driver, from Jiri Pirko. 26) Properly cap __skb_flow_dissect()'s return value, from Eric Dumazet. 27) Fix long standing regression in ipv4 redirect handling, wrt. validating the new neighbour's reachability. From Stephen Suryaputra Lin. 28) If sk_filter() trims the packet excessively, handle it reasonably in tcp input instead of exploding. From Eric Dumazet. 29) Fix handling of napi hash state when copying channels in sfc driver, from Bert Kenward. * git://git.kernel.org/pub/scm/linux/kernel/git/davem/net: (121 commits) mlxsw: spectrum_router: Flush FIB tables during fini net: stmmac: Fix lack of link transition for fixed PHYs sctp: change sk state only when it has assocs in sctp_shutdown bnx2: Wait for in-flight DMA to complete at probe stage Revert "bnx2: Reset device during driver initialization" ps3_gelic: fix spelling mistake in debug message net: ethernet: ixp4xx_eth: fix spelling mistake in debug message ibmvnic: Fix size of debugfs name buffer ibmvnic: Unmap ibmvnic_statistics structure sfc: clear napi_hash state when copying channels mlxsw: spectrum_router: Correctly dump neighbour activity mlxsw: spectrum: Fix refcount bug on span entries bnxt_en: Fix VF virtual link state. bnxt_en: Fix ring arithmetic in bnxt_setup_tc(). Revert "include/uapi/linux/atm_zatm.h: include linux/time.h" tcp: take care of truncations done by sk_filter() ipv4: use new_gw for redirect neigh lookup r8152: Fix error path in open function net: bpqether.h: remove if_ether.h guard net: __skb_flow_dissect() must cap its return value ...
Diffstat (limited to 'include')