├── .gitignore ├── LICENSE ├── project ├── Makefile ├── build.sh ├── function │ ├── api.h │ └── function.c ├── global │ ├── define.h │ ├── global.h │ └── var.h ├── main.c ├── shell │ ├── shell.c │ └── shell.h └── util │ ├── disk.c │ ├── disk.h │ ├── list.c │ ├── list.h │ ├── listmacro.h │ ├── str.c │ ├── str.h │ ├── time.c │ └── time.h ├── readme.md └── test ├── createDisk.c ├── fileopen.c ├── listmacrotest.c ├── listtest.c └── offset.c /.gitignore: -------------------------------------------------------------------------------- 1 | project/fs 2 | project/out 3 | project/mydisk 4 | test/* 5 | !test/*.c 6 | !test/*.h 7 | testfile 8 | .vscode 9 | 10 | # Prerequisites 11 | *.d 12 | 13 | # Object files 14 | *.o 15 | *.ko 16 | *.obj 17 | *.elf 18 | 19 | # Linker output 20 | *.ilk 21 | *.map 22 | *.exp 23 | 24 | # Precompiled Headers 25 | *.gch 26 | *.pch 27 | 28 | # Libraries 29 | *.lib 30 | *.a 31 | *.la 32 | *.lo 33 | 34 | # Shared objects (inc. Windows DLLs) 35 | *.dll 36 | *.so 37 | *.so.* 38 | *.dylib 39 | 40 | # Executables 41 | *.exe 42 | *.out 43 | *.app 44 | *.i*86 45 | *.x86_64 46 | *.hex 47 | 48 | # Debug files 49 | *.dSYM/ 50 | *.su 51 | *.idb 52 | *.pdb 53 | 54 | # Kernel Module Compile Results 55 | *.mod* 56 | *.cmd 57 | .tmp_versions/ 58 | modules.order 59 | Module.symvers 60 | Mkfile.old 61 | dkms.conf 62 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /project/Makefile: -------------------------------------------------------------------------------- 1 | OUTDIR=./out 2 | 3 | fs:main.o function.o disk.o list.o shell.o str.o time.o 4 | cc -o fs $(OUTDIR)/main.o $(OUTDIR)/function.o $(OUTDIR)/disk.o $(OUTDIR)/list.o $(OUTDIR)/shell.o $(OUTDIR)/str.o $(OUTDIR)/time.o 5 | 6 | main.o:main.c ./function/api.h ./shell/shell.h 7 | cc -c main.c 8 | mv main.o $(OUTDIR)/main.o 9 | 10 | function.o:./function/function.c ./function/api.h 11 | cc -c ./function/function.c 12 | mv function.o $(OUTDIR)/function.o 13 | 14 | disk.o:./util/disk.c ./util/disk.h 15 | cc -c ./util/disk.c 16 | mv disk.o $(OUTDIR)/disk.o 17 | 18 | list.o:./util/list.c ./util/list.h 19 | cc -c ./util/list.c 20 | mv list.o $(OUTDIR)/list.o 21 | 22 | shell.o:./shell/shell.c ./shell/shell.h 23 | cc -c ./shell/shell.c 24 | mv shell.o $(OUTDIR)/shell.o 25 | 26 | str.o:./util/str.c ./util/str.h 27 | cc -c ./util/str.c 28 | mv str.o $(OUTDIR)/str.o 29 | 30 | time.o:./util/time.c ./util/time.h 31 | cc -c ./util/time.c 32 | mv time.o $(OUTDIR)/time.o 33 | .PHONY:clean 34 | clean: 35 | -rm $(OUTDIR)/main.o $(OUTDIR)/function.o $(OUTDIR)/disk.o $(OUTDIR)/list.o $(OUTDIR)/shell.o $(OUTDIR)/str.o -------------------------------------------------------------------------------- /project/build.sh: -------------------------------------------------------------------------------- 1 | if test -d ./out 2 | then 3 | make 4 | else 5 | mkdir out 6 | make 7 | fi 8 | -------------------------------------------------------------------------------- /project/function/api.h: -------------------------------------------------------------------------------- 1 | #ifndef __API__ 2 | #define __API__ 3 | void startsys(); 4 | void format(); 5 | void showBlock0(); 6 | int showFAT(int start,int end); 7 | void showFCB(int blocknum,int num_in_block); 8 | void showPresentFCB(); 9 | void showBlockData(int blocknum); 10 | void showfdList(); 11 | void showBlockChain(int blocknum); 12 | int my_cd(char *dirname); 13 | int my_mkdir(char *dirname); 14 | int my_rmdir(char *dirname); 15 | void my_ls(); 16 | int my_create(char *filename); 17 | int my_rm(char *filename); 18 | int my_open(char *filename); 19 | int my_close(int fd); 20 | int my_write(int fd,int *len,char wstyle); 21 | int my_read(int fd,int *len); 22 | int my_in(int fd,char *filename,int *len); 23 | int my_out(int fd,char *filename,int *len); 24 | char *getPwd(); 25 | void exitsys(); 26 | #endif 27 | -------------------------------------------------------------------------------- /project/function/function.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include"../util/disk.h" 9 | #include"../util/time.h" 10 | #include"../global/global.h" 11 | #include"../global/define.h" 12 | void format(){//文件系统格式化 13 | //引导块 BLOCK0 14 | strcpy(block0.identify,"MYDISK"); 15 | strcpy(block0.info,"blocksize:1024B\nblocknum:1024 Disksize:1MB"); 16 | block0.root = ROOT_FCB_LOCATION; 17 | block0.startblock = DATA_INIT_BLOCK; 18 | block0.rootFCB = ROOT_FCB_LOCATION; 19 | fseek(DISK,0,SEEK_SET); 20 | fwrite(&block0,sizeof(BLOCK0),1,DISK); 21 | //两个FAT块 每个占2个磁盘块 每个FAT表项2B大小 22 | FATitem *fat,fi; 23 | fat = (FATitem *)malloc(sizeof(FATitem)*BLOCK_NUMS*2); 24 | memset(fat,0,sizeof(FATitem)); 25 | fseek(DISK,1*BLOCK_SIZE,SEEK_SET); 26 | fwrite(fat,sizeof(FATitem),BLOCK_NUMS*2,DISK); 27 | free(fat); 28 | //根目录区 存放在5号盘块 29 | struct tm *t=getTimeStruct(); 30 | FCB rootFCB; 31 | strcpy(rootFCB.name,"/"); 32 | rootFCB.type = 1; //类型-目录 33 | rootFCB.use = USED; //已使用 34 | rootFCB.time = getTime(t); 35 | rootFCB.date = getDate(t); 36 | rootFCB.base = 6; //起始盘块号 37 | rootFCB.length = 1; //长度 38 | FAT1[5].item=FCB_BLOCK; 39 | FAT2[5].item=FCB_BLOCK; 40 | addFCB(rootFCB,5); 41 | //根目录FCB 存放在6号盘块 42 | initFCBBlock(6,6); 43 | //修改对应FAT表 44 | fi.item = END_OF_FILE; 45 | for(int i=0;i<5;i++) 46 | FAT1[i].item=USED; 47 | rewriteFAT(); 48 | } 49 | 50 | void startsys(){//初始化文件系统 51 | int fd; 52 | //以创建新文件的方式打开 53 | if((fd=open(sysname,O_CREAT|O_EXCL,S_IRWXU))<0){ 54 | //打开失败(通常是已经存在这个文件) 检测失败原因 55 | if(errno==EEXIST){//文件已存在 56 | if((fd = open(sysname,O_RDWR,S_IRWXU))<0){//直接打开这个文件 57 | printf("open:%d %s\n",errno,strerror(errno));//打开失败 58 | exit(-1); 59 | } 60 | } 61 | else//其他原因 62 | exit(-1); 63 | }else{//如果采取新建一个文件,那么新建一个磁盘文件并分配空间 64 | close(fd); 65 | printf("没有找到磁盘文件,开始创建磁盘文件\n"); 66 | createDisk(); 67 | if((fd = open(sysname,O_RDWR,S_IRWXU))<0){//直接打开这个文件 68 | printf("open:%d %s\n",errno,strerror(errno));//打开失败 69 | exit(-1); 70 | } 71 | } 72 | 73 | //变成标准IO流 74 | if((DISK = fdopen(fd,"r+"))==NULL){ 75 | printf("fdopen:%d %s\n",errno,strerror(errno)); 76 | exit(-1); 77 | } 78 | char buff[10]; 79 | if(fread(&block0,sizeof(BLOCK0),1,DISK)==1)//取出第一个磁盘块(引导块) 80 | //判断是否格式化 81 | if(strcmp(block0.identify,"MYDISK")!=0){ 82 | printf("磁盘尚未格式化,开始格式化\n"); 83 | format(); 84 | } 85 | //进行其他初始化操作 86 | strcpy(pwd,"/");//设置当前目录 87 | getFCB(&presentFCB,5,0);//将根目录设置成当前内存中的FCB 88 | getFAT(FAT1,FAT1_LOCATON);//加载FAT1 89 | getFAT(FAT2,FAT2_LOCATON);//加载FAT2 90 | //初始化文件打开表 91 | for(int i=0;iend||start<0||end>FAT_ITEM_NUM){ 107 | printf("fat:invalid num\n"); 108 | return -1; 109 | } 110 | for(int i=start,j=1;ilink)){ 162 | n++; 163 | blockchain *b = list_entry(temp,struct blockchain,link); 164 | if(b->link.next!=&(blc->link)) 165 | printf("%d-",b->blocknum); 166 | else 167 | printf("%d",b->blocknum); 168 | } 169 | printf("\n"); 170 | printf("%d blocks in total\n",n); 171 | } 172 | 173 | int my_mkdir(char *dirname){ 174 | //判断文件名长度 175 | if(strlen(dirname)>FILE_NAME_LEN){ 176 | printf("mkdir: cannot create directory ‘%s’: directory name must less than %d bytes\n",dirname,FILE_NAME_LEN); 177 | return -1; 178 | } 179 | //检查是否重名 180 | if(findFCBInBlockByName(dirname,presentFCB.base)>0){ 181 | printf("mkdir: cannot create directory ‘%s’: File exists\n",dirname); 182 | return -1; 183 | } 184 | //获得当前目录表中空余位置,以便存放FCB 185 | int offset = getEmptyFCBOffset(presentFCB.base); 186 | if(offset<0){ 187 | printf("mkdir:no empty space to make directory\n"); 188 | return -1; 189 | } 190 | //获得空的盘块以便存储新的目录表 191 | int blocknum = getEmptyBlockId(); 192 | FCB fcb; 193 | if(blocknum<0){ 194 | printf("mkdir:no empty block\n"); 195 | return -1; 196 | } 197 | //构造FCB 198 | struct tm *t=getTimeStruct(); 199 | strcpy(fcb.name,dirname); 200 | fcb.type=1; 201 | fcb.use=USED; 202 | fcb.time=getTime(t); 203 | fcb.date=getDate(t); 204 | fcb.base=blocknum; 205 | fcb.length=1; 206 | initFCBBlock(blocknum,presentFCB.base); 207 | addFCB(fcb,presentFCB.base);//在当前目录表加入这个目录项 208 | rewriteFAT(); 209 | } 210 | 211 | int my_rmdir(char *dirname){ 212 | //检查是否有这个目录 213 | int offset; 214 | FCB fcb;//将要删除的FCB 215 | if((offset=findFCBInBlockByName(dirname,presentFCB.base))<0){ 216 | printf("rmdir: cannot remove '%s': No such file or directory\n",dirname); 217 | return -1; 218 | }else{ 219 | // 清空这个FCB指向的盘块 220 | getFCB(&fcb,presentFCB.base,offset); 221 | //判断是否为目录类型 222 | if(fcb.type!=1){ 223 | printf("rmdir: cannot remove '%s': Is a file, please use rm\n",dirname); 224 | return -1; 225 | } 226 | //判断是否删除的是 .或 ..目录 227 | if(strcmp(fcb.name,".")==0||strcmp(fcb.name,"..")==0){ 228 | printf("rmdir: refusing to remove '.' or '..'\n"); 229 | return -1; 230 | } 231 | //判断这个目录是否为空 232 | for(int i=0;ifcb_entry.date; 265 | time = Fnode->fcb_entry.time; 266 | printf("%-12s %-10s %4d/%02d/%02d %02d:%02d:%02d %-6d\n",Fnode->fcb_entry.name, 267 | type[Fnode->fcb_entry.type],getYear(date),getMonth(date),getDay(date), 268 | getHour(date,time),getMinute(time),getSecond(time),Fnode->fcb_entry.length); 269 | } 270 | } 271 | 272 | int my_cd(char *dirname){ 273 | int offset; 274 | if((offset=findFCBInBlockByName(dirname,presentFCB.base))<0){ 275 | printf("cd: %s: No such file or directory\n",dirname); 276 | return -1; 277 | }else{ 278 | FCB fcb; 279 | getFCB(&fcb,presentFCB.base,offset); 280 | if(fcb.type==0){ 281 | printf("cd: %s: Not a directory\n",dirname); 282 | return -1; 283 | } 284 | presentFCB=fcb;//修改当前fcb值 285 | if(strcmp(dirname,".")==0)//当前目录 286 | ; 287 | else if(strcmp(dirname,"..")==0){//上一级目录 288 | if(strcmp(pwd,"/")!=0){//不是根目录情况 289 | char *a = strchr(pwd,'/');//从左往右第一次出现/的位置 290 | char *b = strrchr(pwd,'/');//从右往左第一次出现/的位置 291 | if(a!=b)//判断是否只有一个/字符 不相等则有多个 292 | *b='\0'; 293 | else 294 | *(b+1)='\0'; 295 | } 296 | } 297 | else{//下一级目录 298 | if(strcmp(pwd,"/")!=0) 299 | strcat(pwd,"/"); 300 | strcat(pwd,dirname); 301 | } 302 | return 0; 303 | } 304 | } 305 | 306 | int my_create(char *filename){ 307 | //判断文件名长度 308 | if(strlen(filename)>FILE_NAME_LEN){ 309 | printf("create: cannot create file ‘%s’: file name must less than %d bytes\n",filename,FILE_NAME_LEN); 310 | return -1; 311 | } 312 | int offset = findFCBInBlockByName(filename,presentFCB.base); 313 | if(offset>0){//判断是否已经存在此文件 314 | printf("create: cannot create file ‘%s’: File exists\n",filename); 315 | return -1; 316 | }else{ 317 | offset = getEmptyFCBOffset(presentFCB.base);//判断FCB块是否有剩余空间加入FCB 318 | if(offset<0){ 319 | printf("create: cannot create file ‘%s’: %s Lack of space\n",pwd,filename); 320 | return -1; 321 | }else{ 322 | int blocknum = getEmptyBlockId(); 323 | if(blocknum<0){ 324 | printf("create: cannot create file ‘%s’: %s Lack of space\n",sysname,filename); 325 | return -1; 326 | }else{ 327 | //构建FCB 328 | struct tm *t=getTimeStruct(); 329 | FCB fcb; 330 | strcpy(fcb.name,filename); 331 | fcb.type=0; 332 | fcb.use=USED; 333 | fcb.time=getTime(t); 334 | fcb.date=getDate(t); 335 | fcb.base=blocknum; 336 | fcb.length=1; 337 | addFCB(fcb,presentFCB.base); 338 | //修改FAT 339 | FAT1[blocknum].item=END_OF_FILE; 340 | FAT2[blocknum].item=END_OF_FILE; 341 | rewriteFAT(); 342 | return 0; 343 | } 344 | } 345 | } 346 | return 0; 347 | } 348 | 349 | int my_rm(char *filename){ 350 | int offset = findFCBInBlockByName(filename,presentFCB.base); 351 | if(offset<0){//判断是否已经存在此文件 352 | printf("rm: cannot remove '%s': No such file\n",filename); 353 | return -1; 354 | }else{ 355 | FCB fcb; 356 | getFCB(&fcb,presentFCB.base,offset); 357 | //判断文件类型 358 | if(fcb.type==1){ 359 | printf("rm: cannot remove '%s': Is a directory\n",filename); 360 | return -1; 361 | } 362 | //修改FAT 363 | blockchain *blc; 364 | lslink *temp; 365 | blc = getBlockChain(fcb.base); 366 | list_for_each(temp,&(blc->link)){ 367 | blockchain *b = list_entry(temp,struct blockchain,link); 368 | FAT1[b->blocknum].item=FREE; 369 | FAT2[b->blocknum].item=FREE; 370 | } 371 | FAT1[fcb.base].item=FREE; 372 | FAT2[fcb.base].item=FREE; 373 | rewriteFAT(); 374 | //删除FCB 375 | removeFCB(presentFCB.base,offset); 376 | int fd = findfdByNameAndDir(filename,pwd); 377 | if(fd>=0&&uopenlist[fd].topenfile==USED) 378 | uopenlist[fd].topenfile=FREE; 379 | return 0; 380 | } 381 | } 382 | 383 | int my_open(char *filename){ 384 | int fd; 385 | fd = findfdByNameAndDir(filename,pwd); 386 | if(fd>=0&&uopenlist[fd].topenfile==USED){//判断是否已经打开 387 | printf("open: cannot open file ‘%s’: %s is already open\n",filename,filename); 388 | return -1; 389 | } 390 | if((fd=getEmptyfd())<0){//查看是否有空的fd 391 | printf("open: cannot open file ‘%s’: Lack of empty fd\n",filename); 392 | return -1; 393 | }else{ 394 | int offset = findFCBInBlockByName(filename,presentFCB.base);//获得fcb位置 395 | if(offset<0){ 396 | printf("open: %s: No such file or directory\n",filename); 397 | return -1; 398 | }else{ 399 | //构造打开表项 400 | FCB fcb; 401 | getFCB(&fcb,presentFCB.base,offset); 402 | uopenlist[fd].fcb = fcb; 403 | strcpy(uopenlist[fd].dir,pwd); 404 | uopenlist[fd].count = BLOCK_SIZE*fcb.base+0; 405 | uopenlist[fd].fcbstate = 0; 406 | uopenlist[fd].topenfile = USED; 407 | uopenlist[fd].blocknum = presentFCB.base; 408 | uopenlist[fd].offset_in_block = findFCBInBlockByName(filename,presentFCB.base); 409 | printf("filename:%s fd:%d\n",filename,fd); 410 | return 0; 411 | } 412 | } 413 | } 414 | 415 | int my_close(int fd){ 416 | if(fd>=MAX_FD_NUM||fd<0){ 417 | printf("close: invalid fd\n"); 418 | return -1; 419 | }else{ 420 | if(uopenlist[fd].topenfile==FREE){//判断是否已经关闭 421 | printf("close: cannot close fd ‘%d’: fd %d is already close\n",fd,fd); 422 | return -1; 423 | } 424 | if(uopenlist[fd].fcbstate==1)//fcb被修改了 425 | changeFCB(uopenlist[fd].fcb,uopenlist[fd].blocknum,uopenlist[fd].offset_in_block); 426 | uopenlist[fd].topenfile=FREE;//清空文件打开表项 427 | return 0; 428 | } 429 | } 430 | 431 | //wstyle w-截断写 a-追加写 c-覆盖写 432 | int my_write(int fd,int *sumlen,char wstyle){ 433 | if(fd>=MAX_FD_NUM||fd<0){//判断fd合法性 434 | printf("close: invalid fd\n"); 435 | return -1; 436 | }else{ 437 | if(uopenlist[fd].topenfile==FREE){//判断是否已经关闭 438 | printf("write: cannot write to fd ‘%d’: fd %d is already close\n",fd,fd); 439 | return -1; 440 | }else{ 441 | if(uopenlist[fd].fcb.type==1){//判断如果是目录 442 | printf("write: cannot write to fd ‘%d’: fd %d is a directory\n",fd,fd); 443 | return -1; 444 | } 445 | char str[BLOCK_SIZE],buff[BLOCK_SIZE]; 446 | int blocknum,nextblocknum; 447 | int len,bloffset;//len-一次读取的长度 bloffset-文件指针块内偏移量 448 | *sumlen=0;//sumlen-总长(所有输入长度之和) 449 | memset(str,0,BLOCK_SIZE); 450 | memset(buff,0,BLOCK_SIZE); 451 | //截断写 将文件长度截断成0写 452 | if(wstyle=='w'){ 453 | bloffset = 0; 454 | blocknum = uopenlist[fd].fcb.base; 455 | //做截断处理 456 | if(FAT1[blocknum].item!=END_OF_FILE){ 457 | blockchain *blc = getBlockChain(blocknum); 458 | lslink *temp; 459 | list_for_each(temp,&(blc->link)){ 460 | blockchain *b = list_entry(temp,struct blockchain,link); 461 | //清空对应FAT块 462 | FAT1[b->blocknum].item=FREE; 463 | FAT2[b->blocknum].item=FREE; 464 | uopenlist[fd].fcb.length=0; 465 | } 466 | } 467 | //循环读取直到EOF 每次最多读取一个盘块大小的内容 多余部分留在缓冲区作为下次读取 468 | while(fgets(str,BLOCK_SIZE,stdin)!=NULL){ 469 | len = strlen(str);//记录实际读取到的长度 470 | //printf("len %d\n",len); 471 | if(bloffset+len=MAX_FD_NUM||fd<0){ 647 | printf("read: invalid fd\n"); 648 | return -1; 649 | }else{ 650 | if(uopenlist[fd].topenfile==FREE){//判断是否已经关闭 651 | printf("read: cannot read to fd ‘%d’: fd %d is already close\n",fd,fd); 652 | return -1; 653 | }else{ 654 | if(uopenlist[fd].fcb.type==1){//判断如果是目录 655 | printf("read: cannot read to fd ‘%d’: fd %d is a directory\n",fd,fd); 656 | return -1; 657 | } 658 | char buff[BLOCK_SIZE]; 659 | int blocknum = uopenlist[fd].fcb.base; 660 | while(blocknum!=END_OF_FILE){ 661 | readFromDisk(DISK,buff,BLOCK_SIZE,blocknum*BLOCK_SIZE,0); 662 | *sumlen += strlen(buff); 663 | fputs(buff,stdout); 664 | blocknum=FAT1[blocknum].item; 665 | } 666 | return 0; 667 | } 668 | } 669 | } 670 | 671 | int my_in(int fd,char *filename,int *sumlen){ 672 | if(fd>=MAX_FD_NUM||fd<0){//判断fd合法性 673 | printf("close: invalid fd\n"); 674 | return -1; 675 | }else{ 676 | if(uopenlist[fd].topenfile==FREE){//判断是否已经关闭 677 | printf("write: cannot write to fd ‘%d’: fd %d is already close\n",fd,fd); 678 | return -1; 679 | }else{ 680 | if(uopenlist[fd].fcb.type==1){//判断如果是目录 681 | printf("write: cannot write to fd ‘%d’: fd %d is a directory\n",fd,fd); 682 | return -1; 683 | } 684 | char buff[BLOCK_SIZE]; 685 | int blocknum,nextblocknum; 686 | int len,bloffset;//len-一次读取的长度 bloffset-文件指针块内偏移量 687 | FILE *f = fopen(filename,"rb"); 688 | if(f==NULL){ 689 | printf("open file %s failure\n",filename); 690 | return -1; 691 | } 692 | *sumlen=0;//sumlen-总长(所有输入长度之和) 693 | memset(buff,0,BLOCK_SIZE); 694 | //截断写 将文件长度截断成0写 695 | blocknum = uopenlist[fd].fcb.base; 696 | //做截断处理 697 | if(FAT1[blocknum].item!=END_OF_FILE){ 698 | blockchain *blc = getBlockChain(blocknum); 699 | lslink *temp; 700 | list_for_each(temp,&(blc->link)){ 701 | blockchain *b = list_entry(temp,struct blockchain,link); 702 | //清空对应FAT块 703 | FAT1[b->blocknum].item=FREE; 704 | FAT2[b->blocknum].item=FREE; 705 | uopenlist[fd].fcb.length=0; 706 | } 707 | } 708 | //循环读取直到EOF 每次最多读取一个盘块大小的内容 多余部分留在缓冲区作为下次读取 709 | while(!feof(f)){ 710 | len=fread(buff,1,BLOCK_SIZE,f); 711 | if(len=MAX_FD_NUM||fd<0){ 753 | printf("read: invalid fd\n"); 754 | return -1; 755 | }else{ 756 | if(uopenlist[fd].topenfile==FREE){//判断是否已经关闭 757 | printf("read: cannot read to fd ‘%d’: fd %d is already close\n",fd,fd); 758 | return -1; 759 | }else{ 760 | if(uopenlist[fd].fcb.type==1){//判断如果是目录 761 | printf("read: cannot read to fd ‘%d’: fd %d is a directory\n",fd,fd); 762 | return -1; 763 | } 764 | FILE *f = fopen(filename,"wb"); 765 | char buff[BLOCK_SIZE]; 766 | int blocknum = uopenlist[fd].fcb.base; 767 | int length = uopenlist[fd].fcb.length; 768 | while(FAT1[blocknum].item!=END_OF_FILE){ 769 | readFromDisk(DISK,buff,BLOCK_SIZE,blocknum*BLOCK_SIZE,0); 770 | *sumlen += BLOCK_SIZE; 771 | fwrite(buff,BLOCK_SIZE,1,f); 772 | blocknum=FAT1[blocknum].item; 773 | } 774 | readFromDisk(DISK,buff,BLOCK_SIZE,blocknum*BLOCK_SIZE,0); 775 | *sumlen += length%BLOCK_SIZE; 776 | fwrite(buff,length%BLOCK_SIZE,1,f); 777 | fclose(f); 778 | return 0; 779 | } 780 | } 781 | } 782 | void showfdList(){ 783 | int num=0; 784 | for(int i=0;i 2 | #ifndef __GLOBAL__ 3 | #define __GLOBAL__ 4 | extern char sysname[]; 5 | extern FILE * DISK; 6 | extern BLOCK0 block0; 7 | extern char pwd[]; 8 | extern FCB presentFCB; 9 | extern FATitem FAT1 []; 10 | extern FATitem FAT2 []; 11 | extern useropen uopenlist[]; 12 | extern char * type[]; 13 | #endif -------------------------------------------------------------------------------- /project/global/var.h: -------------------------------------------------------------------------------- 1 | #ifndef __VAR__ 2 | #define __VAR__ 3 | #include 4 | #include"define.h" 5 | #include"../util/list.h" 6 | typedef int Status; 7 | typedef unsigned char byte; 8 | typedef struct BLOCK0{ 9 | char identify[10]; 10 | char info[200]; 11 | unsigned short root;//根目录块号 12 | int startblock;//数据块号 13 | int rootFCB;//根目录FCB位置 14 | }BLOCK0; 15 | 16 | typedef struct FCB{ //16B 17 | char name[FILE_NAME_LEN]; 18 | unsigned type:1;//标志文件类型 1-目录 0-文件 19 | unsigned use:1;//标志使用状态 1-已使用(USED) 0-未使用(FREE) 20 | unsigned short time; 21 | unsigned short date; 22 | unsigned int base;//文件起始盘块 23 | unsigned int length;//文件长度 24 | }FCB; 25 | 26 | typedef struct FCBList{ 27 | FCB fcb_entry; 28 | lslink link; 29 | }FCBList; 30 | 31 | typedef struct FATitem{//FAT表项 2B -32767-32768 32 | signed short item:16; 33 | }FATitem; 34 | 35 | typedef struct useropen{ 36 | FCB fcb; 37 | char dir[80]; 38 | unsigned int count;//文件指针的位置 39 | unsigned fcbstate:1;//标志fcb是否被修改 1-已修改 0-未修改 40 | unsigned topenfile:1;//标志使用状态 1-已使用(USED) 0-未使用(FREE) 41 | int blocknum;//所在块号 42 | int offset_in_block;//所在块号偏移量 43 | }useropen; 44 | 45 | //盘块链 46 | typedef struct blockchain{ 47 | signed short blocknum:16; 48 | lslink link; 49 | }blockchain; 50 | #endif 51 | 52 | -------------------------------------------------------------------------------- /project/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include"function/api.h" 3 | #include"global/var.h" 4 | #include"util/disk.h" 5 | #include"shell/shell.h" 6 | #include"util/time.h" 7 | char sysname[20]="mydisk"; 8 | char pwd[80]; 9 | FILE * DISK; 10 | BLOCK0 block0; 11 | FATitem FAT1[FAT_ITEM_NUM]; 12 | FATitem FAT2[FAT_ITEM_NUM]; 13 | FCB presentFCB; 14 | useropen uopenlist[MAX_FD_NUM]; 15 | char * type[2]={"file","directory"}; 16 | int main() 17 | { 18 | startsys(); 19 | go(); 20 | return 0; 21 | } -------------------------------------------------------------------------------- /project/shell/shell.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include"shell.h" 5 | #include"../global/global.h" 6 | #include"../util/str.h" 7 | #include"../util/time.h" 8 | #include"../function/api.h" 9 | char * header(){ 10 | char *buff; 11 | buff = (char *)malloc(sizeof(char)*100); 12 | sprintf(buff,"\033[33m\033[01m%s\033[0m:\033[36m\033[01m%s\033[0m$ ",sysname,pwd); 13 | return buff; 14 | } 15 | char ** getInstruction(int *argc){ 16 | char *buff; 17 | char **Ins; 18 | buff = (char *)malloc(sizeof(char)*100); 19 | Ins = (char **)malloc(sizeof(char *)*10); 20 | for(int i=0;i<10;i++) 21 | Ins[i] = (char *)malloc(sizeof(char)*10); 22 | printf("%s",header()); 23 | fgets(buff,100,stdin); 24 | buff[strlen(buff)-1]='\0'; 25 | buff = trim(buff); 26 | Ins = split(buff," ",argc); 27 | return Ins; 28 | } 29 | void help(){ 30 | printf("**********************HELP**********************\n"); 31 | printf("%-10s - %s\n","exit","exit system"); 32 | printf("%-10s - %s\n","pwd","print name of current/working directory"); 33 | printf("%-10s - %s\n","ls","list directory contents"); 34 | printf("%-10s - %s\n","mkdir","make directories"); 35 | printf("%-10s - %s\n","cd","change working directory"); 36 | printf("%-10s - %s\n","create","create new file"); 37 | printf("%-10s - %s\n","rm","remove files"); 38 | printf("%-10s - %s\n","rmdir","remove directory"); 39 | printf("%-10s - %s\n","open","open a file"); 40 | printf("%-10s - %s\n","close","close a file"); 41 | printf("%-10s - %s\n","write","write to fd"); 42 | printf("%-10s - %s\n","read","read from fd"); 43 | printf("%-10s - %s\n","opl","show open file list"); 44 | 45 | printf("******************For Developer******************\n"); 46 | printf("%-10s - %s\n","block0","show block0 information"); 47 | printf("%-10s - %s\n","fat","show fat"); 48 | printf("%-10s - %s\n","fcb","show a fcb information use blocknum and offset"); 49 | printf("%-10s - %s\n","pfcb","show current fcb"); 50 | printf("%-10s - %s\n","sbc","show block chain"); 51 | printf("%-10s - %s\n","sbd","show data in block"); 52 | printf("%-10s - %s\n","in","read file to disk from outer file"); 53 | printf("%-10s - %s\n","out","write file from disk to outer file"); 54 | printf("%-10s - %s\n","time","show current time"); 55 | } 56 | int doOpration(int argc,char ** argv){ 57 | //printf("%d **%s**\n",argc,argv[0]); 58 | if(strcmp(argv[0],"help")==0){ 59 | if(argc>1){ 60 | printf("%s : too many arguments\n",argv[0]); 61 | return -1; 62 | } 63 | else{ 64 | help(); 65 | return 0; 66 | } 67 | } 68 | 69 | if(strcmp(argv[0],"exit")==0){ 70 | if(argc>1){ 71 | printf("%s : too many arguments\n",argv[0]); 72 | return -1; 73 | } 74 | else{ 75 | exitsys(); 76 | return -2; 77 | } 78 | } 79 | 80 | if(strcmp(argv[0],"pwd")==0){ 81 | if(argc>1){ 82 | printf("%s : too many arguments\n",argv[0]); 83 | return -1; 84 | } 85 | else{ 86 | printf("%s\n",getPwd()); 87 | return 0; 88 | } 89 | } 90 | 91 | if(strcmp(argv[0],"ls")==0){ 92 | if(argc>1){ 93 | printf("%s : too many arguments\n",argv[0]); 94 | return -1; 95 | } 96 | else{ 97 | my_ls(); 98 | return 0; 99 | } 100 | } 101 | 102 | if(strcmp(argv[0],"mkdir")==0){ 103 | if(argc!=2){ 104 | printf("usage %s [directory name]\n",argv[0]); 105 | return -1; 106 | } 107 | else{ 108 | my_mkdir(argv[1]); 109 | return 0; 110 | } 111 | } 112 | 113 | if(strcmp(argv[0],"cd")==0){ 114 | if(argc!=2){ 115 | printf("usage %s [directory name]\n",argv[0]); 116 | return -1; 117 | } 118 | else{ 119 | my_cd(argv[1]); 120 | return 0; 121 | } 122 | } 123 | 124 | if(strcmp(argv[0],"create")==0){ 125 | if(argc!=2){ 126 | printf("usage %s [file name]\n",argv[0]); 127 | return -1; 128 | } 129 | else{ 130 | my_create(argv[1]); 131 | return 0; 132 | } 133 | } 134 | 135 | if(strcmp(argv[0],"rm")==0){ 136 | if(argc!=2){ 137 | printf("usage %s [file name]\n",argv[0]); 138 | return -1; 139 | } 140 | else{ 141 | my_rm(argv[1]); 142 | return 0; 143 | } 144 | } 145 | 146 | if(strcmp(argv[0],"rmdir")==0){ 147 | if(argc!=2){ 148 | printf("usage %s [directory name]\n",argv[0]); 149 | return -1; 150 | } 151 | else{ 152 | my_rmdir(argv[1]); 153 | return 0; 154 | } 155 | } 156 | 157 | if(strcmp(argv[0],"open")==0){ 158 | if(argc!=2){ 159 | printf("usage %s [file name]\n",argv[0]); 160 | return -1; 161 | } 162 | else{ 163 | my_open(argv[1]); 164 | return 0; 165 | } 166 | } 167 | 168 | if(strcmp(argv[0],"close")==0){ 169 | if(argc!=2){ 170 | printf("usage %s [fd num]\n",argv[0]); 171 | return -1; 172 | } 173 | else{ 174 | int a; 175 | a = atoi(argv[1]); 176 | if(strcmp(argv[1],"0")&&a==0){ 177 | printf("usage %s [fd num]\n",argv[0]); 178 | return -1; 179 | } 180 | my_close(a); 181 | return 0; 182 | } 183 | } 184 | 185 | if(strcmp(argv[0],"write")==0){ 186 | if(argc!=3){ 187 | printf("usage %s [fd] [write method]\n",argv[0]); 188 | return -1; 189 | } 190 | else{ 191 | int a1,len=0; 192 | char a2; 193 | //printf("a1%s a2%s\n",argv[1],argv[2]); 194 | a1 = atoi(argv[1]); 195 | a2 = argv[2][0]; 196 | if((strcmp(argv[1],"0")&&a1==0)){ 197 | printf("usage %s [fd] [write method]\n",argv[0]); 198 | return -1; 199 | } 200 | if(strlen(argv[1])!=1){ 201 | printf("usage %s [fd] [write method]\n",argv[0]); 202 | return -1; 203 | } 204 | if(my_write(a1,&len,a2)==0){ 205 | printf("succeed write to fd %d with %d bytes\n",a1,len); 206 | return 0; 207 | } 208 | return 0; 209 | } 210 | } 211 | 212 | if(strcmp(argv[0],"read")==0){ 213 | if(argc!=2){ 214 | printf("usage %s [fd num]\n",argv[0]); 215 | return -1; 216 | } 217 | else{ 218 | int a,len=0; 219 | a = atoi(argv[1]); 220 | if(strcmp(argv[1],"0")&&a==0){ 221 | printf("usage %s [fd num]\n",argv[0]); 222 | return -1; 223 | } 224 | if(my_read(a,&len)==0) 225 | printf("read fd %d with %d bytes\n",a,len); 226 | return 0; 227 | } 228 | } 229 | 230 | if(strcmp(argv[0],"block0")==0){ 231 | if(argc>1){ 232 | printf("%s : too many arguments\n",argv[0]); 233 | return -1; 234 | } 235 | else{ 236 | showBlock0(); 237 | return 0; 238 | } 239 | } 240 | 241 | if(strcmp(argv[0],"fat")==0){ 242 | if(argc!=3){ 243 | printf("usage %s [blocknum start][blocknum end]\n",argv[0]); 244 | return -1; 245 | } 246 | else{ 247 | int a1,a2; 248 | //printf("a1%s a2%s\n",argv[1],argv[2]); 249 | a1 = atoi(argv[1]); 250 | a2 = atoi(argv[2]); 251 | if((strcmp(argv[1],"0")&&a1==0)||(strcmp(argv[2],"0")&&a2==0)){ 252 | printf("usage %s [blocknum start][blocknum end]\n",argv[0]); 253 | return -1; 254 | } 255 | showFAT(a1,a2); 256 | return 0; 257 | } 258 | } 259 | 260 | if(strcmp(argv[0],"fcb")==0){ 261 | if(argc!=3){ 262 | printf("usage %s [blocknum] [FCB offset in block]\n",argv[0]); 263 | return -1; 264 | } 265 | else{ 266 | int a1,a2; 267 | //printf("a1%s a2%s\n",argv[1],argv[2]); 268 | a1 = atoi(argv[1]); 269 | a2 = atoi(argv[2]); 270 | if((strcmp(argv[1],"0")&&a1==0)||(strcmp(argv[2],"0")&&a2==0)){ 271 | printf("usage %s [blocknum] [FCB offset in block]\n",argv[0]); 272 | return -1; 273 | } 274 | showFCB(a1,a2); 275 | return 0; 276 | } 277 | } 278 | 279 | if(strcmp(argv[0],"opl")==0){ 280 | if(argc>1){ 281 | printf("%s : too many arguments\n",argv[0]); 282 | return -1; 283 | } 284 | else{ 285 | showfdList(); 286 | return 0; 287 | } 288 | } 289 | 290 | if(strcmp(argv[0],"pfcb")==0){ 291 | if(argc>1){ 292 | printf("%s : too many arguments\n",argv[0]); 293 | return -1; 294 | } 295 | else{ 296 | showPresentFCB(); 297 | return 0; 298 | } 299 | } 300 | 301 | if(strcmp(argv[0],"sbc")==0){ 302 | if(argc!=2){ 303 | printf("usage %s [blocknum]\n",argv[0]); 304 | return -1; 305 | } 306 | else{ 307 | int a; 308 | a = atoi(argv[1]); 309 | if(strcmp(argv[1],"0")&&a==0){ 310 | printf("usage %s [blocknum]\n",argv[0]); 311 | return -1; 312 | } 313 | showBlockChain(a); 314 | return 0; 315 | } 316 | } 317 | 318 | if(strcmp(argv[0],"sbd")==0){ 319 | if(argc!=2){ 320 | printf("usage %s [blocknum]\n",argv[0]); 321 | return -1; 322 | } 323 | else{ 324 | int a; 325 | a = atoi(argv[1]); 326 | if(strcmp(argv[1],"0")&&a==0){ 327 | printf("usage %s [blocknum]\n",argv[0]); 328 | return -1; 329 | } 330 | showBlockData(a); 331 | return 0; 332 | } 333 | } 334 | 335 | if(strcmp(argv[0],"in")==0){ 336 | if(argc!=3){ 337 | printf("usage %s [fd] [outer filename]\n",argv[0]); 338 | return -1; 339 | } 340 | else{ 341 | int a1,len=0; 342 | char *a2; 343 | //printf("a1%s a2%s\n",argv[1],argv[2]); 344 | a1 = atoi(argv[1]); 345 | a2 = argv[2]; 346 | if((strcmp(argv[1],"0")&&a1==0)){ 347 | printf("usage %s [fd] [outer filename]\n",argv[0]); 348 | return -1; 349 | } 350 | if(strlen(argv[1])!=1){ 351 | printf("usage %s [fd] [write method]\n",argv[0]); 352 | return -1; 353 | } 354 | if(my_in(a1,a2,&len)==0){ 355 | printf("succeed write to fd %d with %d bytes\n",a1,len); 356 | return 0; 357 | } 358 | return 0; 359 | } 360 | } 361 | 362 | if(strcmp(argv[0],"out")==0){ 363 | if(argc!=3){ 364 | printf("usage %s [fd num] [outer filename]\n",argv[0]); 365 | return -1; 366 | } 367 | else{ 368 | int a,len=0; 369 | a = atoi(argv[1]); 370 | if(strcmp(argv[1],"0")&&a==0){ 371 | printf("usage %s [fd num] [outer filename]\n",argv[0]); 372 | return -1; 373 | } 374 | if(my_out(a,argv[2],&len)==0) 375 | printf("read fd %d with %d bytes\n",a,len); 376 | return 0; 377 | } 378 | } 379 | 380 | if(strcmp(argv[0],"time")==0){ 381 | if(argc>1){ 382 | printf("%s : too many arguments\n",argv[0]); 383 | return -1; 384 | } 385 | else{ 386 | showCurrentTime(); 387 | return 0; 388 | } 389 | } 390 | printf("%s: command not found\n",argv[0]); 391 | return 0; 392 | } 393 | void go(){ 394 | char buff[100]; 395 | char **argv; 396 | int argc,flag; 397 | argv = (char **)malloc(sizeof(char *)*10); 398 | for(int i=0;i<10;i++) 399 | argv[i] = (char *)malloc(sizeof(char)*10); 400 | while(1){ 401 | argv = getInstruction(&argc); 402 | flag = doOpration(argc,argv); 403 | //printf("flag%d\n",flag); 404 | if(flag==-2) 405 | break; 406 | } 407 | return; 408 | } -------------------------------------------------------------------------------- /project/shell/shell.h: -------------------------------------------------------------------------------- 1 | #ifndef __SHELL__ 2 | #define __SHELL__ 3 | #include"../global/var.h" 4 | char * header(); 5 | char ** getInstruction(int *argc); 6 | void go(); 7 | #endif -------------------------------------------------------------------------------- /project/util/disk.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include"disk.h" 4 | #include"../global/global.h" 5 | #include"list.h" 6 | #include"time.h" 7 | void createDisk(){ 8 | char buff[BLOCK_SIZE]={'0'}; 9 | FILE *f = fopen(sysname,"r+"); 10 | for(int i=0;ifcb_entry = fcblist[i]; 142 | list_insert(fcblisthead,&(temp->link),temp); 143 | } 144 | } 145 | } 146 | 147 | int getFCBNum(int blocknum){ 148 | int num=0; 149 | FCB fcblist[FCB_ITEM_NUM]; 150 | readFromDisk(DISK,&fcblist,sizeof(FCB)*FCB_ITEM_NUM,blocknum*BLOCK_SIZE,0); 151 | for(int i=0;ilink),blc); 211 | if(num==1||num==2||num==-1){//磁盘信息块或者是FCB块掠过 212 | first = get_node(struct blockchain); 213 | first->blocknum = blocknum; 214 | list_insert(&(blc->link),&(first->link),blc); 215 | return blc; 216 | } 217 | while(num!=-1){ 218 | blockchain *temp; 219 | temp = get_node(struct blockchain); 220 | temp->blocknum = num; 221 | list_insert(&(blc->link),&(temp->link),blc); 222 | num = FAT1[num].item; 223 | } 224 | return blc; 225 | } -------------------------------------------------------------------------------- /project/util/disk.h: -------------------------------------------------------------------------------- 1 | #include 2 | #ifndef __DISK__ 3 | #define __DISK__ 4 | #include"../global/var.h" 5 | void createDisk(); 6 | int writeToDisk(FILE* DISK,void *ptr,int size,int base,long offset); 7 | int readFromDisk(FILE* DISK,void *buff,int size,int base,long offset); 8 | int getFAT(FATitem * fat,int fat_location); 9 | int changeFAT(FATitem *fat,int fat_location); 10 | void reloadFAT(); 11 | void rewriteFAT(); 12 | int initFCBBlock(int blocknum,int parentblocknum); 13 | int addFCB(FCB fcb,int blocknum); 14 | int changeFCB(FCB newfcb,int blocknum,int offset_in_block); 15 | int removeFCB(int blocknum,int offset_in_block); 16 | int findFCBInBlockByName(char *name,int blocknum); 17 | int getEmptyFCBOffset(int blocknum); 18 | int getFCB(FCB *fcb,int blocknum,int offset_in_block); 19 | int getFCBList(int blocknum,FCBList FLstruct,lslink *fcblisthead); 20 | int getFCBNum(int blocknum); 21 | int getEmptyBlockId(); 22 | int getOpenNum(); 23 | int getEmptyfd(); 24 | int findfdByNameAndDir(char *filename,char *dirname); 25 | int getNextBlocknum(int blocknum); 26 | blockchain* getBlockChain(int blocknum); 27 | #endif -------------------------------------------------------------------------------- /project/util/list.c: -------------------------------------------------------------------------------- 1 | #include"list.h" 2 | /* list_init(lslink *headptr)初始化一个链表 3 | ** headptr - 结点的lslink成员指针 structptr - 结点的首地址*/ 4 | int list_init(lslink *headptr,void *structptr){ 5 | headptr->next = headptr; 6 | headptr->prev = headptr; 7 | headptr->sptr = structptr; 8 | return 0; 9 | } 10 | 11 | /* list_insert(lslink * headptr,lslink * nodeptr)在headptr所在节点前插入一个结点 12 | ** headptr - 结点的lslink成员指针(作为头节点) 13 | ** nodeptr - 结点的lslink成员指针(作为插入的新节点) 14 | ** structptr - 结点的首地址*/ 15 | int list_insert(lslink * headptr,lslink * nodeptr,void *structptr){ 16 | nodeptr->sptr = structptr; 17 | nodeptr->next = headptr; 18 | nodeptr->prev = headptr->prev; 19 | headptr->prev->next = nodeptr; 20 | headptr->prev = nodeptr; 21 | return 0; 22 | } 23 | 24 | /* list_unlink(lslink * nodeptr)将当前结点从链表中分离 25 | ** nodeptr - 结点的lslink成员指针*/ 26 | void list_unlink(lslink * nodeptr){ 27 | if(nodeptr->next!=nodeptr){//判断是否只剩下一个结点 28 | nodeptr->next->prev = nodeptr->prev; 29 | nodeptr->prev->next = nodeptr->next; 30 | } 31 | return; 32 | } 33 | 34 | /* list_destroy(lslink * headptr)销毁整个链表 35 | ** headptr - 结点的lslink成员指针(作为头节点)*/ 36 | void list_destroy(lslink * headptr){ 37 | lslink * temp=headptr->next,*next; 38 | while(temp!=headptr){ 39 | next = temp->next; 40 | free(temp->sptr); 41 | temp = next; 42 | } 43 | free(headptr->sptr); 44 | return; 45 | } -------------------------------------------------------------------------------- /project/util/list.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIST__ 2 | #define __LIST__ 3 | #include 4 | #include"../global/define.h" 5 | //某类结构包含link结构,即可有条件形成一个链表。 6 | //以下是其结构 7 | typedef struct lslink lslink; 8 | typedef struct lslink{ 9 | void * sptr;//用来存放包含lslink的结构的首地址 方便销毁链表 10 | lslink *next; 11 | lslink *prev; 12 | }lslink; 13 | /* offset(TYPE,MEMBER)计算结构成员偏移量 14 | ** TYPE - 数据类型 MEMBER - 成员名*/ 15 | #define offset(TYPE,MEMBER) ((size_t)&((TYPE *)0)->MEMBER) 16 | 17 | /* list_entry(ptr,TYPE,MEMBER) 18 | ** ptr - 结构中lslink类型成员的地址 TYPE - 数据类型 MEMBER - 成员名*/ 19 | #define list_entry(ptr,TYPE,MEMBER) (void *)ptr-offset(TYPE,MEMBER) //一定要有void * 20 | 21 | /* list_for_each(pos, headptr)正向遍历链表 22 | ** pos - 临时变量(lslink *类型) headptr - 链表头指针(lslink *类型)(传进来的这个指针当作链表头)*/ 23 | #define list_for_each(pos, headptr) \ 24 | for (pos = (headptr)->next; pos != (headptr); pos = pos->next) 25 | 26 | /* list_for_each_reverse(pos, headptr)反向遍历链表 27 | ** pos - 临时变量(lslink *类型) headptr - 链表头指针(lslink *类型)(传进来的这个指针当作链表头)*/ 28 | #define list_for_each_reverse(pos, headptr) \ 29 | for (pos = (headptr)->prev; pos != (headptr); pos = pos->prev) 30 | 31 | /* get_node(TYPE)初始化一个TYPE类型的结点 32 | ** TYPE - 数据类型*/ 33 | #define get_node(TYPE) (TYPE *)malloc(sizeof(TYPE)); 34 | 35 | /* list_delete(linkptr,structptr)删除一个结点 36 | ** linkptr - 结点的lslink成员指针 structptr - 结点的结构体指针 37 | ** 注意:删除表头的行为要多加小心*/ 38 | #define list_delete(linkptr,structptr) \ 39 | list_unlink(linkptr); \ 40 | free(structptr); \ 41 | structptr = NULL 42 | 43 | int list_init(lslink *headptr,void *structptr); 44 | int list_insert(lslink * headptr,lslink * nodeptr,void *structptr); 45 | void list_unlink(lslink * nodeptr); 46 | void list_destroy(lslink * headptr); 47 | #endif 48 | -------------------------------------------------------------------------------- /project/util/listmacro.h: -------------------------------------------------------------------------------- 1 | /**************************************************/ 2 | /* 已废弃 */ 3 | /* 无法正常使用 */ 4 | /**************************************************/ 5 | #include 6 | typedef struct list_head list_head; 7 | typedef struct list_head{ 8 | list_head *next; 9 | list_head *prev; 10 | }list_head; 11 | /* offset(TYPE,MEMBER)计算结构成员偏移量 12 | ** TYPE - 数据类型 MEMBER - 成员名*/ 13 | #define offset(TYPE,MEMBER) ((size_t)&((TYPE *)0)->MEMBER) 14 | 15 | /* list_entry(ptr,TYPE,MEMBER) 16 | ** ptr - 结构中head的地址 TYPE - 数据类型 MEMBER - 成员名*/ 17 | #define list_entry(ptr,TYPE,MEMBER) (void *)ptr-offset(TYPE,MEMBER) //一定要有void * 18 | 19 | /* list_for_each(pos, head)遍历链表 20 | ** pos - 临时变量(list_head *类型) head - 链表头指针(list_head *类型)*/ 21 | #define list_for_each(pos, headptr) \ 22 | for (pos = (headptr)->next; pos != (headptr); pos = pos->next) 23 | 24 | /* list_for_each_reverse(pos, head)反向遍历链表 25 | ** pos - 临时变量(list_head *类型) head - 链表头指针(list_head *类型)*/ 26 | #define list_for_each_reverse(pos, headptr) \ 27 | for (pos = (headptr)->prev; pos != (headptr); pos = pos->prev) 28 | 29 | /* get_node(TYPE)初始化一个结点 30 | ** TYPE - 数据类型*/ 31 | #define get_node(TYPE) (TYPE *)malloc(sizeof(TYPE)); 32 | 33 | /* list_init(HEAD)初始化一个链表"头" 34 | ** HEAD - 链表头(list_head 类型)*/ 35 | #define list_init(HEAD) \ 36 | HEAD.next = &(HEAD); HEAD.prev = &(HEAD) 37 | 38 | /* list_insert(HEAD,list_head_for_NODE)在head之前插入结点 39 | ** HEAD - 链表头(list_head 类型) list_head_for_NODE - 新的结构的链表头*/ 40 | #define list_insert(HEAD,list_head_for_NODE) \ 41 | list_head_for_NODE.next = &(HEAD); \ 42 | list_head_for_NODE.prev = HEAD.prev; \ 43 | HEAD.prev->next = &(list_head_for_NODE); \ 44 | HEAD.prev = &(list_head_for_NODE) 45 | 46 | // #define list_delete(struct_ptr,HEAD_MEMBERNAME) \ 47 | // struct_ptr->HEAD_MEMBERNAME.prev->next = struct_ptr->HEAD_MEMBERNAME.next; \ 48 | // struct_ptr->HEAD_MEMBERNAME.next->prev = struct_ptr->HEAD_MEMBERNAME.prev; \ 49 | // free(struct_ptr) 50 | 51 | /* list_delete(ptr,TYPE,MEMBER)删除结点 52 | ** ptr - 结构中head的地址(list_head *类型) pos - 临时变量(list_head *类型) TYPE - 数据类型 MEMBER - head成员名 */ 53 | #define list_delete(ptr,pos,TYPE,MEMBER) \ 54 | ptr->prev->next = ptr->next; \ 55 | ptr->next->prev = ptr->prev 56 | // free(list_entry(pos,TYPE,MEMBER)) 57 | -------------------------------------------------------------------------------- /project/util/str.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | //限定返回个数最多10个 每一个字符串最多10个字符 5 | char ** split(char *string,char * delimiters,int *num){ 6 | char ** result; 7 | char * arg; 8 | int a=1; 9 | result = (char **)malloc(sizeof(char *)*10); 10 | for(int i=0;i<10;i++) 11 | result[i] = (char *)malloc(sizeof(char)*10); 12 | arg = strtok(string,delimiters); 13 | result[0] = arg; 14 | while(arg!=NULL){ 15 | arg = strtok(NULL,delimiters); 16 | result[a] = arg; 17 | a++; 18 | if(a==10){ 19 | a++; 20 | break; 21 | } 22 | } 23 | if(arg!=NULL)//判断参数是否超出个数 24 | *num=-1; 25 | else 26 | *num = a-1; 27 | return result; 28 | } 29 | char * trim(char *str){ 30 | int len=0; 31 | char *temp = str; 32 | int start=0; 33 | if(temp==NULL||*temp=='\0') 34 | return str; 35 | while (*temp!='\0'&&isspace(*temp)){ 36 | ++temp; 37 | ++start; 38 | } 39 | temp = str+strlen(str)-1; 40 | while(*temp!='\0'&&isspace(*temp)) 41 | temp++; 42 | *(temp+1)='\0'; 43 | return str+start; 44 | } -------------------------------------------------------------------------------- /project/util/str.h: -------------------------------------------------------------------------------- 1 | #ifndef __STR__ 2 | #define __STR__ 3 | char ** split(char *string,char * delimiters,int *num); 4 | char * trim(char *str); 5 | int doOpration(int argc,char ** argv); 6 | #endif -------------------------------------------------------------------------------- /project/util/time.c: -------------------------------------------------------------------------------- 1 | #include"time.h" 2 | #include 3 | struct tm* getTimeStruct(){ 4 | time_t t = time(NULL); 5 | return localtime(&t); 6 | } 7 | unsigned short getDate(struct tm* t){ 8 | int year = t->tm_year-70;//以1970为基准 最大存放88年 9 | int mon = t->tm_mon; 10 | int day = t->tm_mday; 11 | unsigned short result=0; 12 | if(t->tm_hour>12)//上下午标志存放在这里 short变量最高位 13 | result = 1<<15; 14 | result += year*12*31+mon*31+day; 15 | return result; 16 | } 17 | 18 | unsigned short getTime(struct tm* t){ 19 | //为了精确表示 只存放12h的时间 标志上下午的标志位放在date里 20 | unsigned short hour = t->tm_hour; 21 | unsigned short min = t->tm_min; 22 | unsigned short sec = t->tm_sec; 23 | if(hour>12) 24 | hour-=12; 25 | return hour*3600+min*60+sec; 26 | } 27 | 28 | unsigned short getHour(unsigned short date,unsigned short time){ 29 | if(date>>15==1) 30 | return 12 + time/3600; 31 | else 32 | return time/3600; 33 | } 34 | 35 | unsigned short getMinute(unsigned short time){ 36 | return (time%3600)/60; 37 | } 38 | 39 | unsigned short getSecond(unsigned short time){ 40 | return (time%3600)%60; 41 | } 42 | 43 | unsigned short getYear(unsigned short time){ 44 | time = time & 32767; 45 | return 1970+time/372;//372=12*31 46 | } 47 | 48 | unsigned short getMonth(unsigned short time){ 49 | time = time & 32767; 50 | return (time%372)/31+1;//372=12*31 51 | } 52 | 53 | unsigned short getDay(unsigned short time){ 54 | time = time & 32767; 55 | return (time%372)%31;//372=12*31 56 | } 57 | 58 | void showCurrentTime(){ 59 | struct tm* ts = getTimeStruct(); 60 | unsigned short date = getDate(ts); 61 | unsigned short time = getTime(ts); 62 | printf("%4d/%02d/%02d %02d:%02d:%02d\n", 63 | getYear(date),getMonth(date),getDay(date), 64 | getHour(date,time),getMinute(time),getSecond(time)); 65 | } -------------------------------------------------------------------------------- /project/util/time.h: -------------------------------------------------------------------------------- 1 | #include 2 | struct tm* getTimeStruct(); 3 | unsigned short getDate(struct tm* t); 4 | unsigned short getTime(struct tm* t); 5 | unsigned short getHour(unsigned short date,unsigned short time); 6 | unsigned short getMinute(unsigned short time); 7 | unsigned short getSecond(unsigned short time); 8 | unsigned short getYear(unsigned short time); 9 | unsigned short getMonth(unsigned short time); 10 | unsigned short getDay(unsigned short time); 11 | void showCurrentTime(); -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # 简单文件系统的实现 2 | 3 | ## 构建运行方式: 4 | 1.进入project目录 5 | 2.创建名为out的文件夹 6 | 3.在终端使用make命令 7 | 4.运行生成的名为fs的程序 8 | 或者 9 | 1.执行build.sh脚本 10 | 2.运行生成的名为fs的程序 11 | 12 | ## 使用方法: 13 | - 运行fs程序以后 14 | 输入help命令,根据相应命令进行操作。 15 | 注:本系统的命令操作,不能照搬linux shell下的命令,可以依据项目下shell目录的shell.c源码或者在输入命令参数出错以后的提示进行操作。 16 | 17 | ## 实验要求和说明: 18 | - 1.在内存中开辟一个虚拟磁盘空间作为文件存储分区,在其上实现一个简单的基于多级目录的单用户单任务系统中的文件系统。在退出该文件系统的使用时,应将该虚拟文件系统以一个文件的方式保存到磁盘上,以便下次可以再将它恢复到内存的虚拟磁盘空间中。 19 | 20 | - 2.文件存储空间的分配可采用显式链接分配或其他的办法。 21 | 22 | - 3.空闲磁盘空间的管理可选择位示图或其他的办法。如果采用位示图来管理文件存储空间,并采用显式链接分配方式,那么可以将位示图合并到 FAT 中。 23 | 24 | - 4.文件目录结构采用多级目录结构。为了简单起见,可以不使用索引结点,其中的每个目录项应包含文件名、物理地址、长度等信息,还可以通过目录项实现对文件的读和写的保护。 25 | 26 | - 5.要求提供以下操作命令: 27 | + my_format:对文件存储器进行格式化,即按照文件系统的结构对虚拟磁盘空间进行布局,并在其上创建根目录以及用于管理文件存储空间等的数据结构。 28 | + my_mkdir:用于创建子目录。 29 | + my_rmdir:用于删除子目录。 30 | + my_ls:用于显示目录中的内容。 31 | + my_cd:用于更改当前目录。 32 | + my_create:用于创建文件。 33 | + my_open:用于打开文件。 34 | + my_close:用于关闭文件。 35 | + my_write:用于写文件。 36 | + my_read:用于读文件。 37 | + my_rm:用于删除文件。 38 | + my_exitsys:用于退出文件系统。 39 | 40 | ### 时间仓促,本项目还有很多地方不完善,提交bug反馈,学习交流,欢迎提issue。 -------------------------------------------------------------------------------- /test/createDisk.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | char buff[1024]={'0'};//1KB 5 | FILE *f = fopen("mydisk","a+"); 6 | for(int i=0;i<1024*1024;i++) 7 | fwrite(buff,sizeof(buff),1,f); 8 | fclose(f); 9 | return 0; 10 | } -------------------------------------------------------------------------------- /test/fileopen.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | int main(){ 7 | int stat = open("myfile",O_CREAT|O_EXCL,S_IRWXU); 8 | printf("%d\n",stat); 9 | perror(strerror(errno)); 10 | printf("%d %s\n",errno,strerror(errno)); 11 | return 0; 12 | } -------------------------------------------------------------------------------- /test/listmacrotest.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include"../project/util/list.h" 4 | 5 | int main() 6 | { 7 | struct mystruct{ 8 | int a; 9 | char b; 10 | double c; 11 | list_head head; 12 | }; 13 | list_head *lstemp,*lhead; 14 | struct mystruct *m,*temp; 15 | //制作链表 16 | m = get_node(struct mystruct); 17 | m->a=1;m->b='1';m->c=1; 18 | list_init(m->head); 19 | printf("%d %c %f\n",m->a,m->b,m->c); 20 | 21 | temp = get_node(struct mystruct); 22 | temp->a=2;temp->b='2';temp->c=2; 23 | list_insert(m->head,temp->head); 24 | 25 | temp = get_node(struct mystruct); 26 | temp->a=3;temp->b='3';temp->c=3; 27 | list_insert(m->head,temp->head); 28 | 29 | temp = get_node(struct mystruct); 30 | temp->a=4;temp->b='4';temp->c=4; 31 | list_insert(m->head,temp->head); 32 | //正向遍历链表 33 | printf("show all\n"); 34 | lhead = &(m->head); 35 | printf("%d %c %f\n",m->a,m->b,m-> c); 36 | list_for_each(lstemp,lhead){ 37 | temp = list_entry(lstemp,struct mystruct,head); 38 | printf("%d %c %f\n",temp->a,temp->b,temp->c); 39 | } 40 | //反向遍历链表 41 | printf("reverse\n"); 42 | list_for_each_reverse(lstemp,lhead){ 43 | temp = list_entry(lstemp,struct mystruct,head); 44 | printf("%d %c %f\n",temp->a,temp->b,temp->c); 45 | } 46 | printf("%d %c %f\n",m->a,m->b,m->c); 47 | //删除结点 48 | printf("%p \n",list_entry(lhead->next,struct mystruct,head)); 49 | lhead = &(m->head); 50 | list_delete(lhead->next,lstemp,struct mystruct,head); 51 | printf("show all\n"); 52 | lhead = &(m->head); 53 | printf("%d %c %f\n",m->a,m->b,m->c); 54 | list_for_each(lstemp,lhead){ 55 | temp = list_entry(lstemp,struct mystruct,head); 56 | printf("%d %c %f\n",temp->a,temp->b,temp->c); 57 | } 58 | 59 | //删除结点 60 | printf("%p \n",list_entry(lhead->next,struct mystruct,head)); 61 | lhead = &(m->head); 62 | list_delete(lhead->next,lstemp,struct mystruct,head); 63 | printf("show all\n"); 64 | lhead = &(m->head); 65 | printf("%d %c %f\n",m->a,m->b,m->c); 66 | list_for_each(lstemp,lhead){ 67 | temp = list_entry(lstemp,struct mystruct,head); 68 | printf("%d %c %f\n",temp->a,temp->b,temp->c); 69 | } 70 | return 0; 71 | } -------------------------------------------------------------------------------- /test/listtest.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include"../project/util/list.h" 3 | #include"../project/util/list.c" 4 | struct mystruct{ 5 | int a; 6 | char b; 7 | double c; 8 | lslink head; 9 | }; 10 | void showAll(lslink *lhead){ 11 | lslink *ltemp; 12 | struct mystruct *temp; 13 | temp = lhead->sptr; 14 | if(temp!=NULL) 15 | printf("%d %c %f\n",temp->a,temp->b,temp->c); 16 | list_for_each(ltemp,lhead){ 17 | temp = ltemp->sptr; 18 | printf("%d %c %f\n",temp->a,temp->b,temp->c); 19 | } 20 | } 21 | int main() 22 | { 23 | 24 | struct mystruct *m,*temp; 25 | lslink *ltemp,*lhead; 26 | //构造链表 27 | m = get_node(struct mystruct); 28 | m->a=1;m->b='1';m->c=1; 29 | lhead = &(m->head); 30 | list_init(lhead,m); 31 | 32 | temp = get_node(struct mystruct); 33 | temp->a=2;temp->b='2';temp->c=2; 34 | list_insert(lhead,&(temp->head),temp); 35 | 36 | temp = get_node(struct mystruct); 37 | temp->a=3;temp->b='3';temp->c=3; 38 | list_insert(lhead,&(temp->head),temp); 39 | 40 | temp = get_node(struct mystruct); 41 | temp->a=4;temp->b='4';temp->c=4; 42 | list_insert(lhead,&(temp->head),temp); 43 | //遍历链表 44 | printf("show all\n"); 45 | temp = list_entry(lhead,struct mystruct,head); 46 | printf("%d %c %f\n",temp->a,temp->b,temp->c); 47 | list_for_each(ltemp,lhead){ 48 | temp = ltemp->sptr; 49 | printf("%d %c %f\n",temp->a,temp->b,temp->c); 50 | } 51 | //反向遍历链表 52 | printf("show all reverse\n"); 53 | 54 | list_for_each_reverse(ltemp,lhead){ 55 | temp = ltemp->sptr; 56 | printf("%d %c %f\n",temp->a,temp->b,temp->c); 57 | } 58 | temp = lhead->sptr; 59 | printf("%d %c %f\n",temp->a,temp->b,temp->c); 60 | //删除结点 61 | temp = lhead->next->sptr;//获得结构体地址,用于下一步传参 62 | list_delete(lhead->next,temp); 63 | printf("show all\n"); 64 | showAll(lhead); 65 | //删除结点 66 | temp = lhead->prev->sptr; 67 | list_delete(lhead->prev,temp); 68 | printf("show all\n"); 69 | showAll(lhead); 70 | //删除结点 71 | temp = lhead->prev->sptr; 72 | list_delete(lhead->prev,temp); 73 | printf("show all\n"); 74 | showAll(lhead); 75 | //删除最后一个结点 76 | temp = lhead->prev->sptr; 77 | list_delete(lhead->prev,temp); 78 | printf("show all\n"); 79 | showAll(lhead); 80 | 81 | printf("destroy test\n"); 82 | //再次构造链表、链表销毁测试 83 | m = get_node(struct mystruct); 84 | m->a=1;m->b='1';m->c=1; 85 | lhead = &(m->head); 86 | list_init(lhead,m); 87 | 88 | temp = get_node(struct mystruct); 89 | temp->a=2;temp->b='2';temp->c=2; 90 | list_insert(lhead,&(temp->head),temp); 91 | 92 | temp = get_node(struct mystruct); 93 | temp->a=3;temp->b='3';temp->c=3; 94 | list_insert(lhead,&(temp->head),temp); 95 | 96 | temp = get_node(struct mystruct); 97 | temp->a=4;temp->b='4';temp->c=4; 98 | list_insert(lhead,&(temp->head),temp); 99 | 100 | printf("show all\n"); 101 | showAll(lhead); 102 | temp = lhead->prev->sptr; 103 | list_destroy(lhead); 104 | printf("after destroy\n"); 105 | showAll(lhead); 106 | 107 | return 0; 108 | } -------------------------------------------------------------------------------- /test/offset.c: -------------------------------------------------------------------------------- 1 | //计算结构体中某个成员的偏移量 2 | #include 3 | #define offset(TYPE,MEMBER) ((size_t)&((TYPE *)0)->MEMBER) 4 | #define list_entry(ptr,TYPE,MEMBER) (ptr)-offset(TYPE,MEMBER) 5 | struct mystruct{ 6 | int a; 7 | char b; 8 | float c; 9 | }; 10 | int main() 11 | { 12 | struct mystruct ms; 13 | ms.a = 1; 14 | ms.b = '2'; 15 | ms.c = 3; 16 | printf("%d\n",sizeof(struct mystruct)); 17 | printf("2 in mystruct %d\n",offset(struct mystruct,b)); 18 | printf("%ld %ld\n",&ms,list_entry(&(ms.b),struct mystruct,b)); 19 | printf("%d %c %f\n",ms.a,ms.b,ms.c); 20 | return 0; 21 | } --------------------------------------------------------------------------------