I'm currently doing Unix Case Study B (still a little behind, but catching up quickly!).
The first part has us using the dd command. I found this funny because over the past month I've picked up this habit of flashing different cell modems on my phone. In order to flash them, I have to connect to my phone using the android debugging bridge (which let's me set up a shell on my phone, accessed through my computer), and then use dd to data dump the data in the cell modem files into the proper related phone directories.
It's important because well, I've used it for tasks before. Being able to use this tool is pretty helpful. It allows me to grab parts of files and put them separately, or move parts of files into other files etc.
Just a fun little thing.
For the past couple of days I've been doing Lab B for unix. I really like having this newfound power to go through files and sort them and do searches and getting what I want out of the files. Very empowering feeling. I like it so much that writing the scripts for this lab are semi enjoyable, until I run into a huge block (like I am right now) that I can't for the life of me figure out.
It's really important to have this ability because when it comes to information manipulation, this ability is absolutely essential. The lab has some pretty excellent examples of uses, ending with scripts that automate the process so that you can just type in a command with some arguments and get masses of data from an html file.
Last night.. or rather earlier this morning, I complete Project 3. Because I love doing things in a timely manner.
Despite the negative Karma, I believe the fact that I made the arrays dynamically allocate themselves so that the answers printed are the TRUE values, instead of being cut off, was well worth the delay.
Once I had the core of the program finished (had it in one gigantic .c file) and working, I had only met 2/3 of the Projects requirements. Now I needed to split up the C source into a multifile version, C++ class based version, and C++ class based multifile version.
Thanks to all the work I put into the class based ,logic gate, C++ multifile program, doing the above was a sinch.
Even though I was half asleep and feeling quite dead after a long day (it was one in the morning) finishing the project was quite effortless. Other than having to look at my logic program for syntax, I already knew precisely what to do. :) It was quite nice, and it's good to be able to do that. Splitting a program into multiple files - even without doing a class based approach - makes the program much more organized.
Classes would make it even more organized: but it is better for longer programs, and the entire structure and gameplan has to be well thought out beforehand. The 'just sit down and spit it out' method doesn't work as well anymore.
The semester is rounding up, and I can't wait to continue taking courses next semester. I've learned a lot.
In regards to unix/linux, I really appreciate how well I know the system now. I know I'm not even near towards being a master at it, but the bit that I do now know has made everyday menial tasks much easier. I've even started writing scripts to do stupid little things. I wrote one that turns off my screen using a keyboard shortcut, and one that allows me to change the # of workplaces in ubuntu.
The code for these I majorly got online, I didn't know that stuff off the top of my head. But I then took that code and put it into scripts, so that should I ever need to use it again I could, quite easily. It's pretty cool. My multitasking productivity has gone up nicely because of it.
Namespaces are basically ways to create global variables, but with a sort of 'subscope' type of feeling. Basically, let's say you have the variable of 'health'. You have the health of your character and the health of a creature. Well, you can't use the same health for both. This is where namespaces come in - we can define your health under the namespace 'Character' or something, and the health of the creature under the namespace of 'Creature'. To call health, you need to specify the namespace::health now - ensuring that there is no mix up with the different healths.
The above paragraph is a pretty good example, but I'm going to put down some super basic code just to get the syntax on here.
#include <stdio.h> #include <stdlib.h> #include <time.h> namespace Character { int health=100; int combat; long long int gold; } namespace Creature { int health=25; int combat; long long int gold = 7; } int main() { printf("Do you wish to enter combat with 'Creature'?\n\n0 for no\n1 for yes\n"); scanf("%u", &Character::combat); if (Character::combat == 0) { printf("Have a nice glory-free day.\n"); return 0; } do { if(Character::combat == 1) { Creature::combat=1; } else { Creature::combat=0; } Creature::health=25; while((Character::combat==1) && (Creature::combat==1)) { Character::health = Character::health - 3; Creature::health = Creature::health - 7; if (Creature::health <= 0) { Character::gold = Creature::gold + Character::gold; printf("You killed 'Creature'.\nYou received %llu gold.\nYou have %u health remaining", Creature::gold, Character::health); printf("\nYou now have %llu gold\n", Character::gold); break; } if (Character::health <= 0) { printf("You have died.\n"); return 0; } } printf("Do you wish to fight another creature? 0 for no 1 for yes\n"); scanf("%u", &Character::combat); } while(Character::combat==1); printf("You have fled from combat.\n"); return 0; }
Alternatively (or additionally), if you want to demonstrate something on the command-line, you can do so as follows:
lab46:~/src/cprog/messingaround$ ./Namespaces Do you wish to enter combat with 'Creature'? 0 for no 1 for yes 1 You killed 'Creature'. You received 7 gold. You have 88 health remaining You now have 7 gold Do you wish to fight another creature? 0 for no 1 for yes 1 You killed 'Creature'. You received 7 gold. You have 76 health remaining You now have 14 gold Do you wish to fight another creature? 0 for no 1 for yes 1 You killed 'Creature'. You received 7 gold. You have 64 health remaining You now have 21 gold Do you wish to fight another creature? 0 for no 1 for yes 1 You killed 'Creature'. You received 7 gold. You have 52 health remaining You now have 28 gold Do you wish to fight another creature? 0 for no 1 for yes 1 You killed 'Creature'. You received 7 gold. You have 40 health remaining You now have 35 gold Do you wish to fight another creature? 0 for no 1 for yes 1 You killed 'Creature'. You received 7 gold. You have 28 health remaining You now have 42 gold Do you wish to fight another creature? 0 for no 1 for yes 1 You killed 'Creature'. You received 7 gold. You have 16 health remaining You now have 49 gold Do you wish to fight another creature? 0 for no 1 for yes 1 You killed 'Creature'. You received 7 gold. You have 4 health remaining You now have 56 gold Do you wish to fight another creature? 0 for no 1 for yes 1 You have died. lab46:~/src/cprog/messingaround$
Functions are basically blocks of code that are run when called, and can be given inputs (parameters). You can pass parameters by value (just give it the identifier), Address (using the & symbol), and by Reference (by pointers, for passing an array).
Functions can return values of different types, whether they be int, float, etc.
Recursion is when you call a function within a function, with a conditional break.
#include<stdio.h> #include<stdlib.h> int sum(int, int, int, int); //function prototype int main() { int a,b,c,d; a=b=c=d=0; printf("\nEnter four numbers\n"); scanf("%d%d%d%d", &a, &b, &c, &d); printf("The sum of %d, %d, %d, and %d is: %d\n", a,b,c,d, sum(a, b, c, d)); return (0); } int sum(int a, int b, int c, int d) { int SUM; SUM = a + b + c + d; return (SUM); }
Alternatively (or additionally), if you want to demonstrate something on the command-line, you can do so as follows:
#include <stdlib.h> #include <stdio.h> int main(int argc, char **argv) { unsigned char i; if(argc < 2) { printf("%8s must be run with 1 or \ more arguments, you only provided %hhu\n", *(argv+0),(argc-1)); exit(1); } printf("You ran this program with %hhu arguments, they are:\n", argc); for(i=0; i<argc; i++) { printf("argv[%hhu]: %s\n", i, *(argv+i)); } return(0); }
Templates are kind of like function overloading. Except, you do the SAME THING given different parameters.
The STL is basically a library just filled with tons of templates to do stuff.
#include <stdlib.h> #include <stdio.h> #include <cstdio> #include <iostream> using namespace std; template <class T> //The T will basically be replaced by whatever T larger (T x, T y) //data type is necessary { if (x >= y) { return x; } else { return y; } } int main() { int a = 1200, b = 12; float c = 3.1415, d = 143.24; char e = 65, f = 'B'; cout << larger(a,b) << endl; cout << larger(c,d) << endl; cout << larger(e,f) << endl; return 0; }
lab46:~/src/cprog/messingaround$ ./Template 1200 143.24 B
Classes are wonderful little things good for organizing larger scale programs.
Objects are the things in the class, functions or variables or constants or friends, etc. Constructor is what runs when the class is being set up for usage. Optionary. Destructor is what runs when the class is deleted. Optionary. Access Control refers to the Public Protect and Private deal going on.
Public - Anybody can access this part. Protected - Only children can access this part (children are other classes that inherit from this class). Private - Only this class can access these parts. Friends - Allow you to rip through Access control, and access private parts from outside the class they belong to.
“This” pointer - When you make a new class to call and define it as a pointer, you have to use → to refer to it instead of a period.
There's two class-based programs in
cd /home/smalik2/src/cprog/c++
Also, Project 3.
The compiler takes your source code and translates it to assembly language.
The preprocessor deals with taking care of stuff before the actual compiling begins.
Flags are the command line arguments to the compiler, such as -02.
Assembler takes the assembly code (step 1 of compiling) and converts it into true machine code.
The Linker takes care of making sure that stuff from other libraries and header files and stuff will work correctly and be there.
Multi-file programs are programs made over multiple files.
I'll compile a multi-file program here.
lab46:~/src/cprog/c++/today$ ls main.cc rectangle.cc rectangle.h shape.cc shape.h square.cc square.h triangle.cc triangle.h lab46:~/src/cprog/c++/today$ g++ -c *.cc lab46:~/src/cprog/c++/today$ ls main.cc rectangle.cc rectangle.o shape.h square.cc square.o triangle.h main.o rectangle.h shape.cc shape.o square.h triangle.cc triangle.o lab46:~/src/cprog/c++/today$ g++ -o Program *.o lab46:~/src/cprog/c++/today$ ./Program 1 for Rectangle 2 for Square 3 for Triangle 2 You chose square. Enter the width/length: 12 The perimeter is: 48 The area is: 144 Einen schönen Tag noch, Ruck. lab46:~/src/cprog/c++/today$
Type Casting is basically converting some type of data into another type. Const is a keyword that means that the type can not be changed. Volatile is a keyword that means you can modify using some tricks.
We didn't really go into this in class, except for when we used arrays.
When we used malloc to create an array, we were type casting it so that the memory we gave to the array would behave properly - according to the data type that we wanted for the array.
float *Array; Array = (float*)malloc(sizeof(float)*50); //(float*) is a type cast.
A typedef basically let's you take something, and create a shortcut for it. This shortcut now represents what you originally typedef, and the two can be used interchangeably. An enum is a data type that consists of named values. A union is kind of like a struct, where it holds a bunch of different types of data. The trick is, you can only have one type of data in it at a given time. Assigning new data, overwrites the old data.
There's a typedef in the EoCE. There's an enum and union in my cprog folder.
Structures are basically The C version of classes. They are heterogeneous data types, and can hold multiple things. Functions and data types. They are really good and useful for organization oriented stuff.
On the EoCE there is a lot of strcuture stuff on 0x6 and 0x7.
justify the use of various programming constructs within code solutions
This objective means being able to build a solution for a problem using some sensible approach. On top of that, you outta be able to come up with very sound reasons for your approach. (Why did I use classes to do this? For example).
I can judge how well I organize my code, and whether or not my methods make sense/ are good.
I normally take a very good approach in how I organize and construct my solutions to problems, so I figure I do a pretty decent job of it.
The compiler takes the code and translates it into assembly code.
The assembler takes the assembly code and translates it into machine code - the resulting file being an object file.
The linker looks at the object file (which is linked to other object files and libraries, like the stuff from stdlib etc) and figures out how it all comes together, and assign memory locations to everything.
When it comes to load time, the loader creates a process and basically sets everything up for your program to run, and then launches into the instructions of your program.
It's possible to do these steps one at a time instead of letting gcc / g++ do it all for you. But it's rather painful.
g++ -c *.cc → will give you .o object files. These are compiled and assembled, so they are in machine code.
g++ -o Program *.o → This will link those object files, and give you a running program. yay!
Source Code is the file that contains the code that you wrote.
Object Code is the file that contains the machine code, after the compiler and assembler are done with it.
Binary Code is the file that contains the machine code, and has been linked.
A library is a file that you can link to that contains many functions. There are math libraries, and then the standard libraries, etc.
We start with c source code files.
Then we translate them into object code files.
Then we finally link it all into a binary code file.
The last step also takes care of attaching the libraries I used and stuff.
lab46:~/src/cprog/projects/CProject2$ ls Arrays.c Functions.h Math.c Project2Monolithic.c lab46:~/src/cprog/projects/CProject2$ gcc -c *.c lab46:~/src/cprog/projects/CProject2$ ls Arrays.c Arrays.o Functions.h Math.c Math.o Project2Monolithic.c Project2Monolithic.o lab46:~/src/cprog/projects/CProject2$ gcc -o Program *.o lab46:~/src/cprog/projects/CProject2$ ls Arrays.c Arrays.o Functions.h Math.c Math.o Program Project2Monolithic.c Project2Monolithic.o lab46:~/src/cprog/projects/CProject2$
Pattern matching is the ability to find things that you're looking for, by using patterns to search for them.
Compiling a multifile program using patterns, instead of typing out a bunch of different file names.
lab46:~/src/cprog/c++/today$ ls main.cc rectangle.cc rectangle.h shape.cc shape.h square.cc square.h triangle.cc triangle.h lab46:~/src/cprog/c++/today$ g++ -c *.cc lab46:~/src/cprog/c++/today$ ls main.cc rectangle.cc rectangle.o shape.h square.cc square.o triangle.h main.o rectangle.h shape.cc shape.o square.h triangle.cc triangle.o lab46:~/src/cprog/c++/today$ g++ -o Program *.o lab46:~/src/cprog/c++/today$ ./Program 1 for Rectangle 2 for Square 3 for Triangle 2 You chose square. Enter the width/length: 12 The perimeter is: 48 The area is: 144 Einen schönen Tag noch, Ruck. lab46:~/src/cprog/c++/today$
Regular Expressions, or RegEx, are algorithms that will match something.
By using these, you can search for, let's say, any word in a txt file that contains the gerund 'ing'.
It's pretty powerful stuff.
I have a file with a bunch of courses. Let's say I only want the Shortcut version (like, BUSN 0003 and FYEX 1000).
I can pipe the output through egrep which uses RegEx to match up to what I want.
The hard part, is coming up with the RegExp.
lab46:~/src/unix/courselist$ cat test.file HIPAA - 77017 - BUSN 0003 - 001 Computer Keyboarding - 77015 - BUOT 1061 - 001 Word Processing for Non-Major - 77016 - BUOT 1062 - 001 Intro Graphical User Interface - 77002 - CSST 1031 - 001 OSHA Special Topics (30ct hr) - 77011 - ENVR 0030 - 001 Natural Gas Equipment Oper - 77014 - ENVR 0072 - 001 First Year Experience - 77005 - FYEX 1000 - 001 First Year Experience - 77004 - FYEX 1000 - 002 BasicLifeSupport-Prof. Rescuer - 77006 - HLTH 1010 - 001 BasicLifeSupport-Prof. Rescuer - 77007 - HLTH 1010 - 002 American History II - 77020 - HIST 1120 - 001 Achievement Motivation - 77021 - HUSR 1000 - 001 Care.&Aging Issue-Healthy Func - 77029 - HUSR 1411 - 001 Caregiving&Aging Issues-Common - 77030 - HUSR 1412 - 001 Lean Special Topic (24cnt) - 77012 - LEAN 0024 - 001 LEN Special Topic (57 cnt hr) - 77027 - LEN 0057 - 001 Excel Advanced Workshop - 77025 - EXCL 0004 - 001 Intro. to MasterCam Design - 77026 - MCAM 0113 - 001 Structures of Mathematics I - 77028 - MATH 1110 - 001 Medical Terminology - 77013 - MEDT 0030 - 001 Nurse Aide/Home Health Aide - 77022 - MDNC 0315 - 001 Child Abuse Recognition - 77023 - PDEV 0011 - 001 Child Abuse Recognition - 77024 - PDEV 0011 - 002 Child Psychology - 77010 - PSYC 2207 - 001 Abnormal Psychology - 77009 - PSYC 2215 - 001 Cross-Country Skiing (co-ed) - 77018 - RECC 1014 - 001 Wilderness Navigation - 77019 - RECC 1015 - 001 Introduction to Sociology - 77003 - SOCI 1010 - 001 The Vegetarian Adventure - 77008 - WELL 1011 - 001 lab46:~/src/unix/courselist$ cat test.file | egrep -o '[A-Z]?[A-Z]{3} [0-9]{4}' BUSN 0003 BUOT 1061 BUOT 1062 CSST 1031 ENVR 0030 ENVR 0072 FYEX 1000 FYEX 1000 HLTH 1010 HLTH 1010 HIST 1120 HUSR 1000 HUSR 1411 HUSR 1412 LEAN 0024 LEN 0057 EXCL 0004 MCAM 0113 MATH 1110 MEDT 0030 MDNC 0315 PDEV 0011 PDEV 0011 PSYC 2207 PSYC 2215 RECC 1014 RECC 1015 SOCI 1010 WELL 1011 lab46:~/src/unix/courselist$
Filtering is basically a program that gets data from standard input and then writes results to standard output. Like running grep on the cat from a file.
So, cat on a file outputs that files contents to standard output. If I pipe this to grep, grep will accept that standard input (that was output from cat) and then return it's answer to standard output.
Here we are printing a .html file to standard output, finding all the instances of CSCS, and then counting how many times that CSCS appears.
lab46:~$ cat fall2011-20110417.html | grep -o 'CSCS' | wc -l 51 lab46:~$
We never really went into networking with UNIX. It's such a broad topic that I don't really know how to get into it either.
In UNIX/Linux the way that the system is set up provides good security by itself.
Since all files have permissions, just setting up the system to provide the correct permissions to the correct users ensures that the wrong people can't get their hands on files they shouldn't.
Taking advantage of the root-group-owner permission system is a great way of providing security in a multi-user system.
On top of it all, UNIX/Linux systems don't really have many viruses to begin with. All the viruses created target Windows and occasionally OS X.
The X Window System is a software system that provides a GUI basically.
It has a very strong UNIX/Linux philosophy operating behind it.
It's designed to accept added functionality, and leaving it up to the user to determine what they want to do with it.
understanding and use of pattern matching
It's pretty straight forward. This means that we can use tools like grep, or even understand the syntax for searching for patterns instead of specifics using things like ls
How well I utilize regexp.
After struggling through the labs related to pattern matching and regexp and grep and all that good stuff, I feel pretty confident in my ability to use patterm matching.
If I have a double pointer, to pass a value to it from scanf do I have to dereference the double pointer only once? (That would make it 'referenced' by one layer, in my mind).
Don't really need resources, it's a straightforward test.
I think it will work because I ran into something similar doing project4. I had an array and was using scanf to fill it, so it was a single pointer. To use scanf, I just called the name. No dereference or anything.
I'm going to write a simple program. Make a double pointer and see if I can scanf and print the value the way I believe it will work.
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int main() 5 { 6 int **numbers; 7 8 *numbers = (int*)malloc(sizeof(int)*1); 9 10 printf("HEY!!! Enter a number. Please? :)\n"); 11 12 scanf("%d", *numbers); 13 14 printf("\nHEY!!! The number you entered? Yeah. It should be %d.\n", **nu mbers); 15 16 return 0; 17 }
lab46:~/src/cprog/messingaround$ ./scanfdoublepointer Segmentation fault lab46:~/src/cprog/messingaround$
Based on the data collected:
What can you ascertain based on the experiment performed and data collected? Document your findings here; make a statement as to any discoveries you've made.
Well, no matter what, I keep getting a segfault. I'm guessing that this would mean my hypothesis was incorrect.
I wonder what would happen if I tried to chmod using permissions like 888, or something like that.
Nothing was consulted.
I'm betting that it either doesn't work, or it defaults to 777 or something.
I am going to try it out and see what happens.
lab46:~/src/unix/scripts$ ls age.sh csd.sh firstforloop.sh guess1.sh mail.sh script2.sh botstart.sh files firstlist.sh histogram.sh script1.sh lab46:~/src/unix/scripts$ chmod 888 age.sh chmod: invalid mode: `888' Try `chmod --help' for more information. lab46:~/src/unix/scripts$ chmod 10292 guess1.sh chmod: invalid mode: `10292' Try `chmod --help' for more information. lab46:~/src/unix/scripts$ chmod 77 age.sh lab46:~/src/unix/scripts$ ls -lh age.sh ----rwxrwx 1 smalik2 lab46 120 Mar 8 11:45 age.sh lab46:~/src/unix/scripts$ chmod 777 age.sh lab46:~/src/unix/scripts$
Based on the data collected:
Chmod doesn't work if you provide something higher than a 7, or longer than 3 digits.
Perform the following steps:
Whose existing experiment are you going to retest? Provide the URL, note the author, and restate their question.
Evaluate their resources and commentary. Answer the following questions:
State their experiment's hypothesis. Answer the following questions:
Follow the steps given to recreate the original experiment. Answer the following questions:
Publish the data you have gained from your performing of the experiment here.
Answer the following:
Answer the following: