CSE 130 Spring 2010 Lab 1 NOTE: It is very important for this course that you obtain a sparky account (ic.sunysb.edu) for completing assignments. The login-id is the same as your NetID. The password is the same. Check if your sparky account is active by connecting to sparky.ic.sunysb.edu either by ssh or telnet. If your sparky account is not working visit the SINC site in the main library building and speak to a representative to resolve this issue. TA will help you with the following skills: ---------------------------------------------------------------------- 1. Accessing your sparky account using SSH (Secure shell) - Turn on the computer, press Control-Alt-Del to sign onto windows. Use your personal account that you have for all CS labs. Did you get an e-mail about this account? If you don't remember, the Login_id is your NetId and password is Sbcs + your 9-digit id- number. So it is a 13-character password. - Windows will ask you to change your password right away. There are instructions on the wall. It should be at least 8 characters long, and must have at least 3 of upper case, lower case, numeric and special characters. It should be an easy to remember pass- word. Write it down somewhere just in case you forget. You will need this password to connect to a computer in any CS lab. - Locate and activate the icon for SSH (Secure shell utility) on the Windows desktop. If not, use all programs link. - Press the spacebar (or click quick-connect) to bring up the con- nection window. In the text box for host name, make sure that the entry is sparky.ic.sunysb.edu (just sparky is fine if you are connecting from a computer on campus). - In the textbox for user name, enter your NetId. This is your NetId@ic.sunysb.edu account. - Click on the Connect button to connect to the server. - When prompted, enter your password (your NetID password) into the textbox and click OK. - If you have not used your sparky account even once, you may be forced to change your password. Change it to something that is easy to remember. You will need it for the next lab. - You might be presented with a list of campus messages. Press the spacebar to skip them and press enter/return when prompted to. - Your ssh window now should show the prompt sparky%, possibly after a message stating that you may have received an e-mail. - This prompt is where you will enter all UNIX commands. You will need these commands to complete C programming assignments required for this course. - BACKSPACE key will not work as you expect. Use DELETE instead. 2. Creating a text file using the pico text editor (Brief) - At the command prompt, type the keyword pico and press enter/return. This will start up the pico text editor in your ssh window. - Type a short sentence into the editor window. Let that sentence be about you. Type in your name, your major and one of your hob- bies, all on separate lines. - Make sure that your sentence/text is at least a couple of lines of text. Note the difference between a line of text and sentence. - Spend some time and get used to working with the arrow keys for navigating through your text file. - To save the file, press the key combination Control-o (oh) This command is called WriteOut. When asked for a file name, enter the name of the file in which you wish to save this text. In this case, name the file AboutMe and press return to save it. - Press the key combination Control-X to exit the text editor and return to the sparky prompt. - To access the file you just saved again, type the command pico followed by the name of the file that you wish to open. In this case, type the command pico AboutMe and press enter. Note that UNIX commands are case-sensitive, meaning capitalization makes a difference and that AboutMe is different than aboutme. - Modify your text so that it now also includes information about your preferred e-mail address. Save the file and exit pico again using the instructions given above. - Pico Summary: 1. Typing will insert text without overwriting the existing text. 2. BACKSPACE key does what you would expect it to do. Unfor- tunately DELETE key does the same the thing. 3. CTRL-d deletes the character highlighted by the cursor. 4. CTRL-k deletes the entire line. 5. CTRL-o saves the file. It is called WriteOut. 6. CTRL-x gets you out of PICO and back to sparky. - Explore the "help" information available for pico. 3. Using other simple UNIX commands (see UNIX-Help on the Web Page) - Enter the command ls at the command prompt and press enter. This is the list command that displays the names of all files in the current directory or folder. - You should currently see the AboutMe file that you just created using pico. - Enter the command ls -l at the command prompt and press enter. This is a variation of the ls command that not only prints names of files but also prints detailed information about each file including the time that it was last modified and the size of the file in bytes/characters. - Type command more AboutMe. This outputs the contents of your file onto the screen without opening up the pico editor. Useful when you want to view large files on screen. Use spacebar to scroll down. - When you are finished with your sparky session, it is important for security purposes that you end your connection to the server. To do this, use either the exit or logout commands. The text in your terminal should turn gray. At this point it is safe to close out SSH. (Note that you don't need to do this right at this moment.) 4. Directory (folder) Management - It is better to organize your files using several directories (or folders). UNIX provides several commands to manage your direc- tories. - Command pwd, prints the name of your working directory. Right after you login, your working directory is your home directory (that has your login/account name). - Command mkdir cse130 will create a directory called cse130. Keep your cse130 files in this directory. If you are using sparky account for some other course such as Business 101 as well, create another directory called bus101. - In order to access files in a directory, you need to change to that directory. Command cd cse130, will change your working directory to cse130. - Command cd (without any directory name), will take you back to your home directory. When you log in on sparky, you are in your home directory. 5. Using your PC (Read) - This would be useful, if you want to do your programming assign- ments on your PC. It is not necessary that you use SSH. If you are having problem installing SSH, then use telnet. Follow instructions that have been on the course web page on how to to use your PC. You will need a C compiler. See instructions on the webpage. 6. Creating and Compiling a simple C program. a. Enter the C program given below that prints these two lines: These lines are printed by the lab1.c program. b. The program is given here, but use your name, and your id number. You may use PICO to type the following program in. But it will save you a lot of time if you just cut and paste this from the CSE 130 webpage to your PICO window. // lab1.c // CSE 130 Spring 2010. Lab 1, Feb 3, 2010. // Shaunak Pawagi, Id-No: xxx-yy-zzzz // Sample program to print two lines. #include int main (void) { printf ("These lines are printed by\n"); printf ("the lab1.c program.\n"); return 0; } c. There are four lines of comments (which start with //) which ignored by the Compiler. Then comes #include statement, which tells compiler that you will be using standard I/O (input/output or read/print) functions from the C library. d. After #include, we start with the main function. Every program must have this function. We declare this to be an integer func- tion. This function has no parameters as indicated by (void). The body of main is enclosed in { and }. e. Note that C statements end with a semicolon. Note also that C is case sensitive, e.g., a capital "S" is different than a small "s". Function printf will print whatever text you have between two double quotes (" "). \n stands for a new line (or next line) character. Follow these steps to create and execute the program. f. Use pico to enter the above program. You can either type it all in or just copy it from this page (to the clipboard of your PC) and paste it in. (If, when pasting, the indenting gets affected with extra spaces, just delete the extras. g. Edit it so it has your name and ID, instead of Pawagi. Save the file with the name lab1.c All C programs must have .c as an extension for its filename h. Use `ls' to see that it is there. i. Compile the C program by typing the Unix command gcc lab1.c (Typically the compiler produces some error messages here, indi- cating that some statements are not correct; if so go back to step f and fix the the errors.) j. Use `ls' to see that the gcc command created a new file named a.out k. Run your program by typing the command ./a.out l. You should see it output the two lines of text. m. Use pico to modify your program so that it has only the first double quote (") in the first printf statement. Delete the second ". printf("These lines are printed by\n ); Save it and compile. What errors do you get during compilation? Ask TA why? n. Use pico again to fix the error of double quote (") and modify your program so that it has one more \n after the word `by' on the first line. Save it, compile it, and execute it, to check if it produces the identical output.